<template>
	<view class="certificates">
		<!-- 自定义导航栏 -->
		<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">资质证书</view>
				<view class="nav-right"></view>
			</view>
		</view>
		
		<!-- 主要内容 -->
		<view class="main-content">
			<view class="tip">请您添加您的资质证书</view>
			
			<!-- 资质证书 -->
			<view class="skills-section">
				<view class="section-title">资质证书</view>
		
				<!-- 资质证书区域 -->
				<view class="custom-tags">
					<view v-for="(item, index) in certificates" :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="showAddCertificatesModal">
						<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="showAddCertificates" mode="center" border-radius="24" width="80%">
			<view class="add-tag-popup">
				<view class="popup-title">添加资质证书</view>
				<view class="popup-content">
					<input v-model="newCertificates" placeholder="请输入资质证书(不要带上,  ; 这种字符)" class="tag-input" maxlength="10" />
				</view>
				<view class="popup-buttons">
					<view class="popup-btn cancel-btn" @click="cancelAdd">取消</view>
					<view class="popup-btn confirm-btn" @click="confirmAdd">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 0, // 状态栏高度
				certificates: [],
				newCertificates: '',
				showAddCertificates: false,
			};
		},
		onLoad(options) {
			// 获取状态栏高度
			let systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight || 0;
			
			// 设置资质证书回显
			if (options.certificates) {
				this.certificates = decodeURIComponent(options.certificates).split(';')
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			showAddCertificatesModal() {
				this.showAddCertificates = true
				this.newCertificates = ''
			},
			cancelAdd() {
				this.showAddCertificates = false
				this.newCertificates = ''
			},
			confirmAdd() {
				if (this.newCertificates.trim()) {
					let realLength = this.certificates.filter(item => item !== '不限').length;
					if (realLength < 10) {
						this.certificates.push(this.newCertificates.trim())
						this.showAddCertificates = false
						this.newCertificates = ''
					} else {
						uni.showToast({
							title: '最多添加10个资质证书',
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: '请输入资质证书',
						icon: 'none'
					})
				}
			},
			removeCustomTag(index) {
				this.certificates.splice(index, 1)
			},
			confirmSelection() {
				const certificates = this.certificates.map(certificate => {
					return {
						userId: '',
						certificateId: '',
						certificateName: certificate,
						isuse: 1
					}
				})
				uni.showLoading({ title: "保存中..." })
				this.$Request
					.postJson('/app/userFirst/updateCertificate', certificates)
					.then((res) => {
						if (res.code == 0) {
							uni.showToast({ title: '保存成功', icon: 'none' })
						    // 实际开发中可以在这里添加跳转逻辑
							uni.$emit('updateResume')
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
							})
						}
					})
					.finally(() => {
						uni.hideLoading()
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common.scss';
	.certificates {
		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;
		}
	}
	
	.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>