<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item label="请假类型" prop="type" required>
				<u-input v-model="form.type" placeholder="请选择请假类型" :select-open="leaveTypeShow" type="select" @click="leaveTypeShow = true" />
			</u-form-item>
			<u-form-item label="请假时间" prop="leaveTime" required>
				<u-input v-model="form.leaveTime" placeholder="请选择请假时间" :select-open="leaveTimeShow" type="select" @click="leaveTimeShow = true" />
			</u-form-item>
			<u-form-item label="上传证明" v-if="form.type === '病假'" prop="leavePic" required>
				<u-upload :action="action" :header="header" :file-list="fileList" max-count="1" :multiple="false" :show-progress="false" @on-success="handlePicSuccess"></u-upload>
			</u-form-item>
			<u-form-item label="请假理由" prop="leaveReason" required>
				<u-input v-model="form.leaveReason" placeholder="请输入请假理由" type="text" />
			</u-form-item>
		</u-form>
		<u-picker mode="selector" v-model="leaveTypeShow" :range="selectorObj" range-key="cateName" @confirm="handleGetType"></u-picker>
		<u-calendar v-model="leaveTimeShow" mode="date" :active-bg-color="mainColor" btn-type="error" availableText="有课"
		 :available="availableList" activeText="已请假" :activeList="activeList" @change="setLeaveTime" max-date="2300-12-31"></u-calendar>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitForm()">提交</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				action: API.uploadFile,
				header: {
					Authorization: uni.getStorageSync('token')
				},
				classId: '',
				studentId: '',
				form: {
					type: '病假',
					leaveTime: '',
					leavePic: '',
					leaveReason: '',
				},
				rules: {
					leaveTime: [{
						required: true,
						message: '请选择请假时间',
						trigger: 'change'
					}],
					leaveReason: [{
						required: true,
						message: '请输入请假理由',
						trigger: 'change'
					}],
					leavePic: [{
						required: true,
						message: '请上传证明照片',
						trigger: 'change'
					}],
				},
				leaveTypeShow: false,
				selectorObj: [
					{
						cateName: '病假',
						type: 1
					},
					{
						cateName: '事假',
						type: 2
					}
				],
				leaveTimeShow: false,
				availableList: [],
				activeList: [],
				fileList: []
			}
		},
		onLoad(options) {
			this.classId = options.id
			this.studentId = options.studentId
			NET.request(API.getLeaveList, {
				id: this.classId
			}, 'POST').then(res => {
				// type: 0:有课  1:请假
				// 有课的日期数据
				this.availableList = res.data.filter(site => site.type == 0).map(site => {
					return site.lastDate
				})
				// 请假的日期数据
				this.activeList = res.data.filter(site => site.type == 1).map(site => {
					return site.lastDate
				})
			}).catch(error => {
				this.$refs.uTips.show({
					title: error.message,
					type: 'warning',
				})
			})
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			//  选择请假类型
			handleGetType(args) {
				this.form.type = this.selectorObj[args[0]]['cateName']
				this.form.leaveTime = ''
				this.form.leavePic = ''
				this.form.leaveReason = ''
				// 移除表单校验结果
				this.$nextTick(() => {
					this.$refs.form.resetFields()
				})
			},
			// 图片上传成功
			handlePicSuccess(args) {
				if(args.status == 10000) {
					this.form.leavePic = args.data.url
				} else {
					this.$refs.uTips.show({
						title: args.message,
						type: 'warning',
					})
				}
			},
			//  设置请假日期
			setLeaveTime(object) {
				// 今天0点的时间戳
				const nowTimeStamp = (new Date(new Date().toLocaleDateString()).getTime()) / 1000
				// 今天后3天的时间戳
				const nextThreeDayTimeStamp = nowTimeStamp + 86400 * 3
				let date = object.year + '-' + object.month + '-' + object.day
				// 当天是否可请假
				if (this.availableList.filter(site => site == date).length) {
					// 是否为病假
					if(this.form.type === '病假') {
						// 病假需提前3天请
						if(this.dateFormatTimeStamp(date) > nextThreeDayTimeStamp) {
							this.form.leaveTime = date
						} else {
							this.form.leaveTime = ''
							this.$refs.uTips.show({
								title: '病假需提前3天请',
								type: 'error',
							})
						}
					} else {
						this.form.leaveTime = date
					} 
				} else {
					this.form.leaveTime = ''
					this.$refs.uTips.show({
						title: '请选择可用日期',
						type: 'error',
					})
				}
			},
			// 日期转时间戳
			dateFormatTimeStamp(date = null,second = true) {
			    if (!date) return parseInt(new Date().getTime() / (second ? 1000 : 1))
			    return parseInt(new Date(date).getTime() / (second ? 1000 : 1))
			},
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitLevelForm, {
							classId: this.classId,
							studentId: this.studentId,
							...{ ...this.form, type:  this.form.type == '事假' ?  1 : 2  }
						}, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '请假成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}).catch(error => {
							this.$refs.uTips.show({
								title: error.message,
								type: 'warning',
							})
						})
					}
				});
			},
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;
		padding: 0 15px 60px 15px;
		box-sizing: border-box;
	}
</style>
