selectPackageDialog.vue 19.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
<!-- 选择套餐/商品弹窗 -->
<template>
  <el-dialog custom-class="select-package-dialog"
             title="选择商品/套餐"
             width="1200px"
             :close-on-click-modal="false"
             :visible.sync="dialogVisible"
             @close="handleClose">
    <div v-loading="tableLoading" class="dialog-content">
      <!-- 左侧:套餐/商品列表 -->
      <div class="left-panel">
        <el-tabs v-model="activeTab" @tab-click="handleTabChange">
          <el-tab-pane label="套餐" name="0"></el-tab-pane>
          <el-tab-pane label="商品" name="1"></el-tab-pane>
        </el-tabs>

        <!-- 搜索筛选区 -->
        <div class="filter-bar">
          <template v-if="activeTab == '0'">
            <el-input v-model="filters.name"
                      placeholder="套餐名称"
                      size="mini"
                      style="width: 140px"
                      clearable />
            <el-input v-model="filters.code"
                      placeholder="套餐ID"
                      size="mini"
                      style="width: 160px"
                      clearable />
          </template>
          <template v-else>
            <el-input v-model="filters.name"
                      placeholder="产品名称"
                      size="mini"
                      style="width: 140px"
                      clearable />
            <el-input v-model="filters.code"
                      placeholder="产品ID/上架ID"
                      size="mini"
                      style="width: 160px"
                      clearable />
          </template>
          <el-select v-model="filters.scene"
                     placeholder="场景"
                     size="mini"
                     style="width: 160px"
                     clearable
                     filterable>
            <el-option v-for="item in sceneOptions"
                       :key="item.baseSceneCode"
                       :label="item.name"
                       :value="item.baseSceneCode" />
          </el-select>
          <el-select v-model="filters.store"
                     placeholder="销售门店"
                     size="mini"
                     style="width: 160px"
                     clearable
                     filterable>
            <el-option v-for="subItem in merchantList"
                       :key="subItem.merchantId"
                       :label="subItem.merchantName"
                       :value="subItem.merchantId" />
          </el-select>
          <el-button size="mini"
                     type="primary"
                     @click="handleSearch">
            查询
          </el-button>
          <el-button size="mini" @click="handleReset">
            重置
          </el-button>
        </div>

        <!-- 表格 -->
        <div class="table-wrapper">
          <el-table :key="activeTab"
                    ref="tableRef"
                    height="100%"
                    size="mini"
                    border
                    :data="tableData"
                    :reserve-selection="true"
                    @selection-change="handleSelectionChange">
            <el-table-column align="center"
                             type="selection"
                             width="40" />
            <!-- 套餐列 -->
            <template v-if="activeTab == '0'">
              <el-table-column align="center"
                               label="套餐名称"
                               min-width="150">
                <template #default="scope">
                  {{ emptyFilter(scope.row.productName) }}
                </template>
              </el-table-column>
              <el-table-column align="center"
                               label="套餐ID"
                               width="200">
                <template #default="scope">
                  {{ emptyFilter(scope.row.packageCode) }}
                </template>
              </el-table-column>
            </template>
            <!-- 商品列 -->
            <template v-else>
              <el-table-column align="center"
                               label="产品名称"
                               width="180">
                <template #default="scope">
                  {{ emptyFilter(scope.row.productName) }}
                </template>
              </el-table-column>
              <el-table-column align="center"
                               label="商品别名"
                               width="180">
                <template #default="scope">
                  {{ emptyFilter(scope.row.goodsAlias) }}
                </template>
              </el-table-column>
              <el-table-column align="center"
                               label="产品ID"
                               width="170">
                <template #default="scope">
                  {{ emptyFilter(scope.row.productId) }}
                </template>
              </el-table-column>
              <el-table-column align="center"
                               label="上架ID"
                               width="170">
                <template #default="scope">
                  {{ emptyFilter(scope.row.goodsCode) }}
                </template>
              </el-table-column>
            </template>
            <el-table-column align="center"
                             label="场景"
                             width="120">
              <template #default="scope">
                {{ getSceneName(scope.row.baseSceneCode) }}
              </template>
            </el-table-column>
            <el-table-column align="center"
                             label="销售门店"
                             width="100">
              <template #default="scope">
                <span class="store-link" @click="handleViewStores(scope.row)">
                  <img src="../../../goods/grounding/images/merchant_icon.png" style="width: 14px; height: 13px; margin-right: 5px" />
                  x{{ scope.row.storeCount != null ? scope.row.storeCount : "-" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column align="center"
                             label="现售价"
                             prop="currentPrice"
                             width="100">
              <template #default="scope">
                {{ priceFilter(scope.row.currentPrice) }}
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页 -->
        <div class="pagination-bar">
          <el-pagination layout="prev, pager, next"
                         small
                         :current-page="pagination.page"
                         :page-size="pagination.pageSize"
                         :total="pagination.total"
                         @current-change="handlePageChange" />
        </div>
      </div>

      <!-- 右侧:已选结果 -->
      <div class="right-panel">
        <div class="right-header">
          <span class="title">已选结果 ({{ selectedList.length }})</span>
          <el-button size="mini"
                     type="text"
                     @click="handleClearAll">
            全部清空
          </el-button>
        </div>
        <div class="selected-list">
          <div v-if="!selectedList.length" class="empty-tip">
            暂无选择
          </div>
          <template v-else>
            <!-- 套餐分组 -->
            <div v-if="packageItems.length" class="group">
              <div class="group-title">
                套餐 ({{ packageItems.length }})
              </div>
              <div v-for="(item, index) in packageItems"
                   :key="`pkg_${item.packageCode || index}`"
                   class="selected-item">
                <div class="item-info">
                  <div class="item-name">
                    {{ item.productName }}
                  </div>
                </div>
                <i class="el-icon-delete" @click="handleRemoveItem(item)"></i>
              </div>
            </div>
            <!-- 商品分组 -->
            <div v-if="productItems.length" class="group">
              <div class="group-title">
                商品 ({{ productItems.length }})
              </div>
              <div v-for="(item, index) in productItems"
                   :key="`prod_${index}`"
                   class="selected-item">
                <div class="item-info">
                  <div class="item-name">
                    {{ item.productName }}
                  </div>
                </div>
                <i class="el-icon-delete" @click="handleRemoveItem(item)"></i>
              </div>
            </div>
          </template>
        </div>
      </div>
    </div>

    <!-- 底部按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button size="mini" @click="handleCancel">
          取 消
        </el-button>
        <el-button size="mini"
                   type="primary"
                   @click="handleConfirm">
          确 定
        </el-button>
      </div>
    </template>

    <!-- 销售门店列表弹窗 -->
    <StoreListDialog append-to-body
                     :merchant-list="merchantList"
                     :store-row="storeRow"
                     :visible.sync="storeDialogVisible" />
  </el-dialog>
</template>

<script>
import { getBaseSceneCodeList } from "@api/productManage";
import { getGoodsListByMerchant, getPackageListByMerchant } from "@/api/marketingActivity";
import StoreListDialog from "./storeListDialog.vue";

export default {
	name: "SelectPackageDialog",

	components: {
		StoreListDialog,
	},

	directives: {},

	props: {
		visible: {
			type: Boolean,
			default: false,
		},
		// 已选中的项(用于回显)
		selectedItems: {
			type: Array,
			default: () => [],
		},
		// 已选使用门店
		merchantList: {
			type: Array,
			default: () => [],
		},
	},

	data() {
		return {
			activeTab: "0",
			filters: {
				name: "",
				code: "",
				scene: "",
				store: "",
			},
			sceneOptions: [],
			storeOptions: [],
			tableData: [],
			pagination: {
				page: 1,
				pageSize: 10,
				total: 0,
			},
			// 按 tab 分别存储选中项,避免切换时互相覆盖
			selectedMap: {
				package: [],
				product: [],
			},
			// 临时选中(当前 tab 表格选中)
			tempSelected: [],
			// 标记是否正在恢复选中状态,防止 selection-change 覆盖数据
			restoringSelection: false,
			// 销售门店弹窗
			storeDialogVisible: false,
			storeRow: {},
			// 表格加载状态
			tableLoading: false,
		};
	},

	computed: {
		dialogVisible: {
			get() {
				return this.visible;
			},
			set(val) {
				this.$emit("update:visible", val);
			},
		},
		// 合并后的已选列表(用于右侧展示)
		selectedList() {
			return [...this.selectedMap.package, ...this.selectedMap.product];
		},
		packageItems() {
			return this.selectedMap.package;
		},
		productItems() {
			return this.selectedMap.product;
		},
	},

	watch: {
		visible(val) {
			if (val) {
				this.initData();
				this.getBaseCodeList();
			}
		},
	},

	methods: {
		priceFilter(num) {
			const pow = 10 ** 2;
			if (this.filterEmpty(num))
				return "-";
			return `¥${(Math.round(num * pow) / pow).toFixed(2)}`;
		},
		filterEmpty(data) {
			return (data ?? "") === "";
		},
		emptyFilter(val) {
			if (val === null || val === undefined || val === "")
				return "-";
			return val;
		},
		getBaseCodeList() {
			this.sceneOptions = [];
			return getBaseSceneCodeList({}).then((res) => {
				this.sceneOptions = res || [];
			});
		},
		// 根据 baseSceneCode 映射场景名称
		getSceneName(code) {
			if (code == null || code === "")
				return "-";
			const item = this.sceneOptions.find(opt => opt.baseSceneCode === code);
			return item ? item.name : code;
		},
		// 查询按钮
		handleSearch() {
			this.pagination.page = 1;
			this.fetchTableData();
		},
		// 重置按钮
		handleReset() {
			this.filters = { name: "", code: "", scene: "", store: "", onlyApplicable: false };
			this.pagination.page = 1;
			this.fetchTableData();
		},
		// 初始化数据
		initData() {
			// 始终默认选中套餐 tab
			this.activeTab = "0";
			// 按类型分组初始化(productType: 0=套餐, 1=商品)
			this.selectedMap = {
				package: this.selectedItems.filter(item => item.productType === 0),
				product: this.selectedItems.filter(item => item.productType === 1),
			};
			this.tempSelected = [];
			this.filters = { name: "", code: "", scene: "", store: "", onlyApplicable: false };
			this.pagination.page = 1;
			// 先加载场景数据,再请求表格(确保映射可用)
			this.getBaseCodeList().then(() => {
				this.fetchTableData();
			});
		},

		// 获取表格数据
		fetchTableData() {
			const isPackage = this.activeTab == "0";
			const eventRequest = isPackage ? getPackageListByMerchant : getGoodsListByMerchant;
			// 默认传全部适用门店;选择了门店筛选则只传该门店
			const storeIds = this.merchantList?.map(val => val.merchantId);
			const params = {
				pageNum: this.pagination.page,
				pageSize: this.pagination.pageSize,
				merchantIds: storeIds,
			};
			// 套餐tab:套餐名称、套餐ID、场景
			if (isPackage) {
				if (this.filters.name)
					params.packageName = this.filters.name;
				if (this.filters.code)
					params.packageId = this.filters.code;
				if (this.filters.scene)
					params.baseSceneCode = this.filters.scene;
				if (this.filters.store)
					params.merchantId = this.filters.store;
			} else {
				// 商品tab:产品名称、产品ID/上架ID、场景
				if (this.filters.name)
					params.goodsName = this.filters.name;
				if (this.filters.code) {
					params.goodsCode = this.filters.code;
				}
				if (this.filters.scene)
					params.baseSceneCode = this.filters.scene;
				if (this.filters.store)
					params.merchantId = this.filters.store;
			}
			this.tableLoading = true;
			// 先标记,防止 tableData 赋值时触发的 selection-change 清空 selectedMap
			this.restoringSelection = true;
			const tabKey = isPackage ? "package" : "product";
			eventRequest(params)
				.then((res) => {
					const savedIds = (this.selectedMap[tabKey] || []).map(item => (isPackage ? item.packageCode : item.goodsCode));
					res?.list?.forEach((val) => {
						val.packageCode = val.packageId;
						val.productName = val.productName || val.packageName;
						val.upgradeProductId = val.productId;
					});
					this.tableData = res?.list || [];
					this.pagination.total = res?.total || 0;

					this.$nextTick(() => {
						if (this.$refs.tableRef) {
							this.tableData.forEach((row) => {
								if (savedIds.includes(isPackage ? row.packageCode : row.goodsCode)) {
									this.$refs.tableRef.toggleRowSelection(row, true);
								}
							});
							// 延迟恢复,确保所有 selection-change 事件处理完
							setTimeout(() => {
								this.restoringSelection = false;
								this.tempSelected = this.$refs.tableRef.selection || [];
							}, 100);
						} else {
							this.restoringSelection = false;
						}
					});
				})
				.finally(() => {
					this.tableLoading = false;
				});
		},

		// tab切换
		handleTabChange() {
			this.pagination.page = 1;
			this.filters = { name: "", code: "", scene: "", store: "", onlyApplicable: false };
			this.fetchTableData();
		},

		// 分页切换
		handlePageChange(page) {
			this.pagination.page = page;
			// TODO: 替换为真实接口请求,传入分页参数
			this.fetchTableData();
		},

		// 表格选中变化
		handleSelectionChange(selection) {
			this.tempSelected = selection;
			// 恢复选中状态期间不更新 selectedMap,避免被清空
			if (this.restoringSelection) {
				return;
			}
			const isPackage = this.activeTab === "0";
			const idField = isPackage ? "packageCode" : "goodsCode";
			const tabKey = isPackage ? "package" : "product";
			// 当前页所有数据的 id 集合
			const currentPageIds = this.tableData.map(row => row[idField]);
			// 保留非当前页的旧选中项(跨页保持)
			const otherPages = (this.selectedMap[tabKey] || []).filter(
				item => !currentPageIds.includes(item[idField]),
			);
			// 当前页以表格实际选中为准(包含新增/排除取消)
			const currentPageItems = selection.map(t => ({ ...t, productType: isPackage ? 0 : 1 }));
			this.$set(this.selectedMap, tabKey, [...otherPages, ...currentPageItems]);
		},

		// 全部清空
		handleClearAll() {
			this.selectedMap = { package: [], product: [] };
			this.$refs.tableRef && this.$refs.tableRef.clearSelection();
		},

		// 移除已选项
		handleRemoveItem(item) {
			const tab = item.productType === 0 ? "package" : "product";
			const idx = this.selectedMap[tab].findIndex(s => tab === "package" ? s.packageCode === item.packageCode : s.goodsCode === item.goodsCode);
			if (idx === -1)
				return;
			this.selectedMap[tab].splice(idx, 1);
			// 取消表格中的选中状态
			this.$nextTick(() => {
				const row = this.tableData.find(s => tab === "package" ? s.packageCode === item.packageCode : s.goodsCode === item.goodsCode);
				if (row && this.$refs.tableRef) {
					this.$refs.tableRef.toggleRowSelection(row, false);
				}
			});
		},

		// 查看销售门店
		handleViewStores(item) {
			this.storeRow = item;
			this.storeDialogVisible = true;
		},

		// 取消
		handleCancel() {
			this.dialogVisible = false;
		},

		// 确定
		handleConfirm() {
			const finalResult = [...this.selectedMap.package, ...this.selectedMap.product];
			if (!finalResult.length) {
				this.$message.warning("请至少选择一项商品或套餐");
				return;
			}
			this.$emit("confirm", finalResult);
			this.dialogVisible = false;
		},

		// 关闭弹窗
		handleClose() {
			this.$emit("update:visible", false);
		},
	},
};
</script>

<style lang="scss" scoped>
.select-package-dialog {
	::v-deep .el-dialog__body {
		padding: 20px;
	}

	.dialog-content {
		display: flex;
		gap: 20px;
		height: 500px;
	}

	.left-panel {
		flex: 1;
		display: flex;
		flex-direction: column;
		min-width: 0;

		::v-deep .el-tabs__header {
			margin-bottom: 16px;
		}

		.filter-bar {
			display: flex;
			align-items: center;
			gap: 12px;
			margin-bottom: 16px;
			flex-wrap: wrap;
		}

		.table-wrapper {
			flex: 1;
			min-height: 0;
			margin-bottom: 16px;
		}

		.pagination-bar {
			display: flex;
			justify-content: flex-end;
		}
	}

	.store-link {
		color: #2a58ad;
		cursor: pointer;
		font-size: 12px;
		white-space: nowrap;
		margin-top: 2px;
		display: inline-block;

		i {
			margin-right: 2px;
		}
	}

	.right-panel {
		width: 280px;
		display: flex;
		flex-direction: column;
		border-left: 1px solid #ebeef5;
		padding-left: 20px;

		.right-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16px;

			.title {
				font-size: 14px;
				font-weight: 500;
				color: #303133;
			}
		}

		.selected-list {
			flex: 1;
			overflow-y: auto;
			min-height: 0;

			.empty-tip {
				text-align: center;
				color: #c0c4cc;
				padding-top: 60px;
				font-size: 14px;
			}

			.group {
				margin-bottom: 16px;

				.group-title {
					font-size: 12px;
					color: #909399;
					margin-bottom: 8px;
				}

				.selected-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 8px 0;
					border-bottom: 1px solid #f0f0f0;

					.item-info {
						flex: 1;
						min-width: 0;

						.item-name {
							font-size: 14px;
							color: #303133;
							word-break: break-all;
							line-height: 1.4;
						}
					}

					.el-icon-delete {
						color: #f56c6c;
						cursor: pointer;
						font-size: 14px;
						margin-left: 8px;
						flex-shrink: 0;

						&:hover {
							color: #f78989;
						}
					}
				}
			}
		}
	}

	.dialog-footer {
		text-align: right;
	}

	// 表格内销售门店链接
	::v-deep .store-link {
		color: #409eff;
		cursor: pointer;

		&:hover {
			color: #66b1ff;
		}

		i {
			margin-right: 2px;
		}
	}
}
</style>