<template>
	<view class="boxconstant min-page">
		<view class="bg-fff u-flex u-m-b-32 top">
			<image style="width: 60rpx; height: 60rpx" src="/pageBwc/static/images/bwc.png"></image>
			<view class="u-flex-1 u-flex u-p-l-24">
				<view class="u-font-28 u-flex-1 u-p-r-4">
					<view class="color-333 font-bold">霸王餐</view>
					<view class="color-666 u-m-t-4 u-font-24">设置充值消费的N倍，当前订单立即免单</view>
				</view>
				<up-switch v-model="form.enable" size="18" :active-value="1" :inactive-value="0" :disabled="isMainShop() ? false : true"></up-switch>
			</view>
		</view>
		<view class="boxconstantbox">
			<view class="boxconstantbox_one">充值设置</view>
			<view class="u-flex u-m-t-32">
				<input class="number-box" type="number" :min="2" v-model="form.rechargeTimes" @blur="rechargeTimesInput" />
				<view class="bei">倍</view>
			</view>
			<view class="color-666 u-font-28 u-m-t-16">用户消费结账时，成功充值订单金额的X倍即可享受免单</view>
			<view class="u-m-t-20 u-m-b-20">
				<up-line class=""></up-line>
			</view>
			<view class="boxconstantbox_one">充值门槛</view>
			<view class="u-flex u-m-t-32">
				<input class="number-box" type="number" v-model="form.rechargeThreshold" @blur="rechargeThresholdInput" />
				<view class="bei">元</view>
			</view>
			<view class="color-666 u-font-28 u-m-t-16">订单的支付金额满足X元，才能使用</view>
		</view>
		<view class="boxconstantbox">
			<view class="boxconstantbox_one">可用门店</view>
			<view class="u-m-t-16">
				<my-shop-select @shop-select="shopSelect" v-model:selShops="form.shopIdList" v-model:useType="form.useShopType"></my-shop-select>
			</view>
		</view>
		<!-- <view class="boxconstantbox"
			style="margin-top:24rpx; padding:32rpx 24rpx; border-radius: 0 0  18px 18px;">
			<view class="boxconstantbox_one" style="margin-bottom: 15rpx;">
				充值说明
			</view>
			<view>
				<up-textarea v-model="form.rechargeDesc" placeholder="请输入内容"></up-textarea>
			</view>
		</view> -->
		<view class="boxconstantbox">
			<view class="boxconstantbox_one">可使用类型</view>
			<view class="u-m-t-16">
				<my-dine-types v-model="form.useType"></my-dine-types>
			</view>
		</view>
		<view class="boxconstantbox">
			<view class="u-flex u-col-center u-row-between">
				<view>
					<view class="color-333 u-font-32 font-bold">与优惠券同享</view>
					<view class="color-666 u-font-24 u-m-t-4">开启后，可与优惠券同时使用</view>
				</view>
				<up-switch v-model="form.withCoupon" size="18"></up-switch>
			</view>
			<view class="u-flex u-col-center u-row-between u-m-t-24">
				<view>
					<view class="color-333 u-font-32 font-bold">与积分抵扣同享</view>
					<view class="color-666 u-font-24 u-m-t-4">开启后，可与积分抵扣同时使用</view>
				</view>
				<up-switch v-model="form.withPoints" size="18"></up-switch>
			</view>
		</view>
		<my-bottom-btn-group @cancel="cancel" @save="editFreeDing" isOpenPermission></my-bottom-btn-group>
		<my-marketing-mask name="霸王餐" v-if="isMarketShow(configInfo, 'shopIdList', 'enable')"></my-marketing-mask>
	</view>
</template>

<script setup>
import { onShow, onLoad } from '@dcloudio/uni-app';
import { reactive, ref, watch } from 'vue';
import { isMainShop, isMarketShow } from '@/store/account';
import { getFreeDing, updateFreeDing } from '@/http/api/freeDing.js';

const configInfo = ref({});

const form = reactive({
	rechargeTimes: 2,
	rechargeThreshold: '',
	enable: false,
	rechargeDesc: '',
	useShopType: 'all',
	useType: [],
	shopIdList: [],
	withCoupon: false,
	withPoints: false
});
onLoad(() => {
	// uni.$utils.inputReg.bind()()
});
onShow(() => {
	getlist();
});
/**
 * 获取配置信息
 */
const getlist = async () => {
	let res = await getFreeDing();
	configInfo.value = res;
	res.shopIdList = res.shopIdList || [];
	Object.assign(form, res);
};

let rechargeTimesInput = (e) => {
	console.log(e);
	if (e.detail.value == '' || e.detail.value < 2) {
		form.rechargeTimes = 2;
		uni.showToast({
			title: '请输入大于等于2的整数',
			icon: 'none'
		});
		return;
	}
};
function rechargeThresholdInput(e) {
	if (e.detail.value == '' || e.detail.value <= 0.01) {
		form.rechargeThreshold = 0.01;
		uni.showToast({
			title: '请输入大于0的数字',
			icon: 'none'
		});
		return;
	}
}
/**
 * 修改配置信息
 */
const editFreeDing = async () => {
	if (!form.rechargeTimes) {
		uni.showToast({
			title: '请输入充值设置',
			icon: 'none'
		});
		return;
	}
	if (!form.rechargeThreshold) {
		uni.showToast({
			title: '请输入充值门槛',
			icon: 'none'
		});
		return;
	}
	if (form.useShopType != 'all' && !form.shopIdList.length) {
		uni.showToast({
			title: '请选择可用门店',
			icon: 'none'
		});
		return;
	}
	if (form.useType.length == 0) {
		uni.showToast({
			title: '请选择可使用类型',
			icon: 'none'
		});
		return;
	}
	let res = await updateFreeDing(form);
	uni.showToast({
		title: '保存成功'
	});
	Object.assign(form, res);
	setTimeout(() => {
		// uni.navigateBack();
	}, 1500);
};

function cancel() {
	uni.navigateBack();
}
</script>

<style lang="scss" scoped>
.boxconstant {
	background: #f7f7f7;
	padding: 32rpx 28rpx;

	.boxconstantbox {
		padding: 32rpx 24rpx;
		border-radius: 16rpx;
		margin-top: 32rpx;
		width: 100%;
		background: #ffffff;

		.boxconstantbox_one {
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: bold;
			font-size: 28rpx;
			color: #333333;
		}

		.boxconstantbox_tow {
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
			// display: flex;
			// justify-content: flex-start;
			// align-items: center;
			// flex-wrap: wrap;
			// align-content: flex-start;
			.text {
				display: inline-flex;
				text-align: center;
				margin: 0 12rpx;
				width: 118rpx;
				height: 48rpx;
				line-height: 48rpx;
				background: #ffffff;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				border: 2rpx solid #e5e5e5;
			}
		}
	}

	.oneboxconstant {
		margin-top: 32rpx;
		width: 100%;
		background: #ffffff;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-wrap: wrap;
		padding: 32rpx 24rpx;

		.oneboxconstant_one {
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
		}
	}

	.save {
		margin: 100rpx auto 50rpx auto;
		width: 530rpx;
		height: 80rpx;
		background: #318afe;
		border-radius: 56rpx 56rpx 56rpx 56rpx;
		font-family: Source Han Sans CN, Source Han Sans CN;
		font-weight: 500;
		font-size: 32rpx;
		color: #ffffff;
		line-height: 80rpx;
		text-align: center;
	}
}
.top {
	padding: 24rpx 20rpx 28rpx 28rpx;
}
.number-box {
	width: 260rpx;
	font-size: 28rpx;
	padding: 0 26rpx;
	border-radius: 6rpx 0 0 6rpx;
	border-top: 2rpx solid #d9d9d9;
	border-bottom: 2rpx solid #d9d9d9;
	border-left: 2rpx solid #d9d9d9;
	background: #fff;
	box-sizing: border-box;
	height: 70rpx;
	line-height: 70rpx;
}
.bei {
	display: flex;
	padding: 0 38rpx;
	height: 70rpx;
	line-height: 70rpx;
	align-items: center;
	border-radius: 0 6rpx 6rpx 0;
	border: 2rpx solid #d9d9d9;
	background: #f7f7fa;
	font-size: 28rpx;
	color: #999999;
}
</style>
