<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-input
        v-model="search"
        placeholder="输入名称进行搜索"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button type="primary" @click="handleAdd" icon="el-icon-plus">新增</el-button>
    </div>
<el-table
      :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%;margin-top:10px;"
      border
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
      row-key="id"
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="所属学科">
        <template slot-scope="scope">
          <el-tag type="success" >{{scope.row.subject_name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否可组织考试">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.can_exam">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否公开">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.is_public">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.sortnum }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope)"
            icon="el-icon-edit"
            :disabled="!checkPermission(['workrule_edit'])"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope)"
            icon="el-icon-delete"
            :disabled="!checkPermission(['workrule_delete'])"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getWorkScopeAll,
  deleteWorkScope
} from "@/api/examtest";
import checkPermission from "@/utils/permission";

export default {
  data() {
    return {
      search: "",
      tableData: [],
      listLoading: true,
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getWorkScopeAll().then(response => {
        this.tableData = response.data;
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.search = ""
      this.getList();
    },
    handleAdd() {
      this.$router.push({path:"/sjmanage/workscope/create"})
    },
    handleEdit(scope) {
      this.$router.push({path:"/sjmanage/workscope/update",query:{id:scope.row.id}})
    },
    handleDelete(scope) {
      this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteWorkScope(scope.row.id).then(response => {
            this.$message({
                type: 'success',
                message: '删除成功!'
              });
          this.getList()
      });
          
        }).catch(() => {      
        });
    },
  }
};
</script>
