addDialog.vue 6.31 KB
<!-- 新增/编辑类目 -->
<template>
  <baseDialog :dialog-config="dialogConfig"
              :is-show.sync="visible"
              class="accountDailog"
              @handleClick="handleAddConfirm">
    <div class="add-dialog">
      <glsx-combi-form class="edit-form-container"
                       ref="addFormRef"
                       label-width="150px"
                       default-width="400px"
                       :form-ref="editChildRef"
                       :form-obj="infoForm"
                       :form-data="editFormData"
                       :form-rules="formRules">
        <div slot="parentCategoryHtml"
             slot-scope="scope">
          <el-cascader v-model="infoForm.parentId"
                       :options="baseData.parentCategory"
                       :props="cascader_props"
                       style="width: 400px"></el-cascader>
        </div>
      </glsx-combi-form>
    </div>
  </baseDialog>
</template>

<script>
import { addGoodsCategory_new, updateGoodsCategory, queryParent } from '@api/sceneCategory'
import { Util, deepClone } from '@/common/util'
export default {
  name: 'AddDialog',
  data() {
    return {
      infoForm: {
        categoryName: '',
        parentId: ''
      },
      dialogConfig: {
        title: '新增类目',
        width: '720px'
      },
      editChildRef: 'editChildRef',
      formRules: {
        categoryName: [{ required: true, message: '请填写类目名', trigger: 'blur' }]
      },
      editFormData: [{
        label: '所属类目',
        name: 'parentId',
        isHide: true,
        addHtml: 'parentCategoryHtml',
        rules: [{ required: true, message: '请选择所属类目', trigger: 'change' }]
      }, {
        label: '类目名',
        name: 'categoryName',
        type: 'input',
        maxlength: 20,
        showLimit: true,
        isClear: true
      }],
      baseData: {
        // pointClassificationOpt: [],
        parentCategory: []
      },
      cascader_props: {
        checkStrictly: true,
        children: 'children',
        value: 'id',
        label: 'categoryName'
      }
    }
  },

  props: {
    addVisible: {
      type: Boolean,
      default: false
    },
    categoryInfo: {
      type: Object,
      default: () => { }
    }
  },
  watch: {

  },
  computed: {
    visible: {
      get() {
        return this.addVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'addVisible' })
      }
    }
  },

  mounted() {
    this.showTitle()
  },

  methods: {
    getOptionsList(formName, hasAll = true, refreshSigle) {
      if (refreshSigle) {
        var item = this[formName].find(k => k.name == refreshSigle)
        var index = this[formName].findIndex(k => k.name == refreshSigle)
        this.$set(this[formName][index], 'optionList', deepClone(this.baseData[item.optionsName]))
        if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
          const temp = {}
          temp[this[formName][index].optionId] = ''
          temp[this[formName][index].optionLabel] = '全部'
          this[formName][index].optionList.unshift(temp)
        }
      } else {
        this[formName].forEach((item, index) => {
          if (item.type === 'select' && this.baseData[item.optionsName].length) {
            this.$set(this[formName][index], 'optionList', deepClone(this.baseData[item.optionsName]))
            if (hasAll && this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this[formName][index].optionLabel] !== '全部') {
              const temp = {}
              temp[this[formName][index].optionId] = ''
              temp[this[formName][index].optionLabel] = '全部'
              this[formName][index].optionList.unshift(temp)
            }
          }
        })
      }
    },

    showTitle() {
      console.log('this.categoryInfo', this.categoryInfo)
      if (this.categoryInfo) {
        if (this.categoryInfo.type == 'edit') {
          this.dialogConfig.title = '编辑类目'
          this.infoForm.categoryName = this.categoryInfo.categoryName
          if (this.categoryInfo.level == 3) {
            this.showOrHideAttr('editFormData', '所属类目', false)
            this.queryParent()
          }
        } else if (this.categoryInfo.type == 'addChild') {
          this.dialogConfig.title = '添加子类目'
        }
      }
    },
    queryParent() {
      var params = {}
      params.categoryType = this.categoryInfo.categoryType
      params.level = 2
      queryParent(params).then((res) => {
        this.baseData.parentCategory.push(res)
        this.infoForm.parentId = this.categoryInfo.parentId
        // this.getOptionsList('editFormData', false)
      })
    },

    showOrHideAttr(formName, label, isHide) {
      const idx = this[formName].findIndex(item => item.label == label)
      this[formName][idx].isHide = isHide
    },

    handleAddConfirm() {
      this.$refs.addFormRef.$refs[this.editChildRef].validate(valid => {
        if (valid) {
          let api = addGoodsCategory_new
          // var params = deepClone(this.infoForm)
          var params = {}
          params.categoryName = this.infoForm.categoryName
          if (this.categoryInfo.type == 'addChild') {
            params.parentId = this.categoryInfo.id
          } else if (this.categoryInfo.type == 'edit') {
            params.id = this.categoryInfo.id
            if (this.categoryInfo.level == 3) { // 编辑三级类目时需要传所属类目id
              // return console.log(this.infoForm.parentId.length)
              if (this.infoForm.parentId.length && this.infoForm.parentId.length > 0) {
                params.parentId = this.infoForm.parentId.pop()
              } else {
                params.parentId = this.infoForm.parentId
              }
            }
            api = updateGoodsCategory
          }
          api(params).then(res => {
            this.notice.okTips('操作成功')
            this.visible = true
          })
        }
      })
    }

  }
}
</script>
<style lang="scss" scoped>
.add-dialog {
  margin: 20px 0;

  .reject-input {
    ::v-deep .el-textarea__inner {
      height: 70px;
    }
  }

  .el-textarea .el-input__count {
    right: 90px;
    bottom: -10px;
    font-size: 12px;
    color: #999999;
  }

  .tips {
    color: #999999;
  }
}
</style>