<template>
	<view class="evidence-add">
		<form>
			<view class="form-item">
				<label>任务描述</label>
				<textarea placeholder-class="input-placeholder" v-model="formData.taskDescribe"
					placeholder="请输入"></textarea>
			</view>
			<view class="form-item">
				<label>时间范围</label>
				<picker @change="onChangeStartTime" mode="date" :value="formData.taskStart">
					<view class="uni-input">
						<text class="val">{{formData.taskStart==""?"开始时间":formData.taskStart}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
				<picker class="end-time" :start="formData.taskStart" @change="onChangeEndTime" mode="date" :value="formData.taskEnd">
					<view class="uni-input">
						<text class="val">{{formData.taskEnd==""?"结束时间":formData.taskEnd}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<button v-if="substatus!=1" :disabled="isSubmitting" type="primary" class="submit-btn" @tap="onEdit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		taskUpdate,
		taskDetail
	} from '@/api/compositive-performance/majorReward.js';
import { onUpdated } from "vue";
	export default {
		data() {
			return {
				isSubmitting:false,
				activityIndex: 0,
				id:"",
				substatus:"",
				formData: {
					taskDescribe: "",
					taskStart: "",
					taskEnd: "",
					fdName: uni.getStorageSync("stuName"),
					years: "",
				}
			}
		},
		onLoad(option) {
			this.formData.years = option.year;
			this.id = option.id;
			this.substatus=option.substatus;
			this.getDetails();
		},
		methods: {
			async getDetails() {
				let result = await taskDetail(this.id);
				console.log(result);
				this.formData = result.data;
			},
			onChangeStartTime(e) {
				this.formData.taskStart = e.detail.value;
			},
			onChangeEndTime(e) {
				this.formData.taskEnd = e.detail.value;
			},
			async onEdit() {
				const requiredFields = [
					"taskDescribe",
					"taskStart",
					"taskEnd",
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完所有内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true;
				let res = await taskUpdate(this.formData);
				if (res.code == 200) {
					uni.showToast({
						title: "修改成功"
					})
					setTimeout(() => {
						this.isSubmitting=false;
						uni.navigateBack({
							success: () => {
								const pages = getCurrentPages();
								if (pages.length > 0) {
									const prevPage = pages[pages.length - 2]
									if (prevPage && typeof prevPage
										.getMaterialsList ===
										'function') {
										prevPage.queryParams.pageNum = 1;
										prevPage.getMaterialsList();
									}
								}
							}
						})
					}, 1000)
				} else {
					uni.showToast({
						title: res.msg
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.evidence-add {
		min-height: 95vh;
		background-color: #F5F5F7;
		padding: 40rpx;

		.form-item {
			padding: 22rpx 40rpx;
			background-color: white;
			margin-bottom: 20rpx;
			border-radius: 16rpx;

			.uni-icons {
				opacity: .5;
				font-weight: bold
			}

			label {
				margin-bottom: 20rpx;
				display: inline-block;
			}

			textarea {
				border: 1px solid #E1E1E1;
				border-radius: 10rpx;
				width: 100%;
				padding: 15rpx;

				.input-placeholder {
					color: #3D3D3D;
					opacity: 0.5;
				}
			}

			.end-time {
				margin-top: 40rpx;
			}

			picker {
				border: 1px solid #E1E1E1;
				height: 70rpx;
				line-height: 70rpx;
				padding: 0 30rpx;
				border-radius: 10rpx;

				.uni-input {
					display: flex;
					color: #797979;

					.val {
						flex: 1;
					}
				}
			}
		}

		.submit-btn {
			margin-top: 60rpx;
		}
	}
</style>