<template>
	<el-dialog v-model="state.dialogVisible" width="30%" draggable title="添加备注" @close="close" destroy-on-close append-to-body>
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading">
			<el-form-item label="实发金额" prop="acutalAmount" :rules="[{ required: true, message: '请填写实发金额', trigger: 'blur' }]">
				<el-input-number v-model="state.ruleForm.acutalAmount" :min="0" :precision="2" class="w100" placeholder="请填写实发金额" />
			</el-form-item>
			<el-form-item label="发放结果" prop="isSend">
				<el-radio-group v-model="state.ruleForm.isSend" @change="selectIsSend">
					<el-radio :value="false">红包发放失败</el-radio>
					<el-radio :value="true">红包发放成功</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="发放失败原因" prop="failCase" v-if="!state.ruleForm.isSend">
				<el-radio-group v-model="state.ruleForm.failCase">
					<el-radio v-for="item in failCaseOptions" :value="item.key" :label="item.value" :key="item.key"></el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="备注" prop="sendRemarks" :rules="[{ required: true, message: '请填写备注', trigger: 'blur' }]">
				<el-input
					type="textarea"
					:autosize="{ minRows: 6, maxRows: 10 }"
					v-model="state.ruleForm.sendRemarks"
					placeholder="请填写备注"
					maxlength="200"
					show-word-limit
				></el-input>
			</el-form-item>
		</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 { citizenRedEnvelopeApprovalRemark, getCitizenRedEnvelopeApprovalRemarkBaseInfo } from '@/api/snapshot/reAudit';
// 引入组件
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		acutalAmount: 0, // 实发金额
		isSend: false,
		sendRemarks: null,
		failCase: 0,
	},
	auditObj: {},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = (row: any) => {
	state.auditObj = row;
	state.dialogVisible = true;
	getBaseData();
};
// 获取基础数据
const failCaseOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await getCitizenRedEnvelopeApprovalRemarkBaseInfo();
		failCaseOptions.value = result.failCase;
	} catch (e) {
		console.log(e);
	}
};
// 选择发送状态
const selectIsSend = (val: boolean) => {
	if (val) {
		state.ruleForm.failCase = null;
		if (state.auditObj.approvedAmount) {
			state.ruleForm.acutalAmount = state.auditObj.approvedAmount;
		}
	} else {
		state.ruleForm.failCase = 0;
	}
};
// 保存
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,
		};
		citizenRedEnvelopeApprovalRemark(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>
