<template>
  <div id="addChapter">
    <el-dialog
      :visible.sync="dialogVisible"
      width="700px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">添加试卷</div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Close@2x.png"
            alt=""
            @click="dialogVisible = false"
          />
        </div>
      </div>
      <div>
        <p>
          <el-button :size="size" type="primary" @click="addChapterList"
            >添加</el-button
          >
        </p>
        <el-form label-width="100px" class="elform_style">
          <div v-for="(item, index) in list" :key="index" class="dis_box">
            <div class="left_box">
              <el-form-item label="标题前缀">
                <el-input v-model="item.prefixName"></el-input>
                <p class="p_style">注：便于检索、归类，以及区分一样的标题</p>
              </el-form-item>
              <el-form-item label="试卷标题" required>
                <el-input v-model="item.examName"></el-input>
                <p class="p_style">
                  注：请尽量规范易懂，方便在题卷目录表呈现给学员
                </p>
              </el-form-item>
              <el-form-item required label="试卷类型">
                <el-select
                  v-model="item.examPaperId"
                  placeholder="请选择试卷类型"
                >
                  <el-option
                    v-for="item in paperexam"
                    :key="item.paperId"
                    :label="item.paperName"
                    :value="item.paperId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="clear_style">
              <el-button :size="size" @click="list.splice(index, 1)"
                >删除</el-button
              >
            </div>
          </div></el-form
        >
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm" :disabled="!list.length"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      size: "small",
      dialogVisible: false,
      list: [],
    };
  },
  computed: {
    ...mapGetters(["paperexam"]),
  },
  methods: {
    addChapterList() {
      this.list.push({});
    },
    openBoxs(arr) {
      this.businList = JSON.parse(JSON.stringify(arr));
      this.list = [{}];
      this.dialogVisible = true;
    },
    submitForm() {
      const arr = [];
      this.list.forEach((item, index) => {
        if ((!item.examName && item.examName !== 0) || !item.examPaperId) {
          arr.push(index + 1);
        }
      });
      if (arr.length) {
        this.$message.error(
          `请检查第${arr.join(",")}条数据的试卷标题或试卷类型，请完成后提交`
        );
        return;
      }
      let data = this.list.map((item) => {
        item.businessList = this.businList;
        item.status = 1;
        item.publishStatus = 1;
        return item;
      });
      this.$api.addMorebankexam(data.reverse()).then((res) => {
        this.dialogVisible = false;
        this.$emit("backData", res.data);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.dis_box {
  background-color: #eee;
  padding: 10px 10px 0px;
  display: flex;
  margin-bottom: 16px;
  .left_box {
    flex: 1;
  }
  .clear_style {
    flex-shrink: 0;
    width: 80px;
    vertical-align: top;
    text-align: center;
  }
}
.elform_style {
  max-height: 620px;
  overflow: auto;
}
.p_style {
  font-size: 12px;
  color: #a4a4a4;
  margin: 0;
}
</style>
