<template>
	<view class="jobDetails">
		<view class="jobInfo">
			<view class="f_b">
				<view class="jobName">{{ detail.name }}</view>
				<view class="jobSalary">{{detail.salaryMin}}-{{detail.salaryMax}}元 {{detail.payTimes ? detail.payTimes + '薪' : ''}}</view>
			</view>
			<view class="companyName">{{detail.industryName}}</view>
			<view class="requirement f_s">
				<view class="f_s">
					<u-icon name="clock"/>
					{{detail.experience}}
				</view>
				<view class="f_s">
					<u-icon name="bookmark"/>
					{{detail.qualification}}
				</view>
				<view class="f_s">
					<u-icon name="bag"/>
					{{ detail.recruitType }}
				</view>
			</view>
			<u-cell icon="map" :title="`${detail.province} ${detail.city} ${detail.region} ${detail.address}`" :border="false"/>
		</view>
		<view class="jobDesc">
			<view class="moduleTitle">职位描述</view>
			<view style="white-space: pre-line;line-height: 2;font-weight: 300;">{{detail.details}}</view>
		</view>
		<view class="companyInfo" @click="toCompanyDetails(detail.org)">
			<view class="moduleTitle">公司信息</view>
			<view class="f_b" style="margin-top: 30rpx;">
				<view class="f_s">
					<u--image
						:src="detail.org.logo"
						width="60rpx"
						height="60rpx"
						radius="4"
					/>
					<text style="padding: 0 10px;color: #666;">{{detail.orgName}}</text>
				</view>
				<u-icon
				  size="20"
				  name="arrow-right"
				/>
			</view>
			<!-- <view class="f_s" style="font-weight: 300;color: #666;margin-top: 20rpx;">
				<u-icon
					size="30"
					color="#999"
					
					name="map-fill"
				/>
				{{detail.org.province}} {{detail.org.city}} {{detail.org.region}} {{detail.address}}
			</view> -->
		</view>
		<view class="footer f_b">
			<u-icon
				size="30"
				name="star-fill"
				color="#f40"
				v-if="collect"
				@click="shoucang"
			/>
			<u-icon
				size="30"
				name="star"
				@click="shoucang"
				v-else
			/>
			<view style="width: 20px;"></view>
			<u-button type="primary" text="已投递" v-if="signUp" disabled></u-button>
			<u-button type="primary" text="投递简历" v-else @click="deliver"></u-button>
		</view>
		<resume-popup
			:visible="visible"
			appId="2021005184646262"
			:outBizNo="outBizNo"
			templateId="DEFAULT"
			onClose="onClose"
			onSuccess="onSuccess"
			onFailure="onFailure"
		/>
		<resume-popup2
			:visible="visible2"
			appId="2021005184646262"
			outBizNo="outBizNo"
			:outJobId="detail.id"
			:jobName="detail.name"
			:recruitmentPlatformName="detail.orgName"
			templateId="DEFAULT"
			requiredAttributes="baseInfo|phone|educationLevel|workExperience"
			onClose="onClose2"
			onSuccess="onSuccess2"
			onFailure="onFailure2"
		/>
	</view>
</template>
<script>
	import { getResume } from '../../util/loginSetInfo.js'
	export default {
		data() {
			return {
				outBizNo: '',
				visible: false,
				visible2: false,
				recruitId: '',
				detail: {},
				userInfo: [],
				token: '',
				signUp: false, // 是否已报名
				collect: false, // 是否已收藏
				source: '',
			}
		},
		onLoad(o) {
			this.$scope.onClose = this.onClose;
			this.$scope.onClose2 = this.onClose2;
			this.$scope.onSuccess = this.onSuccess;
			this.$scope.onSuccess2 = this.onSuccess2;
			this.$scope.onFailure = this.onFailure;
			this.$scope.onFailure2 = this.onFailure2;
			this.recruitId = o.id
			this.source = o.source || ''
			this.token = uni.getStorageSync('jy-recruit-token') || ''
			this.$http.get('/public/recruit/details', {
				id: o.id,
			}).then(res => {
				this.detail = res.data
				this.getSignRecord()
			})
			this.getLoginInfo()
			if (o.source != 'jyzp' && !this.token) {
				const that = this
				uni.login({
					success(e) {
						that.$http.get('/public/ali/getopenId', {
							code: e.code,
						}).then(res => {
							const code = JSON.parse(res.code)
							console.log('getOpenId',code)
							const openId = code.alipay_system_oauth_token_response.open_id
							uni.setStorageSync('openId', openId)
							that.$http.post('/public/person/c', {
								openId: openId
							}).then(res => {
								uni.request({
									// url: 'http://192.168.0.6:8000/loginform',
									url: 'https://jyzp.365jft.com/jfb-recruit/loginform',
									method: 'POST',
									data: {
										account: openId,
										wxType: 'wx'
									},
									header: {
									  'Content-Type': 'application/x-www-form-urlencoded'
									},
									success: (res) => {
										uni.setStorageSync('jy-recruit-token',res.data.token)
										that.token = res.data.token
									},
									fail: (err) => {
									  console.log('请求失败', err);
									}
								});
							})
						})
					}
				})
			}
		},
		methods: {
			generateOutBizNo() {
			  const timestamp = new Date().getTime();
			  const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
			  return `job_auth_${timestamp}_${random}`;
			},
			
			handleTap() {
				this.outBizNo = this.generateOutBizNo()
				this.visible = true;
			},
			onFailure({code, message}) {
				console.log(code, message);
				this.visible = true
			},
			onClose() {
				this.visible = false;
			},
			onFailure2(res) {
				console.log(111111,res);
				this.visible2 = true
			},
			onClose2() {
				this.visible2 = false;
			},
			onSuccess(applyId) {
				this.visible = false;
				getResume(applyId,()=> {
					uni.showToast({
						title: '登录成功',
						icon: 'none'
					})
					this.token = uni.getStorageSync('jy-recruit-token')
					this.getSignRecord()
					this.getLoginInfo()
				})
			},
			onSuccess2(applyId) {
				this.visible2 = false;
				console.log('投递成功',applyId)
				this.signUp = true
				
				if (this.source == 'jyzp') {
					console.log(2)
					uni.showToast({
						title: `报名成功`,
						icon: 'none',
					})
				} else {
					uni.showModal({
						content: '投递成功',
						confirmText: '返回支付宝就业',
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								my.exitMiniProgram(); 
							}
						}
					})
				}
				// 直接通过安心工作证回调投递成功 无需再单独调用投递接口
				// if (this.userInfo.name && this.userInfo.tabList.length > 0) {
				// 	this.$http.post('/signRecord/c', {
				// 		recruitId: this.recruitId,
				// 	}).then(res => {
				// 		this.signUp = true
				// 		uni.showToast({
				// 			title: `报名成功`,
				// 			icon: 'none',
				// 		})
				// 	})
				// } else {
				// 	uni.showToast({
				// 		title: '请先完善简历',
				// 		icon: 'none'
				// 	})
				// 	uni.navigateTo({
				// 		url: `/pages/user/resume/index`
				// 	})
				// }
			},
			toCompanyDetails(org) {
				uni.setStorageSync('ogrDetails',org)
				uni.navigateTo({
					url: '/pages/home/companyDetails'
				})
			},
			getLoginInfo() {
				if (uni.getStorageSync('jy-recruit-token')) {
					this.$http.get('/person/person/details', {}).then(res => {
						this.userInfo = res.data
						this.$http.get('/objective/l', {personId: res.data.id}).then(res => {
							this.userInfo.tabList = res.data.list || []
						})
					})
				}
			},
			shoucang() {
				if (this.token) {
					this.$http.post('/collect/c', {
						recruitId: this.recruitId,
					}).then(res => {
						this.collect = !this.collect
						uni.showToast({
							title: this.collect ? `收藏成功` : '取消成功',
							icon: 'none',
						})
					})
				} else { // 未登录 触发登录
					this.handleTap()
				}
			},
			// 检查是否已报名
			getSignRecord() {
				if (this.token) {
					this.$http.post('/signRecord/have', {
						recruitId: this.recruitId,
					}).then(res => {
						this.signUp = res.data == '1' ? true : false
					})
					this.$http.post('/collect/have', {
						recruitId: this.recruitId,
					}).then(res => {
						this.collect = res.data == '1' ? true : false
					})
				}
			},
			// 投递简历
			deliver() {
				if (this.token) {
					this.outBizNo = this.generateOutBizNo()
					this.visible2 = true;
				} else { // 未登录 触发登录
					this.handleTap()
				}
			},
		}
	}
</script>
<style lang="scss">
.jobDetails{
	padding-bottom: 300rpx;
	overflow: auto;
	box-sizing: border-box;
	.moduleTitle{
		font-size: 36rpx;
		color: #333;
	}
	.jobInfo{
		margin: 30rpx 0;
		background: #fff;
		padding: 30rpx;
		.jobName{
			font-size: 40rpx;
		}
		.jobSalary{
			font-size: 40rpx;
			color: #E26767;
		}
		.companyName{
			padding: 20rpx 0;
			font-weight: 300;
		}
		.requirement{
			.u-icon{
				margin-right: 6rpx;
			}
			.f_s + .f_s{
				margin-left: 20rpx;
			}
			font-weight: 300;
		}
		.u-cell__body{
			padding: 10px 0 !important;
			.u-cell__title-text{
				font-weight: 300;
			}
		}
	}
	.jobDesc{
		background: #fff;
		padding: 10rpx 0 60rpx;
		margin: 30rpx 0;
		color: #666;
		font-size: 28rpx;
		line-height: 2;
		>view{
			padding:10rpx 30rpx;
		}
	}
	.companyInfo{
		background: #fff;
		padding: 30rpx;
	}
	.footer{
		padding: 20rpx 30px calc(env(safe-area-inset-bottom) + 20rpx);
		background: #fff;
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		border: none;
		box-sizing: border-box;
	}
}
</style>
