<!--信用分设置-->
<template>
  <div class="app-container Markconfig">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p">信用分设置</p>
      <p class="page-top-title-button-p" style="width:100px;" v-hasPermiRouter="['exam:markConfig:edit']" @click="addObj">添加分值段</p>
      <p class="page-top-title-out-p" v-hasPermiRouter="['exam:markConfig:edit']" @click="getList">重置</p>
      <p class="page-top-title-submit-p" v-hasPermiRouter="['exam:markConfig:edit']" @click="submitForm">保存</p>
    </div>
    <el-form class="form-content scrollbar-box" ref="form" :model="form" :rules="rules" v-if="pageType">
      <div style="display: flex">
        <el-form-item label="信用分合格分值：" prop="passMark" label-width="160px">
          <el-input
            v-model="form.passMark"
            placeholder="请输入信用分合格分值"
            maxLength="5"
            clearable
            style="width:180px;">
            <template slot="append">分</template>
          </el-input>
        </el-form-item>
        <el-form-item label="信用分合格分值获取条件：" prop="gainSafeCertList" label-width="230px">
          <el-checkbox-group v-model="form.gainSafeCertList" style="width:370px;display: flex;margin-top:2px;">
            <el-checkbox label="gainSafeCert" style="color:#999;">获得安全准入证书</el-checkbox>
            <el-checkbox label="gainSubSafeCert" style="color:#999;">获得实验室安全准入资格</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="黑名单分值：" prop="blackScore" label-width="120px">
          <el-input
            v-model="form.blackScore"
            placeholder="请输入黑名单分值"
            maxLength="5"
            clearable
            style="width:180px;">
            <template slot="append">分</template>
          </el-input>
          <span class="annotation-span" style="margin-left:20px;">信用分小于等于设定的分值即进入黑名单</span>
        </el-form-item>
      </div>
      <el-form-item label="信用分值段设置：" prop="passMark" label-width="160px" style="margin-bottom:10px;">
        <span class="annotation-span">可设置多个分值段，系统根据设置自动处理</span>
      </el-form-item>
      <div class="max-add-for-box">
        <div class="max-add-for-box-min" v-for="(item,index) in form.configDetailList">
          <p class="position-del-p el-icon-circle-close" v-if="index!=0" @click="delObj(index)"></p>
          <div class="for-interval-flex-box">
            <p class="for-interval-title-p min-p-color">信用分值区间</p>
            <el-form-item label="" :rules="rules.beginScore" :prop="'configDetailList.'+ index +'.beginScore'" style="margin-bottom:15px;">
              <el-input
                v-model="item.beginScore"
                placeholder="请输入最小值"
                maxLength="5"
                clearable
                size="small">
                <template slot="append">分</template>
              </el-input>
            </el-form-item>
            <p class="for-interval-p">~</p>
            <el-form-item label="" :rules="rules.endScore" :prop="'configDetailList.'+ index +'.endScore'" style="margin-bottom:15px;">
              <el-input
                v-model="item.endScore"
                placeholder="请输入最大值"
                maxLength="5"
                clearable
                size="small">
                <template slot="append">分</template>
              </el-input>
            </el-form-item>
          </div>
          <div class="for-check-max-box">
            <el-form-item label="" :rules="rules.checkListTwo" :prop="'configDetailList.'+ index +'.checkListTwo'">
              <p class="min-p-color" style="margin-bottom:10px;">处理方式</p>
              <el-form-item label="">
                <el-checkbox-group v-model="item.checkList" style="display: inline-block;width:270px;">
                  <el-checkbox label="warningStatus">警告<span style="color:#999;">（系统推送警告消息）</span></el-checkbox>
                </el-checkbox-group>
                <el-checkbox-group v-model="item.checkList" style="display: inline-block;width:270px;">
                  <el-checkbox label="interviewStatus">约谈<span style="color:#999;">（线下约谈）</span></el-checkbox>
                </el-checkbox-group>
                <el-checkbox-group v-model="item.checkListTwo" style="display: inline-block;width:270px;">
                  <el-checkbox label="testStatus">考试</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <div style="display: flex;">
                <div class="for-check-box" style="min-width:210px;max-width:520px;">
                  <el-checkbox-group v-model="item.checkListTwo" @change="(data)=>checkClickOne(data,item,'xx')" style="height:40px;line-height:40px;width:80px;">
                    <el-checkbox label="learnStatus">学习</el-checkbox>
                  </el-checkbox-group>
                  <p v-if="item.learnStatusType">学习时长：</p>
                  <el-form-item label="" :rules="rules.learnHour" :prop="'configDetailList.'+ index +'.learnHour'" v-if="item.learnStatusType">
                    <el-input
                      style="width:150px;"
                      v-model="item.learnHour"
                      placeholder="请输入"
                      maxLength="5"
                      clearable
                      size="small">
                      <template slot="append">时</template>
                    </el-input>
                  </el-form-item>
                  <p style="width:30px;"></p>
                  <el-form-item label="" :rules="rules.learnMinute" :prop="'configDetailList.'+ index +'.learnMinute'" v-if="item.learnStatusType">
                    <el-input
                      style="width:150px;"
                      v-model="item.learnMinute"
                      placeholder="请输入"
                      maxLength="5"
                      clearable
                      size="small">
                      <template slot="append">分</template>
                    </el-input>
                  </el-form-item>
                </div>
                <div class="for-check-box" style="margin-left:60px;">
                  <el-checkbox-group v-model="item.checkListTwo" @change="(data)=>checkClickTwo(data,item,'zt')" style="height:40px;line-height:40px;width:80px;">
                    <el-checkbox label="practiceStatus">做题</el-checkbox>
                  </el-checkbox-group>
                  <p v-if="item.practiceStatusType">题目数量：</p>
                  <el-form-item label="" :rules="rules.numberTopics" :prop="'configDetailList.'+ index +'.numberTopics'" v-if="item.practiceStatusType">
                    <el-input
                      style="width:150px;"
                      v-model="item.numberTopics"
                      placeholder="请输入"
                      maxLength="5"
                      clearable
                      size="small">
                      <template slot="append">道</template>
                    </el-input>
                  </el-form-item>
                  <p style="width:87px;text-align: right;" v-if="item.practiceStatusType">正确率：</p>
                  <el-form-item label="" :rules="rules.numberAccuracy" :prop="'configDetailList.'+ index +'.numberAccuracy'" v-if="item.practiceStatusType">
                    <el-input
                      style="width:150px;"
                      v-model="item.numberAccuracy"
                      placeholder="请输入"
                      maxLength="5"
                      clearable
                      size="small">
                      <template slot="append">%</template>
                    </el-input>
                  </el-form-item>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="" :rules="rules.handleDay" :prop="'configDetailList.'+ index +'.handleDay'" style="margin-bottom:0;">
              <p class="interval-title-p min-p-color">处理天数时限</p>
              <el-input
                v-model="item.handleDay"
                placeholder="请输入"
                maxLength="5"
                clearable
                style="width:200px;">
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </div>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { examMarkConfigList, examMarkConfigAdd, examMarkConfigEdit } from "@/api/creditViolation/index";

export default {
  name: "Markconfig",
  data() {
    return {
      //页面状态
      pageType:false,
      // 遮罩层
      loading: true,
      //数据
      form:{},
      // 表单校验
      rules: {
        passMark: [
          { required: true, message: "请输入信用分合格分值", trigger: "blur" },
          { required: true, message: "请输入信用分合格分值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        gainSafeCertList: [
          { required: true, message: "请选择信用分合格分值获取条件", trigger: "blur" }
        ],
        blackScore: [
          { required: true, message: "请输入黑名单分值", trigger: "blur" },
          { required: true, message: "请输入黑名单分值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        beginScore: [
          { required: true, message: "请输入最小值", trigger: "blur" },
          { required: true, message: "请输入最小值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        endScore: [
          { required: true, message: "请输入最大值", trigger: "blur" },
          { required: true, message: "请输入最大值", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        checkListTwo: [
          { required: true, message: "请在考试/学习/做题中至少选择一种", trigger: "blur" }
        ],
        learnHour: [
          { required: true, message: "请输入小时", trigger: "blur" },
          { required: true, message: "请输入小时", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        learnMinute: [
          { required: true, message: "请输入分钟", trigger: "blur" },
          { required: true, message: "请输入分钟", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        numberTopics: [
          { required: true, message: "请输入数量", trigger: "blur" },
          { required: true, message: "请输入数量", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        numberAccuracy: [
          { required: true, message: "请输入百分比", trigger: "blur" },
          { required: true, message: "请输入百分比", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
        handleDay: [
          { required: true, message: "请输入处理天数时限", trigger: "blur" },
          { required: true, message: "请输入处理天数时限", validator: this.spaceJudgment, trigger: "blur" },
          { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //勾选学习
    checkClickOne(data,item,name){
      if(name == 'xx'){
        let num = 0;
        for(let i=0;i<data.length;i++){
          if('learnStatus' == data[i]){
            num++
          }
        }
        item.learnStatusType = num != 0;
      }
    },
    //勾选做题
    checkClickTwo(data,item,name){
      if(name == 'zt'){
        let num = 0;
        for(let i=0;i<data.length;i++){
          if('practiceStatus' == data[i]){
            num++
          }
        }
        item.practiceStatusType = num != 0;
      }
    },
    //添加分值段
    addObj(){
      let obj = {
        beginScore:"",//最小信用分
        endScore:"",//最大信用分
        checkList:[],//处理方式
        checkListTwo:[],//处理方式
        warningStatus:"",//警告
        interviewStatus:"",//约谈
        testStatus:"",//考试
        learnStatus:"",//学习
        learnStatusType:false,
        learnHour:"",//学习小时
        learnMinute:"",//学习分钟
        practiceStatus:"",//做题
        practiceStatusType:false,
        numberTopics:"",//做题数量
        numberAccuracy:"",//做题准确率
        handleDay:"",//天数
      };
      this.form.configDetailList.push(obj);
    },
    //删除分段数据
    delObj(index){
      this.form.configDetailList.splice(index,1);
    },
    /** 查询数据 */
    getList() {
      this.pageType = false;
      examMarkConfigList({page:"1",pageSize:"1"}).then( response => {
        if(response.data.records[0]){
          //修改
          let newObj = JSON.parse(JSON.stringify(response.data.records[0]));
          let obj = {
            id:newObj.id,
            passMark:newObj.passMark,
            blackScore:newObj.blackScore,
            gainSafeCertList:[],
            configDetailList:[],
          };
          //处理一级数据
          if(newObj.gainSafeCert){
            obj.gainSafeCert = true;
            obj.gainSafeCertList.push('gainSafeCert');
          }else{
            obj.gainSafeCert = false
          }
          if(newObj.gainSubSafeCert){
            obj.gainSubSafeCert = true;
            obj.gainSafeCertList.push('gainSubSafeCert');
          }else{
            obj.gainSubSafeCert = false
          }
          //处理二级数据
          for(let i=0;i<newObj.configDetailList.length;i++){
            let minObj = {
              beginScore:newObj.configDetailList[i].beginScore,
              endScore:newObj.configDetailList[i].endScore,
              handleDay:newObj.configDetailList[i].handleDay,
              checkList:[],
              checkListTwo:[],
            };
            //警告
            if(newObj.configDetailList[i].warningStatus){
              minObj.warningStatus = true;
              minObj.checkList.push('warningStatus');
            }else{
              minObj.warningStatus = false;
            }
            //约谈
            if(newObj.configDetailList[i].interviewStatus){
              minObj.interviewStatus = true;
              minObj.checkList.push('interviewStatus');
            }else{
              minObj.interviewStatus = false;
            }
            //考试
            if(newObj.configDetailList[i].testStatus){
              minObj.testStatus = true;
              minObj.checkListTwo.push('testStatus');
            }else{
              minObj.testStatus = false;
            }
            //学习
            if(newObj.configDetailList[i].learnStatus){
              minObj.learnStatus = true;
              minObj.learnHour = newObj.configDetailList[i].learnHour;
              minObj.learnMinute = newObj.configDetailList[i].learnMinute;
              minObj.learnStatusType = true;
              minObj.checkListTwo.push('learnStatus');
            }else{
              minObj.learnStatus = false;
              minObj.learnHour = "";
              minObj.learnMinute = "";
              minObj.learnStatusType = false;
            }
            //做题
            if(newObj.configDetailList[i].practiceStatus){
              minObj.practiceStatus = true;
              minObj.numberTopics = newObj.configDetailList[i].numberTopics;
              minObj.numberAccuracy = newObj.configDetailList[i].numberAccuracy;
              minObj.practiceStatusType = true;
              minObj.checkListTwo.push('practiceStatus');
            }else{
              minObj.practiceStatus = false;
              minObj.numberTopics = "";
              minObj.numberAccuracy = "";
              minObj.practiceStatusType = false;
            }
            obj.configDetailList.push(minObj);
          }
          this.$set(this,'form',obj);
          this.pageType = true;
        }else{
          //新提交
          let obj = {
            passMark:"",//信用分合格值
            gainSafeCertList:[],//多选数组库
            gainSafeCert:"",//获得安全准入证书
            gainSubSafeCert:"",//获得实验室安全准入资格
            blackScore:"",//黑名单分值
            configDetailList:[
              {
                beginScore:"",//最小信用分
                endScore:"",//最大信用分
                checkList:[],//处理方式
                checkListTwo:[],//处理方式
                warningStatus:"",//警告
                interviewStatus:"",//约谈
                testStatus:"",//考试
                learnStatus:"",//学习
                learnStatusType:false,
                learnHour:"",//学习小时
                learnMinute:"",//学习分钟
                practiceStatus:"",//做题
                practiceStatusType:false,
                numberTopics:"",//做题数量
                numberAccuracy:"",//做题准确率
                handleDay:"",//天数
              }
            ],
          };
          this.$set(this,'form',obj);
          this.pageType = true;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          for(let i=0;i<self.form.configDetailList.length;i++){
            if(self.form.configDetailList[i].beginScore >= self.form.configDetailList[i].endScore){
              this.msgError('信用分值区间,最小值不能大于等于最大值')
              return
            }
          }
          let newObj = JSON.parse(JSON.stringify(this.form));
          let obj = {
            passMark:newObj.passMark,
            blackScore:newObj.blackScore,
            configDetailList:[],
          };
          if(newObj.id){
            obj.id = newObj.id;
          }
          //处理一级多选
          let gainSafeCertNum = 0;
          let gainSubSafeCertNum = 0;
          for(let i=0;i<newObj.gainSafeCertList.length;i++){
            if(newObj.gainSafeCertList[i] == 'gainSafeCert'){
              gainSafeCertNum++
            }
            if(newObj.gainSafeCertList[i] == 'gainSubSafeCert'){
              gainSubSafeCertNum++
            }
          }
          if(gainSafeCertNum == 0){
            obj.gainSafeCert = false;
          }else{
            obj.gainSafeCert = true;
          }
          if(gainSubSafeCertNum == 0){
            obj.gainSubSafeCert = false;
          }else{
            obj.gainSubSafeCert = true;
          }
          //处理二级子项
          for(let i=0;i<newObj.configDetailList.length;i++){
            let minObj ={
              beginScore:newObj.configDetailList[i].beginScore,
              endScore:newObj.configDetailList[i].endScore,
              handleDay:newObj.configDetailList[i].handleDay,
            };
            //处理二级多选--警告/约谈
            let warningStatusNum = 0;
            let interviewStatusNum = 0;
            for(let o=0;o<newObj.configDetailList[i].checkList.length;o++){
              if(newObj.configDetailList[i].checkList[o] == 'warningStatus'){
                warningStatusNum++
              }
              if(newObj.configDetailList[i].checkList[o] == 'interviewStatus'){
                interviewStatusNum++
              }
            }
            if(warningStatusNum == 0){
              minObj.warningStatus = false;
            }else{
              minObj.warningStatus = true;
            }
            if(interviewStatusNum == 0){
              minObj.interviewStatus = false;
            }else{
              minObj.interviewStatus = true;
            }
            //处理二级多选--考试
            let testStatusNum = 0;
            for(let o=0;o<newObj.configDetailList[i].checkListTwo.length;o++){
              if(newObj.configDetailList[i].checkListTwo[o] == 'testStatus'){
                testStatusNum++
              }
            }
            if(testStatusNum == 0){
              minObj.testStatus = false;
            }else{
              minObj.testStatus = true;
            }
            //处理二级多选--学习
            if(newObj.configDetailList[i].learnStatusType){
              minObj.learnStatus = true;
              minObj.learnHour = newObj.configDetailList[i].learnHour;
              minObj.learnMinute = newObj.configDetailList[i].learnMinute;
            }else{
              minObj.learnStatus = false;
              minObj.learnHour = "";
              minObj.learnMinute = "";
            }
            //处理二级多选--做题
            if(newObj.configDetailList[i].practiceStatusType){
              minObj.practiceStatus = true;
              minObj.numberTopics = newObj.configDetailList[i].numberTopics;
              minObj.numberAccuracy = newObj.configDetailList[i].numberAccuracy;
            }else{
              minObj.practiceStatus = false;
              minObj.numberTopics = "";
              minObj.numberAccuracy = "";
            }
            obj.configDetailList.push(minObj);
          }
          if(this.form.id){
            examMarkConfigEdit(obj).then( response => {
              this.getList();
              this.msgSuccess("保存成功");
            });
          }else{
            examMarkConfigAdd(obj).then( response => {
              this.msgSuccess("保存成功");
            });
          }
        }
      });
    },
  }
};
</script>

<style scoped lang="scss">
  .Markconfig {
    .title-p{
      margin:0;
      line-height:80px;
      font-size:18px;
      color:#0045AF;
      border-bottom:2px solid #E0E0E0;
    }
    .form-content{
      overflow-y: scroll;
      flex:1;
      padding-top:22px;
      .interval-flex-box{
        margin-left:220px;
        display: flex;
        .interval-title-p{
          width:100px;
          line-height:40px;
          font-size:14px;
          margin:0;
        }
        .interval-p{
          width:40px;
          text-align: center;
          line-height:40px;
          margin:0;
        }
      }
      .min-p-color{
        color:#606266;
        font-size:14px;
        margin:0;
      }
      .check-box{
        display: flex;
      }
      .annotation-span{
        font-size:14px;
        font-weight:500;
        line-height:40px;
        color:#999999;
      }
      .max-add-for-box{
        margin-left:140px;
        .max-add-for-box-min{
          border:1px solid #dedede;
          width:1200px;
          margin:20px 20px;
          border-radius:4px;
          padding:20px 20px 40px 20px;
          position: relative;
          .position-del-p{
            position: absolute;
            right:10px;
            top:10px;
            font-size:24px;
            color:#999;
            cursor:pointer;
          }
          .position-del-p:hover{
            color: #E65D6E;
          }
          .for-interval-flex-box{
            display: flex;
            .for-interval-title-p{
              width:100px;
              line-height:40px;
              font-size:14px;
              margin:0;
            }
            .for-interval-p{
              width:40px;
              text-align: center;
              line-height:40px;
              margin:0;
            }
          }
          .for-check-max-box{
            .for-check-box{
              display: flex;
              p{
                margin:0;
                font-size:14px;
                color:#606266;
                font-weight:500;
                line-height:40px;
              }
            }
          }
        }
      }
    }
    .form-bottom-button-box{
      height:80px;
      padding:20px;
      display: flex;
      .null-p-button{
        flex:1;
      }
    }
  }
</style>
