<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="search"></Button>
      </Input>
    </div>
    <!-- 头部信息 ------ -->
    <!-- 试卷状态 -->
    <!-- 状态：已评，待评，打回，仲裁 -->
    <div class="action-paper-state" v-if="rights.paperState">
      <p class="paper-state-cont">{{ step.typeName }}</p>
    </div>
    <!-- 试卷信息 -->
    <div class="action-paper-info" v-if="rights.paperInfo">
      <p v-if="IS_ADMIN">{{ curPaper.examNumber }}</p>
      <p>NO.{{ curPaper.sn }}</p>
    </div>
    <!-- 档位信息 -->
    <!-- 已评（已评档位），打回（建议档位） -->
    <div class="action-grade-info" v-if="rights.gradeInfo">
      <h3 class="grade-info-name">{{ curLevel.name }}</h3>
      <p class="grade-info-range">
        <span>{{ curLevel.range[0] }}</span>
        <span>~</span>
        <span>{{ curLevel.range[1] }}</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>{{ level.name }}</p>
        <p>{{ level.range[0] }}~{{ level.range[1] }}</p>
      </div>
    </div>
    <div class="action-grade-pass" v-if="rights.gradeList">
      <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 { CODE_TYPE } from "@/constants/enumerate";
// 三种情况：
// 管理员(ADMIN)，科组长(MARK_LEADER)，评卷员(MARKER)
// 管理员：查询，头部信息，评卷记录
// 科组长：查询，头部信息，选择档位，评卷记录
// 评卷员：头部信息，选择档位
const initRights = {
  search: false,
  paperState: false,
  paperInfo: false,
  gradeInfo: false,
  gradeList: false,
  gradeHis: false
};

export default {
  name: "grade-action",
  props: {
    paper: {
      type: Object,
      default() {
        return {};
      }
    },
    levels: {
      type: Array,
      default() {
        return [];
      }
    },
    userRole: {
      type: String,
      default: "MARKER"
    },
    step: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      rights: {
        ...initRights
      },
      roleRight: {
        ADMIN: {
          done: ["search", "gradeHis", "paperState", "paperInfo", "gradeInfo"],
          arbitrate: ["search", "gradeHis", "paperState", "paperInfo"]
        },
        MARK_LEADER: {
          done: [
            "search",
            "gradeList",
            "gradeHis",
            "paperState",
            "paperInfo",
            "gradeInfo"
          ],
          arbitrate: [
            "search",
            "gradeList",
            "gradeHis",
            "paperState",
            "paperInfo"
          ]
        },
        MARKER: {
          done: ["gradeList", "paperState", "paperInfo", "gradeInfo"],
          undo: ["gradeList", "paperState", "paperInfo"],
          refuse: ["gradeList", "paperState", "paperInfo", "gradeInfo"]
        }
      },
      filter: {
        codeType: "examNumber",
        code: ""
      },
      CODE_TYPE,
      curPaper: {
        examNumber: "2020105133",
        sn: "2020105133",
        level: ""
      },
      gradingHistory: [],
      curLevel: {
        name: "A",
        range: [95, 100]
      }
    };
  },
  computed: {
    IS_ADMIN() {
      return this.userRole === "ADMIN";
    },
    IS_MARKER() {
      return this.userRole === "MARKER";
    },
    IS_MARK_LEADER() {
      return this.userRole === "MARK_LEADER";
    }
  },
  watch: {
    step(val) {
      this.rebuildRight();
    },
    paper(val) {
      this.rebuildRight();
    }
  },
  mounted() {
    this.initData();
    this.rebuildRight();
  },
  methods: {
    initData() {
      this.gradingHistory = "ABCDEFGHIJK".split("").map((item, index) => {
        return {
          id: index,
          name: `评卷员${index}`,
          value: item
        };
      });
    },
    rebuildRight() {
      this.rights = { ...initRights };
      const rights = this.roleRight[this.userRole][this.step.type] || [];
      rights.map(key => {
        this.rights[key] = true;
      });
      if (this.paper.level) {
        const paperLevel = this.levels.find(
          level => level.name === this.paper.level
        );
        this.curLevel = {
          name: paperLevel.name,
          range: paperLevel.range
        };
      }
    },
    toPass() {
      this.$emit("on-pass");
    },
    search() {
      this.$emit("on-code-search", this.filter);
    }
  }
};
</script>
