<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-input
        v-model="listQuery.search"
        placeholder="名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleSearch"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh-left"
        @click="resetFilter"
      >刷新重置</el-button>
    </div>
    <div style="margin-top:10px">
    <el-button type="primary" @click="handleAdd" icon="el-icon-plus">新增</el-button>
    </div>
<el-table
      :data="tableData.results"
      style="width: 100%;margin-top:10px;"
      border
      stripe
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column align="left" label="工作类别">
        <template slot-scope="scope">{{ scope.row.workscope_name }}</template>
      </el-table-column>
      <el-table-column align="left" label="总分">
        <template slot-scope="scope">{{ scope.row.total_score }}</template>
      </el-table-column>
      <el-table-column align="left" label="通过分">
        <template slot-scope="scope">{{ scope.row.pass_score }}</template>
      </el-table-column>
      <el-table-column align="left" label="限时(分钟)">
        <template slot-scope="scope">{{ scope.row.limit }}</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(['paper_update'])"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            @click="handleClone(scope)"
            :disabled="!checkPermission(['paper_clone'])"
          >克隆</el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope)"
            icon="el-icon-delete"
            :disabled="!checkPermission(['paper_delete'])"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.count>0"
      :total="tableData.count"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getPaperList, deletePaper, clonePaper } from "@/api/examtest";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";

const listQuery = {
  page: 1,
  limit: 20,
  search:''
};
export default {
  components: { Pagination },
  data() {
    return {
      listQuery: Object.assign({}, listQuery),
      tableData: {
          count:0,
          results:[],
      },
      listLoading: true,
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList(query = this.listQuery) {
      this.listLoading = true;
      getPaperList(query).then(response => {
        if(response.data.results){
            this.tableData = response.data;
        }
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        search:''
      };
      this.getList();
    },
    handleSearch() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleAdd() {
      this.$router.push({path:"/sjmanage/paper/create"})
    },
    handleEdit(scope) {
    this.$router.push({path:"/sjmanage/paper/update",query:{id:scope.row.id}})
    },
    handleClone(scope) {
      const loading = this.$loading({
          text: '克隆中..',
        });
      clonePaper(scope.row.id).then(res=>{
        this.getList()
        loading.close()
      }).catch(e=>{loading.close()})
    },
    handleDelete(scope) {
      this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deletePaper(scope.row.id).then(response => {
            this.$message({
                type: 'success',
                message: '删除成功!'
              });
          this.getList()
      });
          
        }).catch(() => {      
        });
    },
  }
};
</script>
