<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row :gutter="20">
        <el-form label-width="80px">
          <el-col :span="6">
            <el-form-item label="描述">
              <el-input v-model="form.KEYWORDS" placeholder="按关键字查询" class="filter-item" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="归属类型">
              <el-select v-model="form.WORK_TYPE" clearable placeholder="请选择">
                <el-option value="1" label="主要负责人" />
                <el-option value="2" label="技术负责人" />
                <el-option value="3" label="操作负责人" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label-width="10px">
              <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
                搜索
              </el-button>
              <el-button v-waves class="filter-item" type="success" icon="el-icon-search" @click="resetting">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="TERM_DESCRIBE" label="描述" />
      <el-table-column label="归属类型" >
        <template slot-scope="{row}">
          <span v-if="row.WORK_TYPE === '1'">主要负责人</span>
          <span v-if="row.WORK_TYPE === '2'">技术负责人</span>
          <span v-if="row.WORK_TYPE === '3'">操作负责人</span>
        </template>
      </el-table-column>
      <el-table-column prop="CREATE_TIME" label="创建时间" />
      <el-table-column prop="SORT" label="顺序" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="{row}">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row)">编辑</el-button>
            </el-col>
            <el-col :span="8">
              <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.TERM_LIBRARY_ID)">删除</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button v-show="false" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <edit ref="edit" :title="title" href="edit" @beforeClose="getList"/>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
import edit from './edit.vue'
export default {
  components: { edit, Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      title: '新增',
      form: {
        KEYWORDS: '',
        WORK_TYPE: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.HIDDEN_LIBRARY_ID
    },
    getQuery() {
      this.getList()
    },
    getList() {
      this.listLoading = true
      requestFN(
        '/riskTermLibrary/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        this.form
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    handleAdd() {
      this.$refs.edit.init()
      this.title = '新增'
    },
    handleEdit(row) {
      this.$refs.edit.init(row)
      this.title = '编辑'
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/riskTermLibrary/delete',
          { TERM_LIBRARY_ID: id }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.TERM_LIBRARY_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/riskTermLibrary/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    hasButton: function() {
      var keys = 'hiddenTermLibrary:add,hiddenTermLibrary:del,hiddenTermLibrary:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.hiddenTermLibraryfhadminadd		// 新增权限
        this.del = data.hiddenTermLibraryfhadmindel		// 删除权限
        this.edit = data.hiddenTermLibraryfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    resetting() {
      this.form = {
        KEYWORDS: '',
        WORK_TYPE: ''
      }
      this.getList()
    },

    getResult(e) {
      this.getList()
    }
  }
}

</script>
