<template>
	<view class="content">
		<view class="shoppingCartproductList">
			<view class="productModel" v-for="(item,index) in ShopCarInfo" :key="index">
				<view class="checkModel">
					<image class="init_image" @click="changeselectIdList(item.catKey)"
						v-if="!selectIdList.includes(item.catKey)"
						src="http://amy.yyinhong.cn/uploads/wximg/no_check.png" mode="">
					</image>
					<image class="init_image" @click="changeselectIdList(item.catKey)"
						v-if="selectIdList.includes(item.catKey)" src="http://amy.yyinhong.cn/uploads/wximg/check.png"
						mode="">
					</image>
				</view>
				<view class="productImg">
					<image class="init_image" :src="item.cover_img" mode="">
					</image>
				</view>
				<view class="productInfo">
					<view class="productName">
						{{item.goods_name}}
					</view>
					<view class="productNorms">
						<span>{{item.attr_name}}</span>
					</view>
					<view class="productPrice">
						<view>
							<span>¥</span>{{item.goods_price}}
						</view>
						<u-number-box v-model="item.num" :min="1" :name="item.catKey" @change="goodsNumChange"
							integer></u-number-box>
					</view>
				</view>
			</view>
		</view>

		<view class="settlementModel">
			<view class="checkModel">
				<image class="init_image" @click="allget" v-if="selectIdList.length  != ShopCarInfo.length"
					src="http://amy.yyinhong.cn/uploads/wximg/no_check.png" mode="">
				</image>
				<image class="init_image" @click="allget"
					v-if="(selectIdList.length  == ShopCarInfo.length) && (selectIdList.length  != 0)"
					src="http://amy.yyinhong.cn/uploads/wximg/check.png" mode="">
				</image>
			</view>
			<view class="basebtn" @click="allget">全选</view>
			<view class="basebtn" style="color: #FF3141;" @click="deleteGoods">删除</view>
			<view class="priceModel"><span>合计：￥</span>{{allPrice}}</view>
			<view class="payBtn" @click="goConfirOrder">
				立即支付
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<my-tabbar :currentTab='2' />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectIdList: [],
				ShopCarInfo: [],
				allPrice: 0
			}
		},
		onShow() {
			this.getShopCarInfo()
		},
		methods: {
			goodsNumChange(e) {
				this.$request('/number-shopping-cart', 'POST', {
					cartKey: e.name,
					num: e.value
				}).then(res => {
					console.log('请求成功');
					this.changeCarprice()
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			changeCarprice() {
				let _this = this;
				_this.allPrice = 0;
				for (let i in _this.ShopCarInfo) {
					if (_this.selectIdList.includes(_this.ShopCarInfo[i].catKey)) {

						let addNum = _this.$utils.amend(_this.ShopCarInfo[i].goods_price, _this.ShopCarInfo[i].num, "*");
						_this.allPrice = _this.$utils.amend(_this.allPrice, addNum, "+")
					}
				}
			},
			changeselectIdList(catKey) {
				console.log(catKey)
				let indexToRemove = this.selectIdList.findIndex(item => item === catKey);

				if (indexToRemove !== -1) {
					// 如果存在相同 value 的对象，则从数组中删除
					this.selectIdList.splice(indexToRemove, 1);
				} else {
					// 如果不存在相同 value 的对象，则添加到数组
					this.selectIdList.push(catKey);
				}
				this.changeCarprice()
			},
			allget() {
				if (this.selectIdList.length == this.ShopCarInfo.length) {
					this.selectIdList = [];
					this.changeCarprice()
					return;
				}
				if (this.selectIdList.length >= 0) {
					this.selectIdList = []
					for (let i in this.ShopCarInfo) {
						this.selectIdList.push(this.ShopCarInfo[i].catKey);
					}
				}
				this.changeCarprice()

			},
			getShopCarInfo() {
				this.$request('/show-shopping-cart', 'GET', {}).then(res => {
					console.log('请求成功');
					this.ShopCarInfo = res.data;
				}).catch(err => {
				})
			},
			goConfirOrder() {
				let confirOrderObj = {
					cartKey: this.selectIdList.join(','),
					type: 'shopcar'
				}
				uni.setStorageSync('confirOrderObj', confirOrderObj)
				uni.navigateTo({
					url: '/pageSub-product/pages/confirmOrder/confirmOrder'
				})
			},
			//删除购物车
			deleteGoods() {
				let cartKey = this.selectIdList.join(',');
				if (cartKey.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请先选择商品'
					})
					return
				}
				this.$request('/reduce-shopping-cart', 'POST', {
					cartKey
				}).then(res => {
					console.log('请求成功');
					this.selectIdList = [];
					this.ShopCarInfo = [];
					this.allPrice = 0;
					this.getShopCarInfo();
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: calc(100vh - 166rpx);
		overflow-y: scroll;
		background: linear-gradient(to bottom, #f3d4d7, #f8f8f8, #f8f8f8);
	}

	.settlementModel {
		position: fixed;
		bottom: 130rpx;
		width: calc(100% - 60rpx);
		padding: 0 30rpx;
		height: 100rpx;
		background-color: #ffffff;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

		.basebtn {
			margin-right: 20rpx;
			font-size: 24rpx;
		}

		.priceModel {
			flex: 1;
			text-align: right;
			font-size: 32rpx;
			margin-right: 20rpx;

			span {
				font-size: 22rpx;
			}
		}
	}

	.shoppingCartproductList {
		border-radius: 20rpx;
		background: #ffffff;
		overflow: hidden;
		width: calc(100% - 60rpx);
		margin: 30rpx;
		margin-bottom: 120rpx;
	}

	.productModel {
		width: calc(100% - 60rpx);
		margin: 30rpx;
		background-color: #ffffff;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

	}

	.checkModel {
		height: 36rpx;
		width: 36rpx;
		margin-right: 30rpx;
		border-radius: 36rpx;
	}

	.productImg {
		// background-color: #f3d4d7;
		width: 180rpx;
		height: 180rpx;
		margin-right: 30rpx;
	}

	.productInfo {
		height: 180rpx;
		flex: 1;
	}

	.productName {
		font-size: 30rpx;
		color: #333333;
		line-height: 36rpx;
		display: -webkit-box;
		/* 使用弹性盒模型 */
		-webkit-box-orient: vertical;
		/* 垂直方向的弹性盒 */
		-webkit-line-clamp: 2;
		/* 显示两行 */
		overflow: hidden;
		/* 隐藏超出部分 */
		text-overflow: ellipsis;
		/* 使用省略号 */
	}

	.productNorms {
		line-height: 58rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		color: #999999;
		font-size: 24rpx;
	}

	.productPrice {
		height: 50rpx;
		display: flex;
		flex-direction: row;
		align-items: baseline;
		justify-content: space-between;
		color: #D61A62;
		font-size: 36rpx;

		span {
			font-size: 24rpx;
			margin-right: 10rpx;
		}
	}

	.payBtn {
		background-color: #D3195E;
		height: 70rpx;
		width: 178rpx;
		text-align: center;
		line-height: 70rpx;
		font-size: 26rpx;
		color: #ffffff;
		border-radius: 70rpx;
	}
</style>