<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<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="请假理由" prop="leaveReason" required>
				<u-input v-model="form.leaveReason" placeholder="请输入请假理由" type="textarea" auto-height :height="100" />
			</u-form-item>
		</u-form>
		<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 {
				classId: '',
				studentId: '',
				form: {
					leaveTime: '',
					leaveReason: '',
				},
				rules: {
					leaveTime: [{
						required: true,
						message: '请选择请假时间',
						trigger: 'change'
					}],
					leaveReason: [{
						required: true,
						message: '请输入请假理由',
						trigger: 'change'
					}],
				},
				leaveTimeShow: false,
				availableList: [],
				activeList: [],
			}
		},
		onLoad(options) {
			this.classId = options.id
			this.studentId = options.studentId
			NET.request(API.getLeaveList, {
				id: this.classId
			}, 'POST').then(res => {
				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: {
			//  设置请假日期
			setLeaveTime(object) {
				let date = object.year + '-' + object.month + '-' + object.day
				if (this.availableList.filter(site => site == date).length) {
					this.form.leaveTime = date
				} else {
					this.form.leaveTime = ''
					this.$refs.uTips.show({
						title: '请选择可用日期',
						type: 'error',
					})
				}
			},
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitLevelForm, {
							classId: this.classId,
							studentId: this.studentId,
							...this.form
						}, '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>
