<template>
  <div class="app-container">
        <div style="display:flex">
          <treeselect v-model="listQuery.cert_field" :multiple="false" :options="cert_fieldOptions" :disable-branch-nodes="true" placeholder="认证领域" @input="handleFilter" style="width: 260px" clearable/>
          <el-input
            v-model="listQuery.search"
            placeholder="专业代码"
            style="width: 300px;margin-left:10px"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <el-button
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >搜索</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-refresh-left"
            @click="resetFilter"
          >刷新重置</el-button>
        </div>
        <div style="margin-top:6px">
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
        </div>
        <el-table
          v-loading="listLoading"
          :data="tableData.results"
          style="width: 100%;margin-top:10px;"
          border
          fit
          stripe
          highlight-current-row
          max-height="500"
        >
          <el-table-column type="index" width="50" />
          <el-table-column label="认证领域">
            <template slot-scope="scope" v-if="scope.row.cert_field_">{{ scope.row.cert_field_.fullname }}</template>
          </el-table-column>
          <el-table-column label="专业">
            <template slot-scope="scope">
              <span v-if="scope.row.cert_field_.code=='CCC'">{{scope.row.major_rule_code}}</span>
              <span v-else>{{scope.row.major_.fullname}}</span>
            </template>
          </el-table-column>
          <el-table-column label="能力来源">
            <template slot-scope="scope">{{ scope.row.afrom_.name }}</template>
          </el-table-column>
          <el-table-column label="能力项">
            <template slot-scope="scope">
              <el-tag v-for="(item, index) in scope.row.auditor_abilitys_" :key="index" style="margin:2px">{{item.name}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="评定日期">
            <template slot-scope="scope">{{ scope.row.access_date }}</template>
          </el-table-column>
          <el-table-column label="是否暂停">
            <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.is_paused">是</el-tag>
              <el-tag type="success" v-else>否</el-tag></template>
          </el-table-column>
                <el-table-column align="center" label="操作" width="260px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            :disabled="!checkPermission(['ability_update'])"
            @click="handleUpdate(scope)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="small"
            :disabled="!checkPermission(['ability_delete'])"
            @click="handleDelete(scope)"
          >删除</el-button>
        </template>
                </el-table-column>
        </el-table>

        <pagination
          v-show="tableData.count>0"
          :total="tableData.count"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
        <el-dialog :title="dgType=='create'?'新增':'编辑'" :visible.sync="dgVisiable" :close-on-click-modal="false" >
        <Abilityform ref="Abilityform" :formData="formData" :action="dgType" @close="closeDg" :cert_fieldOptions="cert_fieldOptions"></Abilityform>
        </el-dialog>
  </div>
  
</template>
<script>
import { getAbilityList, createAbility, updateAbility, deleteAbility} from "@/api/ability"
import  Abilityform from "@/views/ability/abilityform"
import { getDictList } from "@/api/dict"
import Pagination from "@/components/Pagination"
import checkPermission from '@/utils/permission'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { genTree } from '../../utils'
export default {
  name:'Ability',
  props:['employee'],
  components: { Pagination, Treeselect, Abilityform },
  data() {
    return {
      tableData: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20,
        is_enabled:true
      },
      cert_fieldOptions: [],
      formData:{},
      dgType:'create',
      dgVisiable:false
    };
  },
  created() {
    this.listQuery.employee = this.employee
    this.getList();
    this.getTypeOptions()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getAbilityList(this.listQuery).then(response => {
        if (response.data) {
          this.tableData = response.data
        }
        this.listLoading = false
      });
    },
    getTypeOptions() {
      getDictList({type__code:'cert_field'}).then(res=>{
        this.cert_fieldOptions = genTree(res.data)
      })
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
        is_enabled:true,
        employee: this.employee
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.formData = Object.assign({}, {employee:this.employee, is_paused:false})
      this.dgType = 'create'
      this.dgVisiable = true
      // this.$refs.Abilityform.formData = this.formData
      // this.$refs.Abilityform.action = this.dgType
    },
    handleUpdate(scope) {
      this.formData = Object.assign({}, scope.row)
      this.dgType = 'update'
      this.dgVisiable = true
    },
    handleDelete(scope) {
      deleteAbility(scope.row.id).then(res=>{
        this.$message.success('成功')
        this.getList()
      })
    },
    closeDg(val) {
      if(val){
        this.getList()
      }
      this.dgVisiable=false
    },
  }
};
</script>
