<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="couponId">
				<a-select v-model:value="formData.couponId" placeholder="请选择蛋糕券"
						  :options="couponIdList" @change="onChangeCouponId"
				> </a-select>
			</a-form-item>

			<a-form-item label="蛋糕券：" name="couponAmount" v-show = "couponFlag">
				<a-input-number v-model:value="formData.couponAmount" style="width: 100%" :min="0" :max="99999999"
								:precision="2" placeholder="请输入蛋糕券" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="蛋糕券类型：" name="couponType" v-show = "couponFlag">
				<a-radio-group button-style="solid" v-model:value="formData.couponType" disabled>
					<a-radio-button value="1">
						寸
					</a-radio-button>
					<a-radio-button value="2">
						元
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="蛋糕券数量：" name="couponNum" v-show = "couponFlag">
				<a-input-number v-model:value="formData.couponNum" style="width: 100%" :min="0" :max="99999999"
								:precision="0" placeholder="请输入蛋糕券数量" allow-clear disabled/>
			</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 { Modal } from 'ant-design-vue'
	import { createVNode } from 'vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
	import couponConfigApi from "@/api/biz/couponConfigApi";
	import bizCouponRecordApi from "@/api/biz/bizCouponRecordApi";

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const couponIdList = ref()
	const couponFlag = ref(false)

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

	const chongzhiFlag = ref(false)
	const tuikuanFlag = ref(false)
	const qitaFlag = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.userId = formData.value.id
			chongzhiFlag.value = true
			qitaFlag.value = true
			tuikuanFlag.value = false
		}

		couponConfigApi.getMemberList().then((res)=>{
			couponIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.couponAmount + (item.couponType=='1'?'寸':'元') + "*" + item.couponNum
				}
			})
		})
	}

	const onChangeCouponId = (value) => {
		console.log("value:"+value)
		couponConfigApi.couponConfigDetail({id:value}).then((res)=>{
			formData.value.couponAmount = res.couponAmount
			formData.value.couponType = res.couponType
			formData.value.couponNum = res.couponNum
			couponFlag.value = true
		})
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
		couponFlag.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入消费者姓名')],
		accountBalance:  [required('请输入账户余额')],
		voucherBalance:  [required('请输入代金券余额')],
		couponId:  [required('选择蛋糕券')],
	}
	// 验证并提交数据
	//title: '确定要为'+formData.value.name+(formData.value.consumptionOperate=='1'?'赠送':'扣减')+(formData.value.consumptionType=='1'?'代金券':'余额')+formData.value.consumptionMoney+'元吗？',
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizCouponRecordApi
					.giftCoupon(formDataParam)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})


	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
