<template>
  <el-dialog
    class="modify-mark-area"
    :visible.sync="modalIsShow"
    top="0"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    fullscreen
    :show-close="false"
    @open="visibleChange"
  >
    <div class="box-justify" slot="title">
      <h2 class="el-dialog__title">设置分组评卷区域</h2>
      <div>
        <el-button type="success" @click="confirm">确定</el-button>
        <el-button class="btn-back" @click="cancel">
          返回<i class="el-icon-arrow-right"></i>
        </el-button>
      </div>
    </div>
    <div class="tips-info">
      <i class="el-icon-warning"></i>
      按住鼠标左键拖动，框选评卷区域。可以设置多个评卷区域。当不设置评卷区域时，评卷时将展示答题卡所有答题区。
    </div>
    <div v-if="modalIsShow" class="area-container">
      <area-cropper
        v-for="(paper, index) in papers"
        :id="`area-cropper-${index}`"
        :imgUrl="paper.imgUrl"
        ref="AreaCropper"
        :key="paper.imgUrl"
        :paper="paper"
        @curarea-change="cropperCurareaChange"
        @change="(areas) => areaChange(index, areas)"
        @paper-load="() => areaPaperLoaded(index)"
      ></area-cropper>
    </div>

    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import AreaCropper from "./areaCropper/AreaCropper.vue";

export default {
  name: "modify-mark-area",
  components: { AreaCropper },
  props: {
    baseInfo: {
      type: Object,
      default() {
        return {};
      },
    },
    group: {
      type: Object,
      default() {
        return {};
      },
    },
    paperList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      papers: [],
      paperLoadedList: [],
    };
  },
  methods: {
    visibleChange() {
      this.paperLoadedList = [];
      this.papers = this.paperList.map((paper) => {
        let npaper = { ...paper };
        npaper.areas = [];
        return npaper;
      });
      this.group.pictureConfigs.forEach((config) => {
        const index = config.i - 1;
        this.papers[index].areas.push({ ...config });
      });
    },
    areaPaperLoaded(paperIndex) {
      if (this.paperLoadedList.includes(paperIndex)) return;
      this.paperLoadedList.push(paperIndex);
      if (this.paperLoadedList.length === this.paperList.length) {
        this.scrollToFirstArea();
      }
    },
    scrollToFirstArea() {
      if (!this.group.pictureConfigs.length) return;
      let paperIndexs = this.group.pictureConfigs.map((item) => item.i - 1);
      paperIndexs.sort((a, b) => a - b);
      const firstPaperIndex = paperIndexs[0];

      this.$nextTick(() => {
        const areaCropperDom = document.getElementById(
          `area-cropper-${firstPaperIndex}`
        );
        let areaItems = areaCropperDom.querySelectorAll(".element-resize");
        let firstAreaItem = null;
        let topNum = 9999;
        for (let i = 0; i < areaItems.length; i++) {
          const element = areaItems[i];
          if (element.offsetTop < topNum) {
            topNum = element.offsetTop;
            firstAreaItem = element;
          }
        }
        // console.log(firstAreaItem);
        const scrollTop =
          areaCropperDom.offsetTop + firstAreaItem.offsetTop - 100;
        this.$el.querySelector(".el-dialog").scrollTop = scrollTop;
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    areaChange(index, areas) {
      this.papers[index].areas = areas;
    },
    cropperCurareaChange(area) {
      this.$refs.AreaCropper.forEach((cropper) => {
        cropper.curareaChange(area);
      });
    },
    confirm() {
      let areas = [];
      this.papers.forEach((paper, pindex) => {
        if (!paper.areas.length) return;
        paper.areas.forEach((area) => {
          const narea = {
            i: pindex + 1,
            x: area.x,
            y: area.y,
            w: area.w,
            h: area.h,
          };
          areas.push(narea);
        });
      });
      this.$emit(
        "modified",
        Object.assign({}, this.group, { pictureConfigs: areas })
      );
      this.cancel();
    },
  },
};
</script>
