<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建时间"  v-model="model.createTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
              <a-input v-model="model.createBy" placeholder="请输入创建人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="告警检测项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alarmProject">
              <a-input v-model="model.alarmProject" placeholder="请输入告警检测项"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="告警检测项阈值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alarmProjectThreshold">
              <a-input v-model="model.alarmProjectThreshold" placeholder="请输入告警检测项阈值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="告警频次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alarmFrequency">
              <a-input v-model="model.alarmFrequency" placeholder="请输入告警频次"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="当前告警标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alarmNumberTag">
              <a-input v-model="model.alarmNumberTag" placeholder="请输入当前告警标签"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="告警开关" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOppen">
              <j-dict-select-tag type="list" v-model="model.isOppen" dictCode="alarm_is_open" placeholder="请选择告警开关,0:关，1：开" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="适用范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scope">
              <j-dict-select-tag type="list" v-model="model.scope" dictCode="station_code" placeholder="请选择适用范围" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开始时间"  v-model="model.startTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间"  v-model="model.endTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentModel">
              <j-dict-select-tag type="list" v-model="model.equipmentModel" dictCode="equipment_model_code" placeholder="请选择设备型号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否语音播报" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isVoiceAnnouncements">
              <j-dict-select-tag type="list" v-model="model.isVoiceAnnouncements" dictCode="alarm_is_open" placeholder="请选择是否语音播报" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AlarmTestProjectForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           createTime: [
              { required: true, message: '请输入创建时间!'},
           ],
           createBy: [
              { required: true, message: '请输入创建人!'},
           ],
           alarmProject: [
              { required: true, message: '请输入告警检测项!'},
           ],
           alarmProjectThreshold: [
              { required: true, message: '请输入告警检测项阈值!'},
           ],
           alarmFrequency: [
              { required: true, message: '请输入告警频次!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
           ],
           isOppen: [
              { required: true, message: '请输入告警开关,0:关，1：开!'},
           ],
           scope: [
              { required: true, message: '请输入适用范围!'},
           ],
           isVoiceAnnouncements: [
              { required: true, message: '请输入是否语音播报!'},
           ],
        },
        url: {
          add: "/station/alarmTestProject/add",
          edit: "/station/alarmTestProject/edit",
          queryById: "/station/alarmTestProject/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>