<template>
	<view class="evidence-add">
		<view class="form-item">
			<label>值班说明</label>
			<input type="text" v-model="formData.dutyDescription" placeholder="请输入"
				placeholder-class="input-placeholder" />
		</view>
		<view class="form-item">
			<label>值班时间</label>
			<picker @change="onChangeTime" :value="formData.dutyTime" mode="date" class="startTime">
				<view class="uni-input">
					<text class="val">{{formData.dutyTime}}</text>
					<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
				</view>
			</picker>
		</view>
		<button v-if="commitStatus!=1" :disabled="isSubmitting" type="primary" class="submit-btn" @tap="onSubmit">保存修改</button>
	</view>
</template>

<script>
	import {
		kpiFillingRewardsKpiDetail,
		kpiFillingRewardsKpiUpdate
	} from "@/api/instructor/overwork-materials.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	export default {
		data() {
			return {
				isSubmitting:false,
				commitStatus:"",
				formData: {
					dutyDescription: "",
					dutyTime: getCurrentDateTime(),
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: "",
					fillingYear: "",
				}
			}
		},
		onLoad(option) {
			this.formData.id = option.id;
			this.commitStatus=option.commitStatus;
			this.formData.fillingYear = option.year;
			this.formData.fillingMonth = option.month;
			this.getDetail();
		},
		methods: {
			async getDetail() {
				let res = await kpiFillingRewardsKpiDetail(this.formData.id);
				this.formData = res.data;
			},
			onChangeTime(e) {
				this.formData.dutyTime = e.detail.value;
			},
			async onSubmit() {
				const requiredFields = [
					'dutyDescription',
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写值班说明`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true;
				let res = await kpiFillingRewardsKpiUpdate(this.formData);
				if (res.code == 200) {
					uni.showToast({
						title: "修改成功"
					})
					setTimeout(() => {
						this.isSubmitting=false;
						uni.navigateBack({
							success: () => {
								uni.$emit('refreshRewardsKpiList');
							}
						})
					}, 1000)
				}
			},
		}
	}
</script>

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

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

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

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

			input {
				border: 1px solid #E1E1E1;
				border-radius: 10rpx;
				height: 70rpx;
				padding-left: 30rpx;

				.input-placeholder {
					color: #b6b6b6;
				}
			}

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

				&.endTime {
					margin-top: 40rpx;
				}

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

					.val {
						flex: 1;
					}
				}
			}
		}

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