<template>
	<view class="job-skills">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar" :style="{ paddingTop: (12 + statusBarHeight) + 'px' }">
			<view class="navbar-content">
				<view class="nav-left" @click="goBack">
					<u-icon name="close" color="#333" size="32"></u-icon>
				</view>
				<view class="nav-title">
					<template v-if="onlyShowCustomTag">我的技能</template>
					<template v-else>{{this.jobId==0||this.jobId==''?'我的':'岗位'}}跨境标签</template>
				</view>
				<view class="nav-right"></view>
			</view>
		</view>

		<!-- 主要内容 -->
		<view class="main-content">
			<template v-if="!onlyShowCustomTag">
				<view class="job-title">{{ jobTitle }}-跨境标签</view>
				<view class="description">根据你的偏好设置,为你推荐更匹配的职位</view>

				<!-- 运营方向 -->
				<view class="skills-section" v-for="(it, ind) in operationOptions" :key="ind">
					<view class="section-title">{{it.postSkillName}}</view>
					<view class="skills-grid">
						<view v-for="(item, index) in it.childrenList" :key="index" class="skill-tag"
							:class="{ active: selectedOperation.includes(item.postSkillName) }"
							@click="toggleOperation(ind,item.postSkillName)">
							<text>{{ item.postSkillName }}</text>
						</view>
					</view>
				</view>
			</template>
			<view class="tip" v-else>请您添加你的技能标签</view>
			
			<!-- 自定义标签 -->
			<view class="skills-section">
				<view class="section-title">自定义标签</view>

				<!-- 自定义标签区域 -->
				<view class="custom-tags">
					<view v-for="(item, index) in customTags" :key="index" class="custom-tag"
						@click="removeCustomTag(index)">
						<text>{{ item }}</text>
						<u-icon name="close" color="#007AFF" size="18"></u-icon>
					</view>
					<view class="add-tag" @click="showAddTagModal">
						<text>添加标签</text>
						<u-icon name="plus" color="#333" size="18"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部确定按钮 -->
		<view class="fixed-section">
			<u-button type="primary" class="submit-btn" @click="confirmSelection">保存</u-button>
		</view>

		<!-- 添加标签弹窗 -->
		<u-popup v-model="showAddTag" mode="center" border-radius="24" width="80%">
			<view class="add-tag-popup">
				<view class="popup-title">添加自定义标签</view>
				<view class="popup-content">
					<input v-model="newTagName" placeholder="请输入标签名称(不要带上,  ; 这种字符)" class="tag-input" maxlength="10" />
				</view>
				<view class="popup-buttons">
					<view class="popup-btn cancel-btn" @click="cancelAddTag">取消</view>
					<view class="popup-btn confirm-btn" @click="addCustomTag">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let set = 0
	export default {
		data() {
			return {
				statusBarHeight: 0, // 状态栏高度
				jobTitle: '',
				jobId: '',
				showAddTag: false,
				newTagName: '',
				selectedOperation: [],
				customTags: [],
				operationOptions: [],
				tagParam: '' ,// 新增：保存传入的tag参数
				ruleClassifyId:'',
				fromPage: '', // 判断从哪个页面进入 userInfo个人信息、onlineResume在线简历
				disabledSubmit: false
			}
		},
		computed: {
			// 只展示自定义标签（userInfo个人信息、onlineResume在线简历）
			onlyShowCustomTag() {
				if (this.fromPage == 'onlineResume' || this.fromPage == 'userInfo') {
					return true;
				}
				return false;
			}
		},
		onLoad(options) {
			if(options.ruleClassifyId){
				this.ruleClassifyId = options.ruleClassifyId;
			}
			// 保存tag参数
			if(options.tag){
				this.tagParam = decodeURIComponent(options.tag)
			}
			
			// 获取状态栏高度
			let systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight || 0;
			if (options && options.jobTitle) {
				this.jobTitle = decodeURIComponent(options.jobTitle)
			}
			if (options && options.skill) {
				if (options && options.jobId) {
					this.jobId = options.jobId
				}
				if (options && options.skill)
					this.selectedOperation = JSON.parse(decodeURIComponent(options.skill))
			}
			this.getData()
			set = options && options.set ? options.set : 0
			// 判断从哪个页面进入 onlineResume在线简历、userInfo个人信息
			if (options.from) {
				this.fromPage = options.from
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			getData() {
				let data = {
					ruleClassifyId: this.jobId || this.ruleClassifyId || ''
				}
				var action = this.jobId == '' || this.jobId == 0 ? '/app/userFirst/selectSkill' :
					'/app/userFirst/getPostSkill'
				if(this.ruleClassifyId){
					action = '/app/userFirst/getPostSkill'
				}
				// 如果ruleClassifyId等于null字符串，则置空
				if (data.ruleClassifyId == 'null') {
					this.disabledSubmit = true
					data.ruleClassifyId = ''
				} else {
					this.disabledSubmit = false
				}
				this.$Request.getT(action, data).then(res => {
					if (res.code == 0) {
						this.operationOptions = res.data
						
						if (this.onlyShowCustomTag) {
							const tags = res.data.map(tag => tag.skillName)
							this.setCustomTags(tags)
						} else if (this.tagParam) {
							// 数据加载完成后处理标签回显
							this.handleTagDisplay(this.tagParam)
						}
					}
				})
			},
			
			// 设置自定义回显
			setCustomTags(tags) {
				tags.forEach(tag => {
					this.customTags.push(tag)
				})
			},
			
			handleTagDisplay(tagString) {
				const tagArray = tagString
					.split(',')
					.map(tag => tag.trim())
					.filter(tag => tag && tag !== '不限');
		
				// 清空当前选中的标签
				this.selectedOperation = [];
				this.customTags = [];
		
				// 回显系统标签
				this.operationOptions.forEach(category => {
					if (category.childrenList) {
						category.childrenList.forEach(item => {
							if (tagArray.includes(item.postSkillName)) {
								this.selectedOperation.push(item.postSkillName);
							}
						});
					}
				});
		
				// 回显自定义标签
				tagArray.forEach(tag => {
					const isSystemTag = this.operationOptions.some(category =>
						category.childrenList.some(item => item.postSkillName === tag)
					);
					if (!isSystemTag && this.customTags.length < 5) {
						this.customTags.push(tag);
					}
				});
			},
			
			toggleOperation(pIndex, name) {
				const parent = this.operationOptions[pIndex];
		
				// 当前分类下已选中的标签（排除不限）
				const selectedInGroup = parent.childrenList
					.filter(item => item.postSkillName !== '不限' && this.selectedOperation.includes(item.postSkillName));
		
				const index = this.selectedOperation.indexOf(name);
		
				if (index === -1) {
					if (selectedInGroup.length >= 5) {
						uni.showToast({
							title: '每个分类最多选择5个标签',
							icon: 'none'
						});
						return;
					}
					this.selectedOperation.push(name);
				} else {
					this.selectedOperation.splice(index, 1);
				}
			},
			showAddTagModal() {
				this.showAddTag = true
				this.newTagName = ''
			},
			cancelAddTag() {
				this.showAddTag = false
				this.newTagName = ''
			},
			addCustomTag() {
				if (this.newTagName.trim()) {
					let realLength = this.customTags.filter(item => item !== '不限').length;
					if (realLength < 5) {
						this.customTags.push(this.newTagName.trim())
						this.showAddTag = false
						this.newTagName = ''
					} else {
						uni.showToast({
							title: '最多添加5个自定义标签',
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: '请输入标签名称',
						icon: 'none'
					})
				}
			},
			removeCustomTag(index) {
				this.customTags.splice(index, 1)
			},
			confirmSelection() {
				if (this.disabledSubmit) {
					return this.$queue.showToast('请重新选择亿职赞所匹配的岗位')
				}
				// 合并选择的标签和自定义标签并回传
				const allSelectedTags = this.onlyShowCustomTag ? this.customTags : [...this.selectedOperation, ...this.customTags]
				// 触发事件通知上一页面
				uni.$emit('skillsUpdated', {
					jobTitle: this.jobTitle,
					selectedTags: allSelectedTags, // 所有选中的标签（含系统和自定义）
					systemTags: this.selectedOperation, // 系统标签
					customTags: this.customTags, // 自定义标签
					set
				})
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common.scss';
	
	.job-skills {
		height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;
		padding: 0 40rpx 200rpx;
	}

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

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

	.nav-title {
		color: rgba(51, 51, 51, 1);
		font-family: DM Sans;
		font-size: 36rpx;
		font-weight: 700;
		line-height: 26px;
		letter-spacing: 0px;
		text-align: center;
	}

	.main-content {
		margin-top: 20rpx;
		flex: 1;
		overflow: auto;
		
		.tip {
			padding: 72rpx 0 32rpx;
			color: #666;
		}
	}

	.job-title {
		color: rgba(51, 51, 51, 1);
		font-family: DM Sans;
		font-size: 52rpx;
		font-weight: 700;
		line-height: 30px;
		letter-spacing: 0px;
		text-align: left;
		margin-bottom: 30rpx;
	}

	.description {
		margin: 20rpx 0 40rpx 0;
		color: rgba(102, 102, 102, 1);
		font-family: DM Sans;
		font-size: 28rpx;
		font-weight: 400;
		letter-spacing: 0px;
		text-align: left;
		line-height: 1.5;
	}

	.skills-section {
		margin-bottom: 50rpx;
	}

	.section-title {
		color: rgba(34, 37, 42, 1);
		font-family: DM Sans;
		font-size: 36rpx;
		font-weight: 500;
		line-height: 24px;
		letter-spacing: 0px;
		text-align: left;
		margin-bottom: 24rpx;
		padding-left: 4rpx;
	}

	.skills-grid {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}

	.skill-tag {
		padding: 16rpx 24rpx;
		background: #F7F8FA;
		border: 1rpx solid #E5E5EA;
		border-radius: 16rpx;
		transition: all 0.2s ease;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;

		text {
			font-size: 26rpx;
			color: rgba(102, 102, 102, 1);
			font-weight: 400;
			text-align: center;
			line-height: 1.2;
		}

		&:hover {
			background: rgba(153, 196, 250, 0.2);
			border-color: rgba(1, 107, 246, 0.3);
		}

		&.active {
			background: rgba(1, 107, 246, 0.1);
			border: 1rpx solid rgba(1, 107, 246, 1);
			box-shadow: 0 4rpx 12rpx rgba(1, 107, 246, 0.15);

			text {
				color: rgba(1, 107, 246, 1);
				font-weight: 500;
			}
		}

		&:active {
			transform: scale(0.96);
		}
	}

	.custom-tags {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}

	.custom-tag {
		padding: 14rpx 20rpx;
		background: rgba(153, 196, 250, 0.4);
		border: 1rpx solid rgba(1, 107, 246, 1);
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		gap: 10rpx;
		cursor: pointer;
		transition: all 0.2s ease;

		text {
			font-size: 24rpx;
			color: rgba(1, 107, 246, 1);
			font-weight: 400;
		}

		&:hover {
			background: rgba(153, 196, 250, 0.6);
		}

		&:active {
			transform: scale(0.96);
		}
	}

	.add-tag {
		padding: 14rpx 20rpx;
		background: #F7F8FA;
		border: 1rpx dashed #E5E5EA;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		gap: 10rpx;
		cursor: pointer;
		transition: all 0.2s ease;

		text {
			font-size: 24rpx;
			color: rgba(102, 102, 102, 1);
			font-weight: 400;
		}

		&:hover {
			background: #f0f0f0;
			border-color: #d9d9d9;
		}

		&:active {
			transform: scale(0.96);
		}
	}

	// 添加标签弹窗样式
	.add-tag-popup {
		background: #fff;
		border-radius: 24rpx;
		padding: 48rpx 40rpx;
		width: 100%;

		.popup-title {
			color: rgba(34, 37, 42, 1);
			font-family: DM Sans;
			font-size: 36rpx;
			font-weight: 500;
			line-height: 24px;
			text-align: center;
			margin-bottom: 36rpx;
		}

		.popup-content {
			margin-bottom: 40rpx;

			.tag-input {
				width: 100%;
				height: 90rpx;
				padding: 0 24rpx;
				border: 1rpx solid #E5E5EA;
				border-radius: 16rpx;
				font-size: 28rpx;
				color: rgba(34, 37, 42, 1);
				background: #F7F8FA;
				box-sizing: border-box;
			}

			.tag-input::placeholder {
				color: #C9CDD4;
			}
		}

		.popup-buttons {
			display: flex;
			gap: 24rpx;

			.popup-btn {
				flex: 1;
				height: 88rpx;
				border-radius: 16rpx;
				display: flex;
						align-items: center;
				justify-content: center;
				font-size: 30rpx;
				font-weight: 500;
				transition: all 0.2s ease;
				cursor: pointer;
			}

			.cancel-btn {
				background: #F7F8FA;
				color: rgba(102, 102, 102, 1);

				&:active {
					background: #eaeaea;
				}
			}

			.confirm-btn {
				background: #007AFF;
				color: #fff;

				&:active {
					background: #0056CC;
				}
			}
		}
	}
</style>