<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="电站id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationId">
              <j-dict-select-tag type="list" v-model="model.stationId" dictCode="station_code" placeholder="请选择电站id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="责任人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safeguardName">
              <a-input v-model="model.safeguardName" placeholder="请输入责任人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="责任人手机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safeguardPhone">
              <a-input v-model="model.safeguardPhone" placeholder="请输入责任人手机"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="责任人邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safeguardEmail">
              <a-input v-model="model.safeguardEmail" placeholder="请输入责任人邮箱"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="电站发送钉钉群密钥" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationSecretKey">
              <a-input v-model="model.stationSecretKey" placeholder="请输入电站发送钉钉群密钥"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="钉钉机器人地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationWebHook">
              <a-input v-model="model.stationWebHook" placeholder="请输入钉钉机器人地址"  ></a-input>
            </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-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: 'StationSafeguardForm',
    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: {
           stationId: [
              { required: true, message: '请输入电站id!'},
           ],
           safeguardName: [
              { required: true, message: '请输入责任人!'},
           ],
           safeguardPhone: [
              { required: true, message: '请输入责任人手机!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
           ],
           safeguardEmail: [
              { required: true, message: '请输入责任人邮箱!'},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
           ],
           stationWebHook: [
              { required: false},
              { pattern: /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/, message: '请输入正确的网址!'},
           ],
        },
        url: {
          add: "/station/stationSafeguard/add",
          edit: "/station/stationSafeguard/edit",
          queryById: "/station/stationSafeguard/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>