<template>
	<view class="page">
		<u-navbar title="会员配置" :border-bottom="false" :background="bgc" back-icon-color="#111827" title-color='#111827'
			title-size='34' title-bold height='44' id="navbar">
		</u-navbar>
		
		<view class="content-wrapper">
			<!-- 头部装饰背景 -->
			<view class="top-decoration"></view>
			
			<view class="section-header">
				<view class="title-deco"></view>
				<text class="main-title">基础信息</text>
				<!-- <text class="sub-title">Basic Information</text> -->
			</view>
			
			<view class="form-card bounce-in">
				<!-- 会员卡名称 -->
				<view class="form-group">
					<view class="label-line">
						<text class="label-text">会员卡名称</text>
						<text class="required">*</text>
					</view>
					<view class="input-box">
						<u-icon name="file-text" color="#A0AEC0" size="36" style="margin-right: 20rpx;"></u-icon>
						<input class="custom-input" v-model="form.name" placeholder="给会员卡起个好听的名字" placeholder-class="placeholder-style" />
					</view>
				</view>
	
				<!-- 折扣与价格在一行展示 -->
				<view class="row-group">
					<view class="form-group half">
						<view class="label-line">
							<text class="label-text">折扣比例</text>
						</view>
						<view class="input-box">
							<input class="custom-input" type="digit" v-model="form.discountValue" placeholder="0.0" placeholder-class="placeholder-style" />
							<text class="unit-tag">折</text>
						</view>
					</view>
					<view class="form-group half">
						<view class="label-line">
							<text class="label-text">销售价格</text>
							<text class="required">*</text>
						</view>
						<view class="input-box">
							<text class="currency-symbol">¥</text>
							<input class="custom-input" type="digit" v-model="form.price" placeholder="0.00" placeholder-class="placeholder-style" />
						</view>
					</view>
				</view>
			</view>
			
			<view class="section-header" style="margin-top: 40rpx;">
				<view class="title-deco deco-blue"></view>
				<text class="main-title">规则设置</text>
				<!-- <text class="sub-title">Rules Settings</text> -->
			</view>
			
			<view class="form-card bounce-in delay-100">
				<!-- 有效期 -->
				<view class="form-group">
					<view class="label-line">
						<text class="label-text">有效期天数</text>
						<text class="required">*</text>
					</view>
					<view class="input-box">
						<u-icon name="calendar" color="#A0AEC0" size="36" style="margin-right: 20rpx;"></u-icon>
						<input class="custom-input" type="number" v-model="form.validDays" placeholder="请输入有效天数" placeholder-class="placeholder-style" />
						<text class="unit-text">天</text>
					</view>
				</view>
			    
				<!-- 开关组 -->
				<view class="switch-group">
					<view class="switch-item">
						<view class="switch-info">
							<text class="switch-title">立即上架</text>
							<text class="switch-desc">开启后用户端可见并可购买</text>
						</view>
						<switch :checked="status" @change="toggleUseLimits" color="#4297F3" style="transform:scale(0.8)"/>
					</view>
					
					<view class="divider"></view>
					
					<view class="switch-item">
						<view class="switch-info">
							<text class="switch-title">频率限制</text>
							<text class="switch-desc">限制会员卡的使用频率</text>
						</view>
						<switch :checked="form.enableLimit" @change="toggleUseLimit" color="#4297F3" style="transform:scale(0.8)"/>
					</view>
				</view>
		
				<!-- 频率限制详情 -->
				<view v-if="form.enableLimit" class="limit-config-box scale-in">
					<view class="config-row">
						<view class="config-addon">每</view>
						<input class="config-input" type="number" v-model="form.limitRound" />
						<view class="config-addon">天，可用</view>
						<input class="config-input" type="number" v-model="form.limitCount" />
						<view class="config-addon">次</view>
					</view>
				</view>
				
				<!-- 总次数 -->
				<view class="form-group" style="margin-top: 30rpx;">
					<view class="label-line">
						<text class="label-text">总可用次数</text>
					</view>
					<view class="input-box">
						<u-icon name="grid" color="#A0AEC0" size="36" style="margin-right: 20rpx;"></u-icon>
						<input class="custom-input" type="number" v-model="form.limitTotal" placeholder="不填则不限总次数" placeholder-class="placeholder-style" />
						<text class="unit-text">次</text>
					</view>
				</view>
			
				<!-- 备注 -->
				<view class="form-group">
					<view class="label-line">
						<text class="label-text">备注说明</text>
					</view>
					<view class="textarea-box">
						<textarea class="custom-textarea" v-model="form.description" placeholder="请输入备注信息..." placeholder-class="placeholder-style" maxlength="200" />
						<text class="word-count">{{form.description ? form.description.length : 0}}/200</text>
					</view>
				</view>
			</view>
		</view>

		<view class="footer-action">
			<template v-if="tit == '新增会员卡'">
				<button class="action-btn primary-btn hover-effect" @click="handleSubmit">
					<u-icon name="checkmark" size="36" color="#fff" style="margin-right: 10rpx;"></u-icon>
					确认添加
				</button>
			</template>
			<template v-else>
				<view class="btn-group">
					<button class="action-btn delete-btn hover-effect" @click="btndel">删除</button>
					<button class="action-btn primary-btn hover-effect" @click="btnedit">保存修改</button>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F7F8FA",
				},
				form: {
					name: '',
					discountValue: '',
					price: '',
					validDays: '',
					enableLimit: false,
					limitDays: '',
					limitTimes: '',
					description: '',
					type:3,
					limitTotal:'',
					limitUnit:1
				},
				flag: true,
				areaId:'',
				status:true,
				id:'',
				tit:''
			}
		},
		onLoad(option) {
			if(option.id){
				this.id = option.id
				this.tit = '编辑会员卡'
				this.getxq()
			}else{
			   this.areaId = uni.getStorageSync('adminAreaid')
			   this.tit = '新增会员卡'
			}
		},
		onShow() {
			
		},
		methods: {
			// 点击编辑
			btnedit(){
				const submitData = {
					...this.form,
					areaId:this.areaId, 
					status:this.status == true ? 1 : 2
				}
				this.$u.put(`/bst/vip`,submitData).then(res =>{
					if(res.code == 200){
						uni.showToast({
							title: '修改成功',
							icon: 'success',
							duration: 2000
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 点击删除
			btndel(){
				let that = this
				uni.showModal({
					title: '提示',	
					content: '您确定要删除吗？',
					showCancel: true,
					success: function(res) {
						if (res.confirm) {
							that.$u.delete(`/bst/vip/${that.id}`).then(res =>{
								if(res.code == 200){
									uni.showToast({
										title: '删除成功',
										icon: 'success',
										duration: 2000
									})
									setTimeout(()=>{
										uni.navigateBack()
									},1000)
								}else{
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {
				
						}
					}
				})
			},
			// 请求订单详情
			getxq(){
				this.$u.get(`/bst/vip/${this.id}`).then(res =>{
					if(res.code == 200){
						this.form = res.data
						this.areaId = res.data.areaId
						if(res.data.status == 1){
							this.status = true
						}else{
							this.status = false
						}
					}
				})
			},
			toggleUseLimit(e) {
				console.log(e);
				this.form.enableLimit = e.detail.value;
				if (!this.form.enableLimit) {
					this.form.limitRound = '';
					this.form.limitCount = '';
				}
			},
			toggleUseLimits(e) {
				this.status = e.detail.value;
			},
			handleSubmit() {
				if (this.flag == true) {
					this.flag = false
					const submitData = {
						...this.form,
						areaId:this.areaId, 
						status:this.status == true ? 1 : 2
					};
					console.log(submitData);
					this.$u.post("/bst/vip", submitData).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '新增成功',
								icon: 'success',
								duration: 2000
							})
							this.flag = true
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
							this.flag = true
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 动画关键帧
	@keyframes bounceIn {
		from { opacity: 0; transform: translateY(20rpx); }
		to { opacity: 1; transform: translateY(0); }
	}
	@keyframes scaleIn {
		from { opacity: 0; transform: scale(0.95); }
		to { opacity: 1; transform: scale(1); }
	}

	.bounce-in { animation: bounceIn 0.5s ease-out forwards; }
	.scale-in { animation: scaleIn 0.3s ease-out forwards; }
	.delay-100 { animation-delay: 0.1s; opacity: 0; } // 初始opacity为0配合动画

	.page {
		background-color: #F7F8FA;
		min-height: 100vh;
		padding-bottom: 200rpx; // 为底部按钮留出空间
		font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, Segoe UI, Arial, Roboto, 'PingFang SC', sans-serif;
	}

	.content-wrapper {
		padding: 30rpx;
		position: relative;
		z-index: 2;
	}
	
	.top-decoration {
		position: absolute;
		top: -100rpx;
		right: -100rpx;
		width: 400rpx;
		height: 400rpx;
		background: radial-gradient(circle, rgba(66, 151, 243, 0.1) 0%, rgba(255, 255, 255, 0) 70%);
		border-radius: 50%;
		z-index: -1;
		pointer-events: none;
	}

	.section-header {
		display: flex;
		align-items: baseline;
		margin-bottom: 24rpx;
		padding-left: 10rpx;
		
		.title-deco {
			width: 8rpx;
			height: 32rpx;
			background: linear-gradient(to bottom, #4297F3, #2B76E5);
			border-radius: 4rpx;
			margin-right: 16rpx;
			transform: translateY(4rpx);
			
			&.deco-blue {
				background: linear-gradient(to bottom, #8A2BE2, #4297F3); // 不同的渐变色
			}
		}
		
		.main-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #111827;
			margin-right: 12rpx;
		}
		
		.sub-title {
			font-size: 20rpx;
			color: #9CA3AF;
			font-weight: 500;
			text-transform: uppercase;
			letter-spacing: 1rpx;
		}
	}

	.form-card {
		background: #FFFFFF;
		border-radius: 32rpx;
		padding: 40rpx 32rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.04);
		margin-bottom: 20rpx;
	}

	.form-group {
		margin-bottom: 40rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.label-line {
			display: flex;
			margin-bottom: 16rpx;
			
			.label-text {
				font-size: 28rpx;
				color: #4B5563;
				font-weight: 600;
			}
			
			.required {
				color: #EF4444;
				margin-left: 6rpx;
				font-size: 28rpx;
			}
		}
		
		.input-box {
			background: #F9FAFB;
			border: 2rpx solid transparent;
			border-radius: 20rpx;
			height: 96rpx;
			display: flex;
			align-items: center;
			padding: 0 24rpx;
			transition: all 0.3s;
			
			&:focus-within {
				background: #FFFFFF;
				border-color: #4297F3;
				box-shadow: 0 0 0 4rpx rgba(66, 151, 243, 0.1);
			}
			
			.custom-input {
				flex: 1;
				height: 100%;
				font-size: 30rpx;
				color: #1F2937;
			}
			
			.unit-text, .unit-tag {
				font-size: 26rpx;
				color: #6B7280;
				margin-left: 10rpx;
			}
			
			.unit-tag {
				background: #E5E7EB;
				padding: 4rpx 12rpx;
				border-radius: 8rpx;
				font-size: 22rpx;
			}
			
			.currency-symbol {
				font-size: 32rpx;
				color: #1F2937;
				margin-right: 10rpx;
				font-weight: 600;
			}
		}
		
		.textarea-box {
			background: #F9FAFB;
			border-radius: 20rpx;
			padding: 24rpx;
			position: relative;
			border: 2rpx solid transparent;
			transition: all 0.3s;
			
			&:focus-within {
				background: #FFFFFF;
				border-color: #4297F3;
				box-shadow: 0 0 0 4rpx rgba(66, 151, 243, 0.1);
			}
			
			.custom-textarea {
				width: 100%;
				height: 160rpx;
				font-size: 30rpx;
				color: #1F2937;
				line-height: 1.5;
			}
			
			.word-count {
				position: absolute;
				bottom: 16rpx;
				right: 24rpx;
				font-size: 22rpx;
				color: #9CA3AF;
			}
		}
	}

	.row-group {
		display: flex;
		justify-content: space-between;
		
		.half {
			width: 48%;
		}
	}

	.switch-group {
		.switch-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 0;
			
			.switch-info {
				display: flex;
				flex-direction: column;
				
				.switch-title {
					font-size: 30rpx;
					font-weight: 600;
					color: #1F2937;
					margin-bottom: 6rpx;
				}
				
				.switch-desc {
					font-size: 22rpx;
					color: #9CA3AF;
				}
			}
		}
		
		.divider {
			height: 2rpx;
			background: #F3F4F6;
			margin: 20rpx 0;
		}
	}

	.limit-config-box {
		background: #F0F9FF;
		border: 2rpx dashed #BAE6FD;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-top: 20rpx;
		
		.config-row {
			display: flex;
			align-items: center;
			justify-content: center;
			
			.config-addon {
				font-size: 28rpx;
				color: #0369A1;
				margin: 0 12rpx;
			}
			
			.config-input {
				width: 100rpx;
				height: 64rpx;
				background: #FFFFFF;
				border: 2rpx solid #E0F2FE;
				border-radius: 12rpx;
				text-align: center;
				font-size: 32rpx;
				color: #0284C7;
				font-weight: 600;
				
				&:focus {
					border-color: #0EA5E9;
				}
			}
		}
	}

	.footer-action {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: rgba(255, 255, 255, 0.9);
		backdrop-filter: blur(10px);
		padding: 20rpx 32rpx 40rpx; // 适配iPhone X底部
		box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
		z-index: 100;
		
		.action-btn {
			height: 96rpx;
			border-radius: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			font-weight: 600;
			border: none;
			
			&.primary-btn {
				background: linear-gradient(135deg, #4297F3 0%, #2B76E5 100%);
				color: #FFFFFF;
				box-shadow: 0 10rpx 20rpx rgba(66, 151, 243, 0.3);
			}
			
			&.delete-btn {
				background: #FEF2F2;
				color: #EF4444;
				border: 2rpx solid #FEE2E2;
			}
			
			&.hover-effect:active {
				transform: translateY(2rpx);
				filter: brightness(0.95);
			}
		}
		
		.btn-group {
			display: flex;
			justify-content: space-between;
			gap: 20rpx;
			
			.action-btn {
				flex: 1;
			}
		}
	}
	
	.placeholder-style {
		color: #D1D5DB;
		font-size: 28rpx;
	}
</style>