<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: lb
 * @LastEditTime: 2022-04-19 11:59:29
 * @Description:
-->
<template>
  <el-dialog :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter" width="40%" :visible.sync="visible" :close-on-click-modal="false">
    <el-form ref="dataForm" class="dataForm" :model="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmit()">
      <!-- label-width="80px" -->
      <el-form-item :label="$t('module.basicData.equipment.groupName')" prop="name">
        <el-input
          v-model="dataForm.name"
          :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.equipment.groupName')])"
          clearable
        />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.equipment.groupCode')" prop="code">
        <el-input
          v-model="dataForm.code"
          :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.equipment.groupCode')])"
          clearable
        />
      </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>
    <el-row style="text-align: right">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </el-row>
    <!-- <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 { update, add, detail, getCode } from '@/api/basicData/Equipment/equipmentGroup'
import { refineData } from '@/utils/helpers'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        remark: ''
      },
      dataRule: {
        name: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.equipment.groupName')]),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.fillCode()
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          detail(this.dataForm.id).then(res => {
            this.dataForm = refineData(res.data, ['id', 'name', 'code', 'remark'])
            // console.log('after refine: ', this.dataForm) // it works!
          })
        }
      })
    },
    fillCode() {
      getCode().then(res => {
        this.dataForm.code = res.data
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const ajaxAction = this.dataForm.id ? update : add
          ajaxAction(this.dataForm).then(res => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.dataForm >>> .el-form-item__label {
  word-break: break-word;
}
</style>
