<template>
<el-dialog
  v-loading="loading"
  :show-close="false"
  :close-on-click-modal="false"
  :visible.sync="isShow"
  :title="this.curObj && this.curObj.typeName ? '编辑字典' : '添加字典'"
  :fullscreen="false"
  width="470px"
  custom-class="xl-dialog"
  center>
  <base-form style="width:400px" :data="formData" ref="ruleForm" :isInline="false" labelWidth="100px">
    <div slot="footer">
      <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
      <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
      <el-button v-if="!this.curObj.id" class="xl-form-btn t1" @click="close('next')">继续添加</el-button>
    </div>
  </base-form>
</el-dialog>
</template>
<script>
export default {
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  mixins,
  data() {
    return {
      formData: [],
      loading: true
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        let params = {...this.curObj}
        let disabled = false
        if (params.id) disabled = true
        this.formData = [
          { label: '值', key: 'dict_value', disabled, rules: 1 },
          { label: '名称', key: 'dict_label', rules: 1 },
          { label: '自定义值1', key: 'option1' },
          { label: '自定义值2', key: 'option2' },
          { label: '说明', key: 'remark' },
        ]
        this.setDefaultValue(params)
      }
    }
  },
  methods: {
    close (str) {
      if (str === 'confirm' || str === 'next') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            let params = {...oldform}
            params.dict_type = this.parentData.curType
            let apiStr = 'dictdataadd'
            if(this.curObj && this.curObj.id) {
              params.data_id = this.curObj.id
              apiStr = 'dictdataedit'
            }
            this.$api.base[apiStr](params).then(data => {
              this.$msgs('保存成功！')
              if (str === 'next') {
                this.setDefaultValue()
              } else {
                this.$emit('close', params)
              }
              // this.setDefaultValue()
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    }
  }
}
</script>
