<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-select
        v-model="listQuery.is_pass"
        placeholder="是否通过"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option
          v-for="item in passOptions"
          :key="item.key"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
<el-date-picker
      v-model="value"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions">
    </el-date-picker>
    <el-input
        v-model="listQuery.search"
        placeholder="输入用户名或用户单位名搜索"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh-left"
        @click="resetFilter"
      >刷新重置</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportTest" >导出Excel</el-button>
          <div style="margin-top:10px">
</div>
    </div>
<el-table
      :data="tableData.results"
      style="width: 100%;margin-top:10px;"
      border
      stripe
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
      @sort-change="changeSort"
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="类型">
        <template slot-scope="scope">{{ scope.row.type }}</template>
      </el-table-column>
      <el-table-column align="left" label="用户">
        <template slot-scope="scope" v-if="scope.row.consumer_detail">{{ scope.row.consumer_detail.name }}</template>
      </el-table-column>
      <el-table-column align="left" label="单位">
        <template slot-scope="scope" v-if="scope.row.consumer_detail">{{ scope.row.consumer_detail.company_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="得分" sortable='custom' prop="score">
        <template slot-scope="scope">{{ scope.row.score }}</template>
      </el-table-column>
      <el-table-column align="left" label="耗时(时分秒)" sortable='custom' prop="took">
        <template slot-scope="scope">{{ scope.row.took_format }}</template>
      </el-table-column>
      <el-table-column align="left" label="答题时间">
        <template slot-scope="scope">{{ scope.row.start_time }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.type=='正式考试'"
            type="primary"
            size="small"
            @click="handleIssue(scope)"
          >发证</el-button>
          <el-button
          v-if="scope.row.type=='正式考试'"
            type="primary"
            size="small"
            @click="handleExport(scope)"
          >生成Word</el-button>
          <el-button
          v-if="scope.row.type=='正式考试'"
            type="warning"
            size="small"
            @click="handleExport2(scope)"
          >重新生成</el-button>
          <el-button
          v-if="scope.row.type=='正式考试'"
            type="danger"
            size="small"
            @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.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getExamTestlist, exportTest, exportwTest, deleteExamtest,issue } from "@/api/examtest";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";

const listQuery = {
  page: 1,
  limit: 20,
  type:'正式考试',
  search:''
};
export default {
  components: { Pagination },
  data() {
    return {
      listQuery: Object.assign({}, listQuery),
      tableData: {
          count:0,
          results:[],
      },
      listLoading: true,
      typeOptions: [
        { key: "自助模考", label: "自助模考", value: "自助模考" },
        { key: "押卷模考", label: "押卷模考", value: "押卷模考"},
        { key: "正式考试", label: "正式考试", value: "正式考试"},
      ],
      passOptions: [
        { key: true, label: "通过", value: true },
        { key: false, label: "未通过", value: false},
      ],
      pickerOptions: {
          shortcuts: [{
            text: '最近一天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        value: '',
    };
  },
  computed: {},
  watch:{
    value:'setTimeRange',
  },
  created() {
    this.getQuery();
  },
  methods: {
    checkPermission,
    getQuery() {
      if(this.$route.params.exam){
        this.listQuery.exam = this.$route.params.exam;
        this.getList()
      }else{
        this.getList()
      }

    },
    getList() {
      this.listLoading = true;
      getExamTestlist(this.listQuery).then(response => {
        this.tableData = response.data;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        type:'正式考试',
        search:'',
      };
      this.value = []
      this.getList();
    },
    handleExport(scope) {
    const loading = this.$loading({text: '正在生成word...',});
    exportwTest(scope.row.id).then(res=>{
      loading.close()
      window.open(res.data.path, "_blank");
    }).catch(e=>{loading.close()})
    },
    handleDelete(scope){
this.$confirm("确认删除该考试记录吗?将丢失数据!", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error"
      })
        .then(async () => {
          await deleteExamtest(scope.row.id);
          this.getList()
          this.$message({
            type: "success",
            message: "成功删除!"
          });
        })
        .catch(err => {
          console.error(err);
        });
    },
    handleExport2(scope) {
    const loading = this.$loading({text: '正在重新生成word...',});
    exportwTest(scope.row.id, {anew:true}).then(res=>{
      loading.close()
      window.open(res.data.path, "_blank");
    }).catch(e=>{loading.close()})
    },
    exportTest() {
      const loading = this.$loading();
      exportTest(this.listQuery).then(response => {
        loading.close()
        window.open(response.data.path, "_blank");
      });
    },
    handleIssue(scope) {
      const loading = this.$loading({text: '正在发证..',});
      issue(scope.row.id).then(res=>{
        loading.close()
        this.$message.success('发证成功')
      }).catch(e=>{loading.close()})
    },
    setTimeRange(){
      this.listQuery.start = this.value[0],
      this.listQuery.end = this.value[1],
      this.getList()
    },
    changeSort (val) {
      if(val.order == 'ascending'){
        this.listQuery.ordering = val.prop
      }else{
        this.listQuery.ordering = '-' + val.prop
      }
    
      this.getList()

    },
    
  }
};
</script>
