<template>
  <base-model :config="model_cfg">
    <template slot="dialog-body">
      <el-form
        ref="DictionaryTypeConfigForm"
        :model="form"
        :rules="rules"
        label-width="120px"
        size="small"
      >
        <el-form-item label="ConfigTypeId: " prop="Code">
          <el-select v-model="form.ConfigTypeId" filterable style="width:100%;">
            <el-option
              v-for="(item,index) of basicDicList"
              :key="index"
              :label="`${item.Code} (${item.Description})`"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="TypeEnum: " prop="DataTypeEnum">
          <el-select v-model="form.DataTypeEnum" filterable style="width:100%;">
            <el-option
              :label="`config`"
              :value="0"
            />
            <el-option
              :label="`select`"
              :value="1"
            />
            <el-option
              :label="`enum`"
              :value="2"
            />
            <el-option
              :label="`bool`"
              :value="3"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Code: " prop="Code">
          <el-input v-model="form.Code" />
        </el-form-item>
        <!--<el-form-item label="KeyName: " prop="Code">-->
        <!--<el-input v-model="form.KeyName" />-->
        <!--</el-form-item>-->
        <el-form-item label="ShowOrder: " prop="Code">
          <el-input-number v-model="form.ShowOrder" :min="0"  />
        </el-form-item>
        <el-form-item label="Description: " prop="Code">
          <el-input v-model="form.Description" />
        </el-form-item>
        <!-- <el-form-item label="Value: " prop="Code">
          <el-input v-model="form.Value" />
        </el-form-item>
        <el-form-item label="ValueCN: " prop="Code">
          <el-input v-model="form.ValueCN" />
        </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: '',
    ConfigTypeId: null,
    Code: '',
    KeyName: '',
    Description: '',
    Value: '',
    ValueCN: '',
    IsEnable: true,
    DataTypeEnum: null
  }
}
export default {
  name: 'AnonymizationFrom',
  components: { BaseModel },
  props: {
    basicDicList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      btnLoading: false,
      form: formDataDefault(),
      rules: {
        Code: [{ required: true, message: 'Please specify', trigger: 'blur' }],
        DataTypeEnum: [{ required: true, message: 'Please specify', trigger: 'blur' }]
      },
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true }
    }
  },
  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
      console.log(data)
      if (Object.keys(data).length) {
        this.form = { ...data }
      } else {
        this.form = formDataDefault()
      }
      console.log(this.form)
    },
    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>
