<!--
  @description: 数据字典类型编辑、录入
  @Modifier: haofengxian
  @routerPath:
  @date: 2020-7-31-11:47
-->
<template>
  <el-dialog
    :title="typeEntryMap[typeEntryDialogStatus]"
    :visible.sync="typeEntryDialogFormVisible"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      ref="dataForm"
      :rules="typeEntryRules"
      :model="typeEntryData"
      label-width="98px"
      style="width: 400px; margin-left:50px;"
    >
      <el-form-item label="字典分类" prop="typegroupname">
        <el-input v-model="typeEntryData.typegroupname" disabled />
      </el-form-item>
      <el-form-item label="字典名称" prop="typename">
        <el-input v-model="typeEntryData.typename" />
      </el-form-item>
      <el-form-item label="字典编码" prop="typecode">
        <el-input v-model="typeEntryData.typecode" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="typeEntryDialogStatus==='create'" type="primary" @click="typeCreateSure">
        确 定
      </el-button>
      <el-button v-else type="primary" @click="typeUpdateSure">
        确 定
      </el-button>
      <el-button @click="typeEntryDialogFormVisible = false">
        取 消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// 接口
import { saveType, updateType } from '@api/common/tools'
export default {
  name: 'DictionaryTypeEditDialog',
  data() {
    return {
      typeEntryDialogFormVisible: false, // 类型录入
      typeEntryDialogStatus: '',
      typeEntryMap: {
        update: '类型编辑',
        create: '类型录入'
      },
      // 类型录入数据
      typeEntryData: {
        typegroupname: '', // 分类名称
        id: '',
        typecode: '', // 类型编码
        typename: '', // 类型名称
        typegroupid: '', // 分类id
        typesort: 1
      },
      // 类型录入校验
      typeEntryRules: {
        typename: [{ required: true, message: '类型名称范围在1~50位字符', trigger: 'blur' }],
        typecode: [{ required: true, message: '类型编码在1~10位字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 录入/编辑字典类型 typeEntryDialogStatus 编辑/新增状态 typegroup字典分类信息 typeRow编辑时传回的类型信息
    openDictionaryTypeEdit(typeEntryDialogStatus, typegroup, typeRow) {
      this.typeEntryDialogStatus = typeEntryDialogStatus
      // dialogStatus为新增 清空初始化表单
      if (typeEntryDialogStatus === 'create') {
        // 清空
        Object.keys(this.typeEntryData).forEach((val, inx) => {
          this.typeEntryData[val] = ''
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        this.typeEntryData = {
          ...typegroup
        }
      } else {
        // 编辑
        this.typeEntryData = {
          ...typegroup,
          typecode: typeRow.typecode, // 类型编码
          typename: typeRow.typename, // 类型名称
          id: typeRow.id // 类型id
        }
      }
      this.typeEntryDialogFormVisible = true
    },
    // 点击类型录入新增确定
    typeCreateSure() {
      saveType(this.typeEntryData).then(res => {
        this.$message.success(res.header.msg)
        this.$emit('loadData')
        this.typeEntryDialogFormVisible = false
      }).catch(() => {
      })
    },
    // 点击类型录入编辑确定
    typeUpdateSure() {
      updateType(this.typeEntryData).then(res => {
        this.$message.success(res.header.msg)
        this.$emit('loadData')
        this.typeEntryDialogFormVisible = false
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
