<template>
  <div class="business-data-detail">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select
              v-model="filter.semesterId"
              default-select
            ></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
              default-select
              @default-selected="getList"
            ></exam-select>
          </el-form-item>
          <el-form-item label="印刷计划：">
            <print-plan-select
              v-model.trim="filter.printPlanIdList"
              placeholder="印刷计划"
              clearable
              multiple
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              @change="printPlanChange"
            ></print-plan-select>
          </el-form-item>
          <el-form-item label="课程（代码）：" label-width="110px">
            <course-select
              v-model.trim="filter.courseId"
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              :print-plan-id="filter.printPlanIdList"
              placeholder="课程（代码）"
              clearable
            ></course-select>
          </el-form-item>
          <el-form-item label="试卷编号：">
            <paper-number-select
              ref="PaperNumberSelect"
              v-model="filter.paperNumber"
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              :print-plan-id="filter.printPlanIdList"
              :course-id="filter.courseId"
              placeholder="试卷编号"
              clearable
            ></paper-number-select>
          </el-form-item>
          <el-form-item label="考点：" label-width="55px">
            <place-select
              v-model.trim="filter.examPlace"
              :exam-id="filter.examId"
              :print-plan-id="filter.printPlanIdList"
              placeholder="考点"
              clearable
            ></place-select>
          </el-form-item>
          <el-form-item label="考场：" label-width="55px">
            <room-select
              v-model.trim="filter.examRoom"
              :exam-id="filter.examId"
              :print-plan-id="filter.printPlanIdList"
              placeholder="考场"
              clearable
            ></room-select>
          </el-form-item>
          <el-form-item label="考试日期：">
            <el-date-picker
              v-model="createTime"
              type="daterange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="考试开始日期"
              end-placeholder="考试结束日期"
              value-format="timestamp"
              align="right"
              unlink-panels
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="考生：" label-width="70px">
            <el-input
              v-model.trim="filter.studentParams"
              placeholder="学号/姓名"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="studentCode" label="学号" width="150">
        </el-table-column>
        <el-table-column prop="studentName" label="姓名" min-width="140">
        </el-table-column>
        <el-table-column
          prop="printPlanName"
          label="印刷计划"
          min-width="240"
        ></el-table-column>
        <el-table-column
          prop="courseNameCode"
          label="课程（代码）"
          min-width="260"
        >
        </el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          width="160"
        ></el-table-column>
        <el-table-column prop="examDate" label="考试日期" width="100">
        </el-table-column>
        <el-table-column prop="examTime" label="考试时间" width="100">
        </el-table-column>
        <el-table-column prop="examPlace" label="考点" min-width="160">
        </el-table-column>
        <el-table-column prop="examRoom" label="考场" min-width="160">
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'preview')"
              class="btn-primary"
              type="text"
              @click="toPreview(scope.row)"
              >预览</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>

    <!-- PreviewBusinessStudentDetail -->
    <preview-business-student-detail
      :instance="curRow"
      ref="PreviewBusinessStudentDetail"
    ></preview-business-student-detail>
  </div>
</template>

<script>
import { businessDataDetailListPage } from "../api";
import { parseTimeRangeDateAndTime } from "@/plugins/utils";
import PreviewBusinessStudentDetail from "../components/PreviewBusinessStudentDetail";
import pickerOptions from "@/constants/datePickerOptions";

export default {
  name: "business-data-detail",
  components: { PreviewBusinessStudentDetail },
  data() {
    return {
      filter: {
        printPlanIdList: [],
        courseId: "",
        paperNumber: "",
        examPlace: "",
        examRoom: "",
        studentParams: "",
        startDate: "",
        endDate: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      // date-picker
      createTime: [],
      pickerOptions,
    };
  },
  mounted() {
    // this.search();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (this.createTime) {
        datas.startDate = this.createTime[0];
        datas.endDate = this.createTime[1];
      }
      const data = await businessDataDetailListPage(datas);
      this.dataList = data.records.map((item) => {
        const { date, time } = parseTimeRangeDateAndTime(
          item.examStartTime,
          item.examEndTime
        );
        item.examDate = date || "--";
        item.examTime = time || "--";
        return item;
      });
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    printPlanChange() {
      this.filter.examRoom = "";
      this.filter.examPlace = "";
    },
    toPreview(row) {
      this.curRow = row;
      this.$refs.PreviewBusinessStudentDetail.open();
    },
  },
};
</script>
