<template>
  <base-model :config="model_cfg">
    <template slot="dialog-body">
      <el-form
        ref="anonymizationFrom"
        :model="form"
        :rules="rules"
        label-width="160px"
        size="small"
      >
        <el-form-item label="Group" prop="Group">
          <el-input :maxLength="4" v-model="form.Group" />
        </el-form-item>
        <el-form-item label="Element" prop="Element">
          <el-input :maxLength="4" v-model="form.Element" />
        </el-form-item>
        <el-form-item :label="$t('admin:TagDescription')" prop="TagDescription">
          <el-input :maxLength="$inputMax" v-model="form.TagDescription" />
        </el-form-item>
        <el-form-item :label="$t('admin:TagDescriptionCN')" prop="TagDescriptionCN">
          <el-input :maxLength="$inputMax" v-model="form.TagDescriptionCN" />
        </el-form-item>
        <el-form-item :label="$t('admin:ValueRepresentation')" prop="ValueRepresentation">
          <el-input :maxLength="$inputMax" v-model="form.ValueRepresentation" />
        </el-form-item>
        <el-form-item :label="$t('admin:IsFixed')">
          <el-switch
            v-model="form.IsFixed"
            :active-value="true"
            :inactive-value="false"
          />
        </el-form-item>
        <el-form-item :label="$t('admin:ReplaceValue')" prop="ReplaceValue">
          <el-input :maxLength="50" v-model="form.ReplaceValue" />
        </el-form-item>
        <el-form-item :label="$t('admin:IsEnable')">
          <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">{{$t('common:button:cancel')}}</el-button>
      <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">{{ $t('common:button:save') }}</el-button>
    </template>
  </base-model>
</template>
<script>
import { addOrUpdateSystemAnonymization } from '@/api/dictionary'
import BaseModel from '@/components/BaseModel'
const formDataDefault = () => {
  return {
    Group: '',
    Element: '',
    TagDescription: '',
    TagDescriptionCN: '',
    ValueRepresentation: '',
    ReplaceValue: '',
    IsEnable: false,
    IsAdd: false,
    IsFixed: false
  }
}
export default {
  name: 'AnonymizationFrom',
  components: { BaseModel },
  data() {
    return {
      isEnable: false,
      btnLoading: false,
      form: {
        Group: '',
        Element: '',
        TagDescription: '',
        TagDescriptionCN: '',
        ValueRepresentation: '',
        ReplaceValue: '',
        IsEnable: false,
        IsAdd: false
      },
      rules: {
        Group: [{ required: true, message: '请注明', trigger: 'blur' }],
        Element: [{ required: true, message: '请注明', trigger: 'blur' }],
        TagDescription: [{ required: true, message: '请注明', trigger: 'blur' }],
        TagDescriptionCN: [{ required: true, message: '请注明', trigger: 'blur' }],
        ValueRepresentation: [{ required: true, message: '请注明', trigger: 'blur' }],
        ReplaceValue: [{ required: true, message: '请注明', 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.anonymizationFrom.validate(valid => {
        if (valid) {
          this.btnLoading = true
          addOrUpdateSystemAnonymization(this.form).then(res => {
            this.btnLoading = false
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.model_cfg.visible = false
            this.$emit('getList')
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    },
    handleCancle() {
      this.model_cfg.visible = false
    }
  }
}
</script>
