selectDefineProductDialog.vue 10.6 KB
<template>
  <baseDialog :dialog-config="selectDialogConfig"
            :is-show.sync="visible"
            @handleClick="handleConfirm">
    <div v-if="selectDialogConfig.type == 1">
      <el-row class="tableCon">
        <div class="tableBox">
          <div class="tableTop">商品</div>
          <div class="tableContent">
            <div class="searchBox">
              <el-input v-model.trim="upgradeGoodsName" placeholder="搜索商品名称" size="small" clearable style="width: 200px;"></el-input>
              <el-select v-model="brandMerchantCodeList" placeholder="请选择品牌方" multiple filterable collapse-tags clearable>
                <el-option v-for="(item, index) in brandMerchantCodeSelectList" :key="index" :label="item.relatedPartyName" :value="item.id"></el-option>
              </el-select>
              <el-button type="primary" size="small" @click="handleSearch" >搜索</el-button>
            </div>
            <el-table
              ref="seletingTable"
              :data="listData"
              height="364px"
              class="table"
              border
              @select="handleChange"
              @select-all="handleAll">
              <el-table-column type="selection" width="55" align="center" :selectable="selectableFn" />
              <el-table-column prop="upgradeGoodsName" label="商品名称" align="center">
              </el-table-column>
            </el-table>
            <el-pagination
              small
              background
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage"
              :page-size="pageSize"
              layout="total, prev, pager, next"
              :total="total" />
          </div>
        </div>
        <div class="tableBox">
          <div class="tableTop">
            <div>
              <span>已选商品</span>
              <span>{{ selectedData.length ? `(${selectedData.length}个)` : '' }}</span>
            </div>
            <el-button type="text" @click="clearSelect">清空</el-button>
          </div>
          <div class="tableContent">
            <el-table
              class="tableBox table"
              :data="selectedData"
              border
              height="420px">
              <el-table-column prop="upgradeGoodsName" label="商品名称" width="420px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.upgradeGoodsName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="55px">
                <template slot-scope="scope">
                  <el-button
                    @click="handleDelete(scope.row)"
                    type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-row>
    </div>
    <div v-else>
      <el-row class="tableCon">
        <div class="tableBox">
          <div class="tableTop">圈定产品</div>
          <div class="tableContent">
            <el-table
              ref="seletingTable"
              :data="listData"
              height="424px"
              class="table"
              border
              @select="handleChange"
              @select-all="handleAll">
              <el-table-column type="selection" width="55" align="center" :selectable="selectableFn" />
              <el-table-column prop="productName" label="产品名称" align="center">
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div class="tableBox">
          <div class="tableTop">
            <div>
              <span>已选圈定产品</span>
              <span>{{ selectedData.length ? `(${selectedData.length}个)` : '' }}</span>
            </div>
            <el-button type="text" @click="clearSelect">清空</el-button>
          </div>
          <div class="tableContent">
            <el-table
              class="tableBox table"
              :data="selectedData"
              border
              height="424px">
              <el-table-column prop="productName" label="产品名称" align="center">
              </el-table-column>
              <el-table-column label="操作" align="center" width="50px">
                <template slot-scope="scope">
                  <el-button
                    @click="handleDelete(scope.row)"
                    type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-row>
    </div>
  </baseDialog>
</template>
<script>
import { getUpgradeProductList } from '@api/productManage'
import { getBrandSupplyMerchant } from '@api/product'
import { deepClone } from '@/common/util'
export default {
  name: 'SelectDefineProductDialog',
  data() {
    return {
      selectedData: [],
      listData: [],
      // 分页
      total: 0,
      currentPage: 1,
      pageSize: 20,
      upgradeGoodsName: '',
      brandMerchantCodeList: [],
      brandMerchantCodeSelectList: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.selectDialogConfig.selectDialogVisible
      },
      set() {
        this.$emit('selectDialogClose')
      }
    }
  },
  props: {
    selectDialogConfig: {
      type: Object,
      default: () => {}
    }
  },
  mounted() {
    if (this.selectDialogConfig.type == 1) {
      // 获取sku商品列表数据
      this.getSkuGoodsList()
      // 获取品牌方数据
      this.getBrandDataList()
    } else {
      this.listData = deepClone(this.selectDialogConfig.listData)
      this.$nextTick(() => {
        this.recoverySelection()
      })
    }
    this.selectedData = deepClone(this.selectDialogConfig.selectData)
  },
  methods: {
    getBrandDataList() {
      getBrandSupplyMerchant({
        partitionFlag: 1
      }).then(res => {
        this.brandMerchantCodeSelectList = res
      })
    },
    getSkuGoodsList() {
      let params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        upgradeGoodsName: this.upgradeGoodsName,
        brandMerchantCodeList: this.brandMerchantCodeList
      }
      getUpgradeProductList(params).then(res => {
        res?.list?.forEach(val => {
          val.customerId = val.id // 自定义的id用于唯一值判断
          let specificationTypeSequenceArr = val.specificationTypeSequence?.split(';~;')
          let specificationValueSequenceArr = val?.specificationData?.split(';~;')
          const suffixMapping = {
            1: '年',
            2: '(km/万)',
            3: '万'
          }
          if (specificationTypeSequenceArr?.length) {
            specificationValueSequenceArr?.forEach((v, index) => {
              const suffix = suffixMapping[specificationTypeSequenceArr[index]]
              if (suffix) {
                specificationValueSequenceArr[index] = `${v}${suffix}`
              }
            })
            val.listShowSpecificationData = specificationValueSequenceArr?.join('/')
          }
          val.itemId = val.id // 后端需要的skuID 
          val.upgradeGoodsName = val.listShowSpecificationData ? `${val.upgradeGoodsName} ${val.listShowSpecificationData}` : val.upgradeGoodsName
        })
        this.total = res.total
        this.listData = res?.list || []
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    recoverySelection() {
      var exitIds = this.selectedData.map((item) => item.customerId)
      this.listData.forEach((item) => {
        exitIds.includes(item.customerId) &&
          this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    // sku搜索
    handleSearch() {
      this.currentPage = 1
      this.getSkuGoodsList()
    },
    // 翻页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSkuGoodsList()
    },
    selectableFn(row) {
      var isSelect = true
      if (this.selectDialogConfig?.otherSelectData?.filter(val => val.customerId == row.customerId)?.length) {
        isSelect = false
      }
      return isSelect
    },
    // 选择
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(
        (item) => item.customerId === row.customerId
      )
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(
          (item) => item.customerId !== row.customerId
        ))
    },
    // 全选
    handleAll(selection) {
      this.listData.forEach((item) => {
        var isExit = this.selectedData.findIndex(
          (subItem) => subItem.customerId === item.customerId
        )
        var selectFlag = selection?.some(subItem => subItem.customerId == item.customerId)
        if (selection.length && isExit === -1 && selectFlag) {
          this.selectedData.push(item)
        } else if (!selection.length && isExit !== -1 && !selectFlag) {
          this.selectedData.splice(isExit, 1)
        }
      })
    },
    handleDelete(row) {
      this.selectedData = this.selectedData.filter(
        (item) => item.customerId !== row.customerId
      )
      const obj = this.listData.find((v) => v.customerId == row.customerId)
      this.$refs.seletingTable.toggleRowSelection(obj)
    },
    // 清除选中项
    clearSelect() {
      this.selectedData = []
      this.$refs.seletingTable.clearSelection()
    },
    // 确定提交
    handleConfirm() {
      const message = this.selectDialogConfig.type == 1 ? '请选择商品' : '请选择圈定产品'
      if (!this.selectedData?.length) return this.$message.warning(message)
      // 商品最多支持选择99个
      const maxMessage = `最多支持选择99个${this.selectDialogConfig.type == 1 ? '商品' : '圈定产品'}`
      if (this.selectedData?.length > 99) return this.$message.warning(maxMessage)
      let eventType = this.selectDialogConfig.type == 1 ? 'addSku' : 'addProductToPackage'
      this.$emit(eventType, this.selectedData, this.selectDialogConfig.index)
    }
  }
}
</script>
<style lang="scss" scoped>
.tableCon {
  margin-top: 20px;
  display: flex;
  .tableBox {
    flex: 1;
    margin-right: 12px;
    border: 1px solid #e5e5e5;
    .tableTop{
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 48px;
      font-size: 16px;
      color: #333333;
      font-weight: 600;
      background: #f5f5f5;
      padding: 0 16px;
    }
    .tableContent{
      padding: 16px;
      .searchBox{
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
    }
  }
  .tableBox:last-child{
    margin: 0;
  }
  ::v-deep .el-table__header-wrapper th{
    background: #F2F4F7 !important;
  }
}
</style>