<!--新增考试-->
<template>
  <div class="page-container addExamPage">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p"></p>
      <p class="page-top-title-out-p" @click="backPage">取消</p>
      <p class="page-top-title-submit-p" @click="submitForm">提交</p>
    </div>
    <div class="content-box scrollbar-box">
      <p class="title-p">组卷信息</p>
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="适用范围" prop="scopeType" label-width="100px" style="margin-top:10px;">
          <template>
            <el-radio v-model="form.scopeType" label="1">安全准入考试</el-radio>
            <el-radio v-model="form.scopeType" label="2">负面清单考试</el-radio>
            <el-radio v-model="form.scopeType" label="3">黑名单考试</el-radio>
            <el-radio v-model="form.scopeType" label="4">模拟考试</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="组题方式" prop="joinType" label-width="100px">
          <template>
            <el-radio-group v-model="form.joinType" @change="joinTypeClick">
              <el-radio :label="1">自动组题</el-radio>
              <el-radio :label="2">手动组题</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <!--自动组题-->
        <div class="info-box" v-if="form.joinType == 1">
          <div class="info-left-box">
            <div v-for="(bigItem,index) in form.repoList"
                 style="margin-bottom:88px;">
              <el-form-item label="题目分类" :prop="'repoList.'+ index +'.classifyId'" :rules="rules.classifyId" label-width="100px" style="width: 420px;">
                <el-select
                  v-model="bigItem.classifyId"
                  placeholder="请选择分类"
                  :rules="bigItem.rules">
                  <el-option
                    v-for="item in repoOption"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
          <div class="info-right-box">
            <div class="info-right-max-box"
                 style="position: relative;"
                 v-for="(item,index) in form.repoList">
              <i v-if="index!=0" class="el-icon-close del-o" @click="delRepo(index)"></i>
              <div class="info-right-top-box">
                <div>
                  <el-form-item label="单选题数量" :prop="'repoList.'+ index +'.radioCount'" :rules="rules.radioCount">
                    <el-input
                      v-model="item.radioCount"
                      placeholder="请输入单选题数量"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
                <div>
                  <el-form-item label="多选题数量" :prop="'repoList.'+ index +'.multiCount'" :rules="rules.multiCount">
                    <el-input
                      v-model="item.multiCount"
                      placeholder="请输入多选题数量"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
                <div>
                  <el-form-item label="判断题数量" :prop="'repoList.'+ index +'.judgeCount'" :rules="rules.judgeCount">
                    <el-input
                      v-model="item.judgeCount"
                      placeholder="请输入判断题数量"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="info-right-bottom-box">
                <div>
                  <el-form-item label="单选题分值" :prop="'repoList.'+ index +'.radioScore'" :rules="rules.radioScore">
                    <el-input
                      v-model="item.radioScore"
                      placeholder="请输入单选题分值"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
                <div>
                  <el-form-item label="多选题分值" :prop="'repoList.'+ index +'.multiScore'" :rules="rules.multiScore">
                    <el-input
                      v-model="item.multiScore"
                      placeholder="请输入多选题分值"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
                <div>
                  <el-form-item label="判断题分值" :prop="'repoList.'+ index +'.judgeScore'" :rules="rules.judgeScore">
                    <el-input
                      v-model="item.judgeScore"
                      placeholder="请输入判断题分值"

                      style="width:160px;"
                      maxLength="5"
                      @input="changeInput"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
          </div>
        </div>
        <p class="add-button" @click="addObj" v-if="form.joinType == 1">添加分类</p>
        <!--手动组题-->
        <div class="info-box-manual" v-if="form.joinType == 2">
          <div class="info-box-manual-for-max-box" v-for="(item,index) in form.manualList">
            <div class="info-box-manual-for-max-title">
              <p class="name-p">分类名称：{{item.classifyName}}</p>
              <p class="num-p">题目总数：{{item.quIds.length}}</p>
              <p class="info-box-manual-for-right-p-one" @click="addManual(index)">编辑</p>
              <p class="info-box-manual-for-right-p-two" @click="delManual(index)">删除</p>
            </div>
            <div class="info-box-manual-for-left-box">
              <div class="input-box">
                <el-form-item label="单选题数量" :prop="'manualList.'+ index +'.radioCount'" label-width="120px">
                  <el-input
                    v-model="item.radioCount"
                    placeholder=""

                    :disabled="true"
                    size="small"
                  />
                </el-form-item>
                <el-form-item label="单选题分值" :prop="'manualList.'+ index +'.radioScore'" :rules="rules.radioScore" label-width="120px">
                  <el-input
                    v-model="item.radioScore"
                    placeholder="请输入单选题分值"

                    size="small"
                    maxLength="2"
                    @input="changeInputTwo"
                  />
                </el-form-item>
              </div>
              <div class="input-box">
                <el-form-item label="多选题数量" :prop="'manualList.'+ index +'.multiCount'" label-width="120px">
                  <el-input
                    v-model="item.multiCount"
                    placeholder=""

                    :disabled="true"
                    size="small"
                  />
                </el-form-item>
                <el-form-item label="多选题分值" :prop="'manualList.'+ index +'.multiScore'" :rules="rules.multiScore" label-width="120px">
                  <el-input
                    v-model="item.multiScore"
                    placeholder="请输入单选题分值"

                    size="small"
                    maxLength="2"
                    @input="changeInputTwo"
                  />
                </el-form-item>
              </div>
              <div class="input-box">
                <el-form-item label="判断题数量" :prop="'manualList.'+ index +'.judgeCount'" label-width="120px">
                  <el-input
                    v-model="item.judgeCount"
                    placeholder=""

                    :disabled="true"
                    size="small"
                  />
                </el-form-item>
                <el-form-item label="判断题分值" :prop="'manualList.'+ index +'.judgeScore'" :rules="rules.judgeScore" label-width="120px">
                  <el-input
                    v-model="item.judgeScore"
                    placeholder="请输入单选题分值"

                    size="small"
                    maxLength="2"
                    @input="changeInputTwo"
                  />
                </el-form-item>
              </div>
            </div>
          </div>
        </div>
        <p class="add-button" @click="addManual" v-if="form.joinType == 2">添加分类</p>
        <p class="title-p">考试配置</p>
        <div class="configuration-box">
          <div class="configuration-top-box">
            <div>
              <el-form-item label="考试名称" prop="title">
                <el-input
                  v-model="form.title"
                  placeholder="请输入考试名称"

                  maxlength="50"
                  size="small"
                />
              </el-form-item>
            </div>
            <div style="margin-left:20px;">
              <el-form-item label="考试描述" prop="content">
                <el-input
                  v-model="form.content"
                  placeholder="请输入考试描述"

                  size="small"
                  maxlength="50"
                />
              </el-form-item>
            </div>
          </div>
          <div class="configuration-bottom-box">
            <div class="one">
              <el-form-item label="总分数" prop="totalScore" >
                <el-input-number disabled  v-model="form.totalScore" @change="handleChange" label="总分数"></el-input-number>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="及格分数" prop="qualifyScore">
                <el-input-number v-model="form.qualifyScore" @change="handleChange" :min="1" :max="form.totalScore" label="请输入及格分数"></el-input-number>
              </el-form-item>
            </div>
            <div class="two">
              <el-form-item label="考试时长分钟" prop="totalTime" label-width="155px">
                <el-input-number v-model="form.totalTime" @change="handleChange" :min="1" :max="1000" label="请输入考试时常分钟数"></el-input-number>
              </el-form-item>
            </div>
          </div>
          <el-form-item label="考前是否人脸验证" prop="preFaceVerify" label-width="155px">
            <template>
              <el-radio v-model="form.preFaceVerify" :label="false">否</el-radio>
              <el-radio v-model="form.preFaceVerify" :label="true">是</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="考后是否人脸验证" prop="endFaceVerify" label-width="155px">
            <template>
              <el-radio v-model="form.endFaceVerify" :label="false">否</el-radio>
              <el-radio v-model="form.endFaceVerify" :label="true">是</el-radio>
            </template>
          </el-form-item>
        </div>
        <p class="title-p">考试范围</p>
        <el-form-item label="适用学院" prop="deptIds" class="faculty-box">
          <!-- 此处需要多选 -->
          <!--<treeselect v-model="form.deptIds" :normalizer="normalizer"-->
                      <!--:options="deptOptions"-->
                      <!--:multiple="true" :show-count="true"-->
                      <!--placeholder="请选择适用学院" />-->
          <el-cascader
            style="width:400px;"
            ref="cascader"
            v-model="form.deptIds"
            :options="deptOptions"
            :props="{value: 'deptId', label: 'deptName',children:'child',multiple: true,emitPath:false}"
            placeholder="请选择适用学院"
            collapse-tags
            ></el-cascader>
        </el-form-item>
      </el-form>
    </div>
    <!--手动添加分类弹窗-->
    <el-dialog class="add-manual-dialog" :title="manualTitle"  :close-on-click-modal="false"
               :visible.sync="addManualType" v-if="addManualType" width="1200px" append-to-body>
      <el-form :model="manualQueryParams" ref="manualQueryForm" :inline="true" label-width="80px">
        <el-form-item label="题目分类" prop="classifyIds">
          <el-select
            v-model="manualQueryParams.classifyIds"
            ref="manualRef"
            :disabled="manualClassType"
            placeholder="请选择分类">
            <el-option
              v-for="item in repoOption"
              :key="item.classifyId"
              :label="item.title"
              :value="item.classifyId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目名称" prop="content">
          <el-input
            v-model="manualQueryParams.content"
            placeholder="请输入题目名称"

            size="small"
          />
        </el-form-item>
        <el-form-item label="题目类型" prop="quType">
          <el-select
            v-model="manualQueryParams.quType"
            placeholder="请选择题目类型"

            size="small">
            <el-option
              v-for="dict in quTypes"
              :key="dict.type"
              :label="dict.value"
              :value="dict.type">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <p class="page-inquire-common-style-button" @click="manualQueryHandleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="manualQueryResetQuery">重置</p>
        </el-form-item>
      </el-form>
      <div class="add-manual-dialog-list-box">
        <div class="add-manual-dialog-list-box-left">
          <p class="add-manual-dialog-list-box-title">已添加</p>
          <div class="add-manual-dialog-list-for-max-box scrollbar-box">
            <div class="add-manual-dialog-list-for-box" v-for="(item,index) in manualData.quIds" :key="item.id">
              <p>{{item.name}}</p>
              <i class="el-icon-circle-close" @click="manualDialogButtonDel(index)"></i>
            </div>
            <p class="add-manual-dialog-list-for-null" v-if="!manualData.quIds[0]">请再右侧添加题目</p>
          </div>
        </div>
        <div class="add-manual-dialog-list-box-right">
          <el-table :data="manualQueryList" border class="table-box">
            <el-table-column label="题目名称" align="left" prop="content" :show-overflow-tooltip="true"/>
            <el-table-column label="题目类型"  align="left" prop="quType" width="120">
              <template slot-scope="scope">
                <span v-for="(item, index) in quTypes"
                  :key="index"
                  v-if="scope.row.quType == item.type"
                >{{ item.value }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="120">
              <template slot-scope="scope">
                <div class="table-button-box">
                  <p class="table-button-null"></p>
                  <p class="table-button-p"
                     @click="manualDialogButtonAdd(scope.row)"
                  >添加</p>
                  <p class="table-button-null"></p>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <pagination :page-sizes="[20, 30, 40, 50]"
            v-show="manualQuerytotal>0"
            :total="manualQuerytotal"
            layout="total, prev, pager, next, sizes, jumper"
            :page.sync="manualQueryParams.page"
            :limit.sync="manualQueryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="offManualBox">取消</p>
        <p class="dialog-footer-button-primary" @click="addManualObj">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { examQuList,examElClassifyQueryOption,examRepoQueryOption,
  examExamAdd,examExamInfo,examExamEdit} from "@/api/safetyEducationExam/index";
import { systemDeptOrganizeStructure, systemDeptOrganizeStructureByDept } from '@/api/commonality/permission'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  props:{
    repoId:{},
  },
  name: 'addExamPage',
  components: { Treeselect },
  data() {
    return {
      form:{
        code:null,
        faculty:null,
        title: null,
        content: null,
        level: null,
        scopeType: null,
        state: null,
        conversion: null,
        totalScore: null,
        totalTime: null,
        qualifyScore: null,
        preFaceVerify: null,
        endFaceVerify: null,
        // 分类列表
        repoList: [
          {
            classifyId:"",//分类ID
            radioCount:"0",//单选数量
            radioScore:"0",//单选分数
            multiCount:"0",//多选数量
            multiScore:"0",//多选分数
            judgeCount:"0",//判断数量
            judgeScore:"0",//判断分数
          }
        ],
        //手动分类
        manualList:[],
        // 部门
        deptIds: [],
      },
      // 表单校验
      rules: {
        joinType: [
          { required: true, message: "请选择组题方式", trigger: "blur" }
        ],
        level: [
          { required: true, message: "请选择考试难度", trigger: "blur" }
        ],
        classifyId: [
          { required: true, message: "请选择分类", trigger: "blur" }
        ],
        scopeType: [
          { required: true, message: "请选择适用范围", trigger: "blur" }
        ],
        radioCount: [
          { required: true, message: "请输入单选题数量", trigger: "blur" },
          { required: true, message: "请输入单选题数量", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        multiCount: [
          { required: true, message: "请输入多选题数量", trigger: "blur" },
          { required: true, message: "请输入多选题数量", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        judgeCount: [
          { required: true, message: "请输入判断题数量", trigger: "blur" },
          { required: true, message: "请输入判断题数量", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        radioScore: [
          { required: true, message: "请输入单选题分值", trigger: "blur" },
          { required: true, message: "请输入单选题分值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        multiScore: [
          { required: true, message: "请输入多选题分值", trigger: "blur" },
          { required: true, message: "请输入多选题分值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        judgeScore: [
          { required: true, message: "请输入判断题分值", trigger: "blur" },
          { required: true, message: "请输入判断题分值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        title: [
          { required: true, message: "请输入考试名称", trigger: "blur" },
          { required: true, message: "请输入考试名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        content: [
          { required: true, message: "请输入考试描述", trigger: "blur" },
          { required: true, message: "请输入考试描述", validator: this.spaceJudgment, trigger: "blur" }
        ],
        // conversion: [
        //   { required: true, message: "请输入积分换算值", trigger: "blur" }
        // ],
        qualifyScore: [
          { required: true, message: "请输入及格分数", trigger: "blur" },
          { required: true, message: "请输入及格分数", validator: this.spaceJudgment, trigger: "blur" }
        ],
        totalTime: [
          { required: true, message: "请输入考试时常分钟数", trigger: "blur" },
          { required: true, message: "请输入考试时常分钟数", validator: this.spaceJudgment, trigger: "blur" }
        ],
        deptIds: [
          { required: true, message: "请选择适用学院", trigger: "blur" }
        ],
        preFaceVerify: [
          { required: true, message: "请选择考前是否人脸验证", trigger: "blur" }
        ],
        endFaceVerify: [
          { required: true, message: "请选择考后是否人脸验证", trigger: "blur" }
        ],
      },
      // 部门树选项
      deptOptions:[],
      // 归属分类
      repoOption: [],
      // 学科字典
      subClassOptions: [],
      levels: [
        {
          type: 1,
          value: "中等",
        },
        {
          type: 2,
          value: "较难",
        },
        {
          type: 3,
          value: "难",
        },
      ],
      /***************手动添加分类开始***************/
      manualTitle:"",
      //弹窗状态
      addManualType:false,
      //搜索数据
      manualQueryParams:{
        page: 1,
        pageSize:20,
        classifyId:"",
        title:"",
      },
      //搜索数据列表
      manualQueryList:[],
      manualQuerytotal:0,
      //题目类型
      quTypes: [{type: 1, value: "单选题",}, {type: 2, value: "多选题",}, {type: 3, value: "判断题",},
      ],
      //当前选中分类
      manualIndex:0,
      //分类锁死状态
      manualClassType:false,
      //临时数据
      manualData:{
        classifyId:"",
        classifyName:"",
        quIds:[],
      },
      /***************手动添加分类结束***************/
    }
  },
  created() {
    this.getTreeselect();
    this.getClassifys();
    this.getDicts("subject_class").then((response) => {
      this.subClassOptions = response.data;
    });
    if(this.repoId){
      this.getRepoInfo();
    }
  },
  methods: {
    normalizer(node){
        return{
          id:node.deptId,
          label:node.deptName,
          children:node.child
        }
    },
    loadOptions(){

    },
    /***************手动添加分类开始***************/
    //添加/编辑分类
    addManual(index){
      let self = this;
      if(this.form.manualList[0]){
        if (index >= 0){
          this.manualIndex = index;
        } else{
          this.manualIndex = this.form.manualList.length;
        }
      }else{
        this.manualIndex = 0;
      }
      this.manualQueryList = [];
      this.manualQuerytotal = 0;
      if(index >= 0){
        this.manualTitle = "编辑分类";
        let newObj = JSON.parse(JSON.stringify(this.form.manualList[index]));
        this.manualQueryParams.classifyId = newObj.classifyId;
        this.manualClassType = true;
        this.manualData = {
          classifyId:newObj.classifyId,
          classifyName:"",
          quIds:newObj.quIds,
        };
        for(let i=0;i<self.repoOption.length;i++){
          if(newObj.classifyId == self.repoOption[i].id){
            self.manualData.classifyName = newObj.classifyName;
          }
        }
        this.getList();
      }else{
        this.manualTitle = "添加分类";
        this.manualQueryParams.classifyId = "";
        this.manualClassType = false;
        this.manualData = {
          classifyId:"",
          classifyName:"",
          quIds:[],
        };
      }
      this.addManualType = true;
    },
    //删除已有分类
    delManual(index){
      this.form.manualList.splice(index,1);
      this.$forceUpdate();
    },
    //组题方式改变
    joinTypeClick(e){
      if(e == 1){
        if(!this.form.repoList){
          let list = [];
          let obj = {
            classifyId:"",//分类ID
            radioCount:"0",//单选数量
            radioScore:"0",//单选分数
            multiCount:"0",//多选数量
            multiScore:"0",//多选分数
            judgeCount:"0",//判断数量
            judgeScore:"0",//判断分数
          };
          list.push(obj);
          this.$set(this.form,'repoList',list);
          // this.form.repoList = list;
        }
      }else if(e == 2){
        if(!this.form.manualList){
          this.$set(this.form,'manualList',[]);
        }
      }
      this.$forceUpdate();
    },
    //关闭窗口
    offManualBox(){
      this.addManualType = false;
    },
    //弹窗确认保存
    addManualObj(){
      let self = this;
      if(!this.manualData.quIds[0]){
        this.msgError("请先添加题目")
        return
      }
      //单选题数量
      let radioCount = 0;
      //多选题数量
      let multiCount = 0;
      //判断题数量
      let judgeCount = 0;
      let newObj = JSON.parse(JSON.stringify(this.manualData))
      for(let i=0;i<newObj.quIds.length;i++){
        if(newObj.quIds[i].quType == 1){
          radioCount++
        }else if(newObj.quIds[i].quType == 2){
          multiCount++
        }else if(newObj.quIds[i].quType == 3){
          judgeCount++
        }
      }
      let obj = {
        classifyId:newObj.classifyId,//分类ID
        classifyName:newObj.classifyName,//分类ID
        radioCount:radioCount,//单选数量
        radioScore:0,//单选分数
        multiCount:multiCount,//多选数量
        multiScore:0,//多选分数
        judgeCount:judgeCount,//判断数量
        judgeScore:0,//判断分数
        quIds:newObj.quIds,
      };
      if(this.form.manualList[this.manualIndex]){
        this.form.manualList[this.manualIndex] = JSON.parse(JSON.stringify(obj));
      }else{
        this.form.manualList.push(obj);
      }
      this.changeInputTwo();
      this.offManualBox();
    },
    //弹窗列表添加按钮
    manualDialogButtonAdd(row){
      let self = this;
      for(let i=0;i<self.manualData.quIds.length;i++){
        if(row.id == self.manualData.quIds[i].id){
          this.msgError("该题已添加");
          return
        }
      }
      let obj = {
        id:row.id,
        name:row.content,
        quType:row.quType,
      }
      self.manualData.quIds.push(obj);
      self.$forceUpdate();
    },
    //弹窗列表删除按钮
    manualDialogButtonDel(index){
      this.manualData.quIds.splice(index,1);
      this.$forceUpdate();
    },

    //列表查询
    manualQueryHandleQuery(){
      let self = this;
      if(!this.manualQueryParams.classifyIds){
        this.msgError("请先选择分类");
        return
      }

      this.manualClassType = true;
      if(!self.manualData.classifyId){
        this.manualData.classifyId = this.manualQueryParams.classifyIds;
        for(let i=0;i<self.repoOption.length;i++){
          if(self.manualQueryParams.classifyIds == self.repoOption[i].id){
            self.manualData.classifyName = self.repoOption[i].title;
          }
        }
      }
      this.manualQueryParams.page = 1;
      this.getList();
    },
    //列表重置
    manualQueryResetQuery(){
      this.resetForm("manualQueryForm");
      this.manualQueryParams.classifyIds = "";
      this.manualQueryParams.classifyId = "";
      this.manualQueryList = [];
      this.manualData.classifyId = "";
      this.manualData.classifyName = "";
      this.manualData.quIds = [];
      this.manualClassType = false;
      this.$forceUpdate();
    },
    /** 查询分类列表 */
    getList() {
      examQuList(this.manualQueryParams).then((response) => {
        this.manualQueryList = response.data.records;
        this.manualQuerytotal = response.data.total;
      });
    },
    /***************手动添加分类结束***************/
    getRepoInfo(){
      let self = this;
      examExamInfo(this.repoId).then(response => {
        if(response.data.deptStrIds){
          response.data.deptIds=response.data.deptStrIds
        }
        if(response.data.repoList){
          response.data.repoList.forEach(function(item){
            item.classifyId=item.classifyId+''
          })
        }
        if(response.data.joinType == 1){
          this.form = response.data;
          this.form.code = this.form.code+"";
          this.form.scopeType = this.form.scopeType+"";
        }else if(response.data.joinType == 2){
          let newData = JSON.parse(JSON.stringify(response.data));
          let newList = JSON.parse(JSON.stringify(newData.repoList));
          delete newData.repoList
          for(let i=0;i<newList.length;i++){
            let newQuList = JSON.parse(JSON.stringify(newList[i].examQuModelList));
            delete newList[i].examQuList;
            let list = [];
            for(let o=0;o<newQuList.length;o++){
              let obj = {
                id:newQuList[o].quId,
                name:newQuList[o].content,
                quType:newQuList[o].quType,
              }
              list.push(obj);
            }
            newList[i].quIds = list;
          }
          newData.manualList = newList;
          this.form = newData;
          this.form.code = this.form.code+"";
          this.form.scopeType = this.form.scopeType+"";
          this.changeInputTwo();
        }
      });
    },
    /** 查询题目分类 */
    getClassifys(){
      examElClassifyQueryOption({}).then(response => {
        for(let i=0;i<response.data.records.length;i++){
          response.data.records[i].classifyId = response.data.records[i].id;
        }
        this.repoOption = response.data.records;
      });
    },
    //删除分类
    delRepo(index){
      this.form.repoList.splice(index,1)
    },
    //根据id查找name
    getTreeName(list,id){
      let _this=this
      for (let i = 0; i < list.length; i++) {
        let a=list[i]
        if(a.deptId===id){
          return a.deptName
        }else{
          if(a.child && a.child.length>0){
            let res=_this.getTreeName(a.child,id)
            if(res){
              return res
            }
          }
        }
      }
    },
    //提交
    submitForm(){
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.joinType == 1){
            for(let i=0;i<self.form.repoList.length;i++){
              let num = 0;
              if(self.form.repoList[i].radioCount&&self.form.repoList[i].radioScore){
                if(self.accMul(self.form.repoList[i].radioCount,self.form.repoList[i].radioScore)!=0){
                  num++
                }
              }
              if(self.form.repoList[i].multiCount&&self.form.repoList[i].multiScore){
                if(self.accMul(self.form.repoList[i].multiCount,self.form.repoList[i].multiScore)!=0){
                  num++
                }
              }
              if(self.form.repoList[i].judgeCount&&self.form.repoList[i].judgeScore){
                if(self.accMul(self.form.repoList[i].judgeCount,self.form.repoList[i].judgeScore)!=0){
                  num++
                }
              }
              if(num == 0){
                this.msgError('每个分类至少选择一种题型填,并填写数量与分值')
                return
              }
            }
          }else if(this.form.joinType == 2){
            if(!self.form.manualList[0]){
              this.msgError('请至少添加一个分类')
              return
            }
            for(let i=0;i<self.form.manualList.length;i++){
              let num = 0;
              if(self.form.manualList[i].radioCount&&self.form.manualList[i].radioScore){
                if(self.accMul(self.form.manualList[i].radioCount,self.form.manualList[i].radioScore)!=0){
                  num++
                }
              }
              if(self.form.manualList[i].multiCount&&self.form.manualList[i].multiScore){
                if(self.accMul(self.form.manualList[i].multiCount,self.form.manualList[i].multiScore)!=0){
                  num++
                }
              }
              if(self.form.manualList[i].judgeCount&&self.form.manualList[i].judgeScore){
                if(self.accMul(self.form.manualList[i].judgeCount,self.form.manualList[i].judgeScore)!=0){
                  num++
                }
              }
              if(num == 0){
                this.msgError('每个分类至少选择一种题型填,并填写数量与分值')
                return
              }
            }
          }
          let newRepoList = [];
          let newManualList = [];
          if(this.form.joinType == 1){
            newRepoList = JSON.parse(JSON.stringify(this.form.repoList));
          }else if(this.form.joinType == 2){
            newManualList = JSON.parse(JSON.stringify(this.form.manualList));
          }
          let newObj = JSON.parse(JSON.stringify(this.form));
          delete newObj.repoList;
          delete newObj.manualList;
          if(this.form.joinType == 1){
            //自动组题
            newObj.repoList = newRepoList;
          }else if(this.form.joinType == 2){
            //手动组题
            let list = [];
            for(let i=0;i<newManualList.length;i++){
              let obj = {
                classifyId:newManualList[i].classifyId,
                judgeCount:newManualList[i].judgeCount,
                judgeScore:newManualList[i].judgeScore,
                multiCount:newManualList[i].multiCount,
                multiScore:newManualList[i].multiScore,
                radioCount:newManualList[i].radioCount,
                radioScore:newManualList[i].radioScore,
                quIds:[],
              }
              for(let o=0;o<newManualList[i].quIds.length;o++){
                obj.quIds.push(newManualList[i].quIds[o].id)
              }
              list.push(obj);
            }
            newObj.repoList = list;
          }

          let list = this.$refs['cascader'].getCheckedNodes()
          let valueList = [];
          let deptModelList=[];
          list.forEach((item)=>{
            valueList.push(item.value)
            deptModelList.push(
              { 'deptId':item.value,'deptName':item.label }
            )
          })
          newObj.deptIds = valueList;
          newObj.deptModelList=deptModelList;

          // let deptModelList=[];
          // let deptName='';
          // if(newObj.deptIds){
          //   for(let i=0;i<newObj.deptIds.length;i++){
          //     deptName=this.getTreeName(this.deptOptions,newObj.deptIds[i])
          //     deptModelList.push({'deptId':newObj.deptIds[i],'deptName':deptName})
          //   }
          // }
          // newObj.deptModelList=deptModelList;
          //  提交接口
          if(this.repoId){
            newObj.id = this.repoId;
            // this.form.id = this.repoId;
            //处理
            examExamEdit(newObj).then(response => {
              if(response.code==200){
                this.msgSuccess('操作成功')
                this.backPage();
              }
            });
          }else{
            examExamAdd(newObj).then(response => {
              if(response.code==200){
                this.msgSuccess('操作成功')
                this.backPage();
              }
            });
          }
        }
      });
    },
    //计算总分
    changeInput(){
      let self = this;
      let num = 0;
      for(let i=0;i<self.form.repoList.length;i++){
        let radio = self.accMul(self.form.repoList[i].radioCount,self.form.repoList[i].radioScore);
        let multi = self.accMul(self.form.repoList[i].multiCount,self.form.repoList[i].multiScore);
        let judge = self.accMul(self.form.repoList[i].judgeCount,self.form.repoList[i].judgeScore);
        num = self.accAdd(num,radio);
        num = self.accAdd(num,multi);
        num = self.accAdd(num,judge);
      }
      this.form.totalScore = num;
      this.$forceUpdate();
    },
    //手抖组题计算分数
    changeInputTwo(){
      let self = this;
      let num = 0;
      for(let i=0;i<self.form.manualList.length;i++){
        let radio = self.accMul(self.form.manualList[i].radioCount,self.form.manualList[i].radioScore);
        let multi = self.accMul(self.form.manualList[i].multiCount,self.form.manualList[i].multiScore);
        let judge = self.accMul(self.form.manualList[i].judgeCount,self.form.manualList[i].judgeScore);
        num = self.accAdd(num,radio);
        num = self.accAdd(num,multi);
        num = self.accAdd(num,judge);
      }
      this.form.totalScore = num;
      this.$forceUpdate();
    },
    //添加分类
    addObj(){
      let obj = {
        classifyId:"",//分类ID
        radioCount:"0",//单选数量
        radioScore:"0",//单选分数
        multiCount:"0",//多选数量
        multiScore:"0",//多选分数
        judgeCount:"0",//判断数量
        judgeScore:"0",//判断分数
      };
      this.form.repoList.push(obj);
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      systemDeptOrganizeStructure({}).then(response => {
        this.deptOptions = response.data;
      });
    },
    backPage(){
      this.$parent.pageOff();
    },
    handleQuery(){},
    handleChange(){},
    //乘法
    accMul(arg1,arg2){
      var m=0,s1=arg1.toString(),s2=arg2.toString();
      try{m+=s1.split(".")[1].length}catch(e){}
      try{m+=s2.split(".")[1].length}catch(e){}
      return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    },
    //加法
    accAdd(arg1,arg2){
      var r1,r2,m;
      try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
      try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
      m=Math.pow(10,Math.max(r1,r2))
      return ((arg1*m+arg2*m)/m).toFixed(0);
    },
  }
}
</script>

<style lang="scss" scoped>
.addExamPage{
  .content-box {
    flex: 1;
    padding: 0 20px;
  }
  .title-p{
    margin:0;
    line-height:80px;
    font-size:18px;
    color:#0045AF;
    border-bottom:2px solid #E0E0E0;
  }
  .add-button{
    border:1px solid #999;
    font-size:14px;
    line-height:30px;
    width:80px;
    text-align: center;
    color:#999;
    border-radius:4px;
    margin:0 auto;
    cursor:pointer;
  }
  .info-box{
    display: flex;
    .info-left-box{
      width:540px;
    }
    .info-right-box{
      .info-right-max-box{
        .info-right-top-box{
          display:flex;
        }
        .info-right-bottom-box{
          display: flex;
        }
        .del-o{
          position:absolute;
          width:30px;
          height:30px;
          line-height: 30px;
          text-align: center;
          border-radius:50%;
          top:38px;
          right:-30px;
          font-size:25px;
          cursor:pointer;
          color:#999;
        }
        .del-o:hover{
          background: #999;
          color:#fff;
        }
      }
    }
  }
  .info-box-manual{
    overflow: hidden;
    margin-bottom:20px;
    .info-box-manual-for-max-box{
      width:1100px;
      border:1px solid #dedede;
      margin:20px 0 0 20px;
      .info-box-manual-for-max-title{
        padding:0 20px;
        display: flex;
        border-bottom:1px solid #dedede;
        p{
          margin:0;
          line-height:40px;
          font-size:14px;
        }
        .name-p{
          flex:2;
        }
        .num-p{
          flex:1;
        }
        .info-box-manual-for-right-p-one{
          font-size:14px;
          color: #004d8c;
          text-align: center;
          cursor:pointer;
          margin-right:10px;
        }
        .info-box-manual-for-right-p-one:hover{
          color: #0E9EED;
        }
        .info-box-manual-for-right-p-two{
          font-size:14px;
          color: #004d8c;
          text-align: center;
          cursor:pointer;
        }
        .info-box-manual-for-right-p-two:hover{
          color:#E65D6E;
        }
      }
      .info-box-manual-for-left-box{
        display: flex;
        flex:1;
        padding:0 20px;
        .input-box{
          padding-top:22px;
          p:nth-child(1){
            margin-left:20px;
          }
        }
      }
    }
  }
  .configuration-box{
    .configuration-top-box{
      display: flex;
      margin-top:30px;
    }
    .configuration-bottom-box{
      margin-top:24px;
      display: flex;
      div{
        margin-right:10px;
      }
    }
  }
  .faculty-box{
    margin:30px 0 40px;
  }
  .bottom-button-box{
    width:220px;
    margin:0 auto;
    height:40px;
    display: flex;
    p{
      width:100px;
      height:40px;
      line-height:40px;
      margin:0;
      text-align: center;
      font-size:14px;
      border-radius:6px;
      cursor:pointer
    }
    p:nth-child(1){
      margin-right:20px;
      background: #E0E0E0;
      color:#333333;
    }
    p:nth-child(2){
      background: #0045AF;
      color:#ffffff;
    }
  }
}
</style>
<style lang="scss" scoped>
//手动添加题库
.add-manual-dialog{
  .add-manual-dialog-list-box{
    display: flex;
    height:500px;
    .add-manual-dialog-list-box-left{
      flex:1;
      border:1px solid #dedede;
      margin-right:20px;
      display: flex;
      flex-direction: column;
      *{
        margin:0;
      }
      .add-manual-dialog-list-box-title{
        font-size:15px;
        color: #333;
        line-height:31px;
        padding:4px 10px;
        font-weight: 700;
        background-color: #f8f8f9;
        border-bottom:1px solid #dedede;
      }
      .add-manual-dialog-list-for-max-box{
        overflow-y: scroll;
        .add-manual-dialog-list-for-box{
          width:420px;
          margin:0 auto;
          display: flex;
          border-bottom:1px solid #dedede;
          p{
            width:386px;
            flex:1;
            color:#333;
            margin-left:10px;
            line-height:38px;
            font-size:14px;
            display:block;
            overflow:hidden;
            text-overflow:ellipsis;
            white-space:nowrap;
          }
          i{
            color:#999;
            margin:10px;
            font-size:18px;
            cursor:pointer;
          }
        }
        .add-manual-dialog-list-for-null{
          font-size:16px;
          line-height:100px;
          text-align: center;
          color:#dedede;
        }
      }
    }
    .add-manual-dialog-list-box-right{
      border:1px solid #dedede;
      height:497px;
      width:700px;
      display: flex;
      flex-direction: column;
      .el-table--medium th, .el-table--medium td{
        padding:4px 0;
      }
      .pagination-container{
        height:50px;
        margin:0;
      }
    }
  }
}
</style>

