<template>
  <div class="app-container">
    <Form ref="formRef" :formList="formList" :ruleFormData="ruleForm" :config="paramsData && paramsData.readonly ? config : {}" @fnClick="callback"></Form>
  </div>
</template>

<script setup name="Handle">
  import Form from '@/components/form/index.vue';
  import {addAlarmPush, getAlarmPush, updateAlarmPush} from "@/api/disRevenue/resource"
  export default {
    name: 'messagePushDetails',
    components: {Form},
    dicts: ['alarm_type', 'push_method'],
    data() {
      return {
        ruleForm: {},
        formList: [],
        config: {
          buttonGroup: [{title: '返回', fnCode: 'goBack'}]
        },
        paramsData: {}
      }
    },
    created() {
      this.paramsData = this.$route && this.$route.query;
      if (this.paramsData && this.paramsData.id) {
        this.getFormDataList(this.paramsData.id);
      } else {
        this.fnFormList();
      }
    },
    methods: {
      // formList集合
      fnFormList(objVal) {
        let spanNum = this.paramsData && this.paramsData.readonly ? 12 : 16;
        this.formList = [{
          config: {title: '基本信息', readonly: this.paramsData && this.paramsData.readonly},
          controls: {
            id: {label: 'ID',hidden: true},
            configName: {label: '配置名称', span: spanNum, type: 'input', required: true},
            pushMethod: {label: '推送方式', span: spanNum, type: 'select', eventName: 'change', options: this.dict.type.push_method, required: true},
            pushAddress: {label: '推送地址', span: spanNum, type: 'input', required: true, hidden: this.ruleForm.pushMethod === '2' ? true : false},
            pushAlarmTypes: {label: '推送告警类型', span: spanNum, type: 'select', options: this.dict.type.alarm_type, required: true},
            contactPhones: {label: '提示人手机号', span: spanNum, type: 'input', warningTitle: '可填多个手机号，以英文逗号隔开',required: this.ruleForm.pushMethod === '2'},
            createBy: {label: '创建人', span: spanNum, type: 'input', hidden: !(this.paramsData && this.paramsData.readonly)},
            createTime: {label: '创建时间', span: spanNum, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
            updateTime: {label: '修改时间', span: spanNum, type: 'datetime', hidden: !(this.paramsData && this.paramsData.readonly)},
            messageContent: {label: '消息内容', span: spanNum, type: 'textarea', required: true, warningTitle: '可引用的告警字段包括[告警时间]、[IP]、[告警类型]、[告警设备]、[告警内容]、[业务名称]'}
          }
        }];
      },
      // 获取详情
      getFormDataList(id) {
        getAlarmPush(id).then(val => {
          if (val && val.data) {
            this.ruleForm = val.data;
            this.fnFormList();
          }
        }).catch(() => {
          this.$modal.msgError("操作失败")
        });
      },
      // 监听事件
      callback(result, dataVal, formVal) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'submit':
              let fnType = addAlarmPush;
              if (dataVal && dataVal.id) {
                fnType = updateAlarmPush;
              }
              if(this.loading) return;
              this.loading = true;
              fnType(dataVal).then(response => {
                this.$modal.msgSuccess(response.msg);
                this.$router.push("/resource/messagePush");
                this.loading = false;
              }).catch(() => {
                this.$modal.msgError("操作失败")
              });
              break;
            case 'pushMethod':
              this.ruleForm = Object.assign({}, this.ruleForm, this.$refs.formRef.$refs.ruleForm.model);
              if (dataVal === '1') {
                this.formList[0].controls.pushAddress['hidden'] = false;
                this.formList[0].controls.contactPhones['required'] = false;
              } else if (dataVal === '2') {
                this.formList[0].controls.pushAddress['hidden'] = true;
                this.formList[0].controls.contactPhones['required'] = true;
              }
              // 1. 获取表单引用
              const formRef = this.$refs.formRef;
              // 2. 先清除所有表单校验
              if (formRef && formRef.$refs.ruleForm) {
                formRef.$refs.ruleForm.clearValidate(); // 清除校验提示
              }
              // 强制更新表单
              // this.$set(this.ruleForm, 'updateFormVal', Date.now());
              break;
            case 'cancel':
              this.$router.push("/resource/messagePush");
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style>
</style>
