<template>
	<view catchtouchmove="return">
		<u-popup :show="showShopCar && goods.length" mode="bottom" :round="5" :overlay="true"
			:closeOnClickOverlay="true" @close="closeT" zIndex="10071">
			<view class="shop-car" :style="{'padding-bottom':Utils.getPx(bottomPadding)+'px'}">
				<view class="header">
					<text class="left">购物袋</text>
					<text class="right" @click="clearCar"><text class="delete-icon"></text>清空购物车</text>
				</view>
					<scroll-view scroll-y="true" class="scroll-Y">
						<view class="container">
							<view>
								<view class="empty" v-if="show == false">
									<image :src="imgPath" mode="widthFix" style="width: 400rpx;"></image>
									<view class="empty-text">空空如也的购物</view>
									<view class="empty-button" @click="goshopping">去选购</view>
								</view>
								<view v-if="show == true">
									<view class="goods-detail" v-for="(item, index) in goods" :key="index">
										<view class="detail-left">
											<view class="goods-left">
												<view class="checkBox">
													<view class="check"  @click="selected(item)" :class="item.flag?'checked':'nochecked'"></view>
												</view>
												<!-- <u-checkbox-group @change="selected(item)">
													<label>
														<u-checkbox shape="circle" class="selected" color="#555555"
															:checked="item.flag" />
													</label>
												</u-checkbox-group> -->
												<view>
													<image :src="item.pics.thumbnailApplet || item.pics.thumbnail"
													style="width: 120rpx;height: 120rpx;margin-top: 4.24rpx"></image>
												</view>
											</view>
											<view class="size">
												<view class="goods-name">{{ item.name }}</view>
												<view class="goods-skus">
													<text v-for="(rl, index) in item.sku.rules" :key="rl.ruleName">
														{{ rl.ruleName }}
														<text v-if="index != item.sku.rules.length - 1">/</text>
													</text>
												</view>
												<view class="goods-price">
													<text class="price-discount"><text class="moneyLog">￥</text>{{ Utils.isInteger(item.sku.discount) }}</text>
													<text class="price-x" v-show="item.sku.price!=item.sku.discount">￥</text>
													<text class="price-xx" v-show="item.sku.price!=item.sku.discount">{{ Utils.isInteger(item.sku.price) }}</text>
												</view>
											</view>
										</view>
										<view class="detail-right">
											<view class="subtract" @click="reduce(item, index)"></view>
											<text class="num">{{ item.num }}</text>
											<view class="add" @click="add(item)" ></view>
											<!-- <text class="subtract" @click="reduce(item, index)">-</text> -->
											<!-- <u-icon class="subtract" name="minus-circle" @click="reduce(item, index)" color="#2979ff" size="22"></u-icon> -->
											<!-- <text @click="add(item)" class="add">+</text> -->
											<!-- <u-icon class="add" name="plus-circle-fill" color="#2979ff" @click="add(item)" size="22"></u-icon> -->
										</view>
									</view>
								</view>
							</view>
						</view>
					 </scroll-view>

			</view>

		</u-popup>
		<view class="end" v-if="goods.length" :style="{'bottom':shopCar+'rpx'}">
			<view @click.stop="openShopCar" class="end-left">
				<view style="display:flex">
					<view class="car-img">
						<text class="badge" :animation="animationData" v-if="totalNum" >{{ totalNum }}</text>
					</view>
					<text class="shopClassStyle">￥{{ Utils.isInteger(totalPrice.toFixed(2)) }}</text>
				</view>
			</view>
			<view class="end-right goSubmmit" @click="saveReserve" v-if="userms">
				结算
			</view>
			<!-- <view v-if="!userms" class="end-right goSubmmit" style="border-radius: 0;" open-type="getPhoneNumber" @getphonenumber="saveReserve">
				
			</view> -->
			<button v-if="!userms" class="end-right goSubmmit" style="border-radius: 0;" open-type="getPhoneNumber"
				@getphonenumber="saveReserve">结算
			</button>
		</view>
		<view class="shopClose" v-if="!shopState||!isInRange" :style="{'bottom':shopCar+'rpx'}">
			<view class="content">{{!shopState?"门店已休息":"距离较远，门店暂停接单"}}</view>
		</view>
		<show-toast ref="toast"/>
	</view>
</template>

<script>
import { $EventBus } from "../../utils/EventBus";
import Menu from '@/request/menu';
import User from '@/request/user';
import Utils from '@/utils/utils'
export default {
	props:['shopState','isInRange'],
	data() {
		return {
			showShopCar: false,//是否弹出购物车列表
			show: true,
			allchecked: true,
			// checked:true,
			goods: [],//购物车商品信息
			loginInfo: "",
			imgPath: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F01%2F82%2F40%2F596fa6dc00bb4_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633499781&t=d37222e32213957ddbdd01d62e071309',
			Utils,
			shopCar:0,
			BottomSafeHeight:0,
			bottomPadding:0,
			animation:'',
            animationData: {},
			timer:0
		}
	},
	created() {
		$EventBus.$off('updateCar');
	},
	mounted() {
		this.animation = uni.createAnimation()
		this.BottomSafeHeight = uni.getStorageSync('BottomSafeHeight')||50
		console.log(this.BottomSafeHeight,'this.BottomSafeHeight')
		this.bottomPadding = 204
		
		this.shopCar = uni.getStorageSync('shopCar')+5
		if(!this.userms){
			User.wxLoginAndGetOpenid(true).then(loginInfo=>{
				this.loginInfo = loginInfo
			})
		}
		this.goods = uni.getStorageSync('shopCarInfo') || [];
		$EventBus.$on('updateCar', () => {
			this.goods = uni.getStorageSync('shopCarInfo') || [];
		});
	},
	
	methods: {
		// 切换购物车列表显示隐藏
		openShopCar() {
			this.$nextTick(() => {
				this.showShopCar = !this.showShopCar;
			})
		},

		// 翻转动画
		turnAnimation(val) {
			if(this.goods.length>0){
				this.$nextTick(()=>{
					this.animation.opacity(0.5).step({duration:100}).rotateY(90).opacity(1).step({duration:200}).rotateY(30).step({duration:50}).rotateY(-60).step({duration:100}).rotateY(0).step({duration:50})
					this.animationData = this.animation.export()
					this.timer =  setTimeout(() => {
						this.animation.rotateY(0).step({duration:100})
						this.animationData = this.animation.export()
						clearTimeout(this.timer)
					}, 550);
				})
			}

		},

		closeT(e) {
			this.showShopCar = false;
		},
		// 清空购物车
		clearCar() {
			uni.removeStorageSync('shopCarInfo');
			$EventBus.$emit('updateCar');
		},
		// 购物车为空时点击去购物，收起购物袋
		goshopping() {
			this.showShopCar = false;
		},
		//跳转到结算
		saveReserve(e) {
			const settleMentFunc = ()=>{
				if(this.totalPrice < 0 || this.totalNum <= 0) {
					this.showToast({ title: '请选择商品', icon: 'error' })
					return;
				}
				let Authorization = uni.getStorageSync('Authorization');
				let shopCarInfo = uni.getStorageSync('shopCarInfo').filter(v => v.flag == true);
				if (shopCarInfo) {
					if (Authorization) {
						uni.navigateTo({
							url: `/orderSubPackage/pages/settlement/index?buyType=1`
						})
					} else {
						this.loginByPhoneNumber(e)
					}
				}
				return;
			}
			uni.authorize({
				scope: 'scope.userLocation',
				success: () => { //1.1 允许授权
					console.log("允许授权位置");
					settleMentFunc()
				},
				fail: (err) => { //1.2 拒绝授权
					wx.exitMiniProgram({ success: (res) => { } })
					console.log("获取位置失败")
						uni.showModal({
						    content: '检测到您没打开地理位置权限，是否去设置打开？',
							confirmText: "确认",
							cancelText: '取消',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting({//opensetting是调起设置页面的
										success: (res) => {
											console.log(res)
												if(res.authSetting['scope.userLocation'] == true){//判断res.authsetting的值是true还是false
													settleMentFunc()
												}else{
													// console.log("什么也不做");
													}	
												}
											})
									} else {
										console.log('取消');
										return false;
									}
								}
							})
				}
			})
			return

		},
		// 付款前未登录发起授权
		loginByPhoneNumber(e) {
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				e.loginInfo = this.loginInfo
				User.getPhoneNumber(e);
			} else if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
				this.showToast({ title: '已拒绝手机号授权', icon: 'error' })
			}
		},
		change(e) { },
		selected(item) {
			item.flag = !item.flag
			if (!item.flag) {
				this.allchecked = false
			} else {
				const a = this.goods.filter((item) => {
					return item.flag == true
				})
				if (a) {
					this.allchecked = true
				} else {
					this.allchecked = false
				}
			}
			uni.setStorageSync('shopCarInfo', this.goods);
		},
		selectgoods() {
			this.allchecked = !this.allchecked
			if (this.allchecked) {
				this.goods.map(item => {
					item.flag = true
				})
			} else {
				this.goods.map(item => {
					item.flag = false
				})
			}
			uni.setStorageSync('shopCarInfo', this.goods);
		},
		reduce(item, index) {
			item.num -= 1
			if (item.num == 0) {
				this.goods.splice(index, 1);
			}
			uni.setStorageSync('shopCarInfo', this.goods);
			$EventBus.$emit('updateCar');
		},
		add(item) {
			let s = 0;
			this.goods.forEach(function (val) {
				s += Number(val.num);
			}, 0);
			let countOfOrder = uni.getStorageSync('countOfOrder');
			console.log(s , countOfOrder)
			if (s >= countOfOrder) {
				this.showToast({
					title: '最多可一次购买'+countOfOrder+'杯',
					icon: 'none'
				});
				return;
			}
			let num = Number(item.num)
			item.num = num + 1
			uni.setStorageSync('shopCarInfo', this.goods);
			$EventBus.$emit('updateCar');
		}
	},
	watch: {
		totalNum(val){
			this.turnAnimation(val)
		}
	},
	computed: {
		userms() {
			return this.$store.getters.Authorization;
		},
		totalNum() {
			let totalNum = 0;
			this.goods.map(item => {
				item.flag ? totalNum += parseInt(item.num) : totalNum += 0
			})
			return totalNum
		},

		totalPrice() {
			let totalPrice = 0;
			this.goods.forEach(item => {
				const sku = item.sku;
				item.flag ? totalPrice += item.num * sku.discount : totalPrice += 0
			})
			return totalPrice
		}
	}
}

</script>

<style lang="scss">
.shopClassStyle {
	color: #000000;
	font-weight: bold;
	font-size: 32rpx;
	font-weight: 500;
	font-family: Futura-Medium, Futura;
	display: flex;
	margin-left: 32rpx;
	letter-spacing: 1rpx;
	align-items: center;
}

.shop-car {
	display: flex;
	flex-direction: column;

	.header {
		padding:34rpx 31.78rpx 34rpx 31.54rpx;
		display: flex;
		border-bottom: 0.1px solid #F4F4F4;

		.left {
			flex: 1;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
		}

		.right {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			font-size: 20rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;

			.delete-icon {
				width: 18rpx;
				height: 18rpx;
				background: url(../../static/imgs/icon-delete.png) center center no-repeat;
				background-size: cover;
				display: inline-block;
				margin-right: 6rpx;
			}
		}
	}

	.scroll-Y {
		max-height: 750rpx;
		// margin-bottom: 100rpx;
		.container {
			flex: 1;
		}
	}


}

.goods {
	line-height: 80rpx;
	background-color: #FFFFFF;

	&-detail {
		display: flex;
		padding: 30rpx 25.87rpx 30rpx 32rpx;
		background-color: #fff;
		justify-content: space-between;
		align-items: center;

		.detail-left {
			display: flex;
			height: 128rpx;
			align-items: center;
			justify-content: space-between;

			.goods-left {
				display: flex;
				align-items: center;
				.checkBox{
					margin-right: 32rpx;
					width: 40rpx;
					display: flex;
					margin-top: 6rpx;
					align-items: center;
					height: 100%;

					.check{
						width: 100%;
						height: 40rpx;
					}

					.checked{
						background: url('../../static/imgs/duigouxuanzhong.png') center center no-repeat;
						background-size:100% 100%;
					}
					.nochecked{
						background: url('../../static/imgs/weixuanzhong.png') center center no-repeat;
						background-size:100% 100%;
					}
				}
			}
		}

		.size {
			margin-left: 30rpx;
			.goods-name{
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				padding-top: 5rpx;
				font-weight: 500;
				color: #333333;
				line-height: 40rpx;
			}

			.goods-skus {
				margin-top: 4rpx;
				height: 22rpx;
				font-size: 20rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 26rpx;
			}
			
			.goods-price {
				margin-top: 28rpx;

				.moneyLog {
					height: 38rpx;
					font-size: 24rpx;
					font-family: Futura-Medium, Futura;
					font-weight: 500;
					color: #333333;
					line-height: 30rpx;
				}

				.price-discount{
					height: 38rpx;
					font-size: 28rpx;
					font-family: Futura-Medium, Futura;
					font-weight: 500;
					color: #333333;
					line-height: 30rpx;
				}
				.price-x {
					margin-left: 4.2rpx;
					font-size: 20rpx;
					font-family: ArialMT;
					line-height: 22rpx;
					color: #999999;
				}

				.price-xx {
					text-decoration: line-through;
					height: 22rpx;
					font-size: 20rpx;
					font-family: ArialMT;
					color: #999999;
					line-height: 22px;
				}
				}


		}

		.detail-right {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin-top: 6rpx;
			// text {
			// 	width: 40rpx;
			// 	line-height: 40rpx;
			// 	text-align: center;
			// 	display: inline-block;
			// 	margin-right: 10rpx;
			// 	color: #000000;
			// }
			.subtract {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jianhao.png') center center no-repeat;
				background-size:100% 100%;
				
			}
			.num {
				font-size: 32rpx;
				font-family: ArialMT;
				color: #000000;
				line-height: 28rpx;
				margin-left: 26rpx;
				margin-right: 22rpx;
			}
			.add {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jiahao.png') center center no-repeat;
				background-size:100% 100%;
			}
		}
	}
}

.empty {

	position: relative;
	top: 220rpx;
	text-align: center;
	display: flex;
	align-items: center;
	flex-direction: column;

	&-text {
		color: #808080;
		margin-bottom: 50rpx;
	}

	&-button {
		width: 300rpx;
		height: 90rpx;
		color: orange;
		border: 1rpx solid orange;
		text-align: center;
		line-height: 90rpx;
		border-radius: 48rpx;
	}
}

.end {
	width: 100%;
	height: 100rpx;
	background-color: rgb(253, 253, 253);
	position: fixed;
	left: 0;
	box-shadow: 0px -4px 8px 0px rgba(102, 102, 102, 0.1);
	display: flex;
	z-index: 10072;
	align-items: center;

	&-left {
		width: 70%;
		display: flex;
		justify-content: space-between;
		padding: 0 32rpx;

		.end-flex {
			display: flex;
			align-items: center;
		}

		.car-img {
			width: 45rpx;
			height: 54rpx;
			display: inline-block;
			background: url('../../static/imgs/gouwudai.png') center center no-repeat;
			background-size: cover;
			position: relative;

			.badge {
				position: absolute;
				height: 32rpx;
				font-family: Futura-Medium, Futura;
				background: #FF63BA;
				right: -22rpx;
				top: -8rpx;
				min-width: 32rpx;
				font-weight: 500;
				font-size: 20rpx;
				font-weight: normal;
				color: #FFFFFF;
				line-height: 32rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50%;

			}
		}
	}

	&-right {
		width: 169rpx;
		font-size: 32rpx;
		line-height: 100rpx;
		background-color: #0050F6;
		text-align: right;
		color: #fff;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
	}
	.goSubmmit{
		// background: url('../../static/imgs/fukuan.png') center center no-repeat;
		text-align: center;
		font-size: 28rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 100rpx;
		width: 200rpx;
		height: 100%;
		margin-top: -1rpx;
	}
}

.shopClose {
	width: 100%;
	height: 100rpx;
	background: rgba(0,83,255,0.7);
	backdrop-filter: blur(10px);
	position: fixed;
	left: 0;
	display: flex;
	z-index: 10072;
	align-items: center;
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 500;
	color: #FFFFFF;
	text-align: center;
	line-height: 100rpx;

	.content {
		width: 100%;
		text-align: center;
	}
}
</style>
