<template>
	<view class="layout-container">
		<!-- 顶部填充区 -->
		<view class="top-fill" :style="{height: safeArea.statusBarHeight+'px'}"></view>
		
		<!-- 顶部胶囊区 -->
		<view class="capsule-container" 
		:style="{
			height: safeArea.capsuleBarHeight+'px',
			width: safeArea.capsuleBarLeft+'px',
			'padding-top': safeArea.capsuleBarMarginTop+'px', 
			'padding-bottom': safeArea.capsuleBarMarginBottom+'px'}">
			<view class="back-container" :style="{
				height: safeArea.capsuleBarContentHeight+'px',
				width: safeArea.capsuleBarContentHeight+'px',
			}">
				<navigator url="/pages/index/index">
					<uni-icons type="back" :size="safeArea.capsuleBarContentHeight" color="#FFFFFF"></uni-icons>
				</navigator>
			</view>
		</view>
		
		<!-- 内容区 -->
		<view class="content-container">
			<view class="edit-container">
				<view class="input-item">
					<view class="input-title">任务名称</view>
					<input class="input-box" placeholder="请输入任务名称" v-model="punchIn.taskName"/>
				</view>
				<view class="input-item">
					<view class="input-title">奖励倍数</view>
					<input class="input-box" type="number" placeholder="请输入任务倍数" v-model="punchIn.rewardNum"/>
				</view>
				<view class="switch-item">
					<view class="input-title">周末双倍</view>
					<switch class="input-switch" :checked="punchIn.weekendDoubleFlag" @change="weekendDoubleSwitchChange"/>
				</view>
				<view class="switch-item">
					<view class="input-title">全勤奖励</view>
					<switch class="input-switch" :checked="punchIn.fullAttendanceFlag" @change="fullAttendanceSwitchChange"/>
				</view>
				<view class="btn-container">
					<view class="cancel-btn" @click="cancel">取消</view>
					<view class="save-btn" @click="savePunchIn">保存</view>
				</view>
			</view>
		</view>
		
		<!-- 底部填充区 -->
		<view class="bottom-fill" :style="{height: safeArea.bottomHeight+'px'}"></view>

	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { punchInApi } from '@/service/apis';
	import { getSafeArea } from '@/utils/system.js';
	
	/**
	 * 打卡任务
	 */
	const punchIn = ref({
		"rewardNum": 1,
		"weekendDoubleFlag": false,
		"fullAttendanceFlag": false
	});
	
	/**
	 * 安全区
	 */
	const safeArea = ref({});
	
	onLoad(async (e) => {
		if (e.id) {
			const res = await punchInApi.queryPunchInById({"id": e.id});
			punchIn.value = res;
		}
		safeArea.value = getSafeArea();
	});

	const weekendDoubleSwitchChange = (e) => {
		punchIn.value.weekendDoubleFlag = e.detail.value;
	}
	
	const fullAttendanceSwitchChange = (e) => {
		punchIn.value.fullAttendanceFlag = e.detail.value;
	}
	
	/**
	 * 保存打卡任务
	 */
	const savePunchIn = () => {
		punchInApi.addPunchIn(punchIn.value).then(() => {
			uni.showToast({
				title: '保存成功',
				icon: 'success',
				duration: 2000
			});
			setTimeout(() => {
				uni.navigateBack();
			}, 2000);
		});
	}
	
	/**
	 * 后退
	 */
	const cancel = () => {
		uni.navigateBack();
	}
</script>

<style lang="scss" scoped>
	.back-container {
		border-radius: 50%;
		background-color: rgba(0, 0, 0, 0.1);
		
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.edit-container {
		margin-top: 32rpx;
		width: 100%;
		
		.input-item {
			margin-top: 30rpx;
			width: 100%;
			
			.input-title {
				font-size: 36rpx;
				font-weight: 400;
				letter-spacing: 0rpx;
				line-height: 52.13rpx;
				color: rgba(0, 0, 0, 1);
			}
			
			.input-box {
				box-sizing: border-box;
				margin-top: 10rpx;
				width: 100%;
				height: 65rpx;
				padding: 10rpx 10rpx;
				border-radius: 5rpx;
				background: #FFFFFF;
			}
		}
		
		.switch-item {
			margin-top: 30rpx;
			display: flex;
			align-items: center;
			
			.input-title {
				font-size: 36rpx;
				font-weight: 400;
				letter-spacing: 0rpx;
				line-height: 52.13rpx;
				color: rgba(0, 0, 0, 1);
			}
			
			.input-switch {
				position: absolute;
				right: 0rpx;
			}	
		}
	}
	
	.btn-container {
		position: absolute;
		bottom: 44rpx;
		left: 0rpx;
		width: 100%;
		display: grid;
		grid-template-columns: 1fr 1fr;
		place-items: center;
		
		.cancel-btn {
			width: 298rpx;
			height: 67rpx;
			border-radius: 30rpx;
			background: #E5E5E5;
			
			display: flex;
			justify-content: center;
			align-items: center;
			
			font-size: 36rpx;
			font-weight: 400;
			letter-spacing: 0rpx;
			line-height: 52.13rpx;
			color: #FFFFFF;
	
		}
		
		.save-btn {
			width: 298rpx;
			height: 67rpx;
			border-radius: 30rpx;
			background: #2A82E4;
			
			display: flex;
			justify-content: center;
			align-items: center;
	
			font-size: 36rpx;
			font-weight: 400;
			letter-spacing: 0rpx;
			line-height: 52.13rpx;
			color: #FFFFFF;
		}
	}
</style>
