<template>
  <div class="app-container">
        <div>
        <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" >{{ scope.row.school }}</template>
          </el-table-column>
          <el-table-column label="所学专业">
            <template slot-scope="scope">{{ scope.row.major }}</template>
          </el-table-column>
          <el-table-column label="文化程度">
            <template slot-scope="scope">{{ scope.row.level_.name }}</template>
          </el-table-column>
          <el-table-column label="入学日期">
            <template slot-scope="scope">{{ scope.row.admission_date }}</template>
          </el-table-column>
          <el-table-column label="毕业日期">
            <template slot-scope="scope">{{ scope.row.graduation_date }}</template>
          </el-table-column>
                <el-table-column align="center" label="操作" width="260px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            :disabled="!checkPermission(['education_update'])"
            @click="handleUpdate(scope)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="small"
            :disabled="!checkPermission(['education_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" >
        <Educationform ref="Educationform" :formData="formData" :action="dgType" @close="closeDg" :levelOptions="levelOptions"></Educationform>
        </el-dialog>
  </div>
  
</template>
<script>
import { getEducationList, deleteEducation} from "@/api/education"
import  Educationform from "@/views/education/educationform"
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:'Education',
  props:['employee'],
  components: { Pagination, Treeselect, Educationform },
  data() {
    return {
      tableData: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20,
      },
      levelOptions: [],
      formData:{},
      dgType:'create',
      dgVisiable:false
    };
  },
  created() {
    this.listQuery.employee = this.employee
    this.getList();
    this.getTypeOptions()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getEducationList(this.listQuery).then(response => {
        if (response.data) {
          this.tableData = response.data
        }
        this.listLoading = false
      });
    },
    getTypeOptions() {
      getDictList({type__code:'education_level'}).then(res=>{
        this.levelOptions = genTree(res.data)
      })
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
        employee: this.employee
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.formData = Object.assign({}, {employee:this.employee})
      this.dgType = 'create'
      this.dgVisiable = true
      // this.$refs.Educationform.formData = this.formData
      // this.$refs.Educationform.action = this.dgType
    },
    handleUpdate(scope) {
      this.formData = Object.assign({}, scope.row)
      this.dgType = 'update'
      this.dgVisiable = true
    },
    handleDelete(scope) {
      deleteEducation(scope.row.id).then(res=>{
        this.$message.success('成功')
        this.getList()
      })
    },
    closeDg(val) {
      if(val){
        this.getList()
      }
      this.dgVisiable=false
    },
  }
};
</script>
