<template>
	<el-dialog v-model="state.dialogVisible" draggable title="红包审批" @close="close" destroy-on-close append-to-body>
		<el-form :model="state.infoForm" label-width="110px" :disabled="loading" class="show-info-form">
			<el-row :gutter="10">
				<el-col>
					<p class="border-title">来电信息</p>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="受理人">{{ state.infoForm.order?.acceptorName }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="受理时间">{{ formatDate(state.infoForm.order?.creationTime, 'YYY-mm-dd HH:MM:SS') }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单编码">{{ state.infoForm.order?.no }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="所在区域">{{ state.infoForm.order?.county }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="来源方式">{{ state.infoForm.order?.sourceChannel }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="受理类型">{{ state.infoForm.order?.acceptType }}</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="热点分类">{{ state.infoForm.order?.hotspotSpliceName }}</el-form-item>
				</el-col>
				<el-col>
					<p class="border-title mt10">联系人</p>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="来电人"
						>{{ state.infoForm.order?.fromName }} <el-text tag="b" v-if="state.infoForm.order?.isSecret" class="ml5">保密</el-text></el-form-item
					>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="性别">{{ state.infoForm.order?.fromGenderTxt }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="联系电话">{{ state.infoForm.order?.fromPhone }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="地址">{{ state.infoForm.order?.fullAddress }}</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="身份证">{{ state.infoForm.order?.licenceNo }}</el-form-item>
				</el-col>
				<el-col :lg="12" :md="12" :sm="12" :xl="12" :xs="24">
					<el-form-item label="工作单位">{{ state.infoForm.order?.workUnit }}</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<p class="border-title mt10 mb10">
			审批信息<el-text tag="b" type="danger" class="ml5">{{ auditType }}</el-text>
		</p>
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading">
			<el-form-item label="审批状态" prop="status" :rules="[{ required: true, message: '请选择审批状态', trigger: 'change' }]">
				<el-select v-model="state.ruleForm.status" clearable style="max-width: 240px" placeholder="请选择审批状态" @change="selectStatus">
					<el-option v-for="item in auditComBox" :label="item.value" :key="item.key" :value="item.key" />
				</el-select>
				<el-checkbox v-model="state.ruleForm.isSendSms" disabled class="ml5">审批结果通知短信</el-checkbox>
			</el-form-item>
			<el-form-item label="短信模板" prop="smsTemplateId" :rules="[{ required: true, message: '请选择短信模板', trigger: 'change' }]">
				<el-select v-model="state.ruleForm.smsTemplateId" placeholder="请选择短信模板" clearable>
					<el-option v-for="item in smsTemplateList" :value="item.id" :key="item.id" :label="item.content" />
				</el-select>
			</el-form-item>
			<el-form-item label="审批意见" prop="opinion" :rules="[{ required: false, message: '请填写审批意见', trigger: 'blur' }]">
				<el-input
					type="textarea"
					:autosize="{ minRows: 6, maxRows: 10 }"
					v-model="state.ruleForm.opinion"
					placeholder="请填写审批意见"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="红包发放"
				>1.{{ auditType }}红包金额：<el-text tag="b" type="danger">{{ state.infoForm.redPackTxt }}</el-text></el-form-item
			>
			<el-form-item label="红包说明"
				>1.{{ auditType }}红包金额：<el-text tag="b" type="danger">{{ state.infoForm.amount }}</el-text
				>元；</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 { ElCheckbox } from 'element-plus';
import {
	citizenRedEnvelopeApproval,
	getCitizenRedEnvelopeApprovalDetail,
	getCitizenRedEnvelopeApprovalSmsTemplate,
	getGridRedEnvelopeApprovalDetail,
	gridRedEnvelopeApproval,
} from '@/api/snapshot/reAudit';
import { formatDate } from '@/utils/formatTime';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	infoForm: {},
	ruleForm: {
		redPackAuditId: null, // 审批ID
		status: null, // 审批状态
		opinion: null, // 审批意见
		smsTemplateId: null, // 短信模板
		isSendSms: true, // 是否发送短信
	},
	orderId: null,
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const auditComBox = ref<EmptyArrayType>([]); // 审批状态
const auditType = ref<string>('市民');
const openDialog = async (row: any, type?: string) => {
	loading.value = true;
	if (type) auditType.value = type;
	state.dialogVisible = true;
	state.orderId = row.orderId;
	try {
		if (type) {
			// 市民红包审核
			const { result } = await getCitizenRedEnvelopeApprovalDetail(row.orderId);
			auditComBox.value = result.auditComBox ?? [];
			state.infoForm = result;
			state.ruleForm.redPackAuditId = row.id;
			loading.value = false;
		} else {
			// 网格员
			const { result } = await getGridRedEnvelopeApprovalDetail(row.orderId);
			auditComBox.value = result.auditComBox ?? [];
			state.infoForm = result;
			state.ruleForm.redPackAuditId = row.id;
			loading.value = false;
		}
	} catch (error) {
		console.log(error);
		loading.value = false;
	}
};
const ruleFormRef = ref<RefType>();
const smsTemplateList = ref<EmptyArrayType>([]); // 短信模板
// 选择审批状态
const selectStatus = (val: any) => {
	ruleFormRef.value?.resetFields('smsTemplateId');
	if (val || val === 0) {
		loading.value = true;
		getCitizenRedEnvelopeApprovalSmsTemplate({ OrderId: state.orderId, Status: val })
			.then((res) => {
				smsTemplateList.value = res.result ?? [];
				if (smsTemplateList.value.length) {
					// 默认选中第一条
					state.ruleForm.smsTemplateId = smsTemplateList.value[0].id;
				}
				loading.value = false;
			})
			.catch((e) => {
				console.log(e);
				smsTemplateList.value = [];
				loading.value = false;
			});
	} else {
		smsTemplateList.value = [];
	}
};
// 保存
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,
		};
		if (auditType.value === '市民') {
			citizenRedEnvelopeApproval(request)
				.then(() => {
					loading.value = false;
					closeDialog();
					emit('updateList');
					ElMessage.success('红包审批成功');
				})
				.catch(() => {
					loading.value = false;
				});
		} else {
			gridRedEnvelopeApproval(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();
	state.orderId = null;
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
