<template>
	<view>
		<view style="padding:5rpx 20rpx 45rpx 20rpx;">
			<view style="background-color: #f7f7f7;padding: 20rpx;border-radius: 10rpx;color: #000000;">
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								姓名
							</view>
						</uni-col>
						<uni-col :span="19">
							<view>
								<uni-easyinput primaryColor="#37babd" v-model="form.name">
								</uni-easyinput>
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								性别
							</view>
						</uni-col>
						<uni-col :span="19">
							<view class="pickerView">
								<picker @change="sexsBindPickerChange" :value="sexs.index" :range="sexs.data"
									range-key="dictLabel">
									<view class="pickerText"
										v-if="sexs.data && sexs.data.length>0 && sexs.index !== undefined">
										{{ sexs.data[sexs.index].dictLabel }}
									</view>
									<view class="pickerText" v-else>
										待 选 择
									</view>
								</picker>
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								手机号
							</view>
						</uni-col>
						<uni-col :span="19">
							<view>
								<uni-easyinput primaryColor="#37babd" v-model="form.phone">
								</uni-easyinput>
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								生日
							</view>
						</uni-col>
						<uni-col :span="19">
							<view>
								<uni-datetime-picker type="date" :clear-icon="false" v-model="form.birthdate"
									@maskClick="maskClick" @change="onchange()" />
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								毕业院校
							</view>
						</uni-col>
						<uni-col :span="19">
							<view>
								<uni-easyinput primaryColor="#37babd" v-model="form.graduateSchool">
								</uni-easyinput>
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<uni-row>
						<uni-col :span="5">
							<view class="desc" style="font-size: 25rpx;">
								学历学位
							</view>
						</uni-col>
						<uni-col :span="19">
							<view>
								<view class="pickerView">
									<picker @change="degreeBindPickerChange" :value="degree.index" :range="degree.data"
										range-key="dictLabel">
										<view class="pickerText"
											v-if="degree.data && degree.data.length>0 && degree.index !== undefined">
											{{ degree.data[degree.index].dictLabel }}
										</view>
										<view class="pickerText" v-else>
											待 选 择
										</view>
									</picker>
								</view>
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view class="desc">
					<view class="desc" style="font-size: 25rpx;">
						专业技能</view>
					<view>
						<uni-easyinput type="textarea" primaryColor="#37babd" v-model="form.skill">
						</uni-easyinput>
					</view>
				</view>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							入职部门
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="deptBindPickerChange" :value="deptConfig.deptIndex"
								:range="deptConfig.deptDatas" range-key="deptName">
								<view class="pickerText"
									v-if="deptConfig.deptDatas && deptConfig.deptDatas.length>0 && deptConfig.deptIndex !== undefined">
									{{ deptConfig.deptDatas[deptConfig.deptIndex].deptName }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							入职岗位
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="postBindPickerChange" :value="post.index" :range="post.data"
								range-key="postName">
								<view class="pickerText"
									v-if="post.data && post.data.length>0 && post.index !== undefined">
									{{ post.data[post.index].postName }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							报到时间
						</view>
					</uni-col>
					<uni-col :span="19">
						<view>
							<uni-datetime-picker type="date" :clear-icon="false" v-model="form.interviewTime"
								@maskClick="maskClick" @change="onchange()" />
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				面试形式

			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							录用结论
						</view>
					</uni-col>
					<uni-col :span="19">
						<view class="pickerView">
							<picker @change="hireBindPickerChange" :value="hire_result.index" :range="hire_result.data"
								range-key="dictLabel">
								<view class="pickerText"
									v-if="hire_result.data && hire_result.data.length>0 && hire_result.index !== undefined">
									{{ hire_result.data[hire_result.index].dictLabel }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<uni-row>
					<uni-col :span="5">
						<view class="desc" style="font-size: 25rpx;">
							录用职级
						</view>
					</uni-col>
					<uni-col :span="19">
						<view>
							<uni-easyinput primaryColor="#37babd" v-model="form.name">
							</uni-easyinput>
						</view>
					</uni-col>
				</uni-row>
			</view>
			<view class="desc">
				<view class="desc" style="font-size: 25rpx;">
					备注
				</view>
				<view>
					<uni-easyinput type="textarea" primaryColor="#37babd" v-model="form.remark">
					</uni-easyinput>
				</view>
			</view>
		</view>
		<u-gap height="70" bgColor="#ffffff"></u-gap>
		<view class="bottim_view">
			<view>
				<u-row>
					<u-col span="11">
						<u-button :disabled="isDisabled" class="btnDoPay" shape="circle" @click="submit"
							color="rgb(55,186,189)" text="提交"></u-button>
					</u-col>
				</u-row>
			</view>
		</view>
		<!-- 下一步选择人员弹窗 -->
		<uni-popup ref="popup" background-color="#fff">
			<view style="height: 800rpx;">
				<view class="desc">
					<uni-row>
						<uni-col :span="10">
							<view style="font-size: 40rpx;color: rgb(145, 145, 145);">
							</view>
						</uni-col>
						<uni-col :span="11">
							<view style="font-size: 40rpx;color: rgb(98, 98, 98);">
								下一步
							</view>
						</uni-col>
						<uni-col :span="3">
							<view @click="submitPass" style="font-size: 40rpx;color: rgb(55,186,189);">
								确认
							</view>
						</uni-col>
					</uni-row>
				</view>
				<view>
					<!--  -->
					<uni-row>
						<uni-col>
							<view style="padding: 15rpx 20rpx;">
								<uni-data-picker ref="picker" placeholder="请选择" :popup-title="selectTitle"
									:localdata="dataTree" v-model="hxForm.candidate" @change="nextApprovalChange">
								</uni-data-picker>
							</view>
						</uni-col>
					</uni-row>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isDisabled: false,
				selectTitle: '',
				hxForm: {},
				dataTree: [],
				fileList: [],
				form: {},
				header: {
					"Authorization": "Bearer " + uni.getStorageSync('token')
				},
				index: 0,
				sexs: {
					data: [],
					index: undefined
				},
				degree: {
					data: [],
					index: undefined
				},
				interview_type: {
					data: [],
					index: undefined
				},
				post: {
					data: [],
					index: undefined
				},
				hire_result: {
					data: uni.getStorageSync('hire_result'),
					index: undefined
				},
				deptConfig: {
					deptDatas: uni.getStorageSync('deptData'),
					deptIndex: undefined
				},
			}
		},
		onLoad() {
			//机构信息初始化
			this.sexs.data = uni.getStorageSync('sys_user_sex')
			this.degree.data = uni.getStorageSync('degree_dict')
			this.post.data = uni.getStorageSync('postsData')
			this.interview_type.data = uni.getStorageSync('interview_type')
		},
		methods: {
			async submit() {
				this.isDisabled = true
				if (!this.form.name) {
					this.isDisabled = false
					uni.showModal({
						content: '请输入姓名',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.sex) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择性别',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.phone) {
					this.isDisabled = false
					uni.showModal({
						content: '请输入手机号',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (this.form.phone.length != 11) {
					this.isDisabled = false
					uni.showModal({
						content: '请输入手机号',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.birthdate) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择出生日期',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.degree) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择学历',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.skill) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择专业技能',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.jobInterview) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择意向岗位',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.interviewType) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择面试时间',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.interviewer) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择面试官',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.skillScore) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择技术水平',
						title: '提交失败',
						showCancel: false
					})
					return
				}

				if (!this.form.expectSalary) {
					this.isDisabled = false
					uni.showModal({
						content: '请填写期望薪资',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				// if (this.fileList.length === 0) {
				// 	this.isDisabled = false
				// 	uni.showModal({
				// 		content: '请上传简历附件',
				// 		title: '提交失败',
				// 		showCancel: false
				// 	})
				// 	return
				// }

				setTimeout(() => {
					this.isDisabled = false
				}, 1000)
				//统一写入参数
				this.form.userCode = uni.getStorageSync('setUserName')
				this.form.avatar = this.fileList[0].url
				this.form.hireOrDimission = '1'
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/submitApply/personnelHire',
					method: 'post',
					data: this.form
				});
				if (res.code === 200) {
					this.dataTree = res.data.tree
					this.hxForm.taskId = res.data.taskId
					this.$refs.popup.open('bottom')
					this.selectTitle = "请选择" + this.dataTree[0].text
					this.isDisabled = false
				} else {
					this.$showModal(res.msg)
				}
				this.isDisabled = false
			},
			async getPostData() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/posts',
					method: 'get'
				});
				if (res.code === 200) {
					this.post.data = res.data
				} else {

				}
			},
			nextApprovalChange(e) {
				// console.log('onchange:', e.detail.value[1].text);
				this.hxForm.applyUserName = e.detail.value[1].text
				this.hxForm.candidate = e.detail.value[1].value
			},
			onchange(e) {
				setTimeout(() => {

				}, 200)
			},
			maskClick(e) {

			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
			},
			deptBindPickerChange: function(e) {
				this.deptConfig.deptIndex = e.detail.value
				this.form.department = this.deptConfig.deptDatas[this.deptConfig.deptIndex].deptId
			},
			postBindPickerChange: function(e) {
				this.post.index = e.detail.value
				this.form.jobInterview = this.post.data[this.post.index].postId
			},
			hireBindPickerChange: function(e) {
				this.hire_result.index = e.detail.value
				this.form.hire_result = this.hire_result.data[this.hire_result.index].dictValue
			},
			interviewTypeBindPickerChange: function(e) {
				this.interview_type.index = e.detail.value
				this.form.interviewType = this.interview_type.data[this.interview_type.index].dictValue
			},
			sexsBindPickerChange: function(e) {
				this.sexs.index = e.detail.value
				this.form.sex = this.sexs.data[this.sexs.index].dictValue
			},
			degreeBindPickerChange: function(e) {
				this.degree.index = e.detail.value
				this.form.degree = this.degree.data[this.degree.index].dictValue
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			async submitPass() {
				if (!this.hxForm.candidate) {
					uni.showModal({
						content: '请选择' + this.dataTree[0].text,
						title: '提交失败',
						showCancel: false,
					})
				}
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/task/submit/candidate?taskId=' + this.hxForm.taskId +
						'&candidate=' + this.hxForm.candidate + '&applyUserName=' + this.hxForm.applyUserName,
					method: 'get',
				});
				if (res.code === 200) {
					uni.showModal({
						content: '已提交给审批人员',
						title: '提交成功',
						showCancel: false,
						success() {
							uni.navigateBack()
						}
					})
				} else {
					uni.showModal({
						content: res.msg,
						title: '提交失败',
						showCancel: false,
						success() {
							uni.navigateBack()
						}
					})
				}
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen
				if (this.fileList) {
					fileListLen = this.fileList.length
				} else {
					this.fileList = []
					fileListLen = 0
				}
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				this.form.involveFile = this.this.fileList[0].url
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$BASE_URL + '/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: this.header,
						success: (res) => {
							// console.log(JSON.parse(res.data).url);
							setTimeout(() => {
								resolve(JSON.parse(res.data).url)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.desc {
		padding: 10rpx 5rpx;
		color: #818181;
		font-size: 20rpx;
	}

	.pickerView {
		// align-items: flex-end;
		padding: 10rpx 20rpx;
		border: 1rpx #eeeeee solid;
	}

	.pickerText {
		font-size: 26rpx;
		color: #b1b1b1;
	}

	.bottim_view {
		padding: 5px 20px 30px;
		position: fixed;
		left: 0px;
		bottom: 0px;
		width: 100%;
		height: 40px;
		background-color: #ffffff;
	}
</style>
