<template>
  <div class="build-paper-simple">
    <div class="build-step-title">
      <h3><span>确定构成</span></h3>
    </div>
    <el-table :data="baseDataList" border>
      <el-table-column
        label="题型"
        prop="sourceDetailName"
        align="center"
        min-width="120"
      >
      </el-table-column>
      <el-table-column
        label="试题数量"
        prop="questionCount"
        align="center"
        width="120"
      ></el-table-column>
      <el-table-column
        label="抽取试题数量"
        prop="questionSelectCount"
        width="220"
        align="center"
      >
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.selectCount"
            placeholder="请输入抽取试题数量"
            size="mini"
            :step="1"
            :min="1"
            :max="scope.row.questionCount"
            :controls="false"
            :precision="0"
            step-strictly
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column
        label="试题分值"
        prop="questionScore"
        width="220"
        align="center"
      >
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.questionScore"
            placeholder="请输入抽取试题分值"
            size="mini"
            :step="0.1"
            :min="0.1"
            :max="999"
            :controls="false"
            :precision="1"
            step-strictly
          ></el-input-number>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { paperSimpleCountDistributeApi } from "../api";

export default {
  name: "BuildPaperSimple",
  props: {
    courseId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      baseDataList: [],
    };
  },
  mounted() {
    this.getBaseData();
  },
  methods: {
    async getBaseData() {
      const res = await paperSimpleCountDistributeApi({
        courseId: this.courseId,
      });
      this.baseDataList = res.data.map((item) => {
        return {
          ...item,
          questionCount: item.count,
          selectCount: undefined,
          questionScore: undefined,
        };
      });
    },
    getData() {
      let questionInfo = this.baseDataList
        .filter((item) => item.selectCount)
        .map((item) => {
          return {
            count: item.selectCount,
            score: item.questionScore,
            sourceDetailId: item.sourceDetailId,
          };
        });
      return { questionInfo };
    },
    validate() {
      let valid = this.baseDataList.some((item) => item.selectCount);
      if (!valid) {
        this.$message.error("题目数量不能为空！");
        return Promise.reject();
      }

      let unvalid = this.baseDataList
        .filter((item) => item.selectCount)
        .some((item) => !item.questionScore);

      if (unvalid) {
        this.$message.error("有小题未设置分数！");
        return Promise.reject();
      }

      return Promise.resolve(true);
    },
  },
};
</script>
