<template>
  <div class="student-score">
    <div class="part-box">
      <Form ref="FilterForm" label-position="left" inline>
        <FormItem label="工作文件夹：" v-if="IS_INSPECTION">
          <Select
            v-model="filter.workId"
            placeholder="请选择工作文件夹"
            style="width: 150px"
          >
            <Option
              v-for="(work, windex) in works"
              :key="windex"
              :value="work.id"
              :label="work.name"
            ></Option>
          </Select>
        </FormItem>
        <FormItem label="考区：" v-if="IS_INSPECTION">
          <Select v-model="filter.areaCode" placeholder="请选择考区">
            <Option value=""></Option>
          </Select>
        </FormItem>

        <FormItem label="科目：">
          <Select
            v-model="filter.subject"
            placeholder="请选择科目"
            style="width: 100px;"
            clearable
          >
            <Option
              v-for="(item, index) in subjects"
              :key="index"
              :value="item.subject"
              :label="item.name"
            ></Option>
          </Select>
        </FormItem>

        <FormItem label="号码：">
          <Select
            v-model="filter.type"
            placeholder="号码类型"
            style="width: 100px"
          >
            <Option
              v-for="(val, key) in CODE_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></Option>
          </Select>
        </FormItem>
        <FormItem>
          <Input
            v-model.trim="filter.number"
            placeholder="请输入号码"
            clearable
          ></Input>
        </FormItem>
        <FormItem label="姓名：">
          <Input
            v-model.trim="filter.studentName"
            placeholder="请输入姓名"
            clearable
          ></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" icon="ios-search" @click="toSearch"
            >查询</Button
          >
        </FormItem>
      </Form>

      <div class="student-score-content">
        <table class="table" v-if="curStudent.name">
          <tr>
            <td></td>
            <td :colspan="curStudent.scores.length">
              <image-action-list
                :data="curStudent.scores"
                :column-number="3"
                loop
                ref="ImageActionList"
              ></image-action-list>
            </td>
          </tr>
          <tr>
            <td>姓名</td>
            <td :colspan="curStudent.scores.length">{{ curStudent.name }}</td>
          </tr>
          <tr>
            <td>考号</td>
            <td :colspan="curStudent.scores.length">
              {{ curStudent.examNumber }}
            </td>
          </tr>
          <tr>
            <td>总分</td>
            <td :colspan="curStudent.scores.length">
              {{ curStudent.sumScore }}
            </td>
          </tr>
          <tr>
            <td>档位</td>
            <td v-for="(score, sindex) in curStudent.scores" :key="sindex">
              {{ score.level || "空" }}
            </td>
          </tr>
          <tr>
            <td>分数</td>
            <td v-for="(score, sindex) in curStudent.scores" :key="sindex">
              {{ score.score || "空" }}
            </td>
          </tr>
        </table>
      </div>

      <div class="part-page" v-if="total > 1">
        <Page
          :current="current"
          :total="total"
          :page-size="size"
          show-total
          show-elevator
          @on-change="toPage"
        ></Page>
      </div>
    </div>
  </div>
</template>

<script>
import { studentScoreList, workList, subjectList } from "@/api";
import { CODE_TYPE } from "@/constants/enumerate";
import ImageActionList from "./components/ImageActionList";

export default {
  name: "student-score",
  components: { ImageActionList },
  data() {
    return {
      filter: {
        workId: this.$route.params && this.$route.params.workId,
        areaCode: "",
        subject: "",
        type: "",
        number: "",
        studentName: ""
      },
      CODE_TYPE,
      IS_INSPECTION: false,
      works: [],
      subjects: [],
      current: 1,
      total: 0,
      size: 1,
      curStudent: { name: "" },
      students: []
    };
  },
  mounted() {
    this.IS_INSPECTION =
      this.$ls.get("user", { role: "" }).role === "INSPECTION";
    if (this.IS_INSPECTION) this.getWorkList();
    this.getSubjects();
  },
  methods: {
    async getWorkList() {
      const data = await workList();
      this.works = data.map(item => {
        return {
          id: item.id,
          name: item.name
        };
      });
    },
    async getSubjects() {
      const data = await subjectList(this.filter.workId);
      this.subjects = data.filter(item => item.enable);
    },
    async toSearch() {
      if (!this.filter.number && !this.filter.studentName) {
        this.$Message.error("号码和姓名必须填写一个");
        return;
      }
      const data = await studentScoreList(this.filter);
      data.map(student => {
        student.scores.map(score => {
          score.title = score.subjectName;
        });
      });
      this.students = data;
      this.total = this.students.length;
      this.toPage(1);
    },
    toPage(page) {
      this.current = page;
      this.curStudent = this.students[page - 1];
    }
  }
};
</script>
