<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-form-item label="耗材名称" prop="name">
      <el-input v-model="form.name" placeholder="耗材名称"></el-input>
    </el-form-item>
    <el-form-item label="耗材规格" prop="spec">
      <el-input v-model="form.spec" placeholder="耗材规格"></el-input>
    </el-form-item>
    <el-form-item label="耗材单位" prop="unit">
      <el-input v-model="form.unit" placeholder="耗材单位"></el-input>
    </el-form-item>
    <el-form-item label="基础消耗量" prop="basicConsume">
      <el-input-number
        v-model="form.basicConsume"
        :min="0"
        :max="999999999"
        :precision="2"
        :step="1"
        style="width: 100%"
      ></el-input-number>
    </el-form-item>
    <el-form-item label="状态" prop="enabled" v-show="isEdit">
      <el-select v-model="form.enabled" placeholder="状态" style="width: 100%">
        <el-option
          v-for="(item, i) in stateList"
          :key="i"
          :label="item.dataName"
          :value="item.dataCode"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script>
import {
  addConsumable,
  getConsumable,
  consumableUpdate
} from '@/api/basicConfig'
export default {
  name: 'ConsumablesAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        spec: '',
        unit: '',
        basicConsume: '',
        enabled: 1
      },
      rules: {
        name: [{ required: true, message: '请输入耗材名称', trigger: 'blur' }],
        spec: [{ required: true, message: '请输入耗材规格', trigger: 'blur' }]
        // unit: [{ required: true, message: '请输入耗材单位', trigger: 'blur' }],
        // basicConsume: [
        //   { required: true, message: '请输入基础消耗量', trigger: 'blur' }
        // ]
      },
      stateList: JSON.parse(localStorage.getItem('publicList')).enabledVoList,
      isEdit: false
    }
  },
  methods: {
    init(val) {
      if (val) {
        this.form.id = val.id
        getConsumable({ id: val.id }).then((res) => {
          if (res.code === 0) {
            console.log(res)
            this.form.name = res.data.name
            this.form.spec = val.type === 'copy' ? '' : res.data.spec
            this.form.unit = res.data.unit
            this.form.basicConsume = res.data.basicConsume
            this.form.enabled = res.data.enabled
          }
        })
        if (val.type === 'edit') {
          this.isEdit = true
        } else {
          this.isEdit = false
          this.form.id = ''
        }
      } else {
        this.isEdit = false
        this.form.id = ''
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            consumableUpdate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('successSubmit')
              }
            })
          } else {
            addConsumable({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
