<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="字典类型">
        <el-input v-model="form.dictType" :disabled="true" />
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
      </el-form-item>
      <el-form-item label="样式属性" prop="cssClass">
        <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number
          v-model="form.dictSort"
          controls-position="right"
          :min="0" />
      </el-form-item>
      <el-form-item label="回显样式" prop="listClass">
        <el-select v-model="form.listClass">
          <el-option
            v-for="item in listClassOptions"
            :key="item.value"
            :label="item.label + '(' + item.value + ')'"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio-group v-model="form.status">
            <el-radio label="10">正常</el-radio>
            <el-radio label="20">停用</el-radio>
          </el-radio-group>
          <!--          <el-radio-->
          <!--            v-for="dict in dict.type.sys_normal_disable"-->
          <!--            :key="dict.value"-->
          <!--            :label="dict.value"-->
          <!--          >{{dict.label}}</el-radio>-->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="请输入内容"
          type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

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

  export default {
    name: 'DataEdit',
    data() {
      return {
        form: {},
        rules: {
          dictLabel: [
            { required: true, message: '数据标签不能为空', trigger: 'blur' },
          ],
          dictValue: [
            { required: true, message: '数据键值不能为空', trigger: 'blur' },
          ],
          dictSort: [
            { required: true, message: '数据顺序不能为空', trigger: 'blur' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        // 数据标签回显样式
        listClassOptions: [
          {
            value: 'default',
            label: '默认',
          },
          {
            value: 'primary',
            label: '主要',
          },
          {
            value: 'success',
            label: '成功',
          },
          {
            value: 'info',
            label: '信息',
          },
          {
            value: 'warning',
            label: '警告',
          },
          {
            value: 'danger',
            label: '危险',
          },
        ],
      }
    },
    methods: {
      showEdit(row) {
        if (!row.dictCode) {
          this.title = '添加'
          this.form = Object.assign({}, row)
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.dictCode) {
              const { msg } = await dictApi.updateDictData(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              const { msg } = await dictApi.createDictData(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
