<template>
	<view class="page">
		<u-navbar title=" " :border-bottom="false" :background="bgc" back-icon-color="#fff" title-color='#fff'
			title-size='36' height='36' id="navbar">
		</u-navbar>
		<image class="bj" src="https://api.ccttiot.com/smartmeter/img/static/u7VvF1RbLGTzLjXvV1E1" mode="aspectFill">
		</image>
		<view class="bd">
			<view class="top">
				<view :class="topindex == 1 ? 'topactive' : ''" @click="btntop(1)">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uSRx4LB0E5ObsYoWcWM1" mode=""></image>客户存酒
					<text v-if="topindex == 1"></text>
				</view>
				<view :class="topindex == 2 ? 'topactive' : ''" @click="btntop(2)">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uAExIEUQnSDQCCBsPiEH" mode=""></image>客户取酒
					<text v-if="topindex == 2"></text>
				</view>
			</view>
			<view class="cen" v-if="topindex == 1">
				<view class="lt">
					<view :class="ltindex == index ? 'ltactive' : ''" v-for="(item,index) in fenleilist" :key="index"
						@click="btnlt(item,index)">
						{{item.name}}
						<text></text>
					</view>
				</view>
				<view class="rt">
					<view class="list">
						<view class="item_list" v-for="(item,index) in shoplist" :key="index">
							<view class="" style="display: flex;">
								<image :src="item.image" mode="aspectFill"></image>
								<view class="name">
									{{item.name}}
								</view>
							</view>
							<view class="jiajian">
								<image src="https://api.ccttiot.com/smartmeter/img/static/uOTiYDHg47sGg1XmdnYV" mode=""
									@click="decreaseNumber(item, index)"></image>
								<input type="text" v-model="item.totalNum" :disabled="true" />
								<image src="https://api.ccttiot.com/smartmeter/img/static/ucfvIu9ifB0tQa96k8Zr" mode=""
									@click="increaseNumber(item, index)"></image>
							</view>
						</view>
						<view class="wu">
							暂时没有更多商品咯...
						</view>
					</view>
				</view>
			</view>
			<view class="" v-else>
				<view class="cha" v-if="!quflag">
					<input type="text" v-model="phone" placeholder="请输入用户手机号" />
					<view class="" @click="btnseach">
						确定
					</view>
				</view>
				<view class="dangqian" v-if="quflag">
					当前用户:{{phone}}
					<view class="" @click="btnqh">
						切换用户
					</view>
				</view>
				<view class="kehujiu" v-if="quflag">
					<view class="kehulist" v-for="(item,index) in qulist" :key="index">
						<!-- 左侧图片 -->
						<image class="item-image" :src="item.picture" mode="aspectFill"></image>
						<!-- 中间信息 -->
						<view class="zhong">
							<view class="name">{{item.goodsName == null ? '--' : item.goodsName}}</view>
							<view class="remain">剩余 x{{ Number(item.totalNum) - Number(item.takenNum)}}</view>
							<view class="remain">
								位置：{{item.locationName}}
							</view>
						</view>
						<!-- 右侧按钮 -->
						<view class="youbian" @click.stop="showPopup(item)">取出</view>
					</view>
					<view class="" style="width: 100%;text-align: center;margin-top: 40rpx;">
						当前没有更多存酒了...
					</view>
				</view>
			</view>
		</view>

		<!-- 弹窗 -->
		<view v-if="showQuantityPopup" class="quantity-popup">
			<view class="popup-content">
				<view class="popup-title">请选择取出数量</view>
				<view class="quantity-selector">
					<button @click="decreaseQuantity" class="quantity-btn">-</button>
					<view class="quantity-display">{{ quantity }}</view>
					<button @click="increaseQuantity" class="quantity-btn">+</button>
				</view>
				<view class="popup-buttons">
					<button @click="cancelQuantity" class="cancel-btn">取消</button>
					<button @click="confirmQuantity" class="confirm-btn">确定</button>
				</view>
			</view>
		</view>

		<view class="bottc" v-if="topindex == 1">
			<view class="lt">
				本次共存 <text>{{ selectedItems.length }}</text> 件
			</view>
			<view class="rt" @click="btncun">
				立即存酒
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "",
				},
				topindex: 1,
				totalNum: 0,
				fenleilist: [],
				ltindex: 0,
				categoryId: '',
				shoplist: [],
				selectedItems: [],
				phone: '',

				qulist: [],
				quflag: false,

				showQuantityPopup: false,
				quantity: 1, // 默认数量
				minQuantity: 1, // 最小数量
				maxQuantity: 1, // 最大数量
				quid: ''
			}
		},
		onLoad() {

		},
		onShow() {
			this.ltindex = 0
			this.shoplist = []
			this.selectedItems = []
			this.getfenlei()
		},
		methods: {
			// 显示弹窗
			showPopup(item) {
				this.quid = item.id
				this.showQuantityPopup = true
				this.minQuantity = 1 // 最小数量
				this.maxQuantity = Number(item.totalNum) - Number(item.takenNum) // 最大数量
			},
			// 增加数量
			increaseQuantity() {
				if (this.quantity < this.maxQuantity) {
					this.quantity++;
				} else {
					uni.showToast({
						title: '已是最大值',
						icon: 'none',
						duration: 3000
					})
				}
			},
			// 减少数量
			decreaseQuantity() {
				if (this.quantity > this.minQuantity) {
					this.quantity--;
				} else {
					uni.showToast({
						title: '已是最小值',
						icon: 'none',
						duration: 3000
					})
				}
			},
			// 取消
			cancelQuantity() {
				this.showQuantityPopup = false;
				this.quantity = 1; // 重置数量
			},
			// 确定
			confirmQuantity() {
				let data = [{
					storageId: this.quid,
					number: this.quantity
				}]
				this.$u.put(`/bst/storage/getWine`, data).then(res => {
					if (res.code == 200) {
						this.showQuantityPopup = false
						this.quantity = 1
						this.btnseach()
						uni.showToast({
							title: '取出成功',
							icon: 'success',
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 3000
						})
					}
				})
			},
			// 点击切换用户
			btnqh() { 
				this.quflag = false
				this.phone = ''
			},
			// 点击取酒查询客户剩余存酒
			btnseach() {
				this.$u.get(`/bst/storage/getWineByPhone?phone=${this.phone}`).then(res => {
					if (res.code == 200) {
						this.qulist = res.data
						this.quflag = true
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 3000
						})
					}
				})
			},

			// 点击立即存酒
			btncun() {
				if (this.selectedItems.length > 0) {
					uni.navigateTo({
						url: '/page_shanghu/cunqu/cun?arr=' + JSON.stringify(this.selectedItems)
					})
				} else {
					uni.showToast({
						title: '请选择商品',
						icon: 'none',
						duration: 3000
					})
				}
			},
			// 增加数量
			increaseNumber(item, index) {
				item.totalNum += 1;
				this.updateSelectedItems();
			},

			// 减少数量
			decreaseNumber(item, index) {
				if (item.totalNum > 0) {
					item.totalNum -= 1;
					this.updateSelectedItems();
				}
			},

			// 更新已选商品列表
			updateSelectedItems() {
				const newSelected = this.shoplist.filter(item => item.totalNum > 0)
				this.selectedItems = [
					...this.selectedItems.filter(item =>
						newSelected.some(newItem => newItem.id === item.id) || item.totalNum > 0
					),
					...newSelected.filter(newItem =>
						!this.selectedItems.some(item => item.id === newItem.id)
					)
				]
				this.selectedItems = this.selectedItems.filter(item => item.totalNum > 0)
				console.log('最终选中商品:', this.selectedItems)
			},
			// 点击头部切换取酒 存酒
			btntop(num) {
				this.topindex = num
				this.ltindex = 0
				this.shoplist = []
				this.selectedItems = []
				this.getfenlei()
			},
			// 请求酒的分类
			getfenlei() {
				this.$u.get(`/bst/goodsCategory/list?storeId=${this.$store.state.storeId}&pageNum=1&pageSize=999`).then(
					res => {
						if (res.code == 200) {
							this.fenleilist = res.rows
							this.categoryId = res.rows[0].id
							this.getjiulist()
						}
					})
			},
			// 请求酒的列表
			getjiulist() {
				this.$u.get(`/bst/goods/list?categoryId=${this.categoryId}&pageNum=1&pageSize=999&deposit=1`).then(res => {
					if (res.code == 200) {
						this.shoplist = res.rows.map(item => ({
							...item,
							totalNum: 0
						}))
						this.selectedItems.forEach(item => {
							this.shoplist.forEach(val => {
								if (item.id == val.id) {
									val.totalNum = item.totalNum
								}
							})
						})
					}
				}).catch(err => {
					console.error('获取商品列表失败:', err)
				})
			},
			// 点击切换左侧分类
			btnlt(item, index) {
				this.ltindex = index
				this.categoryId = item.id
				this.getjiulist()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.quantity-popup {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}

	.popup-content {
		background-color: #fff;
		padding: 20px;
		border-radius: 10px;
		width: 80%;
		max-width: 300px;
	}

	.popup-title {
		font-size: 18px;
		font-weight: bold;
		text-align: center;
		margin-bottom: 20px;
	}

	.quantity-selector {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.quantity-btn {
		width: 80rpx;
		height: 80rpx;
		line-height: 80rpx;
		border: 1px solid #ddd;
		border-radius: 5px;
		background-color: #f5f5f5;
		font-size: 18px;
	}

	.quantity-display {
		font-size: 20px;
		font-weight: bold;
	}

	.popup-buttons {
		display: flex;
		justify-content: space-between;
	}

	.cancel-btn,
	.confirm-btn {
		flex: 1;
		margin: 0 5px;
		border-radius: 5px;
	}

	.cancel-btn {
		background-color: #f5f5f5;
		color: #333;
	}

	.confirm-btn {
		background-color: #FF8998;
		color: #fff;
	}

	/* 列表项样式 */
	.kehujiu {
		margin-bottom: 20rpx;
		border-radius: 12rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
		height: 62vh;
		overflow: scroll;
	}

	.dangqian {
		width: 100%;
		height: 60rpx;
		line-height: 40rpx;
		text-align: right;
		color: #fff;
		padding: 0 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;

		view {
			color: #FF8998;
		}
	}

	.kehulist {
		display: flex;
		align-items: center;
		padding: 24rpx;
	}

	/* 左侧图片 */
	.item-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		margin-right: 24rpx;
	}

	/* 中间信息 */
	.zhong {
		flex: 1;
	}

	.name {
		font-size: 32rpx;
		color: #fff;
		margin-bottom: 12rpx;
		font-weight: bold;
	}

	.remain {
		font-size: 26rpx;
		color: #999;
	}

	/* 右侧按钮 */
	.youbian {
		padding: 12rpx 24rpx;
		background: #FF8998;
		color: white;
		border-radius: 40rpx;
		font-size: 28rpx;
	}

	.ltactive {
		background: #FF8998;
		color: #3D3D3D !important;
		position: relative;

		text {
			color: #FF8998;
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			bottom: 0;
			display: block;
			width: 0;
			height: 0;
			border-left: 10rpx solid transparent;
			border-right: 10rpx solid transparent;
			border-bottom: 10rpx solid #050404;
		}
	}

	.bottc {
		width: 674rpx;
		height: 90rpx;
		background: #FFDDE1;
		border-radius: 61rpx 61rpx 61rpx 61rpx;
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		bottom: 54rpx;
		display: flex;

		.lt {
			width: 450rpx;
			line-height: 90rpx;
			padding-left: 50rpx;
			box-sizing: border-box;
			font-size: 32rpx;
			color: #3D3D3D;

			view {
				position: relative;

			}
		}

		.rt {
			font-size: 32rpx;
			color: #3D3D3D;
			width: 224rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			background: #FF8998;
			border-radius: 0 61rpx 61rpx 0;
		}
	}

	.topactive {
		color: #FF8998 !important;
	}

	.bd {
		width: 750rpx;
		height: 1282rpx;
		background: #010000;
		position: fixed;
		left: 0;
		bottom: 0;

		.kehujiu {
			.kehulist {
				display: flex;
				justify-content: space-between;

				image {
					width: 140rpx;
					height: 140rpx;
				}
			}
		}

		.cha {
			width: 500rpx;
			margin: auto;
			margin-top: 200rpx;

			input {
				padding-left: 20rpx;
				box-sizing: border-box;
				width: 500rpx;
				height: 80rpx;
				line-height: 80rpx;
				border: 1px solid #3D3D3D;
				border-radius: 20rpx;
				color: #fff;
			}
			view {
				margin: auto;
				margin-top: 50rpx;
				width: 220rpx;
				height: 80rpx;
				font-size: 32rpx;
				color: #fff;
				font-weight: 600;
				text-align: center;
				line-height: 80rpx;
				background-color: #FF8998;
				border-radius: 10rpx;
			}
		}

		.cen {
			display: flex;
			margin-top: 54rpx;

			.lt {
				width: 176rpx;
				height: 58vh;
				overflow-y: scroll;
				text-align: center;
				font-weight: 600;
				font-size: 28rpx;
				color: #FFFFFF;
				margin-right: 18rpx;

				view {
					width: 176rpx;
					height: 120rpx;
					line-height: 120rpx;
				}
			}

			.rt {
				width: 74%;

				.list {
					width: 100%;

					.wu {
						margin-left: 120rpx;
						margin-top: 100rpx;
						color: #eee;
						font-size: 28rpx;
					}

					.item_list {
						display: flex;
						justify-content: space-between;
						margin-bottom: 42rpx;

						.jiajian {
							display: flex;
							align-items: flex-end;
							justify-content: center;
							width: 172rpx;

							image {
								width: 42rpx;
								height: 42rpx;
							}

							input {
								width: 60rpx;
								text-align: center;
								color: #999;
							}
						}

						.name {
							font-size: 28rpx;
							color: #FFFFFF;
							margin-left: 22rpx;
						}

						image {
							width: 140rpx;
							height: 140rpx;
						}
					}
				}
			}
		}

		.top {
			display: flex;
			justify-content: space-between;
			padding: 36rpx 74rpx;
			box-sizing: border-box;

			view {
				font-weight: 600;
				font-size: 36rpx;
				color: #FFFFFF;

				text {
					display: block;
					width: 80rpx;
					height: 4rpx;
					background: #FF8998;
					border-radius: 4rpx 4rpx 4rpx 4rpx;
					margin: auto;
					margin-top: 12rpx;
				}

				image {
					width: 48rpx;
					height: 48rpx;
					vertical-align: middle;
				}
			}
		}
	}

	.bj {
		width: 750rpx;
		height: 398rpx;
		position: fixed;
		top: 0;
		left: 0;
	}
</style>