<template>
	<uv-navbar autoBack title="单笔限额" placeholder />
	<view class="quota-box">
		<view class="quota-title">限额配置</view>
		<view class="quota-label">用户消费单笔限额</view>
		<view class="quota-value">
			<input v-model="onlineOrderConsumptionLimit" class="quota-input" type="text" placeholder="请输入"
				placeholder-class="quota-input-placeholder" />
		</view>
		<view class="quota-btn" @click.stop="handleSubmit">保存</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, unref } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import { updateBusinessStatusApi, getBusinessLimitApi } from "@/api/ShopContent.ts";
	import { validTwoMinNum } from "@/util/RegEx.ts";

	const onlineOrderConsumptionLimit = ref('');
	const submitLoading = ref(false);
	const businessId = ref();
	onLoad((opt) => {
		businessId.value = opt.businessId;
		getBusinessLimit()
	});


	const getBusinessLimit = async () => {
		try {
			uni.showLoading({
				mask: true
			})
			const { data } = await getBusinessLimitApi();
			onlineOrderConsumptionLimit.value = data
		} catch (error) {
			//TODO handle the exception
		} finally {
			uni.hideLoading()
		}
	}








	const handleSubmit = async () => {
		if (submitLoading.value) return false;
		const t = validTwoMinNum(unref(onlineOrderConsumptionLimit))
		if (t) {
			uni.showToast({
				title: t,
				icon: 'none'
			});
			return false
		};
		submitLoading.value = true;
		try {
			const p_ = {
				businessId: unref(businessId),
				onlineOrderConsumptionLimit: unref(onlineOrderConsumptionLimit)
			}
			await updateBusinessStatusApi(p_);
			uni.showToast({
				title: "保存成功",
				icon: 'none',
				duration: 1000
			});
			setTimeout(() => {
				uni.navigateBack()
			}, 1000)
		} catch (error) {

		} finally {
			submitLoading.value = false;
		}

	}
</script>

<style lang="scss" scoped>
	.quota-box {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;

		.quota-title {
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC-Bold;
			font-weight: 700;
			color: #1A1A1A;
			line-height: 45rpx;
		}

		.quota-label {
			padding-top: 20rpx;
			font-size: 28rpx;
			color: #1a1a1a;
			font-family: PingFang SC, PingFang SC-Regular;
			font-weight: 400;
			line-height: 40rpx;
		}

		.quota-value {
			width: 100%;
			height: 75rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.quota-input {
				width: 100%;
				height: 100%;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				color: #1a1a1a;
			}

			.quota-input-placeholder {
				color: #cccccc;

			}
		}

		.quota-btn {
			margin: 67rpx auto 0;
			width: 630rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			background: #eb5153;
			border-radius: 45rpx;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Regular;
			font-weight: 400;
			color: #ffffff;
		}
	}
</style>