<template>
	<u-popup v-model="visible" mode="bottom" border-radius="24" :safe-area-inset-bottom="true" :mask-close-able="true"
		@close="close">
		<view class="condition-popup">
			<!-- 头部 -->
			<view class="header">
				<text class="title">切换品相</text>
				<image src="/pages-sell/static/select-good/icon-close.png" class="close-icon" @click="close"></image>
			</view>

			<!-- SKU 列表 (新样式) -->
			<view class="options-list" v-if="skuList.length > 0">
				<view class="option-item" v-for="(opt, index) in skuList" :key="index"
					:class="{ active: currentCondition === opt.conditionType, disabled: isStockEmpty(opt) }"
					@click="selectSku(opt)">
					<!-- 选中背景图 -->
					<image v-if="currentCondition === opt.conditionType"
						src="/pages-sell/static/select-good/selected.png" class="bg-image"></image>

					<view class="left">
						<text class="opt-name">{{ getConditionName(opt.conditionType) }}</text>
						<!-- 折扣标签 -->
						<view class="opt-discount" :class="{ active: currentCondition === opt.conditionType }"
							v-if="opt.discount">
							<text>{{ opt.discount }}折</text>
						</view>
					</view>

					<view class="right">
						<text v-if="!isStockEmpty(opt)">¥{{ opt.price }} <text v-if="opt.balanceMoney"
								style="font-size: 24rpx; color: #999;">( 余额价 ¥{{ opt.balanceMoney }} )</text></text>
						<text v-else class="no-stock">暂无库存</text>
					</view>
				</view>
			</view>
			<view class="empty-list" v-else>
				<u-loading mode="circle"></u-loading>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				currentCondition: null,
				skuList: [],
				conditionMap: {
					1: '良好',
					2: '中等',
					3: '次品',
					4: '全新'
				}
			};
		},
		methods: {
			open(skuList, currentCondition) {
				this.skuList = skuList || [];
				// 确保类型一致，通常 API 返回 number
				this.currentCondition = Number(currentCondition);
				this.visible = true;
			},
			close() {
				this.visible = false;
			},
			getConditionName(type) {
				return this.conditionMap[type] || '未知';
			},
			isStockEmpty(sku) {
				return !sku.stockNum || sku.stockNum <= 0;
			},
			selectSku(sku) {
				if (this.isStockEmpty(sku)) return;
				if (this.currentCondition === sku.conditionType) return;
				this.$emit('select', sku);
				this.close();
			}
		}
	};
</script>

<style lang="scss" scoped>
	.condition-popup {
		background-color: #fff;
		padding: 30rpx;
		min-height: 400rpx;

		.header {
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			margin-bottom: 30rpx;
			padding-bottom: 30rpx;
			border-bottom: 2rpx dashed #eee;

			.title {
				font-size: 34rpx;
				font-weight: bold;
				color: #333;
			}

			.close-icon {
				position: absolute;
				right: 0;
				top: 0;
				width: 24rpx;
				height: 24rpx;
				padding: 10rpx;
				box-sizing: content-box;
			}
		}

		.options-list {
			margin-bottom: 40rpx;

			.option-item {
				position: relative;
				display: flex;
				justify-content: space-between;
				align-items: center;
				background: #F8F8F8;
				border-radius: 12rpx;
				padding: 24rpx 30rpx;
				margin-bottom: 24rpx;
				border: 2rpx solid #dfdfdf;
				transition: all 0.2s;

				&.active {
					border-color: transparent;
				}

				&.disabled {
					opacity: 0.6;
					background: #f0f0f0;
				}

				.bg-image {
					position: absolute;
					top: -6rpx;
					left: -1%;
					width: 102%;
					height: 110rpx;
					z-index: 0;
				}

				.left,
				.right {
					position: relative;
					z-index: 1;
				}

				.left {
					display: flex;
					align-items: center;

					.opt-name {
						font-size: 30rpx;
						font-weight: bold;
						color: #333;
						margin-right: 16rpx;
					}

					.opt-discount {
						background: #D8D8D8;
						padding: 0 20rpx;
						border-radius: 0 20rpx 0 20rpx;

						text {
							color: #fff;
							font-size: 24rpx;
							display: inline-block;
						}

						&.active {
							background: linear-gradient(0deg, #30E030 0%, #28C445 100%);
						}
					}
				}

				.right {
					font-size: 28rpx;
					color: #D81A00;
					font-weight: bold;

					.no-stock {
						color: #999;
						font-weight: normal;
						font-size: 24rpx;
					}
				}
			}
		}

		.empty-list {
			display: flex;
			justify-content: center;
			padding: 50rpx 0;
		}
	}
</style>