<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" layout="vertical">
			<a-form-item label="用户id：" name="userId">
				<a-input v-model:value="formData.userId" placeholder="请输入用户id" allow-clear />
			</a-form-item>
			<a-form-item label="消费方式 1：代金券消费   2：余额消费  3：代金券+余额：" name="consumptionType">
				<a-input v-model:value="formData.consumptionType" placeholder="请输入消费方式 1：代金券消费   2：余额消费  3：代金券+余额" allow-clear />
			</a-form-item>
			<a-form-item label="代金券消费金额：" name="voucherMoney">
				<a-input v-model:value="formData.voucherMoney" placeholder="请输入代金券消费金额" allow-clear />
			</a-form-item>
			<a-form-item label="账户消费金额：" name="accountMoney">
				<a-input v-model:value="formData.accountMoney" placeholder="请输入账户消费金额" allow-clear />
			</a-form-item>
			<a-form-item label="消费时间：" name="consumptionTime">
				<a-date-picker v-model:value="formData.consumptionTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择消费时间" style="width: 100%" />
			</a-form-item>
			<a-form-item label="消费门店：" name="consumptionOrg">
				<a-input v-model:value="formData.consumptionOrg" placeholder="请输入消费门店" allow-clear />
			</a-form-item>
			<a-form-item label="备注：" name="consumptionRemark">
				<a-input v-model:value="formData.consumptionRemark" placeholder="请输入备注" allow-clear />
			</a-form-item>
			<a-form-item label="操作：1：赠送  2：扣减  3：消费：" name="consumptionOperate">
				<a-input v-model:value="formData.consumptionOperate" placeholder="请输入操作：1：赠送  2：扣减  3：消费" allow-clear />
			</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'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	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(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
