<template>
  <div class="make-tester-wrapper">
    <div class="make-tester-form">
      <el-form
        ref="addform"
        :model="formInfo"
        label-width="120px"
        :rules="rules"
      >
        <el-form-item label="试卷名称" prop="name">
          <el-input :style="{ width: '70%' }" v-model="formInfo.name" />
        </el-form-item>
        <el-form-item label="试卷描述" prop="description">
          <el-input
            :style="{ width: '70%' }"
            v-model="formInfo.description"
          ></el-input>
        </el-form-item>
        <el-form-item label="课程状态" prop="status">
          <el-radio v-model="formInfo.status" label="已关闭"></el-radio>
          <el-radio v-model="formInfo.status" label="已发布"></el-radio>
        </el-form-item>
        <el-form-item label="单选题单项分值" prop="singleChoiceScore">
          <el-input-number
            v-model="formInfo.singleChoiceScore"
            :min="1"
            :max="100"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="多选题单项分值" prop="multipleChoiceScore">
          <el-input-number
            v-model="formInfo.multipleChoiceScore"
            :min="1"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="判断题单项分值" prop="judgmentScore">
          <el-input-number
            v-model="formInfo.judgmentScore"
            :min="1"
            :max="10"
          ></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <div class="selection-type-radio-wrapper">
      <el-radio v-model="curSubjectType" label="单选题" />
      <el-radio v-model="curSubjectType" label="多选题" />
      <el-radio v-model="curSubjectType" label="判断题" />
      <el-button type="primary" @click="addSubject">新增试题</el-button>
    </div>
    <div class="save-btn-wrapper">
      <el-button type="primary" @click="saveSubjects">保存试卷</el-button>
    </div>
    <el-dialog title="添加题目" :visible.sync="addSubjectDialogShow">
      <div class="search-subject-wrapper">
        <el-select v-model="selectDifficulty" placeholder="请选择困难度">
          <el-option
            v-for="item in difficulties"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input
          v-model="searchSubjectKeyword"
          :style="{ marginLeft: '30px' }"
          placeholder="关键字"
        />
        <el-button
          type="primary"
          :style="{ marginLeft: '30px' }"
          @click="initAllQuestion"
          >搜索</el-button
        >
      </div>
      <div class="subject-table-wrapper">
        <div class="mytable-wrapper">
          <div class="mytable-header-wrapper">
            <div class="mytable-header-selection">
              <el-checkbox
                v-model="chosenRadio"
                @input="inputChosenRadio"
              ></el-checkbox>
            </div>
            <div class="mytable-header-question">题干</div>
          </div>
          <div
            v-for="item in subjectTable"
            :key="item.id"
            class="mytable-body-wrapper"
          >
            <div class="mytable-body-selection">
              <el-checkbox
                v-model="item.isChosen"
                @input="tryInput(item)"
              ></el-checkbox>
            </div>
            <div class="mytable-body-question">{{ item.question }}</div>
          </div>
        </div>
        <!-- <el-table
          :data="subjectTable"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            prop="question"
            label="题干"
          />
        </el-table> -->
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="TotalSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <div class="table-wrapper">
        {{ "共选了 " + " " + getSubjectSum + "个题目" }}
      </div>
      <div class="save-wrapper">
        <el-button type="primary" @click="addSubjectDialogShow = false"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOnlineExamSubject, saveManully } from "@/api/test.js";
export default {
  data() {
    return {
      formInfo: {
        name: "",
        description: "",
        status: "已关闭",
        singleChoiceScore: 0,
        judgmentScore: 0,
        multipleChoiceScore: 0
      },
      // 单选题、多选题、判断题、新增试题
      curSubjectType: "单选题",
      rules: {
        name: [
          {
            required: true,
            message: "请输入试卷名称",
            trigger: "blur"
          }
        ]
      },
      addSubjectDialogShow: false,
      searchSubjectKeyword: "",
      selectDifficulty: "",
      difficulties: [
        {
          label: "简单",
          value: "简单"
        },
        {
          label: "一般",
          value: "一般"
        },
        {
          label: "困难",
          value: "困难"
        }
      ],
      subjectTable: [],
      multipleSelection: [],
      pageSize: 10,
      TotalSize: 0,
      currentPage: 1,
      chosenSelection: [],
      chosenRadio: false
    };
  },
  methods: {
    async initAllQuestion() {
      const params = {};
      params.page = this.currentPage - 1;
      params.size = this.pageSize;
      params.type = this.curSubjectType;
      if (this.selectDifficulty) {
        params.difficulty = this.selectDifficulty;
      }
      if (this.searchSubjectKeyword) {
        params.question = this.searchSubjectKeyword;
      }
      await getOnlineExamSubject(params).then(res => {
        console.log(
          res,
          "getOnlineExamSubject getOnlineExamSubject getOnlineExamSubject"
        );
        this.subjectTable = [
          ...res.content.map(item => {
            return {
              ...item,
              isChosen: false
            };
          })
        ];
        this.TotalSize = res.totalElements;
        this.chosenRadio = false;
        this.subjectTable.forEach(subject => {
          const resultArr = this.chosenSelection.filter(
            item => item.id === subject.id
          );
          if (resultArr.length) {
            subject.isChosen = resultArr[0].isChosen;
          }
        });
      });
    },
    addSubject() {
      this.addSubjectDialogShow = true;
      this.initAllQuestion();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(
        this.multipleSelection,
        "this.multipleSelection this.multipleSelection this.multipleSelection"
      );
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (val === 1) {
        this.subjectTable = [];
      }
      if (val === 2) {
        this.subjectTable = [];
      }
    },
    tryInput(item) {
      if (item.isChosen) {
        if (this.chosenSelection.indexOf(item) === -1) {
          this.chosenSelection.push(item);
        }
      } else {
        if (this.chosenSelection.indexOf(item) > -1) {
          this.chosenSelection.splice(this.chosenSelection.indexOf(item), 1);
        }
      }
    },
    saveSubjects() {
      // console.log('saveSubjects saveSubjects saveSubjects')
      this.$refs.addform.validate(valid => {
        if (valid) {
          console.log(
            this.chosenSelection,
            "this.chosenSelection this.chosenSelection this.chosenSelection"
          );
          const mySet = new Set(
            this.chosenSelection.map(item => item.libraryId)
          );
          const myArr = [...mySet];
          const data = {
            name: this.formInfo.name,
            description: this.formInfo.description,
            status: this.formInfo.status,
            libraryId: myArr,
            addJudgementIds: this.chosenSelection
              .filter(item => item.type === "判断题")
              .map(item => item.id),
            addMultipleChoiceIds: this.chosenSelection
              .filter(item => item.type === "多选题")
              .map(item => item.id),
            addSingleChoiceIds: this.chosenSelection
              .filter(item => item.type === "单选题")
              .map(item => item.id),
            singleChoiceScore: this.formInfo.singleChoiceScore,
            multipleChoiceScore: this.formInfo.multipleChoiceScore,
            judgmentScore: this.formInfo.judgmentScore
          };
          saveManully(data)
            .then(res => {
              this.$message({
                type: "success",
                message: "添加试卷成功"
              });
            })
            .catch(() => {
              this.$message.error("添加试卷失败");
            });
        }
      });
    },
    inputChosenRadio($event) {
      console.log($event, "inputChosenRadio inputChosenRadio");
      this.subjectTable.forEach(item => {
        item.isChosen = $event;
        if ($event) {
          if (this.chosenSelection.indexOf(item) === -1) {
            this.chosenSelection.push(item);
          }
        } else {
          if (this.chosenSelection.indexOf(item) > -1) {
            this.chosenSelection.splice(this.chosenSelection.indexOf(item), 1);
          }
        }
      });
    }
  },
  computed: {
    getSubjectSum() {
      return this.chosenSelection.filter(
        item => item.type === this.curSubjectType
      ).length;
    }
  },
  watch: {
    currentPage(newVal, oldVal) {
      console.log(newVal, "currentPage currentPage currentPage");
      console.log(oldVal, "oldVal oldVal oldVal");
      // this.chosenSelection = [...this.chosenSelection, ...this.multipleSelection]
      console.log(
        this.chosenSelection,
        "this.chosenSelection this.chosenSelection this.chosenSelection"
      );
    }
    // chosenRadio(newVal, oldVal) {
    //   console.log(newVal, oldVal, ',,.,,.,...,.')
    //   this.subjectTable.forEach(item => {
    //     item.isChosen = newVal
    //     if (item.isChosen) {
    //       if (this.chosenSelection.indexOf(item) === -1) {
    //         this.chosenSelection.push(item)
    //       }
    //     } else {
    //       if (this.chosenSelection.indexOf(item) > -1) {
    //         this.chosenSelection.splice(this.chosenSelection.indexOf(item), 1)
    //       }
    //     }
    //   })
    // }
  }
};
</script>

<style lang="scss" scoped>
.make-tester-wrapper {
  margin: 10px 10px;
  padding: 10px;
  border-radius: 5px;
  .make-tester-form {
    margin: 10px 10px;
    padding: 10px;
    border-radius: 5px;
    background: #fff;
  }
  .selection-type-radio-wrapper {
    margin: 10px 10px;
    padding: 10px;
    border-radius: 5px;
    display: flex;
    align-items: center;
    background: #fff;
  }
  .save-btn-wrapper {
    margin: 10px 10px;
    padding: 10px;
    border-radius: 5px;
    background: #fff;
  }
}
.search-subject-wrapper {
  display: flex;
  align-items: center;
  margin: 10px 10px;
  padding: 10px;
  border-radius: 5px;
}
.subject-table-wrapper {
  margin: 10px 10px;
  padding: 10px;
  border-radius: 5px;
  .mytable-wrapper {
    padding: 10px;
    margin: 10px 10px;
    .mytable-header-wrapper {
      display: flex;
      padding: 10px;
      border-top: 1px solid #eee;
      border-bottom: 1px solid #eee;
      .mytable-header-selection {
        flex-basis: 10%;
      }
      .mytable-header-question {
        flex-basis: 90%;
      }
    }
    .mytable-body-wrapper {
      display: flex;
      padding: 10px;
      border-bottom: 1px solid #eee;
      .mytable-body-selection {
        flex-basis: 10%;
      }
      .mytable-body-question {
        flex-basis: 90%;
      }
    }
  }
}
.table-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}
.save-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}
</style>
