<template>
	<view>
		<view style="padding:5rpx 20rpx 45rpx 20rpx;">
			<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="data.userName" disabled>
							</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-easyinput primaryColor="#37babd" v-model="data.department" disabled>
							</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="data.hiredate"
								@maskClick="maskClick" @change="onchange()" disabled />
						</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.applyTime"
								@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 class="pickerView">
							<picker @change="bindPickerChange" :value="dimissionType.index" :range="dimissionType.data"
								range-key="dictLabel">
								<view class="pickerText"
									v-if="dimissionType.data && dimissionType.data.length>0 && dimissionType.index !== undefined">
									{{ dimissionType.data[dimissionType.index].dictLabel }}
								</view>
								<view class="pickerText" v-else>
									待 选 择
								</view>
							</picker>
						</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,
				data: {},
				selectTitle: '',
				hxForm: {},
				dataTree: [],
				form: {},
				dimissionType: {
					data: [],
					index: undefined
				}
			}
		},
		onLoad() {
			//写入基本信息
			this.dimissionType.data = uni.getStorageSync('dimission_type')
			this.data = uni.getStorageSync('files')
			this.form.applyUserName = this.data.userName
			//翻译部门
			this.data.department = this.$dictTranslation(this.data.department, 'deptData', 'dept')
		},
		methods: {
			async submit() {
				this.isDisabled = true
				if (!this.form.applyTime) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择离职时间',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				if (!this.form.dimissionType) {
					this.isDisabled = false
					uni.showModal({
						content: '请选择离职原因',
						title: '提交失败',
						showCancel: false
					})
					return
				}
				setTimeout(() => {
					this.isDisabled = false
				}, 1000)
				//统一写入参数
				this.form.personnelId = this.data.id
				this.form.hireOrDimission = '2'
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/submitApply/personnelDimission',
					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
			},
			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
			},
			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()
						}
					})
				}
			},
			onchange(e) {
				setTimeout(() => {

				}, 200)
			},
			maskClick(e) {},
			bindPickerChange: function(e) {
				this.dimissionType.index = e.detail.value
				this.form.dimissionType = this.dimissionType.data[this.dimissionType.index].dictValue
			},
		}
	}
</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>
