<template>
	<view style="height: 100vh;" class="flex flex-direction">
		<!-- 顶部导航栏 -->
		<view class="navbar" :style="{ paddingTop: (12 + statusBarHeight) + 'px' }">
			<view class="navbar-content">
				<view class="navbar-left" @click="goBack">
					<u-icon name="arrow-leftward" size="36" color="#333"></u-icon>
				</view>
				<view class="navbar-title">附件管理</view>
				<view class="navbar-right"></view>
			</view>
		</view>

		<view class="contain flex flex-direction">
		<!-- #ifdef APP-PLUS -->
			<view 
				class="upload-card"
				:is-shadow="true"
				@click="handleWeChatMiniApp"
			>
				<view class="card-icon"></view>
				<view class="card-text">微信小程序上传</view>
				<view class="card-badge"></view>
			</view>
		<!-- #endif -->
			<view 
				class="upload-card"
				:is-shadow="true"
			>
				<view class="card-icon-phone"></view>
				<view class="card-text">手机文件上传</view>
				<fileSelector
					class="upload-file-selector"
					:isShowStyle="false"
					title=""
					allowType=".doc,.docx,.xls,.xlsx,.pdf"
					@fileSelected="uploadResumes"
				/>
			</view>
		</view>
	</view>
</template>

<script>
import fileSelector from '@/uni_modules/zhouquan-fileSelector/components/zhouquan-fileSelector/file-selector.vue';
export default {
	components: {
		fileSelector
	},

	data() {
		return {
			statusBarHeight: 0, // 状态栏高度
			content: [],
			showLoadCard: false,
			showResumesAnalysis: false,
			validInfoCount: 0, // 有效信息数量

			loading: false,
			showTipPopup: false
		};
	},
	onLoad(e) {
		// 获取状态栏高度
		let systemInfo = uni.getSystemInfoSync();
		this.statusBarHeight = systemInfo.statusBarHeight || 0;
	},
	methods: {
		// 返回上一页
		goBack() {
			uni.navigateBack();
		},
		// 保存
		uploadResumes(e) {
			var that = this
			console.log(e.path)
			const validExtensions = ['pdf', 'doc', 'docx'];
			const ext = e.name.split('.').pop().toLowerCase();
			if (!validExtensions.includes(ext)) {
				this.$queue.showToast('仅支持PDF/DOC/DOCX格式')
				return;
			}
			uni.getFileInfo({
				filePath: e.path,
				success: (info) => {
					let attachment_size = (info.size / 1024).toFixed(1)
					that.$queue.uploadFile(e.path, function (path) {
						uni.showLoading({ title: '上传中' })
						if (path)
							that.$Request.postJson("/app/resumes/saveAttachment", {
								attachmentAddress: path,
								attachmentName: e.name,
								attachmentSize: attachment_size
							}).then(res => {
								uni.hideLoading()
								if (res.code === 0) {
									uni.showToast({
										title: '上传成功',
										icon: "none"
									})
									this.goBack()
								} else {
									uni.showToast({
										title: res.msg,
										icon: "none"
									})
								}
							})
						else {
							uni.hideLoading()
							that.$queue.showToast('文件上传失败')
						}
					})
				},
				fail: (err) => {
					uni.hideLoading()
					console.error('获取失败', err);
				}
			});

		},
		// 微信小程序上传
		handleWeChatUpload() {
			plus.share.getServices(res => {
			let weixinService = null;
			for (let i in res) {
				if (res[i].id === 'weixin') {
				weixinService = res[i];
				break;
				}
			}
			const userId =  uni.getStorageSync('userId')
			if (weixinService) {
				weixinService.launchMiniProgram({
				id: 'gh_854ab5288c2d', 
				path: `/pages/index/index?userId=${userId}`,
				type: 0 // 小程序版本类型：0-正式版；1-测试版；2-体验版
				});
			} else {
				console.log('未安装微信或获取微信服务失败');
			}
			});
		},

		// 打开微信小程序
		handleWeChatMiniApp() {
			uni.showModal({
				title: '提示',
				content: '即将离开亿职赞打开微信小程序，请选择是否打开',
				success: (res) => {
					if (res.confirm) {
						this.handleWeChatUpload()
					}
				}
			})
		}
	},


};
</script>

<style lang="scss" scoped>
.navbar {
	padding: 24rpx 32rpx 20rpx 32rpx;
	background: #fff;

	.navbar-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 60rpx;

		.navbar-left {
			width: 60rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.navbar-title {
			color: rgba(23, 23, 37, 1);
			font-family: DM Sans;
			font-size: 36rpx;
			font-weight: 700;
			line-height: 52rpx;
			letter-spacing: 0%;
			text-align: center;
		}

		.navbar-right {
			width: 60rpx;
			height: 60rpx;
		}
	}
}

.contain {
	flex: 1;
	padding: 30rpx;
	background: #f1f5f8;
	.upload-card {
		position: relative;
		display: flex;
		height: 100rpx;
		padding: 0 30rpx;
		background: #fff;
		align-items: center;
		line-height: 100rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;

		.card-icon {
			width: 44rpx;
			height: 44rpx;
			background: url('@/static/images/resumeUpload/miniApp.png') no-repeat center center;
			background-size: 100% 100%;
			margin-right: 10rpx;
		}
		.card-icon-phone {
			width: 44rpx;
			height: 44rpx;
			background: url('@/static/images/resumeUpload/phone.png') no-repeat center center;
			background-size: 100% 100%;
			margin-right: 10rpx;
		}
		.card-badge {
			width: 44rpx;
			height: 44rpx;
			background: url('@/static/images/resumeUpload/recommendation.png') no-repeat center center;
			background-size: 100% 100%;
			margin-right: 10rpx;
		}
		.card-text {
			margin: 0 10rpx;
			font-size: 30rpx;
			color: #666666;
		}

	}
	.upload-file-selector {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
		z-index: 5;
	}

	.upload-button {
		width: 100%;
		display: flex;
		height: 100rpx;
		position: relative;
		background: #fff;
		.upload-button-content {
				display: flex;
				z-index: 2;
				position: absolute;
				top: 0;
				left: 0;
			}
		}
}

</style>
