<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-select
        v-model="listQuery.type"
        placeholder="模考类型"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option
          v-for="item in typeOptions"
          :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-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"
    >
      <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">{{ scope.row.consumer_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.paper_name }}</template>
      </el-table-column>
      <el-table-column align="left" label="得分">
        <template slot-scope="scope">{{ scope.row.score }}</template>
      </el-table-column>
      <el-table-column align="left" label="耗时(秒)">
        <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="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleDetail(scope)"
            icon="el-icon-more"
          ></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 } 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,
      typeOptions: [
        { 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.getList();
  },
  methods: {
    checkPermission,
    getList(query = this.listQuery) {
      this.listLoading = true;
      getExamTestlist(query).then(response => {
        this.tableData = response.data;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        search:'',
      };
      this.value = []
      this.getList();
    },
    handleDetail(scope) {
    //   this.dialogVisible = true
    //   this.question = scope.row
    },
    exportTest() {
      const loading = this.$loading();
      exportTest(this.listQuery).then(response => {
        loading.close()
        window.open(response.data.path, "_blank");
      });
    },
    setTimeRange(){
      this.listQuery.start = this.value[0],
      this.listQuery.end = this.value[1],
      this.getList()
    }
  }
};
</script>
