<template>
	<xn-form-container
		:title="formData.id ? '消费结算' : '消费结算'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol">
			<a-form-item label="会员姓名：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入会员姓名" disabled allow-clear />
			</a-form-item>
			<a-form-item label="糕点：" name="accountBalance">
				<a-input v-model:value="formData.accountBalance" placeholder="请输入账户余额" disabled allow-clear />
			</a-form-item>
			<a-form-item label="积分：" name="voucherBalance">
				<a-input v-model:value="formData.voucherBalance" placeholder="请输入代金券余额" disabled allow-clear />
			</a-form-item>
			<a-form-item label="消费糕点或积分：" name="consumptionMoney">
				<a-input-number v-model:value="formData.consumptionMoney" placeholder="请输入消费糕点或积分" allow-clear :min="0.1" :max="formData.accountBalance+formData.voucherBalance" style="width:100%"/>
			</a-form-item>
			<a-form-item label="手机号：" name="phoneNumber">
				<a-input v-model:value="formData.phoneNumber" placeholder="请输入手机号" disabled allow-clear style="width:100%"/>
			</a-form-item>
			<a-form-item label="验证码：" name="phoneCode">
				<a-input v-model:value="formData.phoneCode" placeholder="请输入验证码" allow-clear style="width:65%"/>
				<a-button style="margin-left:20px;" type="primary" @click="sendMessage()" :disabled="isDisabled">
					{{ isDisabled == true? countdown+'秒后重新发送':'发送验证码' }}
				</a-button>
			</a-form-item>

		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="consumptionRecordForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import consumptionRecordApi from '@/api/biz/consumptionRecordApi'
	import bizUserApi from "@/api/biz/bizUserApi";
	import bizAuthApi from "@/api/biz/bizAuthApi";

	import {message, Modal} from 'ant-design-vue';
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	const labelCol = ref({ span: 5 })
	const wrapperCol = ref({ span: 18 })

	const buttonText = ref('发送验证码')
	const countdown = ref(300);
	const isDisabled = ref(false);

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		clearInterval(timer);
		isDisabled.value = false;
		countdown.value = 300;
		isDisabled.value = false
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.userId = formData.value.id
			formData.value.id = ''
			formData.value.consumptionOperate = '3'
			formData.value.phoneNumber = formData.value.phone
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入消费者姓名')],
		consumptionMoney: [required('请输入消费糕点或积分')],
		accountBalance:  [required('请输入账户余额')],
		voucherBalance:  [required('请输入代金券余额')],
		phoneNumber:  [required('请输入手机号')],
		phoneCode:  [required('请输入验证码')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				consumptionRecordApi
					.consumptionRecordSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	let timer = null; // 定时器
	const sendMessage = () => {
		console.log("1111")
		debugger
		if(!formData.value.phoneNumber){
			message.error('请先输入手机号！')
			return
		}
		bizAuthApi.sendMessage({phone:formData.value.phoneNumber}).then((res)=>{
			/*if(res.code == '200'){
				message.success("发送成功")
			}else{
				message.error(res.msg)
			}*/
			// 禁用按钮
			isDisabled.value = true;
			// 启动倒计时
			timer = setInterval(() => {
				if (countdown.value > 0) {
					countdown.value--;
				} else {
					// 倒计时结束，重置状态
					clearInterval(timer);
					isDisabled.value = false;
					countdown.value = 300;
				}
			}, 1000);
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
