<template>
	<view>
		<!-- 胶囊 -->
		<mvBar :mysNavConfig="mysNavConfig"></mvBar>
		<view :style="{marginTop:navH}"></view>

		<view class="menu">
			<view class="menu-item" @click="handleClickTab(0)">
				<p v-bind:class="showList[0]?'lor':'unlor'">全部</p>
			</view>
			<view class="menu-item" @click="handleClickTab(1)">
				<p v-bind:class="showList[1]?'lor':'unlor'">已报名</p>
			</view>
			<view class="menu-item" @click="handleClickTab(2)">
				<p v-bind:class="showList[2]?'lor':'unlor'">执行中</p>
			</view>
			<view class="menu-item" @click="handleClickTab(3)">
				<p v-bind:class="showList[3]?'lor':'unlor'">已结束</p>
			</view>
		</view>

		<view class="home" v-if="showList[0] && !loading">
			<view class="task" v-for="item in secTaskLists[0]" @click="handleToSecTaskDetail(item)">
				<view class="task-card">
					<view class="task-head">
						<view class="task-head-account">
							<image class="platform-img" :src="item.platform_icon_url"></image>
							<p>{{item.platform_nick_name}}</p>
						</view>
						<view class="task-head-status">
							<p>{{item.task_stage_txt}}</p>
						</view>
					</view>
					<view class="task-info">
						<view class="task-info-left">
							<image class="project-img" :src="item.product_img_url"></image>
							<view class="task-info-txt">
								<p style="color: #000;font-size: 32rpx;">{{item.selection_name}}</p>
								<p v-if="item.sample_mode === 2">预计样品返现金额：￥{{item.show_payment}}</p>
								<p>预计收益：￥{{item.show_task_reward}}</p>
							</view>
						</view>
						<view class="task-info-right" v-if="item.isShowModify">
							<image src="../../static/img/icon-edit.png" style="height: 60rpx;width: 60rpx;"></image>
							<p style="font-size: 32rpx;color: #ffcb41;font-weight: 600">改</p>
						</view>
					</view>
					<view style="text-align: right;" v-if="item.task_stage > 5">
						<p style="display: inline-block;">实际收益：￥{{item.show_real_reward}}</p>
					</view>
				</view>
			</view>
		</view>

		<view class="home" v-if="showList[1] && !loading">
			<view class="task" v-for="item in secTaskLists[1]" @click="handleToSecTaskDetail(item)">
				<view class="task-card">
					<view class="task-head">
						<view class="task-head-account">
							<image class="platform-img" :src="item.platform_icon_url"></image>
							<p>{{item.platform_nick_name}}</p>
						</view>
						<view class="task-head-status">
							<p>{{item.task_stage_txt}}</p>
						</view>
					</view>
					<view class="task-info">
						<view class="task-info-left">
							<image class="project-img" :src="item.product_img_url"></image>
							<view class="task-info-txt">
								<p style="color: #000;font-size: 32rpx;">{{item.selection_name}}</p>
								<p v-if="item.sample_mode === 2">预计样品返现金额：￥{{item.show_payment}}</p>
								<p>预计收益：￥{{item.show_task_reward}}</p>
							</view>
						</view>
						<view class="task-info-right" v-if="item.isShowModify">
							<image src="../../static/img/icon-edit.png" style="height: 60rpx;width: 60rpx;"></image>
							<p style="font-size: 32rpx;color: #ffcb41;font-weight: 600">改</p>
						</view>
					</view>
					<view style="text-align: right;" v-if="item.task_stage > 5">
						<p style="display: inline-block;">实际收益：￥{{item.show_real_reward}}</p>
					</view>
				</view>
			</view>
		</view>

		<view class="home" v-if="showList[2] && !loading">
			<view class="task" v-for="item in secTaskLists[2]" @click="handleToSecTaskDetail(item)">
				<view class="task-card">
					<view class="task-head">
						<view class="task-head-account">
							<image class="platform-img" :src="item.platform_icon_url"></image>
							<p>{{item.platform_nick_name}}</p>
						</view>
						<view class="task-head-status">
							<p>{{item.task_stage_txt}}</p>
						</view>
					</view>
					<view class="task-info">
						<view class="task-info-left">
							<image class="project-img" :src="item.product_img_url"></image>
							<view class="task-info-txt">
								<p style="color: #000;font-size: 32rpx;">{{item.selection_name}}</p>
								<p v-if="item.sample_mode === 2">预计样品返现金额：￥{{item.show_payment}}</p>
								<p>预计收益：￥{{item.show_task_reward}}</p>
							</view>
						</view>
						<view class="task-info-right" v-if="item.isShowModify">
							<image src="../../static/img/icon-edit.png" style="height: 60rpx;width: 60rpx;"></image>
							<p style="font-size: 32rpx;color: #ffcb41;font-weight: 600">改</p>
						</view>
					</view>
					<view style="text-align: right;" v-if="item.task_stage > 5">
						<p style="display: inline-block;">实际收益：￥{{item.show_real_reward}}</p>
					</view>
				</view>
			</view>
		</view>

		<view class="home" v-if="showList[3] && !loading">
			<view class="task" v-for="item in secTaskLists[3]" @click="handleToSecTaskDetail(item)">
				<view class="task-card">
					<view class="task-head">
						<view class="task-head-account">
							<image class="platform-img" :src="item.platform_icon_url"></image>
							<p>{{item.platform_nick_name}}</p>
						</view>
						<view class="task-head-status">
							<p>{{item.task_stage_txt}}</p>
						</view>
					</view>
					<view class="task-info">
						<view class="task-info-left">
							<image class="project-img" :src="item.product_img_url"></image>
							<view class="task-info-txt">
								<p style="color: #000;font-size: 32rpx;">{{item.selection_name}}</p>
								<p v-if="item.sample_mode === 2">预计样品返现金额：￥{{item.show_payment}}</p>
								<p>预计收益：￥{{item.show_task_reward}}</p>
							</view>
						</view>
						<view class="task-info-right" v-if="item.isShowModify">
							<image src="../../static/img/icon-edit.png" style="height: 60rpx;width: 60rpx;"></image>
							<p style="font-size: 32rpx;color: #ffcb41;font-weight: 600">改</p>
						</view>
					</view>
					<view style="text-align: right;" v-if="item.task_stage > 5">
						<p style="display: inline-block;">实际收益：￥{{item.show_real_reward}}</p>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import mvBar from "@/components/mys_navBar/mysNavBar";
	import {
		fansview,
		money
	} from '@/components/utils.js';
	export default {
		components: {
			mvBar,
		},
		data() {
			return {
				navH: getApp().globalData.navHeight,
				mysNavConfig: {
					/* 开启单页显示首页图标 */
					isHome: true,
					/* 固定导航  */
					navFixed: true,
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "带货任务",
						color: "",
						fontSize: "32rpx", // px upx rpx
						fontWeight: "normal", // 100 - 700
					},
					btnType: "type2",
					onLeftClick: "/pages/mycenter/mycenter",
					/* type2 按钮 */
					type2Config: {
						// 左图标
						leftPath: "/static/img/png2.png",
						// 右图标
						rightPath: "/static/img/png4.png",
						// 圆角
						radius: "40rpx",
					},
				},

				loading: false,
				showList: [true, false, false, false],
				secTaskStageList: [],
				secTaskLists: [
					[],
					[],
					[],
					[]
				],
				curTab: 0,
			}
		},
		async onLoad(options) {
			// 判断登录
			await this.isLoginApi();
			if (this.token === '') {
				uni.showModal({
					content: '请先登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login'
							});
						} else if (res.cancel) {
							uni.navigateBack();
						}
					}
				});
			}

			let data = options.textObj.replace(/""/g, "");
			data = JSON.parse(decodeURIComponent(data))
			this.curTab = data.value

			let that = this
			this.showList.forEach(function(item, index) {
				if (index == that.curTab) {
					that.showList[index] = true
				} else {
					that.showList[index] = false
				}
			})
			console.log("showList: ", this.showList)
		},
		async onShow() {
			await this.getInfoTableApi()
			this.handleClickTab(this.curTab)
		},
		methods: {
			async handleClickTab(value) {
				this.loading = true;
				uni.showLoading({
					title: '加载中'
				});

				let that = this
				this.showList.forEach(function(item, index) {
					if (index == value) {
						that.showList[index] = true
					} else {
						that.showList[index] = false
					}
				})
				await this.getSecTaskListApi(parseInt(value) + 1);

				uni.hideLoading();
				this.loading = false;
			},
			handleToSecTaskDetail(item) {
				uni.navigateTo({
					url: '/page_workspace/selection/sectaskdetail?taskId=' + item.task_id,
					success(res) {},
					fail(err) {}
				});
			},
			// 判断登陆是否过期
			isLoginApi() {
				return this.$https.get('/youngee/c/t/g/is-login')
					.then(res => {
						if (res.data.code === 403) {
							// 登陆已失效
							this.token = ""
							uni.clearStorage();
						} else {
							this.token = uni.getStorageSync('token')
						}
					})
			},
			getInfoTableApi() {
				return this.$https.get('/youngee/c/g/get-info-tables')
					.then(res => {
						console.log(res)
						this.secTaskStageList = res.data.data.SecTaskStage
					})
			},
			getSecTaskListApi(value) {
				return this.$https.get('/youngee/c/api/t/secTask/list?task_stage=' + value).then(res => {
					if (res.data.code === 1) {
						let secTaskList = res.data.data.sec_task
						let num = res.data.data.count
						for (let i = 0; i < num; ++i) {
							// 主图
							if (secTaskList[i].product_photo_snap != null && secTaskList[i].product_photo_snap != ''){
								let productPhoto = JSON.parse(secTaskList[i].product_photo_snap);
								for (let j = 0; j < productPhoto.length; j++) {
									if (productPhoto[j].Symbol == 1) {
										secTaskList[i].product_img_url = productPhoto[j].PhotoUrl
									}
								}
							}

							// 待修改标识
							if (secTaskList[i].task_stage == 8 && secTaskList[i].assignment_status == 3) {
								secTaskList[i].isShowModify = true
							} else {
								secTaskList[i].isShowModify = false
							}

							// 任务阶段展示							
							secTaskList[i].task_stage_txt = this.secTaskStageList[secTaskList[i].task_stage - 1]
								.task_stage

							// 任务收益
							secTaskList[i].real_reward = secTaskList[i].task_reward
							secTaskList[i].show_task_reward = money(secTaskList[i].task_reward)
							secTaskList[i].show_real_reward = money(secTaskList[i].real_reward)
							secTaskList[i].show_payment = money(secTaskList[i].talent_payment)
						}
						this.secTaskLists[value-1] = secTaskList
						console.log("secTaskLists: ", this.secTaskLists)
					}

				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	p {
		font-size: 32rpx;
	}

	.home {
		padding-top: 70rpx;
	}

	.menu {
		position: fixed;
		border-bottom: #FCCF41;
		background-color: #FFFFFF;
		justify-content: space-around;
		height: 5%;
		width: 100%;
		display: flex;
		z-index: 10;
	}

	.menu-item p {
		font-weight: 600;

		&.lor {
			border-bottom: 5rpx solid #F0D232;
			color: #F0D232;
			padding-bottom: 15rpx;
		}

		&.unlor {
			border-bottom: 5rpx solid #fff;
			color: #81838f;
			padding-bottom: 15rpx;
		}
	}

	.task-card {
		margin: 10rpx 0;
		// border-bottom: 1rpx #d7d7d7 solid;

		.task-head {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 20rpx;

			.task-head-account {
				display: flex;
			}

			.platform-img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 10rpx;
			}

			.task-head-status p {
				color: red;
			}
		}

		.task-info {
			display: flex;
			justify-content: space-between;
			padding: 10rpx 20rpx;

			.task-info-left {
				display: flex;
				// align-items: center;

				.project-img {
					height: 120rpx;
					width: 120rpx;
					// background-color: #b3c96b;
				}

				.task-info-txt {
					margin-left: 10rpx;
				}

				.task-info-txt p {
					font-size: 24rpx;
					color: #85859d;
					margin-bottom: 10rpx;
				}
			}

			.task-info-right {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				border: #ffcb41 2rpx solid;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.task-info-right p {
				font-size: 24rpx;
			}
		}
	}
</style>