<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item label="学员姓名" prop="studentName" required>
				<u-input v-model="form.studentName" placeholder="请输入学员姓名" />
			</u-form-item>
			<u-form-item label="家长姓名" prop="parentsName" required>
				<u-input v-model="form.parentsName" placeholder="请输入家长姓名" />
			</u-form-item>
			<u-form-item label="学员性别" prop="sex" required right-icon="arrow-right" @click.native="sexShow = true">
				<text>{{form.sex}}</text>
			</u-form-item>
			<u-form-item label="学员年龄" prop="age" required>
				<u-input v-model="form.age" type="number" placeholder="请输入学员年龄" />
			</u-form-item>
			<u-form-item label="学生生日" prop="birthday" required right-icon="arrow-right" @click.native="birthdayShow = true">
				<text>{{form.birthday}}</text>
			</u-form-item>
			<u-form-item label="手机号码" prop="phone" required>
				<u-input v-model="form.phone" type="number" placeholder="请输入手机号码" />
			</u-form-item>
			<u-form-item label="备注">
				<u-input v-model="form.remark" type="textarea" placeholder="请输入备注" auto-height :height="100" />
			</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="birthdayParams" @confirm="setBirthday"></u-picker>
		<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([
				'customStyle',
			])
		},
		data() {
			return {
				form: {
					studentName: '',
					parentsName: '',
					sex: '',
					age: '',
					birthday: '',
					phone: '',
					remark: '',
				},
				rules: {
					studentName: [{
						required: true,
						message: '请输入学员姓名',
						trigger: 'change'
					}],
					parentsName: [{
						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,
				birthdayParams: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
			}
		},
		onLoad() {},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			//  设置性别
			setSex(index) {
				this.form.sex = this.sexList[index].text
			},
			//  设置生日
			setBirthday(object) {
				this.form.birthday = object.year + '-' + object.month + '-' + object.day
			},
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitIntentionForm, {
							...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>
