<template>
  <base-model :config="model_cfg">
    <template slot="dialog-body">
      <el-form
        ref="DictionaryTypeConfigForm"
        :model="form"
        :rules="rules"
        label-width="150px"
        size="small"
      >
        <el-form-item label="Dictionary Type: " prop="Code">
          <el-input v-model="form.Code" />
        </el-form-item>
        <el-form-item label="Description: " prop="Code">
          <el-input v-model="form.Description" />
        </el-form-item>
        <el-form-item label="Is Enable: ">
          <el-switch
            v-model="form.IsEnable"
            :active-value="true"
            :inactive-value="false"
          />
        </el-form-item>
      </el-form>
    </template>
    <template slot="dialog-footer">
      <el-button :disabled="btnLoading" size="small" type="primary" @click="handleCancle">Cancel</el-button>
      <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">Save</el-button>
    </template>
  </base-model>
</template>
<script>
import { addOrUpdateBasicDic } from '@/api/dictionary'
import BaseModel from '@/components/BaseModel'
const formDataDefault = () => {
  return {
    Id: '',
    Code: '',
    KeyName: '',
    Description: '',
    Value: '',
    ValueCN: '',
    DataTypeEnum: 0,
    IsEnable: true
  }
}
export default {
  name: 'AnonymizationFrom',
  components: { BaseModel },
  data() {
    return {
      btnLoading: false,
      form: formDataDefault(),
      rules: {
        Code: [{ required: true, message: 'Please specify', trigger: 'blur' }]
      },
      model_cfg: { visible: false, showClose: true, width: '600px', title: '' }
    }
  },
  mounted() {
    // if (Object.keys(this.data).length && this.data.Id) {
    //   this.form = { ...this.data }
    // }
  },
  methods: {
    openDialog(title, data) {
      this.model_cfg.visible = true
      this.model_cfg.title = title
      if (Object.keys(data).length) {
        this.form = { ...data }
      } else {
        this.form = formDataDefault()
      }
    },
    handleSave() {
      this.$refs.DictionaryTypeConfigForm.validate(valid => {
        if (valid) {
          this.btnLoading = true
          addOrUpdateBasicDic(this.form).then(res => {
            this.btnLoading = false
            this.$message.success('Saved successfully!')
            this.model_cfg.visible = false
            this.$emit('getList')
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    },
    handleCancle() {
      this.model_cfg.visible = false
    }
  }
}
</script>
