<template>
	<view class="container">
		<u-navbar title="会员中心" :border-bottom="false" :background="bgc" back-icon-color="#333333" title-color='#333333'
			title-size='36' height='36' id="navbar">
		</u-navbar>

		<view class="form-container">
			<!-- 会员卡名称 -->
			<view class="form-item">
				<text class="label">会员卡名称</text>
				<input class="input" v-model="form.name" placeholder="请输入会员卡名称" placeholder-class="placeholder" />
			</view>

			<!-- 折扣 -->
			<view class="form-item">
				<text class="label">折扣比例</text>
				<view class="input-with-unit">
					<input class="input" type="text" v-model="form.discountValue" placeholder="0.00"
						placeholder-class="placeholder" />
					<text class="unit">折</text>
				</view>
			</view>

			<!-- 金额 -->
			<view class="form-item">
				<text class="label">销售价格</text>
				<view class="input-with-unit">
					<input class="input" type="text" v-model="form.price" placeholder="0.00"
						placeholder-class="placeholder" />
					<text class="unit">元</text>
				</view>
			</view>

			<!-- 有效期 -->
			<view class="form-item">
				<text class="label">有效期</text>
				<view class="input-with-unit">
					<input class="input" type="number" v-model="form.validDays" placeholder="0"
						placeholder-class="placeholder" />
					<text class="unit">天</text>
				</view>
			</view>

			<!-- 使用限制 -->
			<view class="form-item">
				<view class="switch-container">
					<text class="label">是否上架</text>
					<switch :checked="status" @change="toggleUseLimits" color="#4a8cff" />
				</view>
				<view class="switch-container" style="margin-top: 20rpx;">
					<text class="label">使用次数频率限制</text>
					<switch :checked="form.enableLimit" @change="toggleUseLimit" color="#4a8cff" />
				</view>

				<view v-if="form.enableLimit" class="limit-container">
					<view class="limit-item">
						<text class="limit-label">每</text>
						<input class="limit-input" type="number" v-model="form.limitRound" placeholder="0" />
						<text class="limit-label">天，</text>
					</view>
					<view class="limit-item">
						<text class="limit-label">可用</text>
						<input class="limit-input" type="number" v-model="form.limitCount" placeholder="0" />
						<text class="limit-label">次</text>
					</view>
				</view>
				<view class="limit-container">
					<view class="limit-item">
						<text class="limit-label">总共可使用</text>
						<input class="limit-input" type="number" v-model="form.limitTotal" placeholder="0" />
						<text class="limit-label">次</text>
					</view>
				</view>
			</view>

			<!-- 备注 -->
			<view class="form-item">
				<text class="label">备注说明</text>
				<textarea class="textarea" v-model="form.description" placeholder="请输入备注信息"
					placeholder-class="placeholder" />
			</view>
		</view>

		<button class="submit-btn" @click="handleSubmit" v-if="tit == '新增会员卡'">确认添加</button>
		<view class="anniu" v-else>
			<button class="submit-btn" @click="btndel">删除</button>
			<button class="submit-btn" @click="btnedit">确认</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#f8f8f8",
				},
				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>
	.container {
		padding: 30rpx;
		background-color: #f8f8f8;
		min-height: 100vh;
	}
    .anniu{
		display: flex;
		justify-content: space-between;
		button{
			width: 48%;
			background-color: #4a8cff;
			color: white;
			border-radius: 50rpx;
			height: 90rpx;
			line-height: 90rpx;
			font-size: 34rpx;
			box-shadow: 0 8rpx 16rpx rgba(74, 140, 255, 0.3);
			
			&:active {
				background-color: #3a7be5;
			}
		}
	}
	.header {
		margin-bottom: 50rpx;

		.title {
			font-size: 40rpx;
			font-weight: bold;
			color: #333;
			display: block;
			margin-bottom: 10rpx;
		}

		.subtitle {
			font-size: 28rpx;
			color: #999;
		}
	}

	.form-container {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 0 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.form-item {
		padding: 30rpx 0;
		border-bottom: 1rpx solid #f0f0f0;

		&:last-child {
			border-bottom: none;
		}

		.label {
			font-size: 30rpx;
			color: #666;
			display: block;
			margin-bottom: 20rpx;
		}

		.input {
			height: 80rpx;
			font-size: 32rpx;
			color: #333;
			background-color: #f9f9f9;
			border-radius: 8rpx;
			padding: 0 20rpx;
		}

		.input-with-unit {
			position: relative;

			.input {
				padding-right: 80rpx;
			}

			.unit {
				position: absolute;
				right: 20rpx;
				top: 50%;
				transform: translateY(-50%);
				color: #999;
				font-size: 28rpx;
			}
		}

		.switch-container {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.limit-container {
			margin-top: 20rpx;
			background-color: #f9f9f9;
			border-radius: 8rpx;
			padding: 20rpx;
			display: flex;
			align-items: center;
		}

		.limit-item {
			display: flex;
			align-items: center;

			&:last-child {
				margin-bottom: 0;
			}
		}

		.limit-label {
			font-size: 28rpx;
			color: #666;
			margin: 0 10rpx;
		}

		.limit-input {
			width: 120rpx;
			height: 60rpx;
			background-color: #fff;
			border-radius: 8rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			text-align: center;
			border: 1rpx solid #eee;
		}

		.textarea {
			width: 100%;
			height: 180rpx;
			font-size: 32rpx;
			color: #333;
			background-color: #f9f9f9;
			border-radius: 8rpx;
			padding: 20rpx;
			box-sizing: border-box;
		}
	}

	.placeholder {
		color: #ccc;
		font-size: 30rpx;
	}

	.submit-btn {
		margin-top: 60rpx;
		background-color: #4a8cff;
		color: white;
		border-radius: 50rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 34rpx;
		box-shadow: 0 8rpx 16rpx rgba(74, 140, 255, 0.3);

		&:active {
			background-color: #3a7be5;
		}
	}
</style>