<template>
	<view class="hot-item">
		<!-- Rank -->
		<view class="rank-col">
			<image v-if="rank === 1" src="/pages-sell/static/hot/icon-first.png" class="rank-icon" mode="widthFix"></image>
			<image v-else-if="rank === 2" src="/pages-sell/static/hot/icon-second.png" class="rank-icon" mode="widthFix"></image>
			<image v-else-if="rank === 3" src="/pages-sell/static/hot/icon-third.png" class="rank-icon" mode="widthFix"></image>
			<text v-else class="rank-text">{{ rank < 10 ? '0' + rank : rank }}</text>
		</view>

		<!-- Book Info -->
		<view class="book-info" @click.stop="navigateToDetail">
			<view class="image-wrapper">
				<image :src="item.bookImg" class="book-cover" mode="aspectFill"></image>
				<!-- 状态遮罩 -->
				<view class="status-mask" v-if="item.availableStock === 0">
					<text>暂无库存</text>
				</view>
			</view>
			
			<view class="info-right">
				<text class="title">{{ item.bookName || '-' }}</text>
				<text class="author">{{ item.author || '-' }}</text>
				
				<view class="bottom-row">
					<view class="price-box">
						<text class="currency">¥</text>
						<text class="price">{{ item.bookPrice }}</text>
						<text class="original">¥{{ item.bookOriginalPrice }}</text>
					</view>
					
					<view class="cart-btn" :class="{ 'gray-btn': item.availableStock === 0 && item.hasArrivalNotice === 1 }" @click.stop="handleAction">
						<text v-if="item.availableStock === 0 && item.hasArrivalNotice === 1">取消到货通知</text>
						<text v-else-if="item.availableStock === 0">到货通知</text>
						<text v-else>加入购物车</text>
					</view>
				</view>
			</view>
		</view>

		<!-- Product Selection Popup -->
		<SelectGoodPopup ref="popup" @confirm="onPopupConfirm"></SelectGoodPopup>
	</view>
</template>

<script>
import SelectGoodPopup from '../select-good-popup/index.vue';

export default {
	name: 'HotSellItem',
	components: {
		SelectGoodPopup
	},
	props: {
		rank: {
			type: Number,
			required: true
		},
		item: {
			type: Object,
			required: true
		}
	},
	methods: {
		navigateToDetail() {
			uni.navigateTo({
				url: '/pages-sell/pages/detail?isbn=' + this.item.bookIsbn || ''
			});
		},
		handleAction() {
			if (this.item.availableStock === 0) {
				this.handleNotify();
			} else {
				this.handleAddToCart();
			}
		},
		handleNotify() {
			const isCancel = this.item.hasArrivalNotice === 1;
			const apiUrl = isCancel ? '/token/shop/user/noticeArrivalCancel' : '/token/shop/user/noticeArrival';

			uni.$u.http.post(apiUrl, {
				isbn: this.item.bookIsbn,
			}).then(res => {
				if (res.code === 200) {
					const newValue = isCancel ? 0 : 1;
					this.$emit('update-item', { ...this.item, hasArrivalNotice: newValue });
					this.$set(this.item, 'hasArrivalNotice', newValue);
					this.item.hasArrivalNotice = newValue;
					this.$u.toast(isCancel ? '已取消到货通知' : '到货通知设置成功');
				}
			});
		},
		handleAddToCart() {
			this.item.isbn = this.item.bookIsbn || '';
			this.$refs.popup.open(this.item, 1);
		},
		onPopupConfirm(data) {
			this.$emit('add-cart', data);
		}
	}
}
</script>

<style lang="scss" scoped>
.hot-item {
	display: flex;
	align-items: center;
	padding: 30rpx 0;
	border-bottom: 1rpx solid #f6f7f8;

	.rank-col {
		width: 80rpx;
		display: flex;
		justify-content: center;
		margin-right: 10rpx;

		.rank-icon {
			width: 60rpx;
			height: 60rpx;
		}

		.rank-text {
			font-family: Arial-BoldItalicMT;
			font-size: 36rpx;
			color: #706F6D;
			font-style: italic;
			font-weight: bold;
		}
	}

	.book-info {
		flex: 1;
		display: flex;
		align-items: center;

		.image-wrapper {
			position: relative;
			width: 140rpx;
			height: 180rpx;
			border-radius: 4rpx;
			background-color: #f5f5f5;
			margin-right: 24rpx;
			flex-shrink: 0;
			overflow: hidden;

			.book-cover {
				width: 100%;
				height: 100%;
			}

			.status-mask {
				position: absolute;
				bottom: 0;
				left: 0;
				width: 100%;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.6);
				display: flex;
				justify-content: center;
				align-items: center;

				text {
					color: #fff;
					font-size: 20rpx;
				}
			}
		}

		.info-right {
			flex: 1;
			height: 190rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding: 10rpx 0;

			.title {
				font-size: 32rpx;
				color: #303030;
				font-weight: bold;
				line-height: 1.2;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			.author {
				font-size: 24rpx;
				color: #999999;
			}

			.bottom-row {
				display: flex;
				justify-content: space-between;
				align-items: flex-end;

				.price-box {
					display: flex;
					align-items: baseline;

					.currency {
						font-size: 36rpx;
						color: #D81A00;
						font-weight: bold;
						margin-right: 6rpx;
					}

					.price {
						font-size: 36rpx;
						color: #D81A00;
						font-weight: bold;
						margin-right: 12rpx;
					}

					.original {
						font-size: 24rpx;
						color: #999999;
						text-decoration: line-through;
					}
				}

				.cart-btn {
					width: 160rpx;
					height: 56rpx;
					background: linear-gradient(90deg, #4ED964 0%, #38C248 100%);
					border-radius: 28rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					&.gray-btn {
						background: #cccccc;
					}

					text {
						font-size: 24rpx;
						color: #fff;
						font-weight: 500;
					}
				}
			}
		}
	}
}
</style>
