<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <div style="width: 1000px; margin: auto">

      <div v-for="(item,index) in data" style="border-bottom: #9e9e9e 1px solid;padding: 10px 0;">
        第{{index + 1}}大题：
        <a-input v-model="item.name" placeholder="请输入大题名称" style="width: 53.5%"></a-input>
        请输入分值：
        <a-input v-model="item.point" type="number" placeholder="请输入分值" style="width: 10%"></a-input>
        <a-button type="primary" @click="show1(index)" size="small" style="margin-left:20px;">添加题目</a-button>
        <a-button @click="deleteName(index)" type="danger" size="small" style="margin-left:20px;">删除大题</a-button>
        <div v-for="(item1,index1) in item.question" style="padding: 4px 0;">
          <span style="margin-left: 5px;width: 600px; display: inline-block">{{index1 + 1}}：{{item1.name}}</span>
          请输入分值：
          <a-input type="number" style="width: 10%;" size="small" v-model="item1.point" placeholder="请输入分值"></a-input>
          <a-button @click="deletequestion(index,index1)" type="danger" size="small" style="margin-left:20px;">删除题目
          </a-button>
        </div>
      </div>
      <a-button @click="addName" type="primary" style="margin-top: 20px;">添加大题</a-button>
      <SelectExquestionList ref="selectExquestionList" @ok="questdata"></SelectExquestionList>
    </div>
  </j-modal>

</template>

<script>

import {httpAction, getAction, deleteAction} from '@/api/manage'
import SelectExquestionList from './SelectExquestionList'

export default {
  name: "GroupVolume",
  components: {SelectExquestionList},
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      data: [{paperid: this.paperid, point: 0, name: "", question: []}],
      paperid: "",
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      flag: 0,
      students: [],
      totalPoints: 0,
      confirmLoading: false,
      validatorRules: {},
      url: {
        groupVolume: "/ex/exExamPaper/groupVolume",
        getque: "/ex/exExamPaper/getque",
        delete: "/ex/exHistory/delete",
        groupVolumedelete: "/ex/exExamPaper/groupVolumedelete",
      }

    }
  },
  created() {
  },
  methods: {
    show(paperid, totalPoints) {

      this.visible = true;
      this.paperid = paperid;
      this.data = [{paperid: this.paperid, point: 0, name: "", question: []}];
      this.totalPoints = totalPoints;
      getAction(this.url.getque, {"id": paperid}).then((res) => {
        if (res.success) {
          console.log(res.result)
          this.data = res.result
        } else {
          this.$message.error(res.message);
        }
      });
      /*getAction(this.url.showstudent, {id: examid}).then((res) => {
        if (res.success) {
          console.log(res.result)
          this.students = res.result;
          this.examid = examid;
          this.visible = true;
        } else {
          this.$message.error(res.message);
        }
      });*/
    },
    questdata(val) {
      console.log("-----------")
      console.log(val)
      for (let i = 0; i < val.length; i++) {
        console.log(val[i])
        let x = {sort:i,id: val[i].id, name: val[i].name, point: 0}
        this.data[this.flag].question.push(x)
      }
      // this.data[this.flag].question = val;
    },
    show1(val) {
      this.$refs.selectExquestionList.show()
      this.flag = val;
    },
    addName() {
      // let x = {name: '', question: [{queid: '',quename:'', point: ''}]};
      let x = {paperid: this.paperid, point: 0, name: "", question: []};
      this.data.push(x);
    },
    deleteName(val) {
      this.data.splice(val, 1)
    },
    deletequestion(index, index1) {
      this.data[index].question.splice(index1, 1)
    },
    enterExam(id, eid) {
      console.log(eid)
      this.$refs.stuExamPointsDetailModal.enterExam(id, eid)
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      console.log(this.data[0])
      let flag1 = 0;
      var points1 = 0;
      for (let i = 0; i < this.data.length; i++) {
        console.log(this.data[i].name)
        if(this.data[i].name==""){
          this.$message.error("添加失败！第" + (i + 1) + "大题题目名称为空");
          return;
        }
        var points = 0;
        for (let j = 0; j < this.data[i].question.length; j++) {
          points = points + parseFloat(this.data[i].question[j].point)

        }
        points1 = points1 + parseFloat(this.data[i].point)
        if (points != this.data[i].point) {
          this.$message.error("添加失败！第" + (i + 1) + "题下所有题目分值相加与总分值不相等");
          return;
        }
      }
      if (points1 != this.totalPoints) {
        this.$message.error("添加失败！大题分值相加与总分值不相等");
        return;
      }
      console.log('111111111')
      getAction(this.url.groupVolumedelete, {"paperid":this.paperid}).then((res) => {
        if (res.success) {

          for (let i = 0; i < this.data.length; i++) {
            for (let j = 0;j<this.data[i].question.length;j++){
              this.data[i].question[j].sort=(i+1)+""+(j+1);
            }
            console.log(this.data[i])
            getAction(this.url.groupVolume, {"groupVolume": JSON.stringify(this.data[i]),"flag":i}).then((res) => {
              if (res.success) {
                console.log(res.result)
              } else {
                this.$message.error(res.message);
              }
            });
          }

        } else {
          this.$message.error(res.message);
        }
      });
      console.log('2222222222')

      this.$message.success("提交成功");
      this.$emit('close');
      this.visible = false;
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>
<style scoped>
td {
  border: #5c6065 1px solid;
  height: 40px;
  text-align: center;
}
</style>