<template>
	<el-dialog v-model="state.dialogVisible" draggable title="添加补充发放" @close="close">
		<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="productExpiredTime"
						:rules="[{ required: false, message: '请选择补充奖励类型', trigger: 'change' }]"
					>
						<el-select v-model="state.ruleForm.status" class="w100" placeholder="请选择补充奖励类型">
							<el-option v-for="item in state.auditComBox" :label="item.value" :key="item.key" :value="item.key" />
						</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, defineAsyncComponent, computed, unref, FunctionalComponent } from 'vue';
import { throttle } from '@/utils/tools';
import { ElMessage, FormInstance } from 'element-plus';
import { disabledDate } from '@/utils/constants';
import { ElCheckbox } from 'element-plus';
import { addSnapshotBulletin, editSnapshotBulletinBase } from '@/api/snapshot/info';
import { citizenRedEnvelopeApprovalAdditional, getCitizenRedEnvelopeApprovalAdditionalBaseInfo } from '@/api/snapshot/reAudit';
import dayjs from 'dayjs';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		name: null, // 姓名
		bankCardNo: null, // 卡号
		openBank: null, // 开户行
		replenishAmount: null, // 补充金额
		replenishTime: dayjs().toDate(), // 补充发放时间
		productExpiredTime: null, // 补充奖励类型
		replenishRemark: null, // 备注
		isSendSMS: true, // 发送短信
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();

const bulletinTypeOptions = ref<EmptyArrayType>([]); // 公告类型
const orgsOptions = ref<EmptyArrayType>([]); // 来源单位
const bulletinSource = ref<EmptyArrayType>([]); // 来源
const openDialog = async (row: any) => {
	console.log(row, '测试1231');
	loading.value = true;
	state.dialogVisible = true;
	try {
		const responseAnnounce = await editSnapshotBulletinBase();
		bulletinTypeOptions.value = responseAnnounce.result?.bulletinType ?? [];
		orgsOptions.value = responseAnnounce.result?.orgsOptions ?? [];
		bulletinSource.value = responseAnnounce.result?.bulletinSource ?? [];
		loading.value = false;
	} catch (error) {
		console.log(error);
		loading.value = true;
	}
};
// 获取基础数据
const getBaseData = async () => {
	try {
		const { result } = await getCitizenRedEnvelopeApprovalAdditionalBaseInfo();
		console.log(result, '测试');
	} 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,
		};
		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>
