<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="birthday" required v-show="showinput">
				<u-input v-model="subscribelForm.birthday" placeholder="请选择学生生日" :select-open="birthdayShow" type="select" @click="birthdayShow = 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="studentCardName"  v-show="showinput">
				<u-input v-model="subscribelForm.studentCardName" placeholder="请选择身份类型" type="select" @click="handletypeShowclick(1)" />
			</u-form-item>
			<u-form-item :label="subscribelForm.studentCardName || '身份证号'" prop="studentCardNumber"  v-show="showinput">
				<u-input v-model="subscribelForm.studentCardNumber" placeholder="请输入证件号" @click="handleWriteCardClick(1)" />
			</u-form-item>
			<u-form-item label="家长姓名" prop="fatherName" required  v-show="showinput">
				<u-input v-model="subscribelForm.fatherName" placeholder="请输入家长姓名" />
			</u-form-item>
			<u-form-item label="关系" prop="relation" required  v-show="showinput">
				<u-input v-model="subscribelForm.relation" placeholder="请输入关系" />
			</u-form-item>
			<u-form-item label="身份类型" prop="parentCardName" required  v-show="showinput">
				<u-input v-model="subscribelForm.parentCardName" placeholder="请选择身份类型" type="select" @click="handletypeShowclick(2)"  />
			</u-form-item>
			<u-form-item :label="subscribelForm.parentCardName || '身份证号'" prop="parentCardNumber" required  v-show="showinput">
				<u-input v-model="subscribelForm.parentCardNumber" placeholder="请输入证件号" @click="handleWriteCardClick(2)" />
			</u-form-item>
			<u-form-item label="手机号码" prop="phone" required v-show="showinput">
				<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-action-sheet :list="typeList" v-model="typeShow" @click="setType"></u-action-sheet>
<!-- 		<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">
				{{ subscribelForm.studentId ? '修改' : '新增' }}
			</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 {
				showinput:true,
				subscribelForm: {
					studentName: '',
					sex: '',
					age: '',
					birthday: '',
					studentCard: '',
					studentCardName: '',
					studentCardNumber: '',
					fatherName: '',
					relation: '', 
					parentCard: '',
					parentCardName: '',
					parentCardNumber: '',
					phone: ''
				},
				rules: {
					studentName: [{
						required: true,
						message: '请输入学生姓名',
						trigger: 'change'
					}],
					sex: [{
						required: true,
						message: '请选择学生性别',
						trigger: 'change'
					}],
					birthday: [{
						required: true,
						message: '请选择学生生日',
						trigger: 'change'
					}],
					age: [{
						required: true,
						message: '请输入学生年龄',
						trigger: 'change'
					}],
					fatherName: [{
						required: true,
						message: '请输入家长姓名',
						trigger: 'change'
					}],
					relation: [{
						required: true,
						message: '请输入关系',
						trigger: 'change'
					}],
					parentCardName: [{
						required: true,
						message: '请选择身份类型',
						trigger: 'change'
					}],
					parentCardNumber: [{
						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: '女'
					}
				],
				// 1 学生身份类型  2 家长身份类型
				typeNumber: 1,
				typeShow: false,
				typeList: [
					{
							text: '身份证号'
						},
						{
							text: '护照号'
						}
				],
				birthdayShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				experienceShow: false,
				availableList: []
			}
		},
		onLoad(options) {
			//审核不让收集省份证等信息
			if(Date.parse(new Date())<1661835600000){
				this.showinput=false
			}
			// this.subscribelForm.phone = uni.getStorageSync('userData').phone
			const info = JSON.parse(decodeURIComponent(options.info))
			if(info) {
				let data = {}
				uni.setNavigationBarTitle({
					title: '修改信息'
				});
				if(info.idCard) {
					data['studentCardName'] = info.idCard.split(',')[0]
					data['studentCardNumber'] = info.idCard.split(',')[1]
				}
				console.log(info.parentIdCard)
				if(info.parentIdCard) {
					data['parentCardName'] = info.parentIdCard.split(',')[0]
					data['parentCardNumber'] = info.parentIdCard.split(',')[1]
				}
				if(info.parentIdCard==null){
					info.parentIdCard=''
				}
				if(info.idCard==null){
					info.idCard=''
				}
				this.subscribelForm = { ...info, ...data}
			} else {
				uni.setNavigationBarTitle({
					title: '添加信息'
				});
			}
		},
		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
			},
			//
			handletypeShowclick(index) {
				this.typeNumber = index
				this.typeShow = true
			},
			setType(object) {
				console.log(this.typeNumber);
				if(this.typeNumber == 1) {
					this.subscribelForm.studentCard = object
					this.subscribelForm.studentCardName = this.typeList[object].text
					console.log(this.subscribelForm);
				} else {
					this.subscribelForm.parentCard = object
					this.subscribelForm.parentCardName = this.typeList[object].text
					console.log(this.subscribelForm);
				}
			},
			handleWriteCardClick(index) {
				console.log(index);
				console.log(this.subscribelForm);
				if((index == 1 && !this.subscribelForm.studentCardName) || (index == 2 && !this.subscribelForm.parentCardName)) {
					this.$refs.uTips.show({
						title: '请先选择身份类型',
						type: 'warning',
					})
				}
			},
			//  提交表单
			submitForm() {
				this.$refs.subscribelForm.validate(valid => {
					if (valid) {
						let info = {
							idCard: this.subscribelForm.studentCardName ? this.subscribelForm.studentCardName + ',' +this.subscribelForm.studentCardNumber : '',
							parentIdCard: this.subscribelForm.parentCardName + ',' +this.subscribelForm.parentCardNumber
						}
						NET.request(this.subscribelForm.studentId ? API.updateStudentForm : API.submitStudentForm , { ...this.subscribelForm, ...info }, 'POST').then(res => {
							if(this.subscribelForm.studentId) {
								this.$refs.uTips.show({
									title: '修改成功',
									type: 'success',
								})
								setTimeout(() => {
									uni.navigateBack({})
								}, 1000)
							} else {
								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',
							})
						})
					}
				})
			},
			//  设置体验日期
			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',
					})
				}
			}
		}
	}
</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>
