<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="consumptionOperate">
				<a-radio-group button-style="solid" @change="onOperateChange" v-model:value="formData.consumptionOperate">
					<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="adjustType">
				<a-radio-group button-style="solid" v-model:value="formData.adjustType">
					<a-radio-button value="1" v-show="chongzhiFlag">
						充值
					</a-radio-button>
					<a-radio-button value="2" v-show="tuikuanFlag">
						退款
					</a-radio-button>
					<a-radio-button value="3" v-show="qitaFlag">
						其他
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="调整方式：" name="consumptionType">
				<a-radio-group button-style="solid" v-model:value="formData.consumptionType">
					<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="consumptionMoney">
				<a-input-number v-model:value="formData.consumptionMoney" placeholder="请输入调整糕点或积分" allow-clear :min="0.1" :max="99999999" style="width:100%"/>
			</a-form-item>
			<a-form-item label="说明：" name="consumptionRemark">
				<a-input v-model:value="formData.consumptionRemark" placeholder="请输入说明" 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'
	import { Modal } from 'ant-design-vue'
	import { createVNode } from 'vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-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 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.consumptionOperate = ref('1')
			formData.value.consumptionType = ref('1')
			formData.value.userId = formData.value.id
			formData.value.id = ''
			formData.value.adjustType = ref('1')
			chongzhiFlag.value = true
			qitaFlag.value = true
			tuikuanFlag.value = false
		}
	}

	const onOperateChange = (e) => {
		console.log("value:"+e.target.value)
		if(e.target.value == '1'){
			//选择了赠送,展示充值和其他
			chongzhiFlag.value = true
			qitaFlag.value = true
			tuikuanFlag.value = false
			formData.value.adjustType = ref('1')
		}else{
			//展示退款和其他
			chongzhiFlag.value = false
			tuikuanFlag.value = true
			qitaFlag.value = true
			formData.value.adjustType = ref('2')
		}
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入消费者姓名')],
		consumptionOperate:  [required('请选择调整操作')],
		consumptionType: [required('请选择调整方式')],
		consumptionMoney: [required('请输入调整糕点或积分')],
		consumptionRemark: [required('请输入说明')],
		accountBalance:  [required('请输入账户余额')],
		voucherBalance:  [required('请输入代金券余额')],
		adjustType:  [required('请选择调整类别')],
	}
	// 验证并提交数据
	//title: '确定要为'+formData.value.name+(formData.value.consumptionOperate=='1'?'赠送':'扣减')+(formData.value.consumptionType=='1'?'代金券':'余额')+formData.value.consumptionMoney+'元吗？',
	const onSubmit = () => {
		Modal.confirm({
			title: '确定要为'+formData.value.name+(formData.value.consumptionOperate=='1'?'赠送':'扣减')+formData.value.consumptionMoney+(formData.value.consumptionType=='1'?'积分':'糕点')+'吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				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(() => {})
			},
			onCancel() {}
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
