<!-- 订单确认的使用积分弹窗 -->
<template>
	<su-popup :show="show" type="bottom" round="10" @close="emits('close')" showClose backgroundColor="#ffffff">
		<view class="title ss-m-t-16 ss-m-l-20 ss-flex">使用积分</view>
		<view class="model-box ss-p-x-30">
			<input v-model.number="state.points" class="uni-input input-points ss-m-b-10" type="number"
				placeholder="请输入抵扣积分" oninput="this.value=this.value.replace(/\D/g);" :disabled="state.disabled"/>
			
			
			
		</view>
		<view class="modal-footer text-center">
			<view class="subtitle text-disabled">您的可用积分<text class="text-red">{{currentMemberPoints}}</text></view>
			<view class="subtitle text-disabled">当前订单价格<text class="text-red">￥{{state.currentTotalPrice}}</text>，可使用的最高积分 <text class="text-red">{{canUesPoint}}</text></view>
			<view class="ss-flex ss-m-y-20 ss-col-center">
				<button class="confirm-btn ss-reset-button"
					@tap="state.points = 0;emits('confirm', state.points)">取消</button>
				<button class="confirm-btn ss-reset-button" @tap="onConfirm">确认</button>
			</view>
			
		</view>
	</su-popup>
</template>
<script setup>
	import {
		computed,
		reactive,
		ref,
		watch,
		nextTick,
		watchEffect
	} from 'vue';

	const props = defineProps({
		modelValue: { // 优惠劵列表
			type: Object,
			default () {},
		},
		show: {
			type: Boolean,
			default: false,
		},
		currentMemberPoints: {
			type: Number,
			default: 0
		},
		currentTotalPrice: {
			type: Number,
			default: 0
		},
	});
	const emits = defineEmits(['confirm', 'close']);
	const state = reactive({
		currentMemberPoints: parseFloat(props.currentMemberPoints),
		currentTotalPrice: parseFloat(props.currentTotalPrice),
		points: 0,
		disabled:false
	});
	// 当前可使用的最高积分 是当前的总价格 - 1块钱 并且积分只能是正数
	const canUesPoint = computed(()=>{
		return parseInt(state.currentTotalPrice - 1)
	})

	watchEffect(() => {
		// 使用积分不能大于可用积分
		if (state.points > state.currentMemberPoints) {
			state.points = state.currentMemberPoints; 
			// 使用 nextTick 确保 DOM 更新
			nextTick(() => {
				state.points = state.currentMemberPoints;
			});
		}
		// 使用积分不能大于实际价格
		if (state.points > state.currentTotalPrice) {
			console.log("newValue", state.points)
			console.log("state.currentTotalPrice", state.currentTotalPrice)
			state.points = state.currentTotalPrice; 
			// 使用 nextTick 确保 DOM 更新
			nextTick(() => {
				state.points = canUesPoint.value;
			});
		}
		// 如果计算出来的当前价格等于0 则不给输入
		if(canUesPoint.value == 0){
			state.disabled = true
		}
	})
	// 确认
	const onConfirm = () => {
		// if (!state.points) {
		// 	return false
		// }
		emits('confirm', state.points);
	}
</script>
<style lang="scss" scoped>
	:deep() {
		.uni-checkbox-input {
			background-color: var(--ui-BG-Main);
		}

		.uni-input-wrapper {
			width: 100% !important
		}
	}

	.model-box {
		// height: 10vh;
		text-align: center;
		font-size: 30rpx;

		.input-points {
			text-align: left;
			text-indent: 20rpx;
			height: 80rpx;
			border: 1px solid #bbbbbb;
			border-radius: 10rpx;
		}
	}
.text-disabled {
			color: #bbbbbb;
		}

	.title {
		font-size: 36rpx;
		height: 80rpx;
		font-weight: bold;
		color: #333333;
	}

	.subtitle {
		// font-size: 26rpx;
		font-weight: 500;
		color: #333333;
	}

	.model-content {
		height: 10vh;
	}

	.modal-footer {
		width: 100%;
		// height: 120rpx;
		//   background: #fff;
	}

	.confirm-btn {
		width: 710rpx;
		margin:0 20rpx;
		height: 80rpx;
		background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
		border-radius: 40rpx;
		color: #fff;
	}

	.reason-title {
		font-weight: 600;
		font-size: 20rpx;
		line-height: 26rpx;
		color: #ff0003;
	}

	.reason-desc {
		font-weight: 600;
		font-size: 20rpx;
		line-height: 26rpx;
		color: #434343;
	}
</style>