<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-25 16:26:22
 * @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="160px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.major.ProfessionalName')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.major.ProfessionalName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.major.ProfessionalCoding')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.major.ProfessionalCoding')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.Abbreviation')" prop="abbreviation">
        <el-input v-model="dataForm.abbreviation" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Abbreviation')])" 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 { majorDetail, majorUpdate, majorAdd, majorCode } from '@/api/basicData/GroupModule/major'

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