<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">特殊审批</el-text></p>
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading">
			<el-row :gutter="10">
				<el-col>
					<el-form-item label="审批状态" prop="status" :rules="[{ required: true, message: '请选择审批状态', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.status" clearable placeholder="请选择审批状态" @change="selectStatus">
							<el-option v-for="item in auditComBox" :label="item.value" :key="item.key" :value="item.key" />
						</el-select>
					</el-form-item>
				</el-col>
				<template v-if="state.ruleForm.status === 1">
					<el-col :span="12">
						<el-form-item label="审批类型" prop="auditTypeId" :rules="[{ required: true, message: '请选择审批类型', trigger: 'change' }]">
							<el-select v-model="state.ruleForm.auditTypeId" clearable placeholder="请选择审批类型" @change="selectType">
								<el-option v-for="item in auditType" :value="item.id" :key="item.id" :label="item.dicDataName" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="审批金额" prop="approvedAmount">
							<el-input v-model="state.ruleForm.approvedAmount" placeholder="请输入审批金额" clearable disabled />
						</el-form-item>
					</el-col>
				</template>
				<el-col>
					<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-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 { getSpecialRedEnvelopeApprovalBaseInfo, specialRedEnvelopeApproval } 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, // 审批状态
		auditTypeId: null, // 审批类型
		opinion: '', // 审批意见
		isSendSms: false, // 是否发送短信
		approvedAmount: 0, // 审批金额
	},
	orderId: null,
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const auditComBox = ref<EmptyArrayType>([]); // 审批状态
const auditType = ref<EmptyArrayType>([]); // 审批类型
const openDialog = async (row: any) => {
	loading.value = true;
	state.dialogVisible = true;
	state.orderId = row.orderId;
	try {
		// 特殊审批
		const { result } = await getSpecialRedEnvelopeApprovalBaseInfo(row.orderId);
		auditComBox.value = result.auditComBox ?? [];
		auditType.value = result.auditType ?? [];
		state.infoForm = result;
		state.ruleForm.redPackAuditId = row.id;
		loading.value = false;
	} catch (error) {
		console.log(error);
		loading.value = false;
	}
};
const ruleFormRef = ref<RefType>();
// 选择审批状态
const selectStatus = () => {
	ruleFormRef.value?.resetFields('auditTypeId');
};
// 选择审批类型
const selectType = (val: any) => {
	const item = auditType.value.find((item: any) => item.id === val);
	state.ruleForm.approvedAmount = item.amount;
};
// 保存
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,
		};
		specialRedEnvelopeApproval(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>
