<template>
  <el-form ref="energyQuantityManualForm" :rules="rules" label-width="90px" :model="form">
    <el-row :gutter="20">
      <el-col :span='12'>
        <el-form-item label="能源类型" prop="energyTypeId">
          <el-select v-model="form.energyTypeId" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              v-for="item in this.energyTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span='12'>
        <el-form-item label="水/气表名" prop="tableName">
          <el-select v-model="form.tableName" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              v-for="item in getDictDatas(DICT_TYPE.TABLE_NAME)"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span='12'>
        <el-form-item label="抄表数" prop="readingQuantity">
          <el-input-number v-model="form.readingQuantity" :min="0" :max="999999999999" :controls='false' style="width: 50%;"></el-input-number>
          <el-select v-model="form.unit" placeholder="单位" style="width: 50%;">
            <el-option
              v-for="item in getDictDatas(DICT_TYPE.ENERGY_UNIT)"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span='12'>
        <el-form-item label="抄表日期" prop="recordTime">
          <el-date-picker
            v-model="form.recordTime"
            type="date"
            format="yyyy-MM-dd"
            value-format="timestamp"
            placeholder="选择日期"
            style="width: 100%;">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { energyQuantityManualCreate, energyQuantityManualUpdate, energyQuantityManualGet } from '@/api/base/energyQuantityManual'
import moment from 'moment'
export default {
  name: 'EnergyQuantityManualAdd',
  props: {
    energyTypeList: {
      type: Array,
      required: true,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      form: {
        id: '',
        energyTypeId: '',
        tableName: '',
        readingQuantity: null,
        unit: '',
        recordTime: ''
      },
      rules: {
        energyTypeId: [{ required: true, message: '能源类型不能为空', trigger: 'change' }],
        tableName: [{ required: true, message: '水/气表名不能为空', trigger: 'change' }],
        readingQuantity: [{ required: true, message: '抄表数不能为空', trigger: 'blur' }],
        recordTime: [{ required: true, message: '抄表日期不能为空', trigger: 'change' }]
      },
      isEdit: false
    }
  },
  methods: {
    init(params) {
      this.form.recordTime = moment().valueOf()
      if (params.type === 'add') {
        this.isEdit = false
      } else if (params.type === 'meterReading') {
        this.isEdit = false
        this.form.energyTypeId = params.energyTypeId
        this.form.tableName = params.tableName + ''
      }else {
        this.isEdit = true
        this.form.id = params.id
        energyQuantityManualGet({id: this.form.id}).then(res => {
          if (res.code === 0) {
            this.form.energyTypeId = res.data.energyTypeId
            this.form.tableName = res.data.tableName ? res.data.tableName+'' : ''
            this.form.readingQuantity = res.data.readingQuantity
            this.form.unit = res.data.unit ? res.data.unit+'' : ''
            this.form.recordTime = res.data.recordTime ? res.data.recordTime : null
          }
        })
      }
    },
    submitForm() {
      this.$refs['energyQuantityManualForm'].validate((valid) => {
        if (valid) {
          if (!this.form.unit) {
            this.$modal.msgError("抄表数单位不能为空");
            return false
          }
          if (this.isEdit) {
            // 编辑
            energyQuantityManualUpdate({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            energyQuantityManualCreate({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.energyQuantityManualForm.resetFields()
      this.form.unit = ''
      this.isEdit = false
    }
  }
}
</script>