<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="{span: 5}">
<!--			<a-form-item label="充值门店" name="orgId">
				<a-tree-select
					v-model:value="formData.orgId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择充值门店"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
									children: 'children',
									label: 'name',
									value: 'id'
								}"
					selectable="false"
					tree-line
				/>
			</a-form-item>-->
			<a-form-item label="充值金额及以上：" name="rechargeAmount">
				<a-input-number v-model:value="formData.rechargeAmount" style="width: 100%" :min="0" :max="99999999"
								:precision="2" placeholder="请输入充值金额及以上" allow-clear/>
			</a-form-item>
<!--			<a-form-item label="蛋糕券大小(寸)：" name="couponAmount">
				<a-input-number v-model:value="formData.couponAmount" style="width: 100%" :min="0" :max="99999999"
								:precision="2" placeholder="请输入蛋糕券大小(寸)" allow-clear/>
			</a-form-item>
			<a-form-item label="蛋糕券数量：" name="couponNum">
				<a-input-number v-model:value="formData.couponNum" style="width: 100%" :min="0" :max="99999999"
								:precision="0" placeholder="请输入优惠券数量" allow-clear/>
			</a-form-item>-->
			<a-form-item label="账户到账糕点：" name="accountBalance">
				<a-input-number v-model:value="formData.accountBalance" style="width: 100%" :min="0" :max="99999999"
								:precision="2" placeholder="请输入账户到账糕点" allow-clear/>
			</a-form-item>
			<a-form-item label="是否立即到账：" name="isAccount" >
				<a-radio-group button-style="solid" v-model:value="formData.isAccount" @change="onOperateChange">
					<a-radio-button value="1">
						是
					</a-radio-button>
					<a-radio-button value="0">
						否
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="帮助激活人数：" name="helpCount" v-show="helpFlag" :rules="helpRules">
				<a-input-number v-model:value="formData.helpCount" style="width: 100%" :min="1" :max="99999999"
								:precision="0" placeholder="请输入帮助激活糕点到账人数" allow-clear/>
			</a-form-item>
<!--			<a-form-item label="店员返点比例：" name="rebateRatio">
				<a-input-number v-model:value="formData.rebateRatio" style="width: 100%" :min="1" :max="100"
								:precision="2" placeholder="请输入店员返点比例（积分）" addon-after="%" allow-clear/>
			</a-form-item>
			<a-form-item label="用户返点比例：" name="rebateUserRatio">
				<a-input-number v-model:value="formData.rebateUserRatio" style="width: 100%" :min="1" :max="100"
								:precision="2" placeholder="请输入店员返点比例（积分）" addon-after="%" 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="bizRechargePlanConfigForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import bizRechargePlanConfigApi from '@/api/biz/bizRechargePlanConfigApi'
import bizOrgApi from '@/api/biz/bizOrgApi'
// 抽屉状态
const open = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const treeData = ref([])
const helpFlag = ref(false)
const helpRules = ref([])

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	formData.value.isAccount = ref('1')
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if(formData.value.isAccount == '1'){
			helpFlag.value = false
			helpRules.value = []
		}else{
			helpFlag.value = true
			helpRules.value.push({required:true,message: '请输入帮助激活糕点到账人数'})
		}

	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}

// 获取机构树并加入顶级
bizOrgApi.orgTreeSelector().then((res) => {
	treeData.value = res
})
// 默认要校验的
const formRules = {
	rechargeAmount: [required('请输入充值金额及以上')],
	couponAmount: [required('请输入蛋糕券大小(寸)')],
	couponNum: [required('请输入蛋糕券数量')],
	accountBalance: [required('请输入账户到账糕点')],
	rebateRatio: [required('请输入店员返点比例（积分）')],
	rebateUserRatio:[required('请输入用户返点比例（积分）')],
	orgId: [required('请选择充值门店')],
	isAccount: [required('请选择是否立即到账')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizRechargePlanConfigApi
				.bizRechargePlanConfigSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {
		})
}
//选择是否立即到账
const onOperateChange = (e) => {
	console.log("value:"+e.target.value)
	if(e.target.value == '1'){
		//选择了是
		helpFlag.value = false
		helpRules.value=[]
	}else{
		//选择了否
		helpFlag.value = true
		helpRules.value.push({required:true,message: '请输入帮助激活糕点到账人数'})
	}
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
