<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-04-12 16:24:32
 * @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="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.factory.FactoryName')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.factory.FactoryName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.factory.FactoryCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.factory.FactoryCode')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.factory.Address')" prop="address">
        <el-input v-model="dataForm.address" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.factory.Address')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="description">
        <el-input v-model="dataForm.description" :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 { factoryDetail, factoryUpdate, factoryAdd, factoryCode } from '@/api/basicData/FactoryManagement/factory'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        address: '',
        description: ''
      },
      dataRule: {
        name: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.factory.FactoryName')]),
            trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.factory.FactoryCode')]),
            trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          factoryDetail(this.dataForm.id).then(res => {
            this.dataForm.name = res.data.name
            this.dataForm.code = res.data.code
            this.dataForm.address = res.data.address
            this.dataForm.description = res.data.description
          })
        } else {
          factoryCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.dataForm.name,
            'code': this.dataForm.code,
            'address': this.dataForm.address,
            'description': this.dataForm.description,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            factoryUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            factoryAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
