<template>
	<view>
		<uni-forms ref="baseForm" :modelValue="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="手机号" required>
				<button open-type="getPhoneNumber" type="primary" size="mini"
					@getphonenumber="getPhoneNumber">获取手机号</button>
			</uni-forms-item>
			<uni-forms-item label="微信号" name="weixinId" required>
				<uni-easyinput v-model="baseFormData.weixinId" placeholder="微信号(不便接听电话时微信沟通)" maxlength="25"
					trim="both" />
			</uni-forms-item>
			<uni-forms-item label="英文名" name="englishName">
				<uni-easyinput v-model="baseFormData.englishName" placeholder="请输入你的英文名" maxlength="25" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="身份证号" name="idCard" required>
				<uni-easyinput v-model="baseFormData.idCard" type="idcard" placeholder="请输入你的身份证号" />
			</uni-forms-item>
			<uni-forms-item label="性别" name="sex" required>
				<uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" />
			</uni-forms-item>
			<uni-forms-item label="身份" name="identify" required>
				<uni-data-checkbox v-model="baseFormData.identify" :localdata="identifies" />
			</uni-forms-item>
			<uni-forms-item label="身份证照片" required>
				<text style="color: blue;">(说明：需要能看清楚证件号码、日期及头像)</text>
				<uni-file-picker @select="selectIdCardFront" limit="1" title="身份证正面" />
				<uni-file-picker @select="selcetIdCardBack" limit="1" title="身份证背面" />
			</uni-forms-item>
			<uni-forms-item label="学生/毕业证" required>
				<text style="color: blue;">(已毕业的需要上传毕业证)</text>
				<uni-file-picker limit="1" @select="selectDiplomaPhoto" />
			</uni-forms-item>
			<uni-forms-item label="头像" required>
				<text style="color: blue;">(说明：只能上传证件照，上传其他照片审核不予通过)</text>
				<uni-file-picker limit="1" @select="selectProfilePhoto" />
			</uni-forms-item>
			<uni-forms-item label="毕业/就读学校" name="school" required>
				<uni-easyinput v-model="baseFormData.school" placeholder="请输入学校全称" maxlength="30" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="所学专业" name="major" required>
				<uni-easyinput v-model="baseFormData.major" placeholder="请输入专业名称" maxlength="20" trim="both" />
			</uni-forms-item>
			<uni-forms-item label="学历" name="education" required>
				<uni-data-select v-model="baseFormData.education" :localdata="educations" />
			</uni-forms-item>
			<uni-forms-item label="籍贯" name="nativePlace" required>
				<uni-easyinput v-model="baseFormData.nativePlace" placeholder="xx省xx市xx区" maxlength="30" trim="both" />
			</uni-forms-item>
		</uni-forms>
		<view>
			<button type="primary" @click="submit">提交审核</button>
		</view>
		
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" confirmText="确定" title="温馨提示" :content="authentication" @close="dialogClose" @confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	
	export default {
		computed: {
			...mapState('m_user', ['authentication'])
		},
		data() {
			return {
				isLoading: false, // 节流阀
				baseFormData: {
					name: '',
					weixinId: '',
					idCard: '511002200005061387',
					sex: '男',
					identify: '专职老师',
					school: '',
					major: '',
					education: '',
					nativePlace: '',
					education: '本科在读',
					diplomaPhoto: ''
				},
				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: '博士毕业'
				}],
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						}]
					},
					weixinId: {
						rules: [{
							required: true,
							errorMessage: '请输入微信号',
						}, {
							errorMessage: '请输入正确的微信号',
							pattern: '^[a-zA-Z][a-zA-Z\\d_-]{5,19}$',
						}]
					},
					idCard: {
						rules: [{
							required: true,
							errorMessage: '请输入身份证号',
						}, {
							errorMessage: '请输入正确的身份证号',
							pattern: '^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$',
						}]
					},
					school: {
						rules: [{
							required: true,
							errorMessage: '请输入学校全称',
						}]
					},
					major: {
						rules: [{
							required: true,
							errorMessage: '请输入专业名称',
						}]
					},
					nativePlace: {
						rules: [{
								required: true,
								errorMessage: '请输入籍贯',
							},
							{
								pattern: '[\\u4e00-\\u9fa5]+[\\u7701]{1}[\\u4e00-\\u9fa5]+[\\u5E02]{1}[\\u4e00-\\u9fa5]+[\\u53BF|\\u533A]{1}',
								errorMessage: '请输入正确的格式：xx省xx市xx区/县',
							}
						]
					}
				}
			};
		},
		created() {
		},
		mounted() {
			this.openDialog()
		},
		methods: {
			...mapMutations('m_user', ['updateAuthentication']),
			openDialog() {
				// console.log(this.authentication)
				if (this.authentication !== '') {
					this.$refs.alertDialog.open()
				}
			},
			// 取消按钮
			dialogClose() {
				this.$refs.alertDialog.close()
				if (this.authentication === '正在审核中…') {
					uni.navigateBack()
				} else if (this.authentication === '认证成功,点击前往课程发布') {
					uni.navigateBack()
				} else {
					this.updateAuthentication('')
				}
			},
			// 确定按钮
			dialogConfirm() {
				this.$refs.alertDialog.close()
				if (this.authentication === '正在审核中…') {
					uni.navigateBack()
				} else if (this.authentication === '认证成功,点击前往课程发布') {
					uni.redirectTo({
						url: '/subpkg/course_publish/course_publish'
					})
				} else {
					this.updateAuthentication('')
				}
			},
			// 手机号
			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('获取手机成功！')
				}
			},
			// 上传身份证正面
			selectIdCardFront(e) {
				this.getUploadPaths(e, 'this.baseFormData.idCardFront')
			},
			// 上传身份证背面
			selcetIdCardBack(e) {
				this.getUploadPaths(e, 'this.baseFormData.idCardBack')
			},
			// 上传毕业/学生证
			selectDiplomaPhoto(e) {
				this.getUploadPaths(e, 'this.baseFormData.diplomaPhoto')
			},
			// 上传头像
			selectProfilePhoto(e) {
				this.getUploadPaths(e, 'this.baseFormData.profilePhoto')
			},
			getUploadPaths(e, item) {
				var that = this
				wx.uploadFile({
					url: 'http://192.168.0.207:8222/file/upload',
					filePath: e.tempFilePaths[0],
					name: 'file',
					header: {
						token: uni.getStorageSync('token')
					},
					success(res) {
						const result = JSON.parse(res.data)
						const item1 = item.substring(item.lastIndexOf('.') + 1)
						// console.log(item1)
						that.baseFormData[item1] = result.data.path + '\\' + result.data.fileName
						// console.log(that.baseFormData)
					}
				})
			},
			submit() {
				if (this.isLoading) {
					return uni.$showMsg('操作过快,请耐心等待…')
				}
				this.isLoading = true
				this.$refs.baseForm.validate().then(res => {
					if (this.baseFormData.phone === undefined) return uni.$showMsg('手机号未获取！')
					if (this.baseFormData.idCardFront === undefined) return uni.$showMsg('身份证正面未上传！')
					if (this.baseFormData.idCardBack === undefined) return uni.$showMsg('身份证背面未上传！')
					if (this.baseFormData.diplomaPhoto === undefined) return uni.$showMsg('学生/毕业证未上传！')
					if (this.baseFormData.profilePhoto === undefined) return uni.$showMsg('头像未上传！')

					uni.request({
						url: 'http://192.168.0.207:8222/education/teacher-certifications/saveCertificationInfo',
						data: this.baseFormData,
						header: {
							token: uni.getStorageSync('token')
						},
						method: 'POST',
						success: res => {
							this.isLoading = false
							uni.$showMsg(res.data.message)
							this.updateAuthentication('正在审核中…')
						}
					})
					
					uni.navigateBack()
				}).catch(err => {

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

<style lang="scss" scoped>
.warning {
	color: red
}
</style>
