<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px"
    >
      <el-form-item
        label="状态"
        prop="keyWords4"
      >
        <el-select v-model="form.keyWords1">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="激活"
            :value="1"
          ></el-option>
          <el-option
            label="禁用"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getList"
        >
          查询
        </el-button>
        <el-button
          @click="reset()"
        >
          清除
        </el-button>
        <el-button
          @click="$refs.dialog.show(null,'add')"
        >
          新增
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        节目管理
      </div>
      <el-table
        border
        stripe
        ref="multipleTable"
        tooltip-effect="dark"
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="id"
          label="编号"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          prop="sync_status"
          label="状态"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status===1?'success':'danger'">
              {{ scope.row.status===1?'激活':'禁用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-link
              :type="scope.row.status === 0?'primary':'danger'"
              size="small"
              @click="changeStatus(scope.row)"
            >
              {{ scope.row.status===0?'激活':'禁用' }}
            </el-link>
            <el-link
              type="primary"
              size="small"
              @click="$refs.dialog.show(scope.row,'edit')"
            >
              编辑
            </el-link>

            <el-link
              type="primary"
              size="small"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="getList"
        :total="total">
      </el-pagination>
    </div>

    <Dialog
      ref="dialog"
      @update="getList"></Dialog>
  </div>
</template>

<script>
import { list, deletePerform, updatePerform } from '@/api/perform'
import Dialog from './Dialog'

export default {
  created () {
    this.getList()
  },
  components: {
    Dialog
  },
  data () {
    return {
      form: {
        keyWords1: '',
        keyWords2: '',
        keyWords3: '',
        keyWords4: ''
      },
      total: 1,
      loading: false,
      tableData: [],
      multipleSelection: []
    }
  },
  methods: {
    changeStatus (e) {
      e.status = e.status ? 0 : 1
      let params = JSON.parse(JSON.stringify(e))
      console.log('params', params)
      updatePerform(params).then(res => {
        this.handleClose()
      }).finally(() => {
        this.loading = false
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList () {
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.form))
      list(params).then(res => {
        this.tableData = res.list
        this.total = res.total
        console.log(this.tableData)
        // 更新全局节目信息
        this.$store.commit('SET_PERFORM_LIST', res.list)
      }).finally(() => {
        this.loading = false
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleDelete (id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePerform(id).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.form-wrap {
  .el-input,.el-select,.el-date-editor,.el-date-editor.el-input,.el-input-number {
    width: 300px;
  }
}

</style>
