<template>
  <div class="app-container noticeconfig">
    <div class="title-box">
      <div class="title-min-box" :class="buttonType == 1 ? 'check-box' : ''" @click="titleButton(1)">
        <p>检查通知</p>
        <p></p>
      </div>
      <div v-if="versionField() != 'xiBeiNongLinDaXue'"
        class="title-min-box" :class="buttonType == 2 ? 'check-box' : ''" @click="titleButton(2)">
        <p>预案通知</p>
        <p></p>
      </div>
    </div>
    <div class="content-box scrollbar-box">
      <el-form v-if="buttonType == 1" :model="examineForm" ref="examineForm" :inline="true" label-width="120px">
        <div class="examine-big-box">
          <div class="examine-div-one">
            <p>检查任务通知方式：</p>
            <el-checkbox :disabled="!tableButtonType" v-model="examineType" style="margin:1px 0 0 10px;" @change="examineAll">全选</el-checkbox>
          </div>
          <div class="examine-div-two">
            <el-checkbox-group :disabled="!tableButtonType" v-model="examineForm.data1" @change="examineChange">
              <el-checkbox style="margin-right:48px;"
                v-for="item in examineList" :label="item.key" :key="item.key">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
        <div class="examine-big-box">
          <div class="examine-div-one">
            <p>整改通知方式：</p>
            <el-checkbox :disabled="!tableButtonType" v-model="rectificationType" style="margin:1px 0 0 10px;" @change="rectificationAll">全选</el-checkbox>
          </div>
          <div class="examine-div-two">
            <el-checkbox-group :disabled="!tableButtonType" v-model="examineForm.data2" @change="rectificationChange">
              <el-checkbox style="margin-right:48px;"
                v-for="item in rectificationList" :label="item.key" :key="item.key">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
        <div class="examine-big-box">
          <div class="examine-div-one">
            <p>复核结果通知方式：</p>
            <el-checkbox :disabled="!tableButtonType" v-model="reviewType" style="margin:1px 0 0 10px;" @change="reviewAll">全选</el-checkbox>
          </div>
          <div class="examine-div-two">
            <el-checkbox-group :disabled="!tableButtonType" v-model="examineForm.data3" @change="reviewChange">
              <el-checkbox style="margin-right:48px;"
                v-for="item in reviewList" :label="item.key" :key="item.key">{{item.label}}</el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
      </el-form>
      <el-form v-if="buttonType == 2" :model="form" ref="form" :inline="true" label-width="120px">
        <el-form-item label="广播通知" prop="riskRadio" class="form-item-box">
          <el-input
            style="width:350px;"
            type="textarea"
            maxlength="50"
            :rows="3"
            resize="none"
            placeholder="请输入广播通知内容"
            v-model="form.riskRadio">
          </el-input>
        </el-form-item>
        <el-form-item label="语音播报" prop="riskVoice" class="form-item-box">
          <el-input
            style="width:350px;"
            type="textarea"
            maxlength="50"
            :rows="3"
            resize="none"
            placeholder="请输入语音播报内容"
            v-model="form.riskVoice">
          </el-input>
        </el-form-item>
        <el-form-item label="电话通知" prop="riskPhone" class="form-item-box">
          <el-input
            style="width:350px;"
            type="textarea"
            maxlength="50"
            :rows="3"
            resize="none"
            placeholder="请输入电话通知内容"
            v-model="form.riskPhone">
          </el-input>
        </el-form-item>
        <el-form-item label="短信提示" prop="riskMessage" class="form-item-box">
          <el-input
            style="width:350px;"
            type="textarea"
            maxlength="50"
            :rows="3"
            resize="none"
            placeholder="请输入短信提示内容"
            v-model="form.riskMessage">
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="button-box" v-if="buttonType == 1">
      <p class="null-p"></p>
      <p class="inquire-button-one button-p" @click="upExamineForm" v-hasPermi="['security:notice:add']">
        提交</p>
      <p class="null-p"></p>
    </div>
    <div class="button-box" v-if="buttonType == 2">
      <p class="null-p"></p>
      <p class="inquire-button-one button-p" @click="upData" v-if="form.id" v-hasPermi="['laboratory:noticeconfig:edit']">
        保存</p>
      <p class="inquire-button-one button-p" @click="upData" v-if="!form.id" v-hasPermi="['laboratory:noticeconfig:add']">
        提交</p>
      <p class="null-p"></p>
    </div>
  </div>
</template>

<script>
import { getNoticeconfigInfo, postNoticeconfigInfo, putNoticeconfigInfo } from "@/api/laboratory/noticeconfig";
import { noticeConfigList, noticeConfigAdd } from '@/api/safetyCheck/index'

export default {
  name: "Noticeconfig",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['security:notice:add']),
      buttonType:1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知配置表格数据
      form: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        riskVoice: null,
        riskPhone: null,
        riskMessage: null,
        exitRadio: null,
        exitVoice: null,
        exitPhone: null,
        exitMessage: null
      },
      examineType:false,
      examineList:[],
      rectificationType:false,
      rectificationList:[],
      reviewType:false,
      reviewList:[
        {
          label:"短信通知",
          key:"1",
        },
        {
          label:"系统通知",
          key:"2",
        },
      ],
      examineForm:{
        data1:[],
        data2:[],
        data3:[],
      },
      data1Id:"",
      data2Id:"",
      data3Id:"",
    };
  },
  created() {
    if(this.versionField() == 'xiBeiNongLinDaXue'){
      this.$set(this,'examineList',[
        {
          label:"短信通知",
          key:'1',
        },
        {
          label:"系统通知",
          key:'2',
        },
      ]);
      this.$set(this,'rectificationList',[
        {
          label:"短信通知",
          key:'1',
        },
        {
          label:"系统通知",
          key:'2',
        },
      ]);
      this.noticeConfigList();
    }else{
      this.$set(this,'examineList',[
        {
          label:"短信通知",
          key:"1",
        },
        {
          label:"系统通知",
          key:"2",
        },
        {
          label:"一体机通知",
          key:"3",
        },
      ]);
      this.$set(this,'rectificationList',[
        {
          label:"短信通知",
          key:"1",
        },
        {
          label:"系统通知",
          key:"2",
        },
        {
          label:"一体机通知",
          key:"3",
        },
      ]);
      this.noticeConfigList();
      this.getList();
    }
  },
  methods: {
    //检查通知
    noticeConfigList(){
      noticeConfigList().then( response => {
        for(let i=0;i<response.data.length;i++){
          if(response.data[i].noticeType == 1){
            let data1 = response.data[i].noticeWay.split(',');
            if(this.versionField() == 'xiBeiNongLinDaXue'){
              let list1 = []
              for(let i=0;i<data1.length;i++){
                if(data1[i] != 3){
                  list1.push(data1[i])
                }
              }
              this.$set(this.examineForm,'data1',list1);
              this.$set(this,'examineType',data1[1]?true:false);
            }else{
              this.$set(this.examineForm,'data1',data1);
              this.$set(this,'examineType',data1[2]?true:false);
            }
            this.$set(this,'data1Id',response.data[i].id);
          }else if(response.data[i].noticeType == 2){
            let data2 = response.data[i].noticeWay.split(',');
            if(this.versionField() == 'xiBeiNongLinDaXue'){
              let list2 = []
              for(let i=0;i<data2.length;i++){
                if(data2[i] != 3){
                  list2.push(data2[i])
                }
              }
              this.$set(this.examineForm,'data2',list2);
              this.$set(this,'rectificationType',data2[1]?true:false);
            }else{
              this.$set(this.examineForm,'data2',data2);
              this.$set(this,'rectificationType',data2[2]?true:false);
            }
            this.$set(this,'data2Id',response.data[i].id);
          }else if(response.data[i].noticeType == 3){
            let data3 = response.data[i].noticeWay.split(',');
            this.$set(this.examineForm,'data3',data3);
            this.$set(this,'reviewType',data3[1]?true:false);
            this.$set(this,'data3Id',response.data[i].id);
          }
        }
      });
    },
    //检查通知提交
    upExamineForm(){
      let list = [];
      let obj1 = {
        id:this.data1Id?this.data1Id:'',
        noticeType:1,
        noticeWay:this.examineForm.data1+'',
      };
      list.push(obj1);
      let obj2 = {
        id:this.data2Id?this.data2Id:'',
        noticeType:2,
        noticeWay:this.examineForm.data2+'',
      };
      list.push(obj2);
      let obj3 = {
        id:this.data3Id?this.data3Id:'',
        noticeType:3,
        noticeWay:this.examineForm.data3+'',
      };
      list.push(obj3);
      noticeConfigAdd(list).then( response => {
        this.msgSuccess(response.msg);
        this.noticeConfigList();
      })
    },
    // 匹配
    examineAll(item){
      let self = this;
      if(item){
        let list = [];
        for(let i=0;i<self.examineList.length;i++){
          list.push(self.examineList[i].key)
        }
        this.$set(this.examineForm,'data1',list);
      }else{
        this.$set(this.examineForm,'data1',[]);
      }
    },
    examineChange(item){
      if(this.versionField() == 'xiBeiNongLinDaXue'){
        if(item[1]){
          this.$set(this,'examineType',true);
        }else{
          this.$set(this,'examineType',false);
        }
      }else{
        if(item[2]){
          this.$set(this,'examineType',true);
        }else{
          this.$set(this,'examineType',false);
        }
      }
    },
    rectificationAll(item){
      let self = this;
      if(item){
        let list = [];
        for(let i=0;i<self.rectificationList.length;i++){
          list.push(self.rectificationList[i].key)
        }
        this.$set(this.examineForm,'data2',list);
      }else{
        this.$set(this.examineForm,'data2',[]);
      }
    },
    rectificationChange(item){
      if(this.versionField() == 'xiBeiNongLinDaXue'){
        if(item[1]){
          this.$set(this,'rectificationType',true);
        }else{
          this.$set(this,'rectificationType',false);
        }
      }else{
        if(item[2]){
          this.$set(this,'rectificationType',true);
        }else{
          this.$set(this,'rectificationType',false);
        }
      }
    },
    reviewAll(item){
      let self = this;
      if(item){
        let list = [];
        for(let i=0;i<self.reviewList.length;i++){
          list.push(self.reviewList[i].key)
        }
        this.$set(this.examineForm,'data3',list);
      }else{
        this.$set(this.examineForm,'data3',[]);
      }
    },
    reviewChange(item){
      if(item[1]){
        this.$set(this,'reviewType',true);
      }else{
        this.$set(this,'reviewType',false);
      }
    },
    //切换
    titleButton(type){
      if(type != this.buttonType){
        this.$set(this,'buttonType',type)
      }
    },
    upData(){
      let self = this;
      this.$confirm('是否确定'+(self.form.id?'保存':'提交')+'？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        if(self.buttonType == 1){

        }else if(self.buttonType == 2){
          if(self.form.id){
            self.putNoticeconfigInfo();
          }else{
            self.postNoticeconfigInfo();
          }
        }
      }).then(() => {

      }).catch(() => {});
    },
    postNoticeconfigInfo(){
      postNoticeconfigInfo(this.form).then( response => {
        this.msgSuccess("操作成功")
        this.getList();
      });
    },
    putNoticeconfigInfo(){
      putNoticeconfigInfo(this.form).then( response => {
        this.msgSuccess("操作成功")
        this.getList();
      });
    },
    /** 查询通知配置列表 */
    getList() {
      this.loading = true;
      getNoticeconfigInfo().then( response => {
        this.form =  response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        riskVoice: null,
        riskPhone: null,
        riskMessage: null,
        exitRadio: null,
        exitVoice: null,
        exitPhone: null,
        exitMessage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通知配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNoticeconfig(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改通知配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNoticeconfig(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNoticeconfig(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除通知配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNoticeconfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/noticeconfig/export', {
        ...this.queryParams
      }, `laboratory_noticeconfig.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .noticeconfig{
    display: flex !important;
    flex:1;
    flex-direction: column;
    overflow: hidden;
    *{
      margin:0;
      font-weight:500;
    }
    .title-box{
      height:80px;
      border-bottom:1px solid #dedede;
      overflow-x:scroll;
      overflow-y:hidden;
      white-space: nowrap;
      padding:0 40px;
      .title-min-box{
        cursor: pointer;
        display: inline-block;
        overflow: hidden;
        height:80px;
        width:65px;
        margin:10px 70px 5px 0;
        p:nth-child(1){
          line-height: 40px;
          margin-top: 10px;
          font-size:16px;
          text-align: center;
        }
        p:nth-child(2){
          width:60px;
          height:4px;
          margin:0 auto;
          border-radius:4px;
        }
      }
      .check-box{
        p:nth-child(1){
          color:#0045AF!important;
        }
        p:nth-child(2){
          background: #0045AF!important;
        }
      }
    }
    .title-box::-webkit-scrollbar{
      width: 4px;     /*高宽分别对应横竖滚动条的尺寸*/
      height: 4px;
    }
    .title-box::-webkit-scrollbar-thumb{
      border-radius: 5px;
      background: #CBCDD1;
    }
    .title-box::-webkit-scrollbar-track{
      -webkit-box-shadow: inset 0 0 5px rgba(255,255,255,0);
      border-radius: 0;
      background: rgba(255,255,255,0);
    }
    .content-box{
      flex:1;
      padding:40px 46px 0;
      .examine-big-box{
        display: inline-block;
        overflow: hidden;
        border:1px solid #E0E0E0;
        border-radius:4px;
        margin-left:35px;
        width:450px;
        height:130px;
        div{
          font-size:14px;
          color:#333;
        }
        .examine-div-one{
          display: flex;
          margin:25px 0 30px 12px;
        }
        .examine-div-two{
          margin:0 0 20px 18px;
        }
      }
    }
    .button-box{
      display: flex;
      height:80px;
      .button-p{
        width:80px;
        height:40px;
        margin:20px 0;
      }
      .null-p{
        flex:1;
      }
    }
    .top-title-box{
      display: flex;
      height:80px;
      border-bottom:1px solid #E0E0E0;
      margin-bottom:54px;
      p:nth-child(1){
        line-height:80px;
        font-size:18px;
        color:#0045AF;
        flex:1;
      }
      p:nth-child(2){
        margin-top:20px;
      }
    }
    .form-item-box{
      display: inline-block;
      margin-bottom:28px;
    }
  }
</style>
