<template>
  <el-dialog :visible.sync="visible" :before-close="cancelDialog">
    <el-divider />
    <label slot="title" style="float: left;">{{ dialogTitle }}</label>

    <el-row>
      <el-form ref="form" :model="form" :rules="formRules" label-suffix="：" label-width="100px">
        <el-form-item label="字典标识" prop="dictId">
          <el-input v-model.trim="form.dictId" disabled />
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入字典名称" clearable />
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model.trim="form.value" placeholder="请输入字典值" clearable />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model.trim="form.sort" controls-position="right" clearable />
        </el-form-item>
      </el-form>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <el-divider />
      <el-button @click="cancelDialog">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import dictApi from '@/api/platform/dictApi'

export default {
  name: 'EditDictEntryDialog',
  props: {
    dialogTitle: {
      type: String,
      default: () => {
        return ''
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    form: {
      type: Object,
      default: () => {
        return {
          id: '',
          dictId: '',
          name: '',
          value: '',
          sort: 0
        }
      }
    }
  },
  data() {
    return {
      formRules: {
        name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入字典名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    cancelDialog() {
      this.form = {
        id: '',
        dictId: '',
        name: '',
        value: '',
        sort: 0
      }
      this.$refs['form'].resetFields()
      this.$emit('cancelDialog', false)
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        dictApi.entrySaveOrUpdate(this.form).then(data => {
          this.$refs['form'].resetFields()
          this.$emit('cancelDialog', false)
          this.$emit('refresh')
        })
      })
    }
  }
}

</script>
<style>
</style>
