<template>
	<view class="job-skills">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<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">{{this.jobId==0||this.jobId==''?'我的':'岗位'}}职业技能</view>
				<view class="nav-right"></view>
			</view>
		</view>

		<!-- 主要内容 -->
		<view class="main-content">
			<view class="job-title">{{ jobTitle }}-职业技能</view>
			<view class="description">根据你的偏好设置,为你推荐更匹配的职位,最多选择8个</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(item.postSkillName)">
						<text>{{ item.postSkillName }}</text>
					</view>
				</view>
			</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="bottom-btn-container">
			<view class="confirm-btn" @click="confirmSelection">
				<text>确定</text>
			</view>
		</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 {
				jobTitle: '亚马逊运营总监',
				jobId: '',
				showAddTag: false,
				newTagName: '',
				selectedOperation: [],
				customTags: [],
				operationOptions: []
			}
		},
		onLoad(options) {
			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
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			getData() {
				let data = {
					ruleClassifyId: this.jobId
				}
				var action = this.jobId == '' || this.jobId == 0 ? '/app/userFirst/selectSkill' :
					'/app/userFirst/getPostSkill'
				this.$Request.getT(action, data).then(res => {
					if (res.code == 0) {
						if (this.jobId == '' || this.jobId == 0) {
							res.data.forEach(function(item) {
								item.postSkillName = item.skillName
							})
							this.operationOptions = [{
								childrenList: res.data,
								postSkillName: ''
							}]
						} else
							this.operationOptions = res.data
					}
				})
			},
			toggleOperation(item) {
				const index = this.selectedOperation.indexOf(item)
				if (index > -1) {
					this.selectedOperation.splice(index, 1)
				} else {
					if (this.selectedOperation.length < 8) {
						this.selectedOperation.push(item)
					} else {
						uni.showToast({
							title: '最多选择8个标签',
							icon: 'none'
						})
					}
				}
			},

			showAddTagModal() {
				this.showAddTag = true
				this.newTagName = ''
			},
			cancelAddTag() {
				this.showAddTag = false
				this.newTagName = ''
			},
			addCustomTag() {
				if (this.newTagName.trim()) {
					if (this.customTags.length < 5) {
						if (this.customTags.length + this.selectedOperation.length >= 8) {
							return uni.showToast({
								title: '最多选择8个标签',
								icon: 'none'
							})
						}
						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() {
				// 合并选择的标签和自定义标签并回传
				const allSelectedTags = [...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>
	.job-skills {
		min-height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;
		padding: 88rpx 40rpx 40rpx 40rpx;
	}

	.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;
	}

	.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);
		}
	}

	.bottom-btn-container {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 32rpx;
		background: #fff;
		border-top: 1rpx solid #f0f0f0;
		z-index: 10;
	}

	.confirm-btn {
		width: 100%;
		height: 96rpx;
		background: linear-gradient(90deg, #007AFF 0%, #2DB0F0 100%);
		border-radius: 48rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 6rpx 16rpx rgba(0, 122, 255, 0.2);
		transition: all 0.2s ease;
		cursor: pointer;

		text {
			font-size: 34rpx;
			font-weight: 600;
			color: #fff;
		}

		&:active {
			background: linear-gradient(90deg, #0056CC 0%, #4A9FE7 100%);
			transform: scale(0.98);
			box-shadow: 0 4rpx 12rpx rgba(0, 122, 255, 0.15);
		}
	}

	// 添加标签弹窗样式
	.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>