<template>
  <div class="page-container monitorConfig-addPage">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p">{{propsData.configId?'编辑':'新增'}}</p>
      <p class="page-top-title-out-p" @click="backPage">返回</p>
      <p class="page-top-title-add-p" @click="submitForm">提交</p>
    </div>
    <div class="content-box scrollbar-box">
      <el-form class="add-form-box" :model="newData" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="名称：" prop="monitorName">
          <el-input v-model="newData.monitorName" placeholder="请输入名称" maxLength="20" style="width:340px;"></el-input>
        </el-form-item>
        <el-form-item label="监控设备：" prop="deviceType">
          <el-select v-model="newData.deviceType" placeholder="请选择监控设备" style="width: 340px">
            <el-option
              v-for="dict in moduleTypeOption"
              :key="dict.typeKey"
              :label="dict.typeName"
              :value="dict.typeKey"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态：" prop="monitorType">
          <el-radio-group v-model="newData.monitorType" style="width:340px;">
            <el-radio :label="1">设备上线</el-radio>
            <el-radio :label="2">设备离线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="告警方式：" prop="alarmType">
          <el-select v-model="newData.alarmType" placeholder="请选择告警方式" style="width: 340px">
            <el-option label="立即" :value="1"/>
            <el-option label="累计" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="次数：" prop="count" v-if="newData.alarmType == 2">
          <el-input-number v-model="newData.count" style="width:340px;" controls-position="right" :min="2" :max="10"></el-input-number>
        </el-form-item>
        <!--<div class="for-max-big-box">-->
          <!--<p class="for-title-p"><span>*</span>消息：</p>-->
          <!--<div class="for-big-box">-->
            <!--<div class="for-box">-->

            <!--</div>-->
          <!--</div>-->
        <!--</div>-->
        <el-form-item label="消息：" prop="alarmMessages" style="margin-bottom:0;">
          <div class="messages-max-big-box">
            <div class="messages-big-box">
              <div class="messages-for-box" v-for="(item,index) in newData.alarmMessages">
                <div class="messages-for-min-box">
                  <el-form-item label="" :prop="'alarmMessages.'+index+'.messageType'" :rules="rules.messageType" style="margin-right:20px;">
                    <el-select v-model="item.messageType" placeholder="请选择类型" @change="(val)=>changeMessageType(val,index)" style="width: 200px">
                      <el-option
                        v-for="dict in messageTypeOption"
                        :key="dict.id"
                        :label="dict.name"
                        :value="dict.id"
                      />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="" :prop="'alarmMessages.'+index+'.messageTemplate'" :rules="rules.messageTemplate" style="margin-right:20px;">
                    <el-select v-model="item.messageTemplate" placeholder="请选择模板" style="width: 240px">
                      <el-option
                        v-for="dict in item.messageModeOption"
                        :key="dict.templateId"
                        :label="dict.templateName"
                        :value="dict.templateId"
                      />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="" :prop="'alarmMessages.'+index+'.receiver'" style="margin-right:5px;">
                    <el-input v-model="item.receiver" placeholder="请输入接收人" maxLength="50" style="width:370px;"></el-input>
                  </el-form-item>
                </div>
                <span class="el-icon-remove del-icon-span" @click="delItem(index)"></span>
              </div>
            </div>
            <span class="el-icon-circle-plus add-icon-span" @click="addItem"></span>
          </div>
        </el-form-item>
        <el-form-item label="状态：" prop="state">
          <el-radio-group v-model="newData.state" style="width:340px;">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="说明：" prop="remark">
          <el-input
            type="textarea"
            placeholder="请输入说明"
            v-model="newData.remark"
            resize="none"
            maxLength="100"
            style="width:570px;"
            :rows="4"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import { iotMonitorConfigDetail,iotMonitorConfigAdd,iotMonitorConfigUpdate,iotTypeGetAllTypes,iotAlarmTemplateDrop } from "@/api/iotDevice/index";
  export default {
    name: 'addPage',
    props:{
      propsData:{},
    },
    data(){
      const doubleCheck = (rule, value, callback) => {
        let self = this;
        let num = 0;
        for(let i=0;i<self.newData.alarmMessages.length;i++){
          if(value == self.newData.alarmMessages[i].messageType){
            num++
          }
        }
        if(num > 1){
          return callback(new Error('类型重复'));
        }else{
          callback();
        }
      };
      return{
        //监控设备列表
        moduleTypeOption:[],
        //消息类型列表
        messageTypeOption:[
          {name:"钉钉",id:1},
          {name:"微信",id:2},
          {name:"邮件",id:3},
          {name:"电话",id:4},
          {name:"短信",id:5},
        ],
        newData:{},
        rules:{
          deviceType: [
            { required: true, message: "请选择监控设备", trigger: "blur" },
            { required: true, message: "请选择监控设备", validator: this.spaceJudgment, trigger: "blur" }
          ],
          monitorName: [
            { required: true, message: "请输入名称", trigger: "blur" },
            { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          monitorType: [
            { required: true, message: "请选择设备状态", trigger: "blur" },
            { required: true, message: "请选择设备状态", validator: this.spaceJudgment, trigger: "blur" }
          ],
          alarmType: [
            { required: true, message: "请选择告警方式", trigger: "blur" },
            { required: true, message: "请选择告警方式", validator: this.spaceJudgment, trigger: "blur" }
          ],
          count: [
            { required: true, message: "请输入次数", trigger: "blur" },
            { required: true, message: "请输入次数", validator: this.spaceJudgment, trigger: "blur" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
            { required: true, message: "请选择状态", validator: this.spaceJudgment, trigger: "blur" }
          ],
          alarmMessages: [
            { required: true, message: "请添加消息", trigger: "blur" },
            { required: true, message: "请添加消息", validator: this.spaceJudgment, trigger: "blur" }
          ],
          messageType: [
            { required: true, message: "请选择类型", trigger: "blur" },
            { required: true, message: "请选择类型", validator: this.spaceJudgment, trigger: "blur" },
            { required: true, message: "类型重复", validator: doubleCheck, trigger: "blur" }
          ],
          messageTemplate: [
            { required: true, message: "请选择模板", trigger: "blur" },
            { required: true, message: "请选择模板", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
      }
    },
    created(){
      this.initialize();
    },
    mounted(){

    },
    methods:{
      // 初始化
      initialize(){
        let self = this;
        this.iotTypeGetAllTypes();
        if(this.propsData.configId){
          iotMonitorConfigDetail({id:this.propsData.configId}).then(response => {
            this.$set(this,'newData',response.data);
            for(let i=0;i<self.newData.alarmMessages.length;i++){
              self.iotAlarmTemplateDrop(self.newData.alarmMessages[i].messageType,i).then(res=>{})
            }
          });
        }else{
          this.$set(this,'newData',{
            deviceType:'',
            monitorName:'',
            monitorType:'',
            alarmType:'',
            count:'',
            state:'',
            remark:'',
            alarmMessages:[{messageType:'',messageTemplate:'',receiver:'',messageModeOption:[]}],
          })
        }
      },
      //添加消息项
      addItem(){
        if(!this.newData.alarmMessages[4]){
          this.newData.alarmMessages.push({
            messageType:'',
            messageTemplate:'',
            receiver:''
          })
        }else{
          this.msgError('无法添加更多')
        }
      },
      //删除消息项
      delItem(index){
        if(this.newData.alarmMessages[1]){
          this.newData.alarmMessages.splice(index,1)
        }else{
          this.msgError('至少保留一项')
        }
      },
      //获取监控设备列表
      iotTypeGetAllTypes(){
        iotTypeGetAllTypes(this.queryParams).then(response => {
          this.$set(this,'moduleTypeOption',response.data);
        });
      },
      //获取监控设备列表
      iotAlarmTemplateDrop(messageType,index){
        return new Promise(resolve=>{
          iotAlarmTemplateDrop({alarmType:messageType}).then(response => {
            this.$set(this.newData.alarmMessages[index],'messageModeOption',response.data);
            resolve()
          });
        })
      },
      //选中消息类型
      changeMessageType(val,index){
        iotAlarmTemplateDrop({alarmType:val}).then(response => {
          this.$set(this.newData.alarmMessages[index],'messageTemplate','');
          this.$set(this.newData.alarmMessages[index],'messageModeOption',response.data);
        });
      },
      // 返回按钮
      backPage(){
        this.$parent.tableButton(6);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let obj = JSON.parse(JSON.stringify(this.newData))
            for (let i=0;i<obj.alarmMessages.length;i++){
              delete obj.alarmMessages[i].messageModeOption
            }
            // obj.alarmMessages = JSON.stringify(obj.alarmMessages)
            if(obj.configId){
              obj.configId = this.propsData.configId
              //编辑
              iotMonitorConfigUpdate(obj).then(response => {
                this.msgSuccess(response.message)
                this.$parent.tableButton(6);
              });
            }else{
              //新增
              iotMonitorConfigAdd(obj).then(response => {
                this.msgSuccess(response.message)
                this.$parent.tableButton(6);
              });
            }
          }
        })
      },
    },
  }
</script>

<style scoped lang="scss">
  .monitorConfig-addPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .content-box{
      flex:1;
      padding:40px 20px 20px;
      .messages-max-big-box{
        display: flex;
        .messages-big-box{
          .messages-for-box{
            display: flex;
            padding-bottom:20px;
            .messages-for-min-box{
              display: flex;
            }
            .del-icon-span{
              cursor: pointer;
              color:#FF6666;
              font-size:20px;
              line-height:40px;
              width:40px;
              text-align: center;
            }
          }
        }
        .add-icon-span{
          cursor: pointer;
          color:#0183FA;
          font-size:20px;
          line-height:40px;
          height:40px;
          width:40px;
          text-align: center;
        }
      }
      .for-max-big-box{
        display: flex;
        .for-title-p{
          width:108px;
          font-size:14px;
          line-height:40px;
          margin-bottom:20px;
          text-align: right;
          margin-right:12px;
          span{
            color:#FF6666;
            margin-right:5px;
          }
        }
        .for-big-box{
          background-color: #00a0e9;
          height:40px;
        }
      }
    }
  }
</style>
