<template>
	<view>
		<!-- 非H5撑高元素 -->
		<!-- <view class="status_bar"></view> -->
		<view class="box">
			<view class="box-top">
				<view class="box-top-action">
					<!-- <view class="box-top-action-item">
						<view class="flex box-top-action-item-cir">
							<u-icon name="scan" color="#fff" size="45"></u-icon>
						</view>
						<view class="box-top-action-item-txt">扫一扫</view>
					</view> -->
					<view class="box-top-action-item" @click="toPrize">
						<view class="flex box-top-action-item-cir">
							<u-icon name="gift" color="#fff" size="45"></u-icon>
						</view>
						<view class="box-top-action-item-txt">奖品库</view>
					</view>
				</view>
				<view class="box-top-tip" @click="toRule">
					<view class="flex box-top-tip-txt">
						<u-icon name="file-text" color="#5F5F5F" size="20"></u-icon>
						<text>规则说明</text>
					</view>
				</view>
			</view>
			<view class="box-ticket">
				<carousel :img-list="imgList" url-key="picUrl" @selected="selectedBanner" @changeTicket="getTicket" />
			</view>
			<view class="box-ticket-tip">超值保底100%中奖</view>
			<view class="box-start flex">
				<view class="flex box-start-action" @click="exchange">
					<text>立即</text>
					<text>开刮</text>
				</view>
				<view class="box-start-all" @click="toTicket">
					<view class="flex icon">
						<image class="icon-image" src="../../static/icon/index_all.png" mode="aspectFill"></image>
					</view>
					<view class="title">更多盲票</view>
				</view>
			</view>
		</view>
		<custom-tab-bar :activeValue="'index'" />

		<pay-popup :pay-show="payShow" :pay-info="payInfo" @close="close" @success="toProcess" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import CustomTabBar from '../../components/custom-tab-bar/custom-tab-bar.vue'
	import Carousel from '../../components/vear-carousel/vear-carousel'
	import PayPopup from '../../components/pay-popup/pay-popup.vue'
	export default {
		components: {
			CustomTabBar,
			Carousel,
			PayPopup
		},
		data() {
			return {
				imgList: [],
				value: 1,
				payShow: false,
				checked: false,

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

				currentIndex: 0,

				payInfo: {}
			};
		},
		onShow(opthios) {
			this.getList()
		},
		methods: {
			getList() {
				uni.showLoading({
					title: '加载中'
				});
				let data = {
					categoryId: '',
					tagId: '',
					type: 'online',
					noToken: true
				}
				$http.post(`/api/v1/mp/user/mall/ticket/list?pageNum=${this.pageNum}&pageSize=100`, data).then(
					res => {
						uni.hideLoading();
						if (res.code == 0) {
							res.rows.forEach(item => {
								let picUrlArr = item.picUrl.split(',')
								item.picUrl = env.filePublic + picUrlArr[0]
							})
							this.total = res.total
							this.imgList = res.rows
						}
					}).catch(() => {
					uni.hideLoading();
				})
			},

			selectedBanner(item, index) {
				uni.navigateTo({
					url: `/pages/ticketBox/detail?id=${ item.boxId }`
				})
			},

			getTicket(index) {
				this.currentIndex = index
			},

			close() {
				this.payShow = false
			},

			toProcess(id) {
				this.payShow = false
				uni.navigateTo({
					url: `/pages/process/index?id=${ id }`
				})
			},

			exchange() {
				this.payInfo = this.imgList[this.currentIndex]
				let data = {
					couponIds: [],
					autoCoupon: 1,
					boxId: this.payInfo.boxId,
					orderNum: 1
				}
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					if (res.code == 0) {
						let info = {
							...res.data,
							...this.payInfo
						}
						this.payInfo = info
						this.payShow = true
					}
				}).catch(() => {
					uni.$u.toast('开刮失败，请重试！');
				})
			},

			toTicket() {
				uni.navigateTo({
					url: '/pages/ticketBox/index'
				})
			},

			toPrize() {
				uni.navigateTo({
					url: '/pages/prize/index'
				})
			},
			
			toRule(){
				uni.navigateTo({
					url: '/pages/about/rule'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.status_bar {
		width: 100%;
		height: var(--status-bar-height);
	}

	/deep/ .u-tabbar__placeholder {
		display: none;
	}
</style>

<style lang="scss" scoped>
	.box {
		width: 100%;
		min-height: calc(100vh - 50px);
		background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/background.png) center center;

		&-top {
			display: flex;
			justify-content: space-between;
			padding: 100rpx 40rpx 0;

			&-action {
				display: flex;

				&-item {
					margin-right: 76rpx;

					&-cir {
						width: 104rpx;
						height: 104rpx;
						background-color: #FE5924;
						border-radius: 50%;
						margin-bottom: 14rpx;
					}

					&-txt {
						text-align: center;
						color: #5F5F5F;
					}
				}
			}

			&-tip {
				display: flex;
				flex-direction: column;
				align-items: flex-end;
				justify-content: flex-end;
				color: #5F5F5F;
			}
		}

		&-ticket {
			margin: 100rpx 0 0;
			height: 580rpx;
		}

		&-ticket-tip {
			margin-top: 20rpx;
			line-height: 40rpx;
			text-align: center;
			font-size: 40rpx;
			color: #FE5924;
		}

		&-start {
			align-items: flex-end;
			position: relative;
			margin-top: 60rpx;

			&-action {
				flex-direction: column;
				width: 220rpx;
				height: 220rpx;
				font-size: 36rpx;
				border-radius: 50%;
				background-color: #FE5924;
				border: 4rpx solid #FFFFFF;

				text {
					display: inline-block;
					padding-left: 8rpx;
					text-align: center;
					color: #FFFFFF;
					line-height: 50rpx;
					letter-spacing: 8rpx;
				}
			}

			&-all {
				position: absolute;
				right: 70rpx;

				.icon{
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					background-color: #FE5924;
					
					.icon-image{
						width: 80rpx;
						height: 80rpx;
					}
				}
				
				.title{
					margin-top: 8rpx;
					font-size: 34rpx;
					color: #EA350A;
					text-align: center;
				}
			}
		}
	}
</style>
