DictModal2.vue 4.39 KB
<template>
  <a-modal :title="title" :width="600" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk"
    @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字典名称">
          <a-input placeholder="请输入字典名称" v-decorator="['parentCodeName', validatorRules.parentCodeName]" />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字典编码">
          <a-input placeholder="请输入字典编码" v-decorator="['parentCode', validatorRules.parentCode]" />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="描述">
          <a-input v-decorator="['parentCodeRemark']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { saveSysDictionaryParentCode, modifySysDictionaryParentCode } from '@/api/configApi'

export default {
  name: 'DictModal2',
  data() {
    return {
      value: 1,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        parentCodeName: { rules: [{ required: true, message: '请输入字典名称!' }] },
        parentCode: {
          rules: [{ required: true, message: '请输入字典编码!' }]
        }
      },
      parentCodeId:'',
      oldParentCode:'',
    }
  },
  created() {
  },
  methods: {
    handleChange(value) {
      this.model.status = value
    },
    add() {
      // this.parentCodeId = "add"
      this.edit({})
    },
    edit(record) {
      if (record.id) {
        this.visiblekey = true
      } else {
        this.visiblekey = false
      }
      this.parentCodeId = record.parentCode
      this.oldParentCode = record.parentCode
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'parentCodeName', 'parentCode', 'parentCodeRemark'))
      })
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          values.parentCodeName = (values.parentCodeName || '').trim()
          values.parentCode = (values.parentCode || '').trim()
          values.parentCodeRemark = (values.parentCodeRemark || '').trim()
          let old = {oldParentCode:this.oldParentCode}
          var formData = Object.assign(this.model, values,old)
          let obj
          console.log(formData)
          if (!this.parentCodeId) {
            // alert(this.parentCodeId)
            saveSysDictionaryParentCode(formData).then((res) => {
              if (res.status.statusCode == 0) {
                that.$message.success(res.status.statusReason)
                that.$emit('ok')
              } else {
                that.$message.warning(res.status.statusReason)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          } else {
            // alert(this.parentCodeId)
            modifySysDictionaryParentCode(formData).then((res) => {
              if (res.status.statusCode == 0) {
                that.$message.success(res.status.statusReason)
                that.$emit('ok')
              } else {
                that.$message.warning(res.status.statusReason)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
          // obj.then((res) => {
          //   if (res.status.statusCode == 0) {
          //     that.$message.success(res.status.statusReason)
          //     that.$emit('ok')
          //   } else {
          //     that.$message.warning(res.status.statusReason)
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false
          //   that.close()
          // })
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>