<template>
	<view class="">
		<nav-bar title="任务详情" goBack />
		<view class="task-details-box" v-if="taskInfo ">
			<view class="details-content">
				<view class="task-title">
					{{taskInfo.title || taskInfo.taskTitle}}
				</view>
				<view class="task-explain">
					{{taskInfo.remark }}
				</view>
				<share :sharePoster="taskInfo.sharePoster" :taskId="taskInfo.taskId" />

				<taskContent>
					<view class="task-info-item">
						<text class="task-info-title">任务时间：</text>
						<text class="task-info-val">{{taskInfo.startTime }} - {{taskInfo.endTime }}</text>
					</view>
					<view class="task-info-item">
						<text class="task-info-title">任务领取后完成时间：</text>
						<text class="task-info-val">{{taskInfo.completionDays }}天</text>
					</view>
					<view class="task-info-item">
						<text class="task-info-title">任务奖励：</text>
						<text class="task-info-val task-info-integral">{{taskInfo.rewardPoints }}积分</text>
					</view>
				</taskContent>
			</view>


			<template v-if="taskInfo && taskInfo.taskType === 1 ">
				<!-- 存在子任务 -->
				<view class="gap"></view>
				<view class="task-sublevel">
					<view class="sublevel-title">
						<!-- completeSort -->
						任务过程：<text> ({{ taskInfo.completeSort === 1 ? '顺序完成' : '随机完成'}}) </text>
					</view>
					<view class="sublevel-list">
						<view class="sublevel-item"
							v-for="(item , index) in (receiveInfo ? receiveInfo.childTaskVoList : taskInfo.childTaskVoList )">
							<view class="sublevel-item-num">
								{{item.location || item.childTaskLocation}}
							</view>
							<view class="sublevel-item-content">
								<view class="sublevel-item-title zw-one-row">
									<text class="title-name zw-one-row">{{item.title || item.childTaskTitle}}</text>
									<template v-if="receiveTaskId">
										<tag class="sublevel-status" v-if="item.checkStatus === 0" bgColor="#edfcfd"
											color="#3CB7D2" :tag-name="$TaskStatusText(item.checkStatus)" />
										<tag class="sublevel-status" v-if="item.checkStatus === 1" bgColor="#FDFAED"
											color="#FFB300" :tag-name="$TaskStatusText(item.checkStatus)" />
										<tag class="sublevel-status" v-if="item.checkStatus === 2" bgColor="#FDEDED"
											color="#FF0000" :tag-name="$TaskStatusText(item.checkStatus)" />
										<tag class="sublevel-status" v-if="item.checkStatus === 3" bgColor="#EDFDF2"
											color="#00B819" :tag-name="$TaskStatusText(item.checkStatus)" />
									</template>
								</view>
								<view class="sublevel-item-text">
									<view class="content-text">
										{{item.remark || item.childTaskRemark}}
									</view>
									<!-- checkStatus 审状态（0未提交,1待审核,2驳回,3审核通过）,可用值:NOT_SUBMIT,PASS,REJECT,WAIT -->
									<!-- 驳回 - 去提交 / 审核中 -- 去查看 / 进行中 -- 去提交 / 已完成 -- 去查看 -->
									<template v-if="receiveTaskId || receiveInfo">
										<view class="content-btn" v-if="[0, 2].includes(item.checkStatus)"
											@click.stop="onSubmit( 1, index , false , 'submitDetails')">
											去提交
										</view>
										<view class="content-btn" v-if="[1, 3].includes(item.checkStatus)"
											@click.stop="onSubmit( 1,  index , true , 'taskAuditDetails')">
											去查看
										</view>
									</template>

								</view>
								<view class="reject-cause" v-if="receiveTaskId && item.checkStatus === 2">
									<text class="reject-title">驳回原因：</text>
									<text class="reject-hint">{{ item.rejectReason }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</template>
		</view> 
		<template v-if="taskInfo && !btnLoading">
			<!-- 任务大厅进来领取 -->
			<taskBottomBtn v-if="taskId && receiveInfo === null" pageBgc="#f5f5f5" :paddTop="5"
				:btnColor="taskInfo.publishedStatus === 0 ? '#ccc' : '' " @onClick="onReceive" btn-name="领取任务" />
			<!-- 我的任务 -  单任务提交 -->
			<taskBottomBtn v-if=" taskInfo.taskType === 0 && (receiveTaskId || receiveInfo ) "
				@onClick="onSubmit(0, 0 , false , 'submitDetails')" btn-name="去提交" />
		</template>

	</view>
</template>

<script>
	import {
		getTaskInfo_Api,
		TaskGetMyTaskInfo_Api,
		getrReceiveTask_Api,
		getMyTaskInfo_Api,
		onSubmitCheck_Api
	} from "@/api/task.js"
	import share from "./components/share.vue"
	import taskContent from "./components/task-content.vue"
	import taskBottomBtn from "./components/task-bottom-btn.vue"
	import tag from "./components/tag.vue"
	import {
		onSubmint_val
	} from "./task_common.js"
	export default {
		name: 'yige-task-details',
		components: {
			share,
			taskContent,
			taskBottomBtn,
			tag
		},
		data() {
			return {
				taskId: null,
				receiveTaskId: null,
				taskType: null, // // 任务类型：1：任务大厅 - 领取任务； 2：我的任务 - 提交任务
				config: {
					back: true,
					title: '任务详情',
					color: 'black',
					backgroundColor: [1, '#fff'],
					statusBarFontColor: 'black'
				},
				taskInfo: {},
				btnLoading: false,
				receiveInfo: null
			}
		},
		onLoad(opt) {
			this.taskType = Number(opt.taskType) || null;
			this.taskId = opt.taskId;
			this.receiveTaskId = opt.receiveTaskId;
			this.btnLoading = true;
			this.init()
		},
		methods: {
			init() {
				if (this.taskId) {
					// 大厅任务详情
					this.getTaskInfo();
					this.TaskGetMyTaskInfo()
				}
				if (this.receiveTaskId) {
					this.getMyTaskInfo();
				}
			},
			// 任务大厅进来
			getTaskInfo() {
				getTaskInfo_Api(this.taskId).then(res => {
					this.taskInfo = res.data || null;

				})
			},
			TaskGetMyTaskInfo() {
				TaskGetMyTaskInfo_Api(this.taskId).then(res => {
					const data = res.data || null;
					this.receiveInfo = data;
				}).finally(() => {
					this.btnLoading = false
				})
			},
			// 任务领取
			onReceive() {
				if (!this.taskInfo.publishedStatus) {
					uni.showToast({
						title: '当前任务未开放',
						icon: 'none'
					});
					return
				};
				getrReceiveTask_Api(this.taskId).then(res => {

					this.TaskGetMyTaskInfo()


					uni.showToast({
						title: '领取成功',
						icon: 'none'
					})
				})
			},

			// 我的任务列表进来 - 单任务详情
			getMyTaskInfo() {
				getMyTaskInfo_Api(this.receiveTaskId).then(res => {
					this.taskInfo = res.data || null;
					this.receiveInfo = res.data || null
				}).finally(() => {
					this.btnLoading = false
				})
			},
			// 去查看
			// task-examine
			// onExamine() {
			// 	let path = '/pages/task/submit-task';
			// 	if (type === 0) {
			// 		// 单任务提交
			// 		path = `${path}?receiveTaskId=${this.receiveTaskId}`
			// 	} else {
			// 		// 多任务下子任务提交
			// 		path = `${path}?receiveChildTaskId=${receiveChildTaskId}`
			// 	};

			// 	uni.navigateTo({
			// 		url: path
			// 	})
			// },
			// 

			/**
			 * 提交 / 查看
			 * 
			 * type: 0： 单任务 , 1: 多任务
			 * 
			 * checkPage 是否查看： false: 提交 ， true: 查看
			 * 
			 */
			onSubmit(type = 0, index = 0, checkPage = false, page) {
				const taskInfo_ = this.receiveInfo ? this.receiveInfo.childTaskVoList[index] : this.taskInfo
					.childTaskVoList[index]

				onSubmint_val(type, taskInfo_, checkPage, page)
			},
			// onSubmit_more(type , index ){
			// 	const taskInfo = this.taskInfo.childTaskVoList[0];
			// 	onSubmint_val(type, taskInfo, checkPage)
			// }
		}
	}
</script>

<style lang="scss" scoped>
	@import "./common.scss";

	.task-details-box {
		width: 100%;

		.details-content {
			padding: 0 30rpx 30rpx;
		}

		.task-sublevel {
			width: 100%;

			.sublevel-title {
				width: 100%;
				height: 91rpx;
				padding: 0 30rpx;
				border-bottom: 1rpx solid #e6e6e6;
				line-height: 90rpx;
				font-size: 30rpx;
				// font-family: .PingFang SC, .PingFang SC-Semibold;
				font-weight: 500;
				text-align: left;
				color: #1a1a1a;

				// letter-spacing: -0.6px;
				text {
					font-size: 30rpx;
					// font-family: .PingFang SC, .PingFang SC-Light;
					font-weight: 300;
					// text-align: left;
					color: #1a1a1a;
					// letter-spacing: -0.6px;
				}
			}

			.sublevel-list {
				width: 100%;
				padding: 0 30rpx;

				.sublevel-item {
					padding: 20rpx 0 15rpx;
					display: flex;
					flex-direction: row;
					justify-content: flex-start;
					border-bottom: 1rpx solid #e6e6e6;

					.sublevel-item-num {
						width: 57rpx;
						height: 57rpx;
						border-radius: 50%;
						background: linear-gradient(180deg, #45c7d5, #3eb8d0);
						text-align: center;
						line-height: 57rpx;
						font-size: 30rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						text-align: center;
						color: #ffffff;
					}

					.sublevel-item-content {
						width: calc(100% - 57rpx);
						padding-left: 13rpx;

						.sublevel-item-title {
							width: 100%;
							height: 57rpx;
							line-height: 57rpx;
							font-size: 28rpx;
							// font-family: PingFang SC, PingFang SC-Bold;
							font-weight: Light;
							color: #1a1a1a;
							display: flex;
							flex-direction: row;
							align-items: center;

							.title-name {
								padding-right: 20rpx;
							}

							.sublevel-status {
								flex-shrink: 0;

							}


						}

						.sublevel-item-text {
							font-size: 26rpx;
							// font-family: .PingFang SC, .PingFang SC-Light;
							font-weight: Light;
							color: #808080;
							line-height: 38rpx;
							display: flex;
							flex-direction: row;
							align-items: center;
							justify-content: space-between;

							.content-btn {
								flex-shrink: 0;
								margin-left: 20rpx;

								width: 135rpx;
								height: 54rpx;
								background: linear-gradient(75deg, #3cb7d2 10%, #46c8d5 89%);
								border-radius: 27rpx;

								font-size: 24rpx;
								font-family: PingFang SC, PingFang SC-Regular;
								font-weight: Regular;
								text-align: center;
								color: #ffffff;
								line-height: 54rpx;
							}

							// content-btn
						}

						.reject-cause {
							width: 100%;
							padding-top: 20rpx;
							// text-align: center;
							font-size: 26rpx;
							display: flex;
							font-weight: Regular;
							align-items: stretch;

							.reject-title {
								flex-shrink: 0;
								font-weight: 500;
							}
						}
					}
				}
			}
		}

	}

	.task-btn-box {
		width: 100%;
		height: 150rpx;

		.task-btn {
			width: 100%;
			height: 150rpx;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 1;

			.task-text {
				margin: 20rpx auto;
				width: 690rpx;
				height: 80rpx;
				background: linear-gradient(92deg, #3cb7d2 2%, #44c5d5 98%);
				border-radius: 40rpx;
				text-align: center;
				line-height: 80rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: Regular;
				color: #ffffff;
			}
		}
	}

	.gap {
		width: 100%;
		height: 10rpx;
		background: #f5f5f5;
	}
</style>