<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-07-21 13:59:43
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="130px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.storageBox.name')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.name')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.code')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.code')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.StorageQuantity')" prop="quantity">
        <el-input-number v-model="dataForm.quantity" :min="0" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.StorageQuantity')])" />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.alias')" prop="aliasName">
        <el-input v-model="dataForm.aliasName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.alias')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.status')" prop="status">
        <el-select v-model="dataForm.status" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.status')])" clearable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="remark">
        <el-input v-model="dataForm.remark" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Remarks')])" clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { storageBoxDetail, storageBoxUpdate, storageBoxAdd, storageBoxCode } from '@/api/basicData/Cache/storageBox'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        status: 0,
        aliasName: '',
        quantity: 0,
        remark: ''
      },
      options: [
        {
          value: 0,
          label: '正常'
        },
        {
          value: 1,
          label: '维修中'
        },
        {
          value: 2,
          label: '报废'
        }
      ],
      dataRule: {
        name: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.storageBox.name')]),
            trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.storageBox.code')]),
            trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          storageBoxDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        } else {
          storageBoxCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = this.dataForm
          data.id = this.dataForm.id
          if (this.dataForm.id) {
            storageBoxUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            storageBoxAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
