<template>
	<view style="background-color: #F3F4F8;">

		<view class="task_bg u-absolute">
			<image class="task_bg" src="@/static/task/task_bg.png"></image>
		</view>
		<view class="icon">
			<image class="task_icon1" src="@/static/task/task_icon1.png"></image>
			<view class="icon_title">新人好礼送不停</view>
			<image class="task_icon2" src="@/static/task/task_icon2.png"></image>
		</view>

		<view class="contentTop ">
			<view>
				<image src="@/static/task/renwubg.png" mode=""></image>
				<view>已连续签到 <text class="num">{{ datas.signDays }}</text> 天</view>
			</view>
			<view>
				<view v-for="item in datas.signInList" :key="item.id"
					:style="item.status == 1 ? 'color:#EFA765' : 'color:#999'">
					<text class="" style="font-size: 28rpx;">
						{{ item.status == 1 ? '已签到' : '待签到' }}
					</text>
					<view :style="item.status == 1 ? 'color:#999' : 'color:#EFA765'">
						{{ item.signDay.substr(5, 8) }}
					</view>
					<image v-if="item.status == 0" src="@/static/task/xing (1).png" mode=""></image>
					<image v-else src="@/static/task/xing (2).png" mode=""> </image>
				</view>
			</view>
		</view>
		<view class="content " v-if="datas.list.length">
			<view class="cell " v-for="(item, index) in datas.list" :key="index">
				<view class="cell_left ">
					<view class="cell_title ">
						<view class=" title" :style="{ alignSelf: item.rewardImg ? 'center' : 'flex-start' }">{{
							item.title
						}}</view>
						<image v-if="item.rewardImg" class="cell_icon" :src="item.rewardImg" mode=""></image>
						<view class="tip">{{ item.rewardDetail }}</view>
					</view>
					<view class="subhead ">{{ item.detail }}</view>
				</view>
				<view class="cell_right ">
					<view v-if="item.disabled" class="btn "
						:style="{ backgroundColor: item.buttonBgColor, color: item.buttonFontColor }"
						@tap="goNav(item)">
						{{ item.type == 1 ? item.buttonTitle : item.number ? `${item.discNumber}/${item.number}` : (
							item.discNumber <= 0 ? item.buttonTitle : `剩余${item.discNumber}次`) }}
					</view>
					<view v-else class="btn  disabled">
						{{ item.type == 1 ? item.buttonTitle : item.number ? `${item.discNumber}/${item.number}`
									: (item.discNumber <= 0 ? item.buttonTitle : `剩余${item.discNumber}次`) }}
						<view v-if="item.buttonUnderContent && item.buttonUnderUrl" class=" tip"
							@tap="goNav(item, item.buttonUnderUrl)">
							{{ item.buttonUnderContent }}
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 激励视频广告 -->
		<!-- <ad-rewarded-video v-if="adRewardedVideo" ref="adRewardedVideo" adpid="1531580352" :loadnext="true"
			v-slot:default="{ loading, error }" :url-callback="datas.urlCallback" @load="onadload" @close="onadclose"
			@error="onaderror">
			<view class="ad-error" v-if="error">{{ error }}</view>
		</ad-rewarded-video> -->
	</view>
</template>

<script setup>
	import {
		reactive,
		getCurrentInstance,
		nextTick
	} from "vue";
	import {
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		getUserSignData,
		selectTaskCenter,
		taskReceive
	} from '@/api/task/index.js'
	const currentInstance = getCurrentInstance()
	let datas = reactive({
		signDays: 0,
		signInList: [],
		list: [],
		// ios审核
		isExamine: uni.getStorageSync('isExamine'),
		urlCallback: {},
		adRewardedVideo: true,

	})
	onShow(() => {
		getTaskdata()
		getsignIn()

		// nextTick(() => {
		// currentInstance.ctx.$refs.adRewardedVideo.load();
		// })
	})
	//广告播放成功回调
	async function onadclose(e) {
		const detail = e.detail
		if (detail && detail.isEnded) {
			// 正常播放结束
			// /sqx_fast/app/ad/state
			let res = await this.$Request.getT('app/ad/state', {
				extraKey: this.urlCallback.extra
			})
			this.$Request.getT('/app/common/type/921').then(res => {
				if (res.code == 0) {
					console.log(res)
					uni.showToast({
						title: '获得' + res.data.value + '分钟免费时长',
						icon: 'none'
					})
				}
			})
		} else {
			// 播放中途退出
		}
	}
	async function goNav(item, url) {
		// jumpType (integer, optional): 跳转类型 1 内部路径 2 外部路径 ,
		// type (integer, optional): 任务类型 1 普通任务 2 打卡任务 9 其它 ,
		if (url) {
			console.log(url, 'debug12')
			let urls = ''
			if (url == '/pages/task/prizeList') {
				urls = url + '?source=2'
			}
			if (url == '/pages/task/receiveMember') {
				urls = url + `?standard=${item.discNumber == null ? true : false}&taskId=${item.id}`
			}
			uni.navigateTo({
				url: urls
			})
		} else {
			if (item.jumpType == 0) {
				if (item.title.indexOf("新人福利") != -1) {
					let res = await taskReceive({
						id: item.id
					})
					if (res.code == 0) {
						uni.showToast({
							title: res.id == 15 ? '签到成功' : '领取成功',
							icon: 'none'
						})
						setTimeout(() => {
							getTaskdata()
							getsignIn()
						}, 1000)
					} else {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				} else if (item.buttonTitle.indexOf("观看视频") != -1) {
					// 首次加载广告 
					datas.urlCallback = {
						userId: uni.getStorageSync('userId'),
						extra: uni.getStorageSync('userId') + "" + new Date().getTime(),
					}
					currentInstance.ctx.$refs.adRewardedVideo.show();
				} else {
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			} else if (item.jumpType == 1) {
				let buttonUrl = item.buttonUrl
				console.log(buttonUrl, 'debug')
				if (item.title && item.title.indexOf('每周打卡奖励') != -1) {
					buttonUrl = item.buttonUrl + '?source=2'
				}
				if (item.title && item.title.indexOf('每月打卡奖励1') != -1) {
					buttonUrl = item.buttonUrl + '?source=3'
				}
				if (item.title && item.title.indexOf('每月打卡奖励2') != -1) {
					buttonUrl =
						`${item.buttonUrl}?standard=${item.discNumber == null ? true : false}&taskId=${item.id}`
				}

				uni.navigateTo({
					url: buttonUrl
				})

			} else if (item.jumpType == 3) {
				uni.switchTab({
					url: item.buttonUrl
				})
			}
		}

	}

	function onadload(e) {
		datas.adRewardedVideo = true;
	}
	// 获得签到
	async function getsignIn() {
		let res = await getUserSignData()
		datas.signInList = res.recordList
		datas.signDays = res.signDays
	}
	// 获取任务列表
	async function getTaskdata() {
		let res = await selectTaskCenter()
		if (!datas.isExamine) {
			let arrData = []
			res.forEach(ele => {
				if (ele.title.indexOf('分享奖励') == -1 && ele.title.indexOf('新人福利') == -1 &&
					ele.title.indexOf('观看视频奖励') == -1) {
					arrData.push(ele)
				}
			})
			datas.list = arrData
		} else {
			datas.list = res
		}
	}
</script>

<style scoped lang="scss">
	.u-absolute {
		position: absolute;
	}

	.task_bg {
		width: 100% !important;
		height: 494rpx !important;
	}

	.icon {
		position: relative;
		padding: 32rpx;

		.icon_title {
			color: #000000;
			margin-left: 28rpx;
			top: -20rpx;
			font-size: 32rpx;
			font-weight: bold;
			position: relative;
		}

		.task_icon1 {
			width: 74rpx !important;
			height: 78rpx !important;
			margin-left: 235rpx;
			position: relative;
		}

		.task_icon2 {
			width: 126rpx !important;
			height: 120rpx !important;
			top: -40rpx;
			position: relative;
		}
	}

	.contentTop {
		border-radius: 16rpx;
		top: -115rpx;
		padding: 20rpx;
		margin-bottom: 32rpx;
		position: relative;
		background-color: #ffffff;
		color: #666666;
		margin: 32rpx;

		>view:first-child {
			>image {
				width: 100%;
				height: 78rpx;
				position: absolute;
				left: 0;
				top: 0;
				z-index: 1
			}

			>view {
				z-index: 9999;
				position: absolute;
				text-align: center;
				width: 98%;

				.num {
					color: #EC6F48;
					margin: 0 10rpx;
				}
			}
		}

		>view:last-child {
			margin-top: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			>view {
				position: relative;

				>view {
					margin-top: 20rpx;
					width: 86rpx;
					height: 94rpx;
					background: linear-gradient(180deg, #FFF7E3 0%, #FFFFFF 100%);
					border-radius: 14rpx 14rpx 0rpx 0rpx;
				}

				>image {
					width: 52rpx;
					height: 48rpx;
					position: absolute;
					top: 110rpx;
					left: 14rpx;
				}
			}
		}
	}

	.content {
		border-radius: 16rpx;
		top: -115rpx;
		padding: 28rpx;
		background-color: #ffffff;
		color: #666666;
		margin-left: 32rpx;
		margin-right: 32rpx;
		position: relative;
		border-radius: 16rpx;
		top: -115rpx;
		padding: 28rpx;

		.cell {
			padding: 32rpx 0;
			border-bottom: 2rpx solid #EBEBEB;
			display: flex;
			justify-content: space-between;

			.cell_left {
				flex-direction: column;
				margin-top: 10rpx;

				.cell_title {
					margin-bottom: 12rpx;
					color: #333;
					display: flex;

					.title {
						display: flex;
						flex-shrink: 0;
						font-size: 28rpx;
						font-weight: bold;
					}

					.cell_icon {
						width: 48rpx !important;
						height: 34rpx !important;
						margin-left: 16rpx;
					}

					.tip {
						margin-left: 22rpx;
						color: #FC5B67;
						font-size: 24rpx;
					}
				}

				.subhead {
					color: #999;
					font-size: 24rpx;
				}
			}

			.cell_right {
				flex-direction: column;
				align-items: center;
				flex-shrink: 0;
				margin-left: 20rpx;

				.btn {
					width: 148rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					background-color: #EC6F48;
					color: #FFFFFF;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					font-size: 24rpx;
					font-weight: bold;
				}

				.tip {
					color: #EC6F48;
					height: 44rpx;
					line-height: 44rpx;
					margin-top: 5rpx;
					font-size: 22rpx;
				}

				.opt {
					color: #D39B7E;
					background-color: #FBF3EB;
				}

				.disabled {
					color: #999999;
					background-color: #E2E2E2;
				}
			}

		}

		.cell:last-child {
			border-bottom: none;
		}
	}
</style>