<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-row>
      <el-col :span="12">
        <el-form-item label="方案名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="方案编码" prop="code">
          <el-input v-model="form.code" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="统计类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in getDictDatas(DICT_TYPE.STATISTIC_TYPE)"
              :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="energyTypeId">
          <el-select v-model="form.energyTypeId" placeholder="请选择" style="width: 100%;" filterable>
            <el-option
              v-for="item in energyListType"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getCode, createEnergyStatistics, updateEnergyStatistics, getEnergyStatistics } from "@/api/monitoring/energyStatistics";
import { getEnergyTypeListAll } from '@/api/base/energyType'
import { number } from 'echarts';
export default {
  name: 'EnergyStatisticsAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        type: '',
        energyTypeId: ''
      },
      plcList: [],
      objList: [],
      isEdit: false, //是否是编辑
      rules: {
        name: [{ required: true, message: '方案名称不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '统计类型不能为空', trigger: 'change' }],
        energyTypeId: [{ required: true, message: '能源类型不能为空', trigger: 'change' }]
      },
      energyListType: [],
    }
  },
  methods: {
    init(id) {
      getEnergyTypeListAll().then((res) => {
        this.energyListType = res.data || []
      })
      if (id) {
        this.isEdit = true
        this.form.id = id
        getEnergyStatistics( id ).then((res) => {
          if (res.code === 0) {
            this.form = res.data
            this.form.type = this.form.type + ''
          }
        })
      } else {
        getCode().then((res) => {
          this.form.code = res.data
        })
        this.isEdit = false
        this.form.id = ''
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            updateEnergyStatistics({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            createEnergyStatistics({...this.form}).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
