<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="采集时刻" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择采集时刻" v-decorator="['time']" :trigger-change="true" style="width: 100%"
                      :showtime=true dateFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['deviceId']" placeholder="请输入设备名称"></a-input>-->
              <j-select-device placeholder="请选择设备名称" :multiple="false" v-decorator="['deviceId']"
                               dicCode="bu_device,device_name,id"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测电流data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataaa1']" placeholder="请输入A测电流data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测电流data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataaa2']" placeholder="请输入A测电流data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测电流data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databa1']" placeholder="请输入B测电流data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测电流data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databa2']" placeholder="请输入B测电流data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测震动data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataam1']" placeholder="请输入A测震动data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测震动data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataam2']" placeholder="请输入A测震动data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测震动data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databm1']" placeholder="请输入B测震动data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测震动data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databm2']" placeholder="请输入B测震动data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测震动data3" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databm3']" placeholder="请输入B测震动data3"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测震动data4" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databm4']" placeholder="请输入B测震动data4"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测温度data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataac1']" placeholder="请输入A测温度data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测温度data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataac2']" placeholder="请输入A测温度data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测湿度data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataar1']" placeholder="请输入A测湿度data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="A测湿度data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dataar2']" placeholder="请输入A测湿度data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测温度data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databc1']" placeholder="请输入B测温度data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测温度data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databc2']" placeholder="请输入B测温度data2"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测湿度data1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databr1']" placeholder="请输入B测湿度data1"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="B测湿度data2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['databr2']" placeholder="请输入B测湿度data1"></a-input>
            </a-form-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-item label="备用" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input v-decorator="['datah1']" placeholder="请输入采集数据h1"></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="24">-->
          <!--            <a-form-item label="备用" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input v-decorator="['datah2']" placeholder="请输入采集数据h2"></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="24">-->
          <!--            <a-form-item label="备用" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input v-decorator="['datai1']" placeholder="请输入采集数据i1"></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="24">-->
          <!--            <a-form-item label="备用" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input v-decorator="['datai2']" placeholder="请输入采集数据i2"></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JSelectDevice from '@comp/jeecgbiz/JSelectDevice'

  export default {
    name: 'BuSensorDeviceDataForm',
    components: {
      JFormContainer,
      JDate,
      JSelectDevice,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/sensor/buSensorDeviceData/add",
          edit: "/sensor/buSensorDeviceData/edit",
          queryById: "/sensor/buSensorDeviceData/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'time','deviceId','dataaa1','dataaa2','databa1','databa2','dataam1','dataam2','databm1','databm2','databm3','databm4','dataac1','dataac2','dataar1','dataar2','databc1','databc2','databr1','databr2','datah1','datah2','datai1','datai2'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'time','deviceId','dataaa1','dataaa2','databa1','databa2','dataam1','dataam2','databm1','databm2','databm3','databm4','dataac1','dataac2','dataar1','dataar2','databc1','databc2','databr1','databr2','datah1','datah2','datai1','datai2'))
      },
    }
  }
</script>