<template>
  <el-form ref="wasteWaterAddForm" :rules="rules" label-width="80px" :model="form">
    <el-row :gutter="20">
      <el-col :span='12'>
        <el-form-item label="指标名称" prop="name">
          <el-input v-model="form.name" :disabled='isEdit'></el-input>
        </el-form-item>
      </el-col>
      <el-col :span='12'>
        <el-form-item label="指标编码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span='12'>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择" style="width: 100%;" :disabled='form.method == 2'>
            <el-option
              v-for="item in getDictDatas(DICT_TYPE.ENVIRONMENT_CHECK_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="minValue">
          <el-input-number v-model="form.minValue" placeholder="最小值" :max="9999999" style="width: 100%;"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span='12'>
        <el-form-item label="最大值" prop="maxValue">
          <el-input-number v-model="form.maxValue" placeholder="最大值" :max="9999999" style="width: 100%;"></el-input-number>
        </el-form-item>
      </el-col>
      <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 { environmentalCheckGet, environmentalCheckUpdate, environmentalCheckCreate, getEnvironmentalCheckCode } from '@/api/safetyEnvironmental/environmental'
export default {
  name: 'WasteWaterAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        unit: '',
        minValue: null,
        maxValue: null,
        remark: '',
        checkType: 1
      },
      isEdit: false,
      rules: {
        name: [{ required: true, message: "指标名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "指标编码不能为空", trigger: "blur" }],
        // unit: [{ required: true, message: "unit不能为空", trigger: "change" }],
        minValue: [{ required: true, message: "最小值不能为空", trigger: "blur" }],
        maxValue: [{ required: true, message: "最大值不能为空", trigger: "blur" }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        environmentalCheckGet({id: this.form.id}).then(res => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
        // 编码
        getEnvironmentalCheckCode().then(res => {
          this.form.code = res.data || ''
        })
      }
    },
    submitForm() {
      this.$refs['wasteWaterAddForm'].validate((valid) => {
        if (valid) {
          if (this.form.minValue > this.form.maxValue) {
            this.$modal.msgError('最小值不能大于最大值')
            return false
          }
          console.log(this.form)
          if (this.isEdit) {
            //编辑
            environmentalCheckUpdate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            environmentalCheckCreate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.wasteWaterAddForm.resetFields()
      this.isEdit = false
    }
  }
}
</script>