<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="deviceId">
              <j-select-device v-model="model.deviceId" placeholder="请输入设备名称" :multiple="false" dicCode="bu_device,device_name,id"></j-select-device>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="deviceName" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceName">-->
<!--              <a-input v-model="model.deviceName" placeholder="请输入deviceName"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="设备编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceCode">
              <a-input v-model="model.deviceCode" placeholder="请输入设备编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有效工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="usefulTime">
              <a-input v-model="model.usefulTime" placeholder="请输入有效工时"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="无效工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invalidTime">
              <a-input v-model="model.invalidTime" placeholder="请输入无效工时"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有效电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="usefulElectric">
              <a-input v-model="model.usefulElectric" placeholder="请输入有效电流"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="无效电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invalidElectric">
              <a-input v-model="model.invalidElectric" placeholder="请输入无效电流"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有效功耗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalUsefulPower">
              <a-input v-model="model.totalUsefulPower" placeholder="请输入有效功耗"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="无效功耗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalInvalidPower">
              <a-input v-model="model.totalInvalidPower" placeholder="请输入无效功耗"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总功耗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalPower">
              <a-input v-model="model.totalPower" placeholder="请输入总功耗"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="开机时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开机时间" v-decorator="['openTime']" :trigger-change="true" style="width: 100%"
                      :showtime=true dateFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-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'
import JFormContainer from "@comp/jeecg/JFormContainer";
import JSelectDevice from "@comp/jeecgbiz/JSelectDevice";

export default {
  name: 'BuDeviceEnergyStatisticsForm',
  components: {
    JFormContainer,
    JSelectDevice,
  },
  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: {
      },
      url: {
        add: "/statistics/buDeviceEnergyStatistics/add",
        edit: "/statistics/buDeviceEnergyStatistics/edit",
        queryById: "/statistics/buDeviceEnergyStatistics/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>