<template>
	<el-dialog v-model="state.dialogVisible" draggable title="添加补充发放" @close="close" destroy-on-close append-to-body>
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading" scroll-to-error>
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="姓名" prop="name" :rules="[{ required: false, message: '请填写姓名', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.name" placeholder="请填写姓名" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="卡号" prop="bankCardNo" :rules="[{ required: false, message: '请填写卡号', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.bankCardNo" placeholder="请填写卡号" clearable type="number"></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="开户行" prop="openBank" :rules="[{ required: false, message: '请填写开户行', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.openBank" placeholder="请填写开户行" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="补充金额" prop="replenishAmount" :rules="[{ required: false, message: '请填写补充金额', trigger: 'blur' }]">
						<el-input-number v-model="state.ruleForm.replenishAmount" :min="0" :precision="2" class="w100" placeholder="请填写补充金额" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="补充发放时间" prop="replenishTime" :rules="[{ required: false, message: '请选择补充发放时间', trigger: 'change' }]">
						<el-date-picker
							v-model="state.ruleForm.replenishTime"
							type="datetime"
							placeholder="请选择补充发放时间"
							class="w100"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
						/>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="补充奖励类型" prop="replenishType" :rules="[{ required: false, message: '请选择补充奖励类型', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.replenishType" class="w100" placeholder="请选择补充奖励类型">
							<el-option v-for="item in replenishTypeOptions" :label="item.dicDataName" :key="item.dicDataValue" :value="item.dicDataValue" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="">
						<el-checkbox v-model="state.ruleForm.isSendSMS">发送短信</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="备注" prop="replenishRemark" :rules="[{ required: true, message: '请填写备注', trigger: 'blur' }]">
						<el-input
							type="textarea"
							:autosize="{ minRows: 6, maxRows: 10 }"
							v-model="state.ruleForm.replenishRemark"
							placeholder="请填写备注"
							maxlength="200"
							show-word-limit
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx" name="noticeDetail">
import { reactive, ref } from 'vue';
import { throttle } from '@/utils/tools';
import { ElMessage, FormInstance } from 'element-plus';
import { ElCheckbox } from 'element-plus';
import { citizenRedEnvelopeApprovalAdditional, getCitizenRedEnvelopeApprovalAdditionalBaseInfo } from '@/api/snapshot/reAudit';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		name: null, // 姓名
		bankCardNo: null, // 卡号
		openBank: null, // 开户行
		replenishAmount: null, // 补充金额
		replenishTime: null, // 补充发放时间 默认当前时间
		replenishType: '1', // 补充奖励类型
		replenishRemark: null, // 备注
		isSendSMS: true, // 发送短信
	},
	auditObj: {},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = (row: any) => {
	getBaseData();
	state.ruleForm.replenishTime = new Date().toISOString();
	state.auditObj = row;
	state.dialogVisible = true;
};
// 获取基础数据
const replenishTypeOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await getCitizenRedEnvelopeApprovalAdditionalBaseInfo();
		replenishTypeOptions.value = result.replenishType;
	} catch (e) {
		console.log(e);
	}
};
// 保存
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		const request = {
			...state.ruleForm,
			redPackAuditId: state.auditObj.id,
		};
		citizenRedEnvelopeApprovalAdditional(request)
			.then(() => {
				loading.value = false;
				closeDialog();
				emit('updateList');
				ElMessage.success('补充发放成功');
			})
			.catch(() => {
				loading.value = false;
			});
	});
}, 300);
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
