<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">岗位职业技能</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">
				<view class="section-title">运营方向</view>
				
				<!-- 技能标签网格 -->
				<view class="skills-grid">
					<view 
						v-for="(item, index) in operationOptions" 
						:key="index"
						class="skill-tag"
						:class="{ active: selectedOperation.includes(item) }"
						@click="toggleOperation(item)"
					>
						<text>{{ item }}</text>
					</view>
				</view>
			</view>
			
			<!-- 电商品类 -->
			<view class="skills-section">
				<view class="section-title">电商品类</view>
				
				<!-- 技能标签网格 -->
				<view class="skills-grid">
					<view 
						v-for="(item, index) in categoryOptions" 
						:key="index"
						class="skill-tag"
						:class="{ active: selectedCategory.includes(item) }"
						@click="toggleCategory(item)"
					>
						<text>{{ item }}</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="24"></u-icon>
					</view>
					<view class="add-tag" @click="showAddTagModal">
						<text>添加标签</text>
						<u-icon name="plus" color="#333" size="24"></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>
export default {
	data() {
		return {
			jobTitle: '亚马逊运营总监',
			showAddTag: false,
			newTagName: '',
			selectedOperation: ['独立站'],
			selectedCategory: ['独立站'],
			customTags: ['全领域'],
			operationOptions: [
				'精品铺货', '独立站', '3C数码', '美妆日用', '服装配饰', '家居用品',
				'户外运动', '母婴用品', '食品饮料', '宠物用品', '汽车用品', '图书文具',
				'数码配件', '办公用品', '礼品玩具', '健康保健', '珠宝首饰', '箱包皮具',
				'手表眼镜', '乐器音响', '摄影摄像', '运动健身', '旅游出行', '教育培训',
				'金融服务', '生活服务', '其他'
			],
			categoryOptions: [
				'精品铺货', '独立站', '3C数码', '美妆日用', '服装配饰', '家居用品',
				'户外运动', '母婴用品', '食品饮料', '宠物用品', '汽车用品', '图书文具',
				'数码配件', '办公用品', '礼品玩具', '健康保健', '珠宝首饰', '箱包皮具',
				'手表眼镜', '乐器音响', '摄影摄像', '运动健身', '旅游出行', '教育培训',
				'金融服务', '生活服务', '其他'
			]
		}
	},
	onLoad(options) {
		if (options.jobTitle) {
			this.jobTitle = decodeURIComponent(options.jobTitle)
		}
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		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'
					})
				}
			}
		},
		toggleCategory(item) {
			const index = this.selectedCategory.indexOf(item)
			if (index > -1) {
				this.selectedCategory.splice(index, 1)
			} else {
				if (this.selectedCategory.length < 8) {
					this.selectedCategory.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) {
					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() {
			// 返回选中的技能设置
			uni.navigateBack({
				delta: 1
			})
			
			// 通知父页面更新技能设置
			uni.$emit('skillsUpdated', {
				jobTitle: this.jobTitle,
				operation: this.selectedOperation,
				category: this.selectedCategory,
				customTags: this.customTags
			})
			
			uni.showToast({
				title: '技能设置已保存',
				icon: 'success'
			})
		}
	}
}
</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;
}

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

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

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

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

.skill-tag {
	width: calc((100% - 72rpx) / 6);
	padding: 8rpx 6rpx;
	background: #F7F8FA;
	border: 1rpx solid #F7F8FA;
	border-radius: 12rpx;
	transition: all 0.3s ease;
	display: flex;
	align-items: center;
	justify-content: center;
	
	text {
		font-size: 20rpx;
		color: rgba(102, 102, 102, 1);
		font-weight: 400;
		text-align: center;
		line-height: 1.2;
	}
	
	&.active {
		background: rgba(153, 196, 250, 0.4);
		border: 0.5px solid rgba(1, 107, 246, 1);
		
		text {
			color: rgba(1, 107, 246, 1);
			font-weight: 400;
		}
	}
	
	&:active {
		transform: scale(0.95);
	}
}

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

.custom-tag {
	padding: 8rpx 12rpx;
	background: rgba(153, 196, 250, 0.4);
	border: 0.5px solid rgba(1, 107, 246, 1);
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	gap: 8rpx;
	
	text {
		font-size: 18rpx;
		color: rgba(1, 107, 246, 1);
		font-weight: 400;
	}
}

.add-tag {
	padding: 8rpx 12rpx;
	background: #F7F8FA;
	border: 1rpx solid #F7F8FA;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	gap: 8rpx;
	
	text {
		font-size: 18rpx;
		color: rgba(102, 102, 102, 1);
		font-weight: 400;
	}
}

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

.confirm-btn {
	width: 100%;
	height: 88rpx;
	background: linear-gradient(90deg, #007AFF 0%, #5AC8FA 100%);
	border-radius: 44rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	
	text {
		font-size: 32rpx;
		font-weight: 600;
		color: #fff;
	}
	
	&:active {
		background: linear-gradient(90deg, #0056CC 0%, #4A9FE7 100%);
	}
}

// 添加标签弹窗样式
.add-tag-popup {
	background: #fff;
	border-radius: 24rpx;
	padding: 40rpx;
	
	.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: 30rpx;
	}
	
	.popup-content {
		margin-bottom: 30rpx;
		
		.tag-input {
			width: 100%;
			height: 80rpx;
			padding: 0 20rpx;
			border: 1rpx solid #E5E5EA;
			border-radius: 12rpx;
			font-size: 28rpx;
			color: rgba(34, 37, 42, 1);
			background: #F7F8FA;
		}
	}
	
	.popup-buttons {
		display: flex;
		gap: 20rpx;
		
		.popup-btn {
			flex: 1;
			height: 80rpx;
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			font-weight: 500;
		}
		
		.cancel-btn {
			background: #F7F8FA;
			color: rgba(102, 102, 102, 1);
		}
		
		.confirm-btn {
			background: #007AFF;
			color: #fff;
		}
	}
}
</style>
