<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="equipmentid">
              <j-search-select-tag v-model="model.equipmentid" placeholder="请选择设备" dict="tpm_equipment,equipmentname,id" @change="handleChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentcode">
              <a-input v-model="model.equipmentcode" placeholder="请输入设备编号" :disabled="true" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计划日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plandate">
              <!-- 编辑/单天新增 -->
              <j-date v-if="statusButton == 1" placeholder="请选择计划日期" v-model="model.plandate" style="width: 100%" />
              <!-- 时间段新增 -->
              <a-range-picker v-if="statusButton == 2" date-format="YYYY-MM-DD" v-model="model.plandate" @change="onChangeDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="能源分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="energytypeid">
              <j-search-select-tag v-model="model.energytypeid" placeholder="请选择能源分类" dict="base_energy_type,name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计划值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planvalue">
              <a-input-number v-model="model.planvalue" placeholder="请输入计划值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" rows="3" ></a-textarea>
            </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: 'EnergyPlanForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          equipmentcode: ''
        },
        statusButton: '',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           equipmentid: [
              { required: true, message: '请输入设备名称!'},
           ],
           plandate: [
              { required: true, message: '请输入计划日期!'},
           ],
           energytypeid: [
              { required: true, message: '请选择能源分类!'},
           ],
           planvalue: [
              { required: true, message: '请输入计划值!'},
           ],
        },
        url: {
          // add: "/energyPlan/energyPlan/add",
          add: "/energyPlan/energyPlan/rangeAdd",
          edit: "/energyPlan/energyPlan/edit",
          queryById: "/energyPlan/energyPlan/queryById",
          queryByDeviceId: "/tpmEquipment/tpmEquipment/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      // 时间处理
      onChangeDate(date, dateString){
        console.log(date, dateString);
        this.model.beginTime= dateString[0]
        this.model.endTime= dateString[1]
      },
      handleChange(value){
        console.log(111,value)
        getAction(this.url.queryByDeviceId, {id:value}).then((res) => {
          console.log(88,res.result.equipmentcode)
          this.model.equipmentcode = res.result.equipmentcode
        })
      },
      add () {
        // this.edit(this.modelDefault);
        this.statusButton =2
        this.visible = true;
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.statusButton =1
        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';
              this.model.plandate = ''
            }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>