<template>
  <div class="grade-action">
    <!-- 查询 -->
    <div class="action-search" v-if="rights.search">
      <Input v-model.trim="filter.code" placeholder="请输入号码">
        <Select
          v-model="filter.codeType"
          placeholder="类型"
          slot="prepend"
          style="width: 100px"
        >
          <Option
            v-for="(val, key) in CODE_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></Option>
        </Select>
        <Button icon="ios-search" slot="append" @click="searchCode"></Button>
      </Input>
    </div>
    <!-- 头部信息 ------ -->
    <!-- 试卷状态 -->
    <!-- 状态：已评，待评，打回，仲裁 -->
    <div class="action-paper-state">
      <p class="paper-state-cont">{{ stepLabel }}</p>
    </div>
    <!-- 试卷信息 -->
    <div class="action-paper-info">
      <p v-if="IS_ADMIN">{{ curPaper.examNumber }}</p>
      <p>NO.{{ curPaper.sn }}</p>
    </div>
    <!-- 档位信息 -->
    <!-- 已评（已评档位），打回（建议档位） -->
    <div class="action-grade-info" v-if="rights.gradeInfo">
      <p v-if="curPaper.rejected">建议档位</p>
      <h3 class="grade-info-name">{{ curLevel.name }}</h3>
      <p class="grade-info-range">
        <span>{{ curLevel.minScore }}</span>
        <span>~</span>
        <span>{{ curLevel.maxScore }}</span>
      </p>
    </div>
    <!-- 选择档位 -->
    <div class="action-grade-list" v-if="rights.gradeList">
      <div
        class="action-grade-list-item"
        v-for="(level, index) in levels"
        :key="index"
      >
        <p @click="selectLevel(level)">{{ level.name }}</p>
        <p>{{ level.minScore }}~{{ level.maxScore }}</p>
      </div>
    </div>
    <div class="action-grade-pass" v-if="rights.gradeList && IS_MARKER">
      <Button type="primary" @click="toPass">跳过</Button>
    </div>

    <!-- 评卷记录 -->
    <div class="action-grade-history" v-if="rights.gradeHis">
      <h3>评卷记录</h3>
      <div class="action-grade-list grade-history-list">
        <div
          class="action-grade-list-item"
          v-for="(his, hindex) in gradingHistory"
          :key="hindex"
        >
          <p>{{ his.name }}</p>
          <p>{{ his.value }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { markHistoryList } from "@/api";
import { CODE_TYPE } from "@/constants/enumerate";
// 三种情况：
// 管理员(ADMIN)，科组长(MARK_LEADER)，评卷员(MARKER)
// 管理员：查询，头部信息，评卷记录
// 科组长：查询，头部信息，选择档位，评卷记录
// 评卷员：头部信息，选择档位
/*
[curpaper template]
{
  "id": 165,
  "sn": "029947536",
  "examNumber": "1901040084",
  "level": "A",
  "score": null,
  "redoLevel": null,
  "updatedOn": 1591767742000,
  "imgSrc": "",
  "thumbSrc": "",
  "markByLeader": false,
  "markedLogic": true,
  "areaCode": "2",
  "inspectScore": null,
  "inspectLevel": null,
  "inspector": null,
  "sheetSrc": null,
  "stage": "LEVEL",
  "test": 0,
  "paperTest": 0,
  "markResults": [],
  "rejected": false,
  "arbitrated": false,
  "sample": false,
  "tagged": false,
  "missing": false,
  "manual": false
}
*/

const initRights = {
  search: false,
  gradeInfo: false,
  gradeList: false,
  gradeHis: false
};

export default {
  name: "grade-action",
  props: {
    curPaper: {
      type: Object,
      default() {
        return {};
      }
    },
    levels: {
      type: Array,
      default() {
        return [];
      }
    },
    userRole: {
      type: String,
      default: "MARKER"
    }
  },
  data() {
    return {
      rights: {
        ...initRights
      },
      roleRight: {
        ADMIN: {
          done: ["search", "gradeHis", "gradeInfo"],
          arbitrate: ["search", "gradeHis"]
        },
        MARK_LEADER: {
          done: ["search", "gradeList", "gradeHis", "gradeInfo"],
          arbitrate: ["search", "gradeList", "gradeHis"]
        },
        MARKER: {
          done: ["gradeList", "gradeInfo"],
          undo: ["gradeList"],
          reject: ["gradeList", "gradeInfo"]
        },
        STANDARD: ["gradeInfo"]
      },
      filter: {
        codeType: "examNumber",
        code: ""
      },
      CODE_TYPE,
      stepDict: {
        undo: "待评",
        done: "已评",
        reject: "打回",
        arbitrate: "仲裁",
        sample: "标准卷"
      },
      stepType: "",
      stepLabel: "",
      gradingHistory: [],
      curLevel: {}
    };
  },
  computed: {
    IS_ADMIN() {
      return this.userRole === "ADMIN";
    },
    IS_MARKER() {
      return this.userRole === "MARKER";
    },
    IS_MARK_LEADER() {
      return this.userRole === "MARK_LEADER";
    }
  },
  watch: {
    curPaper(val) {
      this.rebuildRight();
    }
  },
  mounted() {
    this.rebuildRight();
  },
  methods: {
    getStepType() {
      const paper = this.curPaper;
      if (paper.sample) return "sample";
      if (paper.level) return "done";
      if (paper.arbitrated) return "arbitrate";
      if (paper.rejected) return "reject";
      if (!paper.rejected && !paper.arbitrated && !paper.level) return "undo";
      return;
    },
    rebuildRight() {
      let roleRights = [];
      this.stepType = this.getStepType();
      this.stepLabel = this.stepDict[this.stepType];
      if (this.stepType === "sample") {
        roleRights = this.roleRight.STANDARD;
      } else {
        roleRights = this.roleRight[this.userRole][this.stepType] || [];
      }
      this.rights = { ...initRights };
      roleRights.map(key => {
        this.rights[key] = true;
      });
      if (this.curPaper.level) {
        this.curLevel = this.levels.find(
          level => level.name === this.curPaper.level
        );
      }
      if (this.rights.gradeHis) {
        this.getMarkHistory();
      }
    },
    async getMarkHistory() {
      const data = await markHistoryList(this.curPaper.id, "LEVEL");
      this.gradingHistory = data.map(item => {
        return {
          id: item.markerId,
          name: item.marker,
          value: item.result
        };
      });
    },
    selectLevel(level) {
      if (this.IS_MARK_LEADER) {
        this.$emit(
          "on-leader-level",
          {
            curPaperId: this.curPaper.id,
            curLevel: this.curPaper.level,
            selectLevel: level.name
          },
          this.gradingHistory
        );
        return;
      }
      this.$emit("on-select-level", level);
    },
    toPass() {
      this.$emit("on-pass");
    },
    searchCode() {
      this.$emit("on-code-search", this.filter);
    }
  }
};
</script>
