<template>
	<view class="content">
		<u-form :model="subscribelForm" ref="subscribelForm" label-width="140">
			<u-form-item label="学生姓名" prop="studentName" required>
				<u-input v-model="subscribelForm.studentName" placeholder="请输入学生姓名" />
			</u-form-item>
			<!-- <u-form-item label="家长姓名" prop="fatherName" required>
				<u-input v-model="subscribelForm.fatherName" placeholder="请输入家长姓名" />
			</u-form-item> -->
			<u-form-item label="学生性别" prop="sex" required>
				<u-input v-model="subscribelForm.sex" placeholder="请选择学生性别" :select-open="sexShow" type="select" @click="sexShow = true" />
			</u-form-item>
			<u-form-item label="学生年龄" prop="age" required>
				<u-input v-model="subscribelForm.age" placeholder="请输入学生年龄" type="number" />
			</u-form-item>
			<!-- <u-form-item label="学生生日" prop="birthday" required>
				<u-input v-model="subscribelForm.birthday" placeholder="请选择学生生日" :select-open="birthdayShow" type="select" @click="birthdayShow = true" />
			</u-form-item> -->
			<u-form-item label="手机号码" prop="phone" required>
				<u-input v-model="subscribelForm.phone" type="number" placeholder="请输入手机号码" />
			</u-form-item>
			<!-- <u-form-item label="体验日期" placeholder="请选择体验日期" prop="experience">
				<u-input v-model="subscribelForm.experience" :select-open="experienceShow" type="select" @click="experienceShow = true" />
			</u-form-item> -->
		</u-form>
		<u-action-sheet :list="sexList" v-model="sexShow" @click="setSex"></u-action-sheet>
		<u-picker v-model="birthdayShow" mode="time" :params="params" @confirm="setBirthday"></u-picker>
		<u-calendar v-model="experienceShow" mode="date" :active-bg-color="mainColor" btn-type="error" availableText="有课"
		 :available="availableList" @change="setExperience" 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 {
				subscribelForm: {
					studentName: '',
					// fatherName: '',
					sex: '',
					age: '',
					// birthday: '',
					phone: '',
					// placeSchool: '',
					// experience: '',
				},
				rules: {
					studentName: [{
						required: true,
						message: '请输入学生姓名',
						trigger: 'change'
					}],
					fatherName: [{
						required: true,
						message: '请输入家长姓名',
						trigger: 'change'
					}],
					sex: [{
						required: true,
						message: '请选择学生性别',
						trigger: 'change'
					}],
					age: [{
						required: true,
						message: '请输入学生年龄',
						trigger: 'change'
					}],
					birthday: [{
						required: true,
						message: '请选择学生生日',
						trigger: 'change'
					}],
					phone: [{
							required: true,
							message: '请输入手机号码',
							trigger: 'change'
						},
						{
							pattern: /^1[0-9]{10}$/g,
							transform(value) {
								return String(value);
							},
							message: '请输入正确格式的手机号码'
						},
					],
				},
				sexShow: false,
				sexList: [{
						text: '男'
					},
					{
						text: '女'
					}
				],
				birthdayShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				experienceShow: false,
				availableList: []
			}
		},
		onLoad(options) {
			this.subscribelForm.phone = uni.getStorageSync('userData').phone
		},
		onReady() {
			this.$refs.subscribelForm.setRules(this.rules);
		},
		methods: {
			//  设置性别
			setSex(index) {
				this.subscribelForm.sex = this.sexList[index].text
			},
			//  设置生日
			setBirthday(object) {
				this.subscribelForm.birthday = object.year + '-' + object.month + '-' + object.day
			},
			//  设置体验日期
			setExperience(object) {
				let date = object.year + '-' + object.month + '-' + object.day
				if (this.availableList.filter(site => site == date).length) {
					this.subscribelForm.experience = date
				} else {
					this.subscribelForm.experience = ''
					this.$refs.uTips.show({
						title: '请选择可用日期',
						type: 'error',
					})
				}
			},
			//  提交表单
			submitForm() {
				this.$refs.subscribelForm.validate(valid => {
					if (valid) {
						NET.request(API.submitStudentForm, this.subscribelForm, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '新增成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '/pagesMember/subscribelSuccess'
								});
							}, 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>
