<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-07-21 14:39:23
 * @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="140px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.storageBox.PositionCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.PositionCode')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.PositionCodeAlias')" prop="aliasName">
        <el-input v-model="dataForm.aliasName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.PositionCodeAlias')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.storageBox.PositionNo')" prop="serial">
        <el-input v-model="dataForm.serial" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.storageBox.PositionNo')])" 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 { PositionDetailInfoAdd } from '@/api/basicData/Cache/storageBox'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        storageTankId: 0,
        code: '',
        aliasName: '',
        serial: ''
      },
      dataRule: {
        code: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.storageBox.PositionCode')]),
            trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id, code) {
      this.dataForm.storageTankId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.code = code
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = this.dataForm
          PositionDetailInfoAdd(data).then(res => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
