<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="serverName">
              <a-input v-model="model.serverName" placeholder="请输入上传服务器名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="传感器ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sensorId">
              <a-input v-model="model.sensorId" placeholder="请输入传感器ID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态 (0正常 1预警 2报警)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input-number v-model="model.status" placeholder="请输入状态 (0正常 1预警 2报警)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报警字段名 逗号分隔" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="statusname">
              <a-input v-model="model.statusname" placeholder="请输入报警字段名 逗号分隔"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="消息类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataType">
              <a-input-number v-model="model.dataType" placeholder="请输入消息类型" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="消息ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataId">
              <a-input-number v-model="model.dataId" placeholder="请输入消息ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="通信协议类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="protocolType">
              <a-input-number v-model="model.protocolType" placeholder="请输入通信协议类型" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="通信协议版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="protocolVersion">
              <a-input-number v-model="model.protocolVersion" placeholder="请输入通信协议版本号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="城市ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <a-input-number v-model="model.cityId" placeholder="请输入城市ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="线路ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineId">
              <a-input-number v-model="model.lineId" placeholder="请输入线路ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备内部ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inId">
              <a-input-number v-model="model.inId" placeholder="请输入设备内部ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="子设备ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subId">
              <a-input-number v-model="model.subId" placeholder="请输入子设备ID" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上传时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upTime">
              <j-date placeholder="请选择上传时间" v-model="model.upTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="一位端前牵" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data1">
              <a-switch v-model="model.data1" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="一位端后退" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data2">
              <a-switch v-model="model.data2" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="二位端前牵" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data3">
              <a-switch v-model="model.data3" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="二位端后退" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data4">
              <a-switch v-model="model.data4" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车空档位信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data5">
              <a-switch v-model="model.data5" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车自动一档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data6">
              <a-switch v-model="model.data6" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车自动二档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data7">
              <a-switch v-model="model.data7" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车自动三档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data8">
              <a-switch v-model="model.data8" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车手动一档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data9">
              <a-switch v-model="model.data9" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车手动二档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data10">
              <a-switch v-model="model.data10" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车手动三档信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data11">
              <a-switch v-model="model.data11" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车速度报警信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data12">
              <a-switch v-model="model.data12" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="紧急制动缓解开关信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data13">
              <a-switch v-model="model.data13" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="停车制动信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data14">
              <a-switch v-model="model.data14" checked-children="是" un-checked-children="否" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="蓄电池电压" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata1">
              <a-input-number v-model="model.rdata1" placeholder="请输入蓄电池电压" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="蓄电池电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata2">
              <a-input-number v-model="model.rdata2" placeholder="请输入蓄电池电流" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="机车速度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata3">
              <a-input-number v-model="model.rdata3" placeholder="请输入机车速度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发动机转速" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata4">
              <a-input-number v-model="model.rdata4" placeholder="请输入发动机转速" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发动机机油压力信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata5">
              <a-input-number v-model="model.rdata5" placeholder="请输入发动机机油压力信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发动机水温信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata6">
              <a-input-number v-model="model.rdata6" placeholder="请输入发动机水温信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="传动箱转速信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata7">
              <a-input-number v-model="model.rdata7" placeholder="请输入传动箱转速信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="变扭器传动油压力信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata8">
              <a-input-number v-model="model.rdata8" placeholder="请输入变扭器传动油压力信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="变扭器传动油温度信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata9">
              <a-input-number v-model="model.rdata9" placeholder="请输入变扭器传动油温度信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="柴油油位信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata10">
              <a-input-number v-model="model.rdata10" placeholder="请输入柴油油位信号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公里数信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rdata11">
              <a-input-number v-model="model.rdata11" placeholder="请输入公里数信息" style="width: 100%" />
            </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: 'BuZyJcwOnlineForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      // 查询类型: online/history
      dataType: {
        type: String,
        default: 'online'
      },
      // 历史数据查询月份
      queryMonth: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          data1: [{ type: 'boolean', message: '一位端前牵必须是布尔值', trigger: 'change' }],
          data2: [{ type: 'boolean', message: '一位端后退必须是布尔值', trigger: 'change' }],
          data3: [{ type: 'boolean', message: '二位端前牵必须是布尔值', trigger: 'change' }],
          data4: [{ type: 'boolean', message: '二位端后退必须是布尔值', trigger: 'change' }],
          data5: [{ type: 'boolean', message: '机车空档位信号必须是布尔值', trigger: 'change' }],
          data6: [{ type: 'boolean', message: '机车自动一档信号必须是布尔值', trigger: 'change' }],
          data7: [{ type: 'boolean', message: '机车自动二档信号必须是布尔值', trigger: 'change' }],
          data8: [{ type: 'boolean', message: '机车自动三档信号必须是布尔值', trigger: 'change' }],
          data9: [{ type: 'boolean', message: '机车手动一档信号必须是布尔值', trigger: 'change' }],
          data10: [{ type: 'boolean', message: '机车手动二档信号必须是布尔值', trigger: 'change' }],
          data11: [{ type: 'boolean', message: '机车手动三档信号必须是布尔值', trigger: 'change' }],
          data12: [{ type: 'boolean', message: '机车速度报警信号必须是布尔值', trigger: 'change' }],
          data13: [{ type: 'boolean', message: '紧急制动缓解开关信号必须是布尔值', trigger: 'change' }],
          data14: [{ type: 'boolean', message: '停车制动信号必须是布尔值', trigger: 'change' }],
          serverName: [{ max: 50, message: '上传服务器名长度不能超过50个字符', trigger: 'blur' }],
          sensorId: [{ max: 50, message: '传感器ID长度不能超过50个字符', trigger: 'blur' }],
          statusname: [{ max: 100, message: '报警字段名长度不能超过100个字符', trigger: 'blur' }]
        },
        url: {
          add: "/bu_zy_jcw/buZyJcwOnline/add",
          edit: "/bu_zy_jcw/buZyJcwOnline/edit",
          queryById: "/bu_zy_jcw/buZyJcwOnline/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';
            }
            
            // For historical data editing, we need to pass dataType and month as query parameters
            if (this.dataType === 'history' && this.queryMonth) {
              // Add query parameters to the URL
              const queryParams = `?dataType=${this.dataType}&month=${this.queryMonth}`;
              httpurl += queryParams;
            }
            
            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>