<template>
  <div class="app-container">
      
        <div style="margin-top:6px">
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
        </div>
        <el-table v-loading="listLoading"
                  :data="train.results"
                  style="width: 100%;margin-top:10px;"
                  border
                  fit
                  stripe
                  highlight-current-row
                  max-height="600">
          <el-table-column type="index" width="50" />
          <el-table-column align="header-center" label="培训主题">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="开始时间">
            <template slot-scope="scope">
              {{ scope.row.start_time }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="结束时间">
            <template slot-scope="scope">
              {{ scope.row.end_time }}
            </template>
          </el-table-column>
          <el-table-column align="header-center" label="学时">
            <template slot-scope="scope">
              {{ scope.row.hours }}
            </template>
          </el-table-column>
          <el-table-column align="header-center" label="培训机构">
            <template slot-scope="scope">
              {{ scope.row.train_org }}
            </template>
          </el-table-column>
         
          <el-table-column label="培训讲师">
            <template slot-scope="scope">
              <span>{{ scope.row.teacher }}</span>
            </template>
          </el-table-column>
          <el-table-column label="培训内容">
            <template slot-scope="scope">
              <span>{{ scope.row.content }}</span>
            </template>
          </el-table-column>
          <el-table-column label="培训备注">
            <template slot-scope="scope">
              <span>{{ scope.row.remark }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="260px">
            <template slot-scope="scope">
              <el-button type="primary"
                         size="small"
                         :disabled="!checkPermission(['implementrule_update'])"
                         @click="handleUpdate(scope)">编辑</el-button>
              <el-button type="danger"
                         size="small"
                         :disabled="!checkPermission(['implementrule_delete'])"
                         @click="handleDelete(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="train.count>0"
          :total="train.count"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
  </div>
</template>

<script>
import { getTrainList,deleteTrain,createTrain } from "@/api/train"
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 {
  components: { Pagination, Treeselect },
  data() {
    return {
      train: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20
      },
      typeOptions: [],
    };
  },
  created() {
    this.getList();
    this.getTypeOptions()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getTrainList(this.listQuery).then(response => {
        if (response.data) {
          this.train = response.data
        }
        this.listLoading = false
      });
    },
    getTypeOptions() {
      getDictList({type__code:'cert_field'}).then(res=>{
        this.typeOptions = genTree(res.data)
      })
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({path:"/employee/train/create"})
    },
    handleUpdate(scope) {
      this.$router.push({path:"/employee/train/update",query:{id:scope.row.id}})
    },
   handleDelete(scope) {
      this.$confirm('确定删除本条数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteTrain(scope.row.id).then(res=>{
        this.$message.success('删除成功')
              this.getList()
    })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
},
  }
};
</script>
