<template>
	<view class="content">
		<!-- 商品列表 -->
		<view class="shopbox" v-if="shopList.length !== 0">
			<view class="goods-list">
				<view class="item" v-for="(item,index) in shopList" :key="index">
					<label style="margin-right: 30upx;">
						<checkbox :value="item.checked" :checked="item.checked" @click="checkChange(item)" />
					</label>
					<image class="goods-img" @click="showProduct(item)" :src="item.imgUrl" mode="aspectFit"></image>
					<view class="info-box">
						<view>
							<view class="title-box">
								<view class="title ellipsis">{{ item.productName }}</view>
							</view>
						</view>
						<view class="price-num">
							<view class="price">
								<text class="unit">¥</text>
								<text class="text">{{item.price}}</text>
							</view>
							<view class="num-box">
								<view class="img-box" @click="delNum(item)">
									<image v-if="item.cartNum <= 1" src="/static/images/jian.png" mode=""></image>
									<image v-else src="/static/images/jian2.png" mode=""></image>
								</view>
								<input type="number" @change="changeNum($event,item)" :value="item.cartNum" />
								<view class="img-box" @click="addNum(item)">
									<image src="/static/images/add.png" mode=""></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 140rpx;width: 100%;"></view>
		<view v-if="shopList.length == 0" class="no-data-box">
			<image src="/static/images/no_data.png" mode="aspectFit"></image>
			<view class="empty-title">暂无数据</view>
		</view>

		<!-- 底部按钮 -->
		<view class="btn-foot">
			<view class="left">
				<label>
					<checkbox :checked="checkAll" @click="handleCheckAll()" />
				</label>
				<text class="text">全选</text>
				<text class="text" @click="delCart()">删除</text>
			</view>
			<view class="right">
				<view class="total">
					<text class="label">合计：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num">{{totalMoney.toFixed(2)}}</text>
					</view>
				</view>
				<view class="btn" @click="submit">结算</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		queryLiveCartList,
		modifyLiveCart,
		delLiveCart,
		liveOrderKey
	} from "@/api/order.js"

	export default {
		data() {
			return {
				shopList: [],
				totalMoney: 0.00,
				checkAll: false,
				orderKey: ""
			}
		},
		onLoad() {
			this.queryLiveCart();
		},
		methods: {
			// 获得key
			getKey() {
				liveOrderKey().then(res => {
					if (res.code == 200) {
						this.orderKey = res.orderKey
						uni.navigateTo({
							url: '/pages_shop/confirmCreateOrder?type=cart&orderKey=' + this.orderKey
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			},
			// 修改购物车
			modifyLiveCart(item) {
				let data = {
					checked: item.checked ? 1 : 0,
					cartId: item.cartId,
					cartNum: item.cartNum
				};
				modifyLiveCart(data).then(
					res => {
						if (res.code == 200) {
							this.computedMoney();
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
							});
						}
					}
				);
			},
			// 查询购物车列表
			queryLiveCart() {
				queryLiveCartList().then(
					res => {
						if (res.code == 200) {
							this.shopList = res.rows;
							this.shopList.forEach(item => {
								item.checked = item.checked == 1;
							});
							this.updateCheckAllState();
							this.computedMoney();
						} else {
							uni.showToast({
								icon: 'none',
								title: "请求失败",
							});
						}
					}
				);
			},
			// 删除购物车
			delCart() {
				const data = this.shopList
					.filter(item => item.checked)
					.map(item => item.cartId);

				if (data.length === 0) {
					uni.showToast({
						icon: 'none',
						title: "请选择商品删除"
					});
					return;
				}
				delLiveCart(data).then(res => {
					if (res.code === 200) {
						uni.showToast({
							icon: 'success',
							title: "操作成功"
						});
						this.queryLiveCart();
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				});
			},
			// 计算价格
			computedMoney() {
				this.totalMoney = this.shopList.reduce((total, item) => {
					return total + (item.checked ? item.price * item.cartNum : 0);
				}, 0);
			},
			// 更新全选状态
			updateCheckAllState() {
				if (this.shopList.length === 0) {
					this.checkAll = false;
					return;
				}
				this.checkAll = this.shopList.every(item => item.checked);
			},
			// 全选
			handleCheckAll() {
				this.checkAll = !this.checkAll;
				this.shopList.forEach(item => {
					item.checked = this.checkAll;
					this.modifyLiveCart(item);
				});
				this.computedMoney();
			},
			//选择
			checkChange(item) {
				item.checked = !item.checked;
				this.updateCheckAllState();
				this.computedMoney();
				this.modifyLiveCart(item);
			},
			// 改数量
			changeNum(e, item) {
				item.cartNum = e.detail.value.replace(/\D/g, '')
				if (item.cartNum <= 1) {
					uni.showToast({
						title: "已经是底线啦!",
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (item.cartNum < 1) {
					item.cartNum = 1
				}
				if (item.cartNum >= item.stock) {
					item.cartNum = item.stock;
				}
				this.modifyLiveCart(item)
			},
			// 购物车减法
			delNum(item) {
				if (item.cartNum <= 1) {
					uni.showToast({
						title: "已经是底线啦!",
						icon: "none",
						duration: 2000
					});
					return;
				}
				item.cartNum--
				if (item.cartNum < 1) {
					item.cartNum = 1
				}
				this.modifyLiveCart(item)
			},
			// 购物车加法
			addNum(item) {
				item.cartNum++
				if (item.cartNum >= item.stock) {
					item.cartNum = item.stock;
				}
				this.modifyLiveCart(item)
			},
			// 结算
			submit() {
				const selectedItems = this.shopList.filter(item => item.checked);

				if (selectedItems.length === 0) {
					uni.showToast({
						icon: 'none',
						title: "请选择商品"
					});
					return;
				}
				this.getKey()
			},
			showProduct(item) {
				uni.navigateTo({
					url: './goods?productId=' + item.productId
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.content {
		height: 100%;
		padding: 20upx;

		.shopbox {
			background: #FFFFFF;
			border-radius: 16rpx;
		}

		.no-data-box {
			display: flex;
			flex-direction: column;
			align-items: center;
		}

		.goods-list {
			.item {
				box-sizing: border-box;
				height: 221upx;
				background: #FFFFFF;
				border-radius: 16upx;
				margin-bottom: 20upx;
				padding: 30upx;
				display: flex;
				align-items: center;

				&:last-child {
					margin-bottom: 0;
				}

				.goods-img {
					width: 160upx;
					height: 160upx;
					background: #FFFFFF;
					margin-right: 30upx;
					flex-shrink: 0;
				}

				.info-box {
					height: 160upx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					width: calc(100% - 255upx);

					.title-box {
						width: 100%;
						display: flex;
						align-items: center;

						.title {
							flex: 1;
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 1;
						}
					}

					.price-num {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.price {
							display: flex;
							align-items: flex-end;

							.unit {
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #FF6633;
								line-height: 1.2;
								margin-right: 4upx;
							}

							.text {
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #FF6633;
								line-height: 1;
							}
						}

						.num-box {
							display: flex;
							align-items: center;

							.img-box {
								width: 60upx;
								height: 60upx;
								border: 1px solid #dddddd;
								display: flex;
								align-items: center;
								justify-content: center;

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

							input {
								width: 60upx;
								height: 60upx;
								line-height: 60upx;
								font-size: 28upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								border-top: 1px solid #dddddd;
								border-bottom: 1px solid #dddddd;
								text-align: center;
							}
						}
					}
				}
			}
		}

		.btn-foot {
			box-sizing: border-box;
			width: 100%;
			height: 121upx;
			background: #FFFFFF;
			padding: 16upx 30upx 16upx 60upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 99;

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

				.text {
					margin-left: 14upx;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 1;
				}
			}

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

				.total {
					display: flex;
					align-items: flex-end;
					margin-right: 36upx;

					.label {
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1.5;
					}

					.price {
						display: flex;
						align-items: flex-end;

						.unit {
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1.2;
							margin-right: 10upx;
						}

						.num {
							font-size: 30upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1;
						}
					}
				}

				.btn {
					width: 200upx;
					height: 88upx;
					line-height: 88upx;
					text-align: center;
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					background: #0bb3f2;
					border-radius: 44upx;
				}
			}
		}
	}
</style>