<template>
  <Modal
    class="modify-leader-grading"
    v-model="modalIsShow"
    title="科组长评档"
    :mask-closable="false"
    @on-visible-change="visibleChange"
  >
    <div class="leader-grading">
      <div class="leader-level">
        <p>已选档位</p>
        <h3>{{ LevelInfo.selectedLevel }}</h3>
      </div>
      <RadioGroup v-model="actionType">
        <div class="leader-aciton" v-if="standardVolume">
          <Radio size="large" label="sampling">设为标准卷</Radio>
        </div>
        <div class="leader-aciton" v-if="oneClickLevel">
          <Radio size="large" label="leveling" :disabled="noneedLevelOrReject"
            >一键定档</Radio
          >
        </div>
        <div class="leader-aciton" v-if="levelCallback">
          <Radio size="large" label="reject" :disabled="noneedLevelOrReject"
            >建议档位打回</Radio
          >
        </div>
      </RadioGroup>
      <div
        class="leader-markers"
        v-if="actionType === 'reject' && !noneedLevelOrReject"
      >
        <CheckboxGroup v-model="selectedMarkers">
          <Checkbox
            v-for="marker in markers"
            :key="marker.id"
            :label="marker.id"
            >{{ marker.name }}</Checkbox
          >
        </CheckboxGroup>
      </div>
    </div>

    <div slot="footer">
      <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
        >确认</Button
      >
      <Button shape="circle" @click="cancel">取消</Button>
    </div>
  </Modal>
</template>

<script>
import { leaderGradingPaper } from "@/api";

export default {
  name: "modify-leader-grading",
  props: {
    LevelInfo: {
      type: Object,
      default() {
        return {
          paperId: "",
          curLevel: "",
          selectedLevel: ""
        };
      }
    },
    markers: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      standardVolume: false,
      oneClickLevel: false,
      levelCallback: false,
      noneedLevelOrReject: false,
      actionType: null,
      selectedMarkers: []
    };
  },
  methods: {
    initData(val) {
      this.noneedLevelOrReject =
        this.LevelInfo.curLevel === this.LevelInfo.selectedLevel;
      const user = this.$ls.get("user");
      this.standardVolume = user.standardVolume;
      this.levelCallback = user.levelCallback;
      this.oneClickLevel = user.oneClickLevel;
      this.selectedMarkers = this.markers.map(item => item.id);
    },
    visibleChange(visible) {
      if (visible) {
        this.initData(this.instance);
      } else {
        this.isSubmit = false;
        this.actionType = null;
      }
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      if (!this.actionType) {
        this.$Message.error("请选择操作类型！");
        return;
      }
      if (this.actionType === "reject" && !this.selectedMarkers.length) {
        this.$Message.error("请指定评卷员！");
        return;
      }
      const datas = {
        action: this.actionType,
        level: this.LevelInfo.selectedLevel
      };
      if (this.actionType === "reject")
        datas.range = this.selectedMarkers.join();

      if (this.isSubmit) return;
      this.isSubmit = true;
      let result = true;
      const paper = await leaderGradingPaper(
        this.LevelInfo.paperId,
        datas
      ).catch(() => {
        result = false;
      });
      this.isSubmit = false;

      if (!result) return;

      this.$Message.success("操作成功！");
      this.$emit("modified", datas, paper);
      this.cancel();
    }
  }
};
</script>
