<template>
  <el-form ref="form" :rules="rules" label-width="80px" :model="form">
    <el-form-item label="备件类别" prop="type">
      <el-select v-model="form.type" placeholder="备件类别" style="width: 100%">
        <el-option
          v-for="(item, i) in sparePartsTypeList"
          :key="i"
          :label="item.dataName"
          :value="item.dataName"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备件名称" prop="name">
      <el-input v-model="form.name" placeholder="备件名称"></el-input>
    </el-form-item>
    <el-form-item label="备件型号" prop="model">
      <el-input v-model="form.model" placeholder="无型号请填写为 /"></el-input>
    </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 {
  addSpareParts,
  getSpareParts,
  sparePartsUpdate
} from '@/api/basicConfig'
export default {
  name: 'SparePartsAdd',
  data() {
    return {
      form: {
        id: '',
        type: '',
        name: '',
        model: '',
        enabled: 1
      },
      sparePartsTypeList: JSON.parse(localStorage.getItem('publicList'))
        .sparePartsTypeList,
      stateList: JSON.parse(localStorage.getItem('publicList')).enabledVoList,
      rules: {
        type: [
          { required: true, message: '请选择备件类别', trigger: 'change' }
        ],
        name: [{ required: true, message: '请输入备件名称', trigger: 'blur' }],
        model: [
          {
            required: true,
            message: '请输入备件型号',
            trigger: 'blur'
          }
        ]
      },
      isEdit: false
    }
  },
  methods: {
    init(val) {
      if (val) {
        this.form.id = val.id
        getSpareParts({ id: val.id }).then((res) => {
          if (res.code === 0) {
            this.form.type = res.data.type
            this.form.name = res.data.name
            this.form.model = val.type === 'copy' ? '' : res.data.model
            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) {
            sparePartsUpdate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('successSubmit')
              }
            })
          } else {
            addSpareParts({ ...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>
