<template>
	<view>
		<uni-forms ref="baseForm" :model="baseFormData" labelWidth="75px" :rules="rules" validateTrigger="bind">
			<uni-forms-item label="姓名" name="name" required>
				<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" maxlength="6" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="手机号" name="phone" required>
				<button open-type="getPhoneNumber" type="primary" size="mini"
					@getphonenumber="getPhoneNumber">获取手机号</button>
			</uni-forms-item>
			<uni-forms-item label="微信号" name="wxid" required>
				<uni-easyinput v-model="baseFormData.wxid" placeholder="微信号(不便接听电话时微信沟通)" maxlength="25"
					trim="both" />
			</uni-forms-item>
			<uni-forms-item label="地址:" name="locationStr" required>
				<uni-easyinput type="text" v-model="baseFormData.locationStr" @focus="getLocation" maxlength="250" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="选择课程" required>
				<uni-data-picker placeholder="请选择课程" popup-title="课程大纲-具体课程" v-model="baseFormData.subjectSmall" :clearIcon="false"
					:localdata="courseTree" />
			</uni-forms-item>
			<uni-forms-item label="辅导方式" required>
				<uni-data-checkbox v-model="baseFormData.mode" :localdata="modes" />
			</uni-forms-item>
			<uni-forms-item label="学员性别" required>
				<uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" />
			</uni-forms-item>
			<uni-forms-item label="课时费" name="salary" required :errorMessage="errorPrice">
				<uni-easyinput v-model="baseFormData.salary" type="number" placeholder="请输入课时金额" />
			</uni-forms-item>
			<uni-forms-item label="上课时间" required>
			</uni-forms-item>
			<view class="tr-container">
				<view class="tr_1">
					<text class="th_0" decode="true">&ensp;&ensp;&ensp;&ensp;</text>
					<text class="th_1">周一</text>
					<text class="th_1">周二</text>
					<text class="th_1">周三</text>
					<text class="th_1">周四</text>
					<text class="th_1">周五</text>
					<text class="th_1">周六</text>
					<text class="th_1">周日</text>
				</view>
				<view class="tr_2">
					<checkbox-group @change="chechboxChangeAM">
						<view class="th2_0">上午</view>
						<label v-for="item in timeAM" :key="item.value">
							<checkbox class="th2_1" :value="item.value"></checkbox>
						</label>
					</checkbox-group>
				</view>
				<view class="tr_2">
					<checkbox-group @change="chechboxChangePM">
						<view class="th2_0">下午</view>
						<label v-for="item in timePM" :key="item.value">
						<checkbox class="th2_1" :value="item.value"></checkbox>
						</label>
					</checkbox-group>
				</view>
				<view class="tr_2">
					<checkbox-group @change="chechboxChangeEvening">
						<view class="th2_0">晚上</view>
						<label v-for="item in timeEvening" :key="item.value">
						<checkbox class="th2_1" :value="item.value"></checkbox>
						</label>
					</checkbox-group>
				</view>
			</view>
			<uni-forms-item label="期望目标" name="goal">
				<uni-easyinput type="textarea" v-model="baseFormData.goal" maxlength="100" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="教员身份" required>
				<uni-data-checkbox v-model="baseFormData.teacherType" :localdata="identifies" />
			</uni-forms-item>
			<uni-forms-item label="教员学历" required>
				<uni-data-select v-model="baseFormData.teacherEdu" :localdata="educations" />
			</uni-forms-item>
			<uni-forms-item label="教员性别" required>
				<uni-data-checkbox v-model="baseFormData.teacherGender" :localdata="sexsTeacher" />
			</uni-forms-item>
			<uni-forms-item label="教员照片" required>
				<uni-data-checkbox v-model="baseFormData.teacherProfilePhoto" :localdata="hasPhotoes" />
			</uni-forms-item>
			<uni-forms-item label="对教员要求" name="teacherDemanded">
				<uni-easyinput type="textarea" v-model="baseFormData.teacherDemanded" maxlength="100" trim="both" />
			</uni-forms-item>
		</uni-forms>
		<view>
			<button type="primary" @click="submit">提交需求</button>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	
	export default {
		computed: {
			...mapState('m_user', ['userinfo'])
		},
		data() {
			return {
				loading: false,
				errorPrice: '',
				courseAM: [],
				coursePM: [],
				courseEV: [],
				// 表单数据
				baseFormData: {
					name: '',
					phone: '',
					wxid: '',
					subjectSmall: '1-1',
					mode: '教员上门',
					sex: '男',
					salary: 0,
					courseWeekday: [],
					goal: '',
					teacherType: '专职教员',
					teacherEdu: '',
					teacherGender: '男',
					teacherProfilePhoto: '有',
					teacherDemanded: '',
					locationStr: '',
					locationAl: ''
				},
				coursePrice: [],
				// 选择课程
				courseTree: [{
						text: '一年级',
						value: '1-0',
						children: [{
								text: '1.1班',
								value: '1-1'
							}
						]
					}
				],
				// 单选辅导方式数据源
				modes: [{
					text: '教员上门',
					value: '教员上门'
				}, {
					text: '学员上门',
					value: '学员上门'
				}, {
					text: '线上辅导',
					value: '线上辅导'
				}],
				// 单选性别数据源
				sexs: [{
					text: '男',
					value: '男'
				}, {
					text: '女',
					value: '女'
				}],
				// 单选老师身份
				identifies: [{
					text: '专职教员',
					value: '专职教员'
				}, {
					text: '学生教员',
					value: '学生教员'
				}],
				educations: [{
					value: '专科在读',
					text: '专科在读及以上'
				}, {
					value: '专科毕业',
					text: '专科毕业及以上'
				}, {
					value: '本科在读',
					text: '本科在读及以上'
				}, {
					value: '本科毕业',
					text: '本科毕业及以上'
				}, {
					value: '硕士在读',
					text: '硕士在读及以上'
				}, {
					value: '硕士毕业',
					text: '硕士毕业及以上'
				}, {
					value: '博士在读',
					text: '博士在读及以上'
				}, {
					value: '博士毕业',
					text: '博士毕业及以上'
				}, {
					value: '不限',
					text: '不限'
				}],
				// 单选老师性别
				sexsTeacher: [{
					text: '男',
					value: '男'
				}, {
					text: '女',
					value: '女'
				}],
				// 单选老师有无照片
				hasPhotoes: [{
					text: '有',
					value: '有'
				}, {
					text: '无',
					value: '无'
				}],
				// 上课时间
				// 上午
				timeAM: [{
					value: 'A1',
						name: '1'
						
					},
					{
						name: '2',
						value: 'A2'
					},
					{
						name: '3',
						value: 'A3'
					},
					{
						name: '4',
						value: 'A4'
					},
					{
						name: '5',
						value: 'A5'
					},
					{
						name: '6',
						value: 'A6'
					},
					{
						name: '7',
						value: 'A7'
					}
				],
				// 下午
				timePM: [{
					value: 'P1',
						name: '1'
						
					},
					{
						name: '2',
						value: 'P2'
					},
					{
						name: '3',
						value: 'P3'
					},
					{
						name: '4',
						value: 'P4'
					},
					{
						name: '5',
						value: 'P5'
					},
					{
						name: '6',
						value: 'P6'
					},
					{
						name: '7',
						value: 'P7'
					}
				],
				// 晚上
				timeEvening: [{
						name: '1',
						value: 'E1'
					},
					{
						name: '2',
						value: 'E2'
					},
					{
						name: '3',
						value: 'E3'
					},
					{
						name: '4',
						value: 'E4'
					},
					{
						name: '5',
						value: 'E5'
					},
					{
						name: '6',
						value: 'E6'
					},
					{
						name: '7',
						value: 'E7'
					}
				],
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						}]
					},
					wxid: {
						rules: [{
							required: true,
							errorMessage: '请输入微信号',
						}, {
							errorMessage: '请输入正确的微信号',
							pattern: '^[a-zA-Z][a-zA-Z\\d_-]{5,19}$',
						}]
					},
					salary: {
						rules: [{
							required: true,
							errorMessage: '请输入课时费'
						}]
					},
				}
			};
		},
		created() {
			this.getPriceAndTree()
		},
		methods: {
			// 手机号
			// 手机号
			async getPhoneNumber(e) {
				// console.log(e.detail.code)
				if (e.detail.code === undefined) return uni.$showMsg('获取手机号失败！')
				const query = {
					code: e.detail.code
				}
				const {
					data: result
				} = await uni.$http.get('/ucenter/mini-program-openid-uid/wxGetPhone', query)
				if (result.code === 20000) {
					this.baseFormData.phone = result.data.phone
					return uni.$showMsg('获取手机成功！')
				}
			},
			// 树形结构的课程和价格表
			async getPriceAndTree() {
				const {
					data: result
				} = await uni.$http.get('/education/course-price/treeAndPrice')
				// console.log(result)
				this.courseTree = result.data.treeCourse
				this.coursePrice = result.data.coursePrice
			},
			// 上午复选框
			chechboxChangeAM(e) {
				this.courseAM.push(e.detail.value)
			},
			// 下午复选框
			chechboxChangePM(e) {
				this.coursePM.push(e.detail.value)
			},
			// 晚上复选框
			chechboxChangeEvening(e) {
				this.courseEV.push(e.detail.value)
			},
			// 获取老师地理位置
			getLocation() {
				// wx.choosePoi({
				// 	success: res => {
				// 		console.log(res)
				// 		if (res.type === 0) {
				// 			// 不显示位置
				// 			this.baseFormData.locationStr = ''
				// 		} else if (res.type === 2) {
				// 			// 详细位置
				// 			this.baseFormData.locationStr = res.address
				// 		} else if (res.type === 1) {
				// 			// 概要位置
				// 			this.baseFormData.locationStr = res.city
				// 		}
						
				// 	}
				// })
				wx.chooseLocation({
					success: res => {
						if (res.errMsg === 'chooseLocation:ok') {
							this.baseFormData.locationStr = res.address + res.name
							this.baseFormData.locationAl = res.latitude + ',' + res.longitude
						} 
					}
				})
			},
			// 提交
			submit() {
				if (this.isLoading) {
					return uni.$showMsg('操作过快,请耐心等待…')
				}
				this.isLoading = true
				this.baseFormData.courseWeekday = []
				this.$refs.baseForm.validate().then(res => {
					if (this.baseFormData.phone === '') return uni.$showMsg('手机号未获取！')
					if (this.baseFormData.locationStr === '') return uni.$showMsg('请选择地址！')
					if (this.baseFormData.subjectSmall === '1-1' || this.baseFormData.subjectSmall === '') return uni.$showMsg('请选择课程')
					if (this.courseAM.length === 0 && this.coursePM.length === 0 && this.courseEV.length === 0) return uni.$showMsg('请选择上课时间！')
					if (this.courseAM[this.courseAM.length - 1] == '' && this.coursePM.length === 0 && this.courseEV.length === 0) return uni.$showMsg('请选择上课时间！')
					if (this.courseAM.length === 0 && this.coursePM[this.coursePM.length - 1] == '' && this.courseEV.length === 0) return uni.$showMsg('请选择上课时间！')
					if (this.courseAM.length === 0 && this.coursePM.length === 0 && this.courseEV[this.courseEV.length - 1] == '') return uni.$showMsg('请选择上课时间！')
					if (this.courseAM[this.courseAM.length - 1] == '' && this.coursePM[this.coursePM.length - 1] == '' && this.courseEV.length === 0) return uni.$showMsg('请选择上课时间！')
					if (this.courseAM[this.courseAM.length - 1] == '' && this.coursePM.length === 0 && this.courseEV[this.courseEV.length - 1] == '') return uni.$showMsg('请选择上课时间！')
					if (this.courseAM.length === 0 && this.coursePM[this.coursePM.length - 1] == '' && this.courseEV[this.courseEV.length - 1] == '') return uni.$showMsg('请选择上课时间！')
					if (this.courseAM[this.courseAM.length - 1] == '' && this.coursePM[this.coursePM.length - 1] == '' && this.courseEV[this.courseEV.length - 1] == '') return uni.$showMsg('请选择上课时间！')
					if (this.courseAM.length > 0) {
						this.baseFormData.courseWeekday.push(this.courseAM[this.courseAM.length - 1])
					}
					if (this.coursePM.length > 0) {
						this.baseFormData.courseWeekday.push(this.coursePM[this.coursePM.length - 1])
					}
					if (this.courseEV.length > 0) {
						this.baseFormData.courseWeekday.push(this.courseEV[this.courseEV.length - 1])
					}
					// 上课时间
					this.baseFormData.courseWeekday = this.baseFormData.courseWeekday.join()
					// console.log(this.baseFormData.courseWeekday)
					// 教员学历
					if (this.baseFormData.teacherEdu === '') return uni.$showMsg('请选择教员学历！')
					
					// 课时费
					let city = this.userinfo.city
					for(let i = 0; i < this.coursePrice.length; i++) {
						if(this.coursePrice[i].courseName === this.baseFormData.subjectSmall) {
							// console.log(this.coursePrice[i].priceMapping.stu_price_2_l)
							// 二线城市
							if (city != '北京' || city != '上海' || city != '广州' || city != '深圳') {
								// 学生
								if (this.baseFormData.teacherType === '学生教员') {
									if (this.coursePrice[i].priceMapping.stu_price_2_l > this.baseFormData.salary || this.baseFormData.salary > this.coursePrice[i].priceMapping.stu_price_2_h) {
										this.errorPrice = '价格在' + this.coursePrice[i].priceMapping.stu_price_2_l + '~' + this.coursePrice[i].priceMapping.stu_price_2_h + '之间'
										return uni.$showMsg('请合理选择课时费')
									}
								} else {
									if (this.coursePrice[i].priceMapping.fulltime_price_2_l > this.baseFormData.salary || this.baseFormData.salary > this.coursePrice[i].priceMapping.fulltime_price_2_h) {
										this.errorPrice = '价格在' + this.coursePrice[i].priceMapping.fulltime_price_2_l + '~' + this.coursePrice[i].priceMapping.fulltime_price_2_h + '之间'
										return uni.$showMsg('请合理选择课时费')
									}
								}
							} else {
								// 学生
								if (this.baseFormData.teacherType === '学生教员') {
									if (this.coursePrice[i].priceMapping.stu_price_1_l > this.baseFormData.salary || this.baseFormData.salary > this.coursePrice[i].priceMapping.stu_price_1_h) {
										this.errorPrice = '价格在' + this.coursePrice[i].priceMapping.stu_price_1_l + '~' + this.coursePrice[i].priceMapping.stu_price_1_h + '之间'
										return uni.$showMsg('请合理选择课时费')
									}
								} else {
									if (this.coursePrice[i].priceMapping.fulltime_price_1_l > this.baseFormData.salary || this.baseFormData.salary > this.coursePrice[i].priceMapping.fulltime_price_1_h) {
										this.errorPrice = '价格在' + this.coursePrice[i].priceMapping.fulltime_price_1_l + '~' + this.coursePrice[i].priceMapping.fulltime_price_1_h + '之间'
										return uni.$showMsg('请合理选择课时费')
									}
								}
							}
						}
					}
					console.log(this.baseFormData)
					
					uni.request({
						url: 'http://192.168.0.207:8222/education/student-requirements/publishRequirements',
						data: this.baseFormData,
						header: {
							token: uni.getStorageSync('token')
						},
						method: 'POST',
						success: res => {
							uni.$showMsg(res.data.message)
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}
					})
					
				}).catch(err => {

					this.isLoading = false
					// console.log(err)
				})
				this.isLoading = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tr-container{
	    display: flex;
	    position: relative;
	    width: 96%;
	    flex-direction: column;
	    font-size: 26rpx;
	    /* border: 1rpx solid gray; */
	    margin: -10px 20rpx;
		margin-bottom: 20px;
	}
	
	.tr_1 {
	    display: flex;
	    position: relative;
	    height: 80rpx;
	    line-height: 80rpx;
	}
	.tr_2{
	    display: block;
	    height: 80rpx;
	    line-height: 80rpx;
	}
	
	.th_0,
	.th_1,
	.th_2,
	.th2_0,
	.th2_1,
	.th2_2{
	    width: 12%;
	    height: 80rpx;
	    line-height: 80rpx;
	    border-right: 1rpx solid gray;
	    border-bottom: 1rpx solid gray;
	    text-align: center;
	}
	
	.th_0,
	.th_1,
	.th_2{
	    border-top: 1rpx solid gray;
	}
	
	.th_0,
	.th2_0{
	    border-left: 1rpx solid gray;
	}
	
	.th2_0{
	    float: left;
	    width: 12%;
	    height: 80rpx;
	    line-height: 80rpx;
	    text-align: center;
	    /* background-color: greenyellow; */
	}
</style>
