<template>
	<view>
		<view v-if="pagesNum > 1">
			<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的奖品库"></u-navbar>
		</view>
		<view v-else>
			<u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" title="我的奖品库">
				<view class="nav-left flex" slot="left" @click="toIndex">
					<u-icon name="arrow-left" size="20" color="#333"></u-icon>
				</view>
			</u-navbar>
		</view>

		<!-- 奖品选择 -->
		<view class="prize-state">
			<!-- 类型 -->
			<view class="prize-state-tabs">
				<u-tabs @change="changeTab" :scrollable="false" :list="stateArr" lineWidth="30" lineHeight="3"
					:current='currentIndex' lineColor="#F9822C " :activeStyle="{
										color: '#333 ',
										transform: 'scale(1.1)',
										width: '120px'
									}" :inactiveStyle="{
										color: '#999',
										transform: 'scale(1)',
										width: '120px'
									}" itemStyle="padding-left: 20px; padding-right: 15px; height: 44px; text-align: center;">
				</u-tabs>
			</view>
			<!-- 商品状态 -->
			<view class="prize-state-type flex" v-if="state == 0">
				<view class="prize-state-type-item" :class="{ 'prize-state-type-item__action': index == typeIndex }"
					v-for="(item, index) in typeList" :key="index" @click="typeIndex = index">{{ item.name }}</view>
			</view>
		</view>
		<!-- 实物商品 -->
		<view class="prize-goods" v-if="state == 0">
			<view class="prize-goods-list">
				<view class="prize-goods-list-item" v-for="(item, index) in list" :key="index">
					<!-- 商品 -->
					<view class="prize-goods-list-item__content flex">
						<view class="flex">
							<u-checkbox-group>
								<u-checkbox size="24" :value="item.checked" shape="circle" :checked="item.checked"
									activeColor="#F9822C" @change="changeChecked($event, item)"></u-checkbox>
							</u-checkbox-group>
						</view>
						<view class="info flex" @click="toPrizeGoods(item)">
							<image :src="item.picUrl" mode="aspectFill"></image>
							<view class="desc flex">
								<view class="ells title">{{ item.title }}</view>
								<view class="num flex">
									<view class="sku ells-one">规格：{{ item.properties || '-' }}</view>
									<view class="">数量：{{ item.goodsNum }}</view>
								</view>
							</view>
						</view>
					</view>
					<!-- 来源 -->
					<view class="prize-goods-list-item__btn flex">
						<view class="type">来自：{{ item.inType && item.inType.desc }}</view>
						<view class="btn">兑换盲豆</view>
					</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length && !loading">
				<u-empty text="数据为空" mode="order" />
			</view>
		</view>
		<!-- 优惠券 -->
		<view class="prize-coupon" v-else>
			<view class="prize-coupon-list">
				<!-- <navigator :url="`/pages/prize/detail?info=${ JSON.stringify(item) }`" class="flex prize-coupon-list-item" hover-class="navigator-hover" v-for="(item, index) in list" :key="index">
					<image :src="item.picUrl" mode="aspectFill"></image>
					<view class="flex info">
						<view class="flex desc">
							<view class="title">{{ item.title }}</view>
							<view class="txt">使用期限：{{ $parseTime(item.validStart, '{y}.{m}.{d}') }}-{{ $parseTime(item.validEnd, '{y}.{m}.{d}') }}</view>
							<view class="txt">使用范围：{{ item.useAreaDesc }}</view>
						</view>
						<view class="flex btn">
							<view class="amt"><text>￥</text>{{ item.discount / 100 }}</view>
							<view class="action">立即使用</view>
						</view>
					</view>
				</navigator> -->

				<view @click="toPrizeDetail(item)" class="prize-coupon-list-item" hover-class="navigator-hover"
					v-for="(item, index) in list" :key="index">
					<image src="../../static/icon/coupon_bg.png" mode=""></image>
					<view class="info">
						<view class="info-item">
							<text>使用期限：</text>
							<text>{{ $parseTime(item.validStart, '{y}.{m}.{d}') }}-{{ $parseTime(item.validEnd, '{y}.{m}.{d}') }}</text>
						</view>
						<view class="info-item">
							<text>使用范围：</text>
							<text>{{ item.useAreaDesc || '-' }}</text>
						</view>
					</view>
					<view class="info-now">
						<view></view>
						<view v-if="JSON.parse(item.useArea).value != '4'">立即使用</view>
						<view v-else>查看详情</view>
					</view>
					<view class="flex price-title">
						<view class="price"><text>￥</text>{{ item.discount / 100 }}</view>
						<view class="title">{{ item.title }}</view>
					</view>
				</view>

			</view>
			<view class="flex empty" v-if="!list.length && !loading">
				<u-empty text="数据为空" mode="order" />
			</view>
		</view>

		<view class="prize-action">
			<!-- 实物商品提货 -->
			<view class="flex prize-action-goods" v-if="state == 0">
				<view class="flex checkbox">
					<view class="all">全选</view>
					<u-checkbox-group>
						<u-checkbox v-model="checkedAll" shape="circle" size="24" :checked="checkedAll"
							activeColor="#F9822C" @change="changeCheckedAll($event)"></u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="btn" @click="toSettlement">立即提货</view>
			</view>
			<!-- 优惠券使用记录 -->
			<view class="flex prize-action-coupon" @click="toCoupon" v-else>
				<view class="title">优惠券使用记录</view>
				<u-icon name="arrow-right" size="15" color="#333"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				loading: false,
				stateArr: [{
					name: ' 实物商品',
				}, {
					name: ' 优惠券',
				}],
				typeList: [{
					name: '待兑换'
				}, {
					name: '已兑换'
				}],
				typeIndex: 0,
				state: 0,
				checkedAll: false,

				pageNum: 1,
				total: 0,
				list: [],

				currentIndex: 0,
				pagesNum: "",
			};
		},
		onShow() {
			this.pagesNum = getCurrentPages().length
			this.pageList()
			this.couponNum()
		},
		onLoad(opthios) {

			if (opthios != undefined) {
				if (opthios.coupon == 1) {
					this.currentIndex = 1
					this.state = 1
				} else {
					this.currentIndex = 0
					this.state = 0
				}
			}
		},
		methods: {
			getList() {
				let _this = this
				let url = _this.state == 0 ? '/api/v1/mp/user/mine/prize/list' : '/api/v1/mp/user/mine/coupon/list'
				let data = _this.state == 0 ? {} : {
					status: 1
				}
				uni.showLoading({
					title: '加载中'
				});
				this.loading = true
				this.checkedAll = false
				$http.post(`${ url }?pageNum=${_this.pageNum}&pageSize=20`, data).then(res => {
					uni.hideLoading();
					this.loading = false
					if (res.code == 0) {
						res.rows.forEach(item => {
							let picUrlArr = item.picUrl.split(',')
							item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/170'
							if (_this.state == 0) {
								item.checked = false
								item.inType = JSON.parse(item.inType)
							}
						})
						_this.total = res.total
						_this.list = _this.list.concat(res.rows)
					}
				}).catch(() => {
					uni.hideLoading();
					this.loading = false
				})
			},

			toIndex() {
				uni.switchTab({
					url: '/pages/user/index'
				})
			},

			pageList() {
				this.pageNum = 1
				this.list = []
				this.getList()
			},

			// 切换奖品
			changeTab(e) {
				if (e.index == 0) {
					this.state = 0
				} else if (e.index == 1) {
					this.state = 1
				}
				this.pageList()
				this.checkedAll = false
			},

			changeChecked(e, item) {
				this.$set(item, 'checked', e)
				let flag = this.list.every(item => item.checked == true)
				this.checkedAll = flag
				this.$forceUpdate()
			},

			changeCheckedAll(e) {
				this.checkedAll = e
				this.list.forEach(item => {
					item.checked = e
				})
				this.$forceUpdate()
			},


			// 查看优惠券使用记录
			toCoupon() {
				uni.navigateTo({
					url: '/packagePrize/coupon/use'
				})
			},

			// 立即提货
			toSettlement() {
				let arr = []
				this.list.forEach(item => {
					if (item.checked) {
						arr.push(item)
					}
				})
				if (!arr.length) {
					uni.$u.toast('请选择商品');
					return
				}
				uni.showLoading({
					title: '提货中'
				});
				let ids = JSON.stringify(arr.map(item => item.storageId))
				uni.hideLoading();
				uni.navigateTo({
					url: `/packageGoods/order/settlement?ids=${ ids }`
				})
			},

			couponNum() {
				$http.post(`/api/v1/mp/user/mine/prize/list?pageNum=1&pageSize=10`, {

				}).then(res => {
					$http.post(`/api/v1/mp/user/mine/coupon/list?pageNum=1&pageSize=10`, {
						status: 1
					}).then(data => {
						if (res.code == 0) {
							this.stateArr = [{
									name: ' 实物商品 (' + res.total + ') '
								},
								{
									name: ' 优惠券 (' + data.total + ') '
								},
							]
						} else {
							this.stateArr = [{
									name: ' 实物商品 (0)'
								},
								{
									name: ' 优惠券 (0)'
								},
							]
						}
					})
				});
			},

			toPrizeGoods(item) {
				uni.navigateTo({
					url: `/packagePrize/goods/detail?id=${ item.goodsId }`
				})
			},

			toPrizeDetail(item) {
				let data = JSON.parse(item.useArea)
				let type = JSON.parse(item.type)
				// 1判断type,如果为门店直接跳转不需要判断useArec
				if (type.value == 2) {
					// data.value 2为指定优惠券 0为通用优惠券
					uni.navigateTo({
						url: `/packagePrize/coupon/detail?id=${ item.id }`
					})
				} else {
					// 3为线上票使用
					if (data.value == 3) {
						uni.navigateTo({
							url: `/packageGoods/ticket/index`
						})
					}

					//0为通用优惠券
					if (data.value == 0) {
						uni.navigateTo({
							url: `/packageGoods/ticket/index`
						})
					}

					// 4为线下票使用
					if (data.value == 4) {
						uni.navigateTo({
							url: `/packagePrize/coupon/detail?id=${ item.id }`
						})
					}
				}
			}
		},

		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 20) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('已经到底了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.empty {
		height: 60vh;
	}

	.prize-state {
		position: fixed;
		background-color: #FFFFFF;
		width: 100%;
		z-index: 10;
		box-shadow: 0 2rpx 2rpx #ececec;

		// 类型
		&-tabs {
			display: flex;
			box-shadow: 0 5rpx 5rpx #ececec;
		}

		// 商品状态
		&-type {
			justify-content: flex-start;
			padding: 24rpx 34rpx;

			&-item {
				margin-right: 34rpx;
				padding: 12rpx 36rpx;
				border-radius: 24rpx;
				color: #FFAB4D;
			}

			&-item__action {
				background: #FFAB4D;
				color: #fff;
			}
		}
	}

	// 实物商品
	.prize-goods {
		margin-top: 180rpx;
		padding: 34rpx 34rpx 200rpx;

		&-list {

			&-item {
				padding: 34rpx 22rpx 34rpx;
				background-color: #fff;
				border-radius: 18rpx;
				margin-bottom: 30rpx;
				
				&__content {
					justify-content: space-between;
					padding-bottom: 22rpx;
					border-bottom: 1px solid #E5E5E5;
					
					.info {
						padding-left: 16rpx;
						flex: 1;
						justify-content: flex-start;
						
						image {
							width: 200rpx;
							height: 200rpx;
							border-radius: 12rpx;
						}
						
						.desc {
							height: 200rpx;
							padding-left: 22rpx;
							flex: 1;
							font-size: 30rpx;
							flex-direction: column;
							align-items: flex-start;
							justify-content: space-between;
							
							.title {
								line-height: 40rpx;
								font-weight: bold;
							}
							
							.num {
								width: 100%;
								color: #666666;
								font-size: 26rpx;
								justify-content: space-between;
								
								.sku {
									flex: 1;
								}
							}
						}
					}
				}
				
				&__btn {
					justify-content: space-between;
					padding-top: 22rpx;
					
					.type {
						font-size: 30rpx;
						color: #999999;
						line-height: 30rpx;
						padding-left: 70rpx;
					}
					
					.btn {
						width: 200rpx;
						height: 66rpx;
						line-height: 66rpx;
						text-align: center;
						color: #fff;
						background: #F9822C;
						border-radius: 34rpx;
					}
				}
			}

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

	.prize-coupon {
		margin-top: 85rpx;
		padding: 40rpx 20rpx 150rpx;

		&-list {

			&-item {
				position: relative;
				background-color: #FFFFFF;
				margin-bottom: 20rpx;
				border-radius: 10rpx;

				image {
					width: 100%;
					height: 156rpx;
					margin-bottom: 24rpx;
				}

				.info {
					padding-left: 40rpx;
					padding-bottom: 8rpx;

					.info-item {
						line-height: 40rpx;
						color: #333333;
						margin-bottom: 12rpx;
					}
				}

				.info-now {
					margin-top: 10rpx;
					color: #848484;
					font-size: 30rpx;
					height: 100rpx;
					line-height: 90rpx;
					text-align: center;
				}

				.info-now view {
					height: 2rpx;
					margin: 0 10rpx;
					background-image: linear-gradient(to right, #cfcfcf 0%, #cfcfcf 50%, transparent 1%);
					background-size: 25rpx 10rpx;
				}

				.price-title {
					justify-content: flex-start;
					position: absolute;
					top: 0;
					width: 100%;
					padding: 36rpx 0 0 40rpx;

					.price {
						color: #FFFFFF;
						font-size: 60rpx;
						margin-right: 20rpx;

						text {
							font-size: 40rpx;
						}
					}

					.title {
						color: #FFFFFF;
						font-size: 48rpx;
					}
				}
			}

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

	.prize-action {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 10;
		box-shadow: 0 -4rpx 10rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		width: 100%;

		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		&-goods {
			justify-content: space-between;
			padding: 20rpx 40rpx;

			.all {
				margin-right: 10rpx;
			}

			.btn {
				width: 280rpx;
				height: 90rpx;
				font-size: 36rpx;
				line-height: 90rpx;
				border-radius: 10rpx;
				background-color: rgba(235, 112, 9, 100);
				color: rgba(255, 255, 255, 100);
				text-align: center;
			}
		}

		&-coupon {
			padding: 30rpx 40rpx;

			.title {
				margin-right: 20rpx;
				line-height: 40rpx;
			}
		}
	}
</style>
