index.vue 6.96 KB
<!-- 选择商品弹窗 -->
<template>
  <glsx-dialog class="muti-pro" title="选择商品" :isVisible.sync="isMutiSelectVisible" width="1380px" @confirmDialog="handleMutiSelectConfirm" @closeDialog="handleMutiSelectClose">
    <GLCombiForm ref="goodsRef" @handleOk="handleSearch" @handleRest="handleReset" :formObj="goodsFormObj" :formData="goodsFormData"></GLCombiForm>
    <GLTable class="pointTable" @setTableFinishFun="setTableFinishFun" ref="tableRef" :pageConf="pageConf" @sortChange="sortChange" :searchForm="searchForm" :tableParams="tableParams" :getListApi="getListApi" :beforeSearchFn="beforeSearchFn">
      <div class="setPriceSlot" slot="setPriceSlot" slot-scope="scope">
        <span class="unedited">{{ scope.scope.redeemPoints }}积分 + {{ scope.scope.salePrice }}元</span>
      </div>
    </GLTable>
  </glsx-dialog>
</template>

<script>
// import { getGoodsList } from '@/api/modules/common'
import glsxDialog from '@/components/glsx-dialog'
export default {
  name: 'selectMutiProDialog',
  data() {
    return {
      size: 'small',
      // 表格组件
      // getListApi: getGoodsList,
      productList: [],
      getListApi: this.$apis.platformGoods.getListApi,
      pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
      goodsFormObj: {
        goodsPosition: 2,
        businessType: 1,
        goodsSkuName: '',
        goodsSource: ''
      },
      goodsFormData: [
        {
          label: '商品名称',
          name: 'goodsSkuName',
          type: 'input',
          maxlength: 50,
          isClear: true
        },
        {
          label: '商品来源',
          name: 'goodsSource',
          type: 'select',
          optionList: [],
          optionId: 'confId',
          optionLabel: 'confLabel',
          optionsName: 'goodsSourceList'
        }
      ],
      // 表格参数
      tableParams: {
        listName: 'list',
        // 行数据的key
        mutiSelectUniqueKey: 'id',
        // 多选ref的名称
        mutiSelectName: 'list',
        tableColumns: [
          {
            label: '商品名称',
            prop: 'goodsSkuName'
          },
          {
            label: '兑换价',
            width: 220,
            addHtml: 'setPriceSlot',
            prop: 'salePrice'
          },
          {
            label: '商品来源',
            prop: 'goodsSource',
            width: 150,
            formatter: cellValue => {
              return this.baseData.goodsSourceList.find(item => item.confId == cellValue.goodsSource)?.confLabel || '--'
            }
          },
          {
            label: '库存',
            width: 150,
            prop: 'goodsStock',
            formatter: cellValue => {
              return cellValue.goodsStock != null ? cellValue.goodsStock : cellValue.minGoodsStock + '-' + cellValue.maxGoodsStock
            }
          }
        ]
      },
      baseData: {
        goodsSourceList: [
          {
            confId: '1',
            confLabel: '云中鹤'
          },
          {
            confId: '2',
            confLabel: '广联自营'
          }
        ]
      }
    }
  },

  props: {
    isMutiSelectVisible: {
      type: Boolean,
      default: false
    },
    selectedProList: {
      type: Array,
      default: () => []
    },
    // getListApi: {
    //   type: Function,
    //   default: () => { }
    // },
    searchForm: {
      type: Object,
      default: () => { }
    }
  },

  components: {
    glsxDialog
  },

  mounted() {
    this.getOptionsList('goodsFormData')
  },

  methods: {
    beforeSearchFn() {
      return new Promise((resolve, reject) => {
        var selectedData = this.$refs?.tableRef?.mutiSelectArr[this.tableParams.mutiSelectName] || []
        let list = this.productList || []
        this.selectedProList.forEach(item => {
          if (!list?.map(k => k.id).includes(item.id) && !selectedData?.map(k => k.id).includes(item.id)) {
            selectedData.push(item)
          }
        })
        this.selectedProList = this.$common.utils.deepClone(selectedData)
        this.searchForm = this.$common.utils.deepClone(this.goodsFormObj)
        resolve()
      })
    },

    setTableFinishFun(list) {
      this.productList = list
      this.recoverySelected()
    },
    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', this.$common.utils.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', this.$common.utils.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)
            }
          }
        })
      }
    },
    handleReset() {
      this.goodsFormObj = {
        goodsPosition: 2,
        businessType: 1,
        goodsSkuName: '',
        goodsSource: ''
      }
      this.handleSearch()
    },
    handleSearch() {
      this.$refs.tableRef.refreshTable()
    },
    recoverySelected() {
      if (this.selectedProList.length) {
        this.selectedProList.forEach(row => {
          const obj = this.productList.find(item => item.id == row.id)
          obj && this.$refs.tableRef.$refs[this.tableParams.mutiSelectName + 'MutiTable'].toggleRowSelection(obj, true)
        })
      }
    },
    handleMutiSelectConfirm() {
      var selectedData = this.$refs.tableRef.mutiSelectArr[this.tableParams.mutiSelectName]
      this.selectedProList.forEach(item => {
        if (!this.productList.map(k => k.id).includes(item.id) && !selectedData.map(k => k.id).includes(item.id)) {
          selectedData.push(item)
        }
      })
      if (selectedData && selectedData.length > 100) {
        this.$message({
          message: '最多选择100件商品',
          type: 'error',
          customClass: 'msgHierarchy'
        })
        return true
      }
      console.log(this.$common.utils.deepClone(selectedData), 'selectedData===')
      this.$emit('handleMutiSelectConfirm', selectedData)
    },
    handleMutiSelectClose() {
      this.$emit('handleMutiSelectClose')
    }
  }
}
</script>
<style>
.muti-pro .m-t-10 {
  margin-top: 10px;
}
</style>