<template>
	<view class="content">
		<u-form :model="subscribelForm" ref="subscribelForm" label-width="140">
			<u-form-item label="学生姓名" prop="studentName">
				<u-input v-model="subscribelForm.studentName" disabled />
			</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">
				<u-input v-model="subscribelForm.sex" disabled />
			</u-form-item>
			<u-form-item label="学生年龄" prop="age">
				<u-input v-model="subscribelForm.age" disabled />
			</u-form-item>
			<u-form-item label="学生班级" prop="name">
				<u-input v-if="!subscribelForm.experience" v-model="subscribelForm.name" placeholder="请输入学生班级" type="select" @click="handleClassInfoShow" />
				<text v-else>{{ subscribelForm.name }}</text>
			</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-item label="上课教练" placeholder="请选择上课教练" prop="coachName" v-if="subscribelForm.classId==0">
				<u-search placeholder="请输入教练名" v-model="subscribelForm.coachName" focus :animation="false" @custom='handleCoachListShow' @search='handleCoachListShow' @clear="clear"/>
				<!--<u-input v-if="!subscribelForm.experience" v-model="subscribelForm.coachName" type="select" @click="handleCoachListShow" />-->
			</u-form-item>
			<u-form-item v-else label="上课教练" placeholder="请选择上课教练" prop="coachName">
				<u-input v-if="!subscribelForm.experience" v-model="subscribelForm.coachName" type="select" @click="handleCoachListShow" />
				<text v-else>{{ subscribelForm.coachName }}</text>
			</u-form-item>
		</u-form>
		<!-- 性别 -->
		<!-- <u-action-sheet :list="sexList" v-model="sexShow" @click="setSex"></u-action-sheet> -->
		<!-- 班级 -->
		<!-- <u-action-sheet :list="classList" v-model="classShow" @click="HandleChooseClassClick"></u-action-sheet> -->
		<u-picker v-model="classShow" mode="selector" :range="classList" range-key="text" @confirm="HandleChooseClassClick"></u-picker>
		<!-- 教练 -->
		<!-- <u-action-sheet :list="coachList" v-model="coachShow" @click="HandleChooseCoachClick"></u-action-sheet> -->
		<u-picker v-model="coachShow" mode="selector" :range="coachList" range-key="text" @confirm="HandleChooseCoachClick"></u-picker>
		<!-- 学生生日 -->
		<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" v-if="!subscribelForm.experience">
			<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 {
				// 场馆id
				venueId: null,
				subscribelForm: {
					studentId: '',
					studentName: '',
					sex: '',
					age: '',
					classId: '',
					name: '',
					coachId: '',
					coachName: '',
					// 预约id
					subscribeId: ''
				},
				sexShow: false,
				sexList: [{
						text: '男'
					},
					{
						text: '女'
					}
				],
				birthdayShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				experienceShow: false,
				availableList: [],
				classShow: false,
				classList: [],
				coachShow: false, 
				coachList: [],
			}
		},
		onLoad(options) {
			const item = JSON.parse(decodeURIComponent(options.item));
			this.venueId = item.venueId
			this.subscribelForm = { ...item, classId: item.classId, sex:item.sex == 0 ? '男' : '女', coachName: item.coachId != -1 ? item.coachName : ''}
		},
		onReady() {
			this.$refs.subscribelForm.setRules(this.rules);
		},
		methods: {
			//  显示班级列表
			handleClassInfoShow() {
				this.classShow = true
				this.getClassFromVenue()
				console.log(this.venueId);
			},
			//  获取班级列表
			getClassFromVenue() {
				NET.request(API.getClassListByVenueId, { id:this.venueId }, 'POST').then(res => {
					if(res.status === 10000) {
						this.classList = res.data.map( item => { return { id: item.id, text: item.name } })
						this.classList = this.classList.reverse()
						this.subscribelForm.coachId = ''
						this.subscribelForm.coachName = ''
					} else {
						this.$refs.uTips.show({
							title: res.message,
							type: 'error',
						})
					}
				})
			},
			//  选择班级
			HandleChooseClassClick(index) {
				this.subscribelForm.classId = this.classList[index].id
				this.subscribelForm.name = this.classList[index].text
			},
			//  显示教练列表
			handleCoachListShow() {
				this.coachList = []
				this.coachShow = true
				if(this.subscribelForm.classId==0){
					//  教练列表
					NET.request(API.getCoachListAll, {}, 'GET').then(res=> {
						if(res.status == 10000) {
							var coachSearch = res.data.filter((item) => {
							  return item.nickname.indexOf(this.subscribelForm.coachName)>-1;
							});
							this.coachList = coachSearch.map( item => {
								return { id: item.coachId, text: item.nickname }
							})
							console.log(this.coachList)
						} else {
							this.$refs.uTips.show({
								title: res.message,
								type: 'error',
							})
						}
					})
				}else{
					//  教练列表
					NET.request(API.getCoachList, {venueId: this.venueId, classId: this.subscribelForm.classId }, 'POST').then(res=> {
						if(res.status == 10000) {
							this.coachList = res.data.map( item => { return { id: item.coachId, text: item.nickname }})
						} else {
							this.$refs.uTips.show({
								title: res.message,
								type: 'error',
							})
						}
					})
				}
			},
			//  选择教练
			HandleChooseCoachClick(index) {
				this.subscribelForm.coachId = this.coachList[index].id
				this.subscribelForm.coachName = this.coachList[index].text
			},
			//  设置性别
			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() {
				if(!this.subscribelForm.coachId || !this.subscribelForm.coachName) {
					this.$refs.uTips.show({
						title: '请选择教练',
						type: 'warning',
					})
					return
				}
				this.$refs.subscribelForm.validate(valid => {
					if (valid) {
						const data = {
							id: parseInt(this.subscribelForm.subscribeId),
							classId: parseInt(this.subscribelForm.classId),
							studentId: parseInt(this.subscribelForm.studentId),
							coachId: parseInt(this.subscribelForm.coachId)
						}
						NET.request(API.updateAppointment, data, 'POST').then(res => {
							if(res.status == 10000) {
								this.$refs.uTips.show({
									title: '保存成功',
									type: 'success',
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 1000)
							} else {
								this.$refs.uTips.show({
									title: res.message,
									type: 'warning',
								})
							}
						}).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>

