<template>
	<div class="auxiliary-notice-detail-container layout-pd">
		<el-card shadow="never" v-loading="loading">
			<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" class="show-info-form">
				<template v-if="['通知详情'].includes(noticeType)">
					<el-row :gutter="0">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="通知类型">
								{{ state.ruleForm.circularTypeName }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="通知标题">
								{{ state.ruleForm.title }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="必须阅读">
								{{ state.ruleForm.isMustRead ? '是' : '否' }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="失效时间：">
								{{ formatDate(state.ruleForm.lostEfficacyTime, 'YYYY-mm-dd HH:MM:SS') }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="通知对象：">
								{{ state.ruleForm.circularTypeText }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="已阅读数量：">
								<el-popover :width="450" trigger="click" popper-class="notice-container">
									<template #reference>
										<el-button placement="right" link type="primary">{{ state.ruleForm?.readedNum + '/' + state.ruleForm?.needReadNum }}</el-button>
									</template>
									<div class="notice-container-box">
										<div class="notice-container-box-inner">
											已读：{{ state.ruleForm?.readedNum }}
											<el-scrollbar class="mt5" v-if="state.ruleForm?.circularReadGroups?.length">
												<el-tag v-for="item in state.ruleForm?.circularReadGroups.filter((i) => i.isRead)"
													>{{ state.ruleForm.circularType === 1 ? item.userName : item.orgName }}
													<span v-if="item.isTimeOut" class="color-danger">(超时阅读)</span>
												</el-tag>
											</el-scrollbar>
										</div>
										<div class="notice-container-box-inner">
											未读：{{ state.ruleForm?.needReadNum - state.ruleForm?.readedNum }}
											<el-scrollbar class="mt5 mb10" v-if="state.ruleForm.circularReadGroups?.length">
												<el-tag v-for="item in state.ruleForm.circularReadGroups.filter((i) => !i.isRead)">{{
													state.ruleForm.circularType === 1 ? item.userName : item.orgName
												}}</el-tag>
											</el-scrollbar>
										</div>
									</div>
								</el-popover>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
							<el-form-item label="文档内容：">
								<div v-html="state.ruleForm.content"></div>
							</el-form-item>
						</el-col>
					</el-row>
				</template>
				<template v-if="['公告详情'].includes(noticeType)">
					<el-row :gutter="10">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="公告类型：">
								{{ state.ruleForm.bulletinTypeName }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="公告标题：">
								{{ state.ruleForm.title }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="失效时间：">
								{{ formatDate(state.ruleForm.loseEfficacyTime, 'YYYY-mm-dd HH:MM:SS') }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
							<el-form-item label="来源单位：">
								{{ state.ruleForm.sourceOrgName }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
							<el-form-item label="公告范围：">
								{{ state.ruleForm.pushRangesText }}
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="editor-content-view">
							<el-form-item label="文档内容：">
								<div v-html="state.ruleForm.content"></div>
							</el-form-item>
						</el-col>
					</el-row>
				</template>
			</el-form>
			<el-button type="primary" @click="onAudit" title="通知审批" v-if="state.ruleForm?.circularState == 1" v-auth="'auxiliary:notice:audit'">
				审批
			</el-button>
			<el-button type="primary" @click="onAudit" title="公告审批" v-if="state.ruleForm?.bulletinState == 1" v-auth="'auxiliary:bulletin:audit'">
				审批
			</el-button>
		</el-card>
		<el-dialog v-model="state.dialogVisible" width="500px" draggable :title="noticeType" @close="close">
			<el-form :model="state.examineForm" label-width="110px" ref="examineFormRef">
				<el-form-item label="审核结果" prop="isPass" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
					<el-radio-group v-model="state.examineForm.isPass">
						<el-radio :value="true">同意</el-radio>
						<el-radio :value="false">不同意</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="审核理由" prop="reason" :rules="[{ required: true, message: '请填写审核理由', trigger: 'blur' }]">
					<el-input v-model="state.examineForm.reason" placeholder="请填写审核理由" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
						<el-button type="primary" @click="onSubmit(examineFormRef)" :loading="loading">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script setup lang="ts" name="auxiliaryNoticeDetail">
import { reactive, onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import { bulletinAudit, bulletinDetail, circularAudit, circularDetail, circularRead } from '@/api/auxiliary/notice';
import mittBus from '@/utils/mitt';
import { FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: <EmptyObjectType>{},
	examineForm: <EmptyObjectType>{
		isPass:true,
		reason:'',
	},
});
const noticeType = ref<any>('通知详情');
const loading = ref<boolean>(false);
const isRead = ref<string | string[]>('0');
const getDetail = async (id: string | string[]) => {
	loading.value = true;
	try {
		switch (noticeType.value) {
			case '通知详情':
				if (isRead.value === '1') {
					// 阅读
					const readCircularRes = await circularRead(id);
					state.ruleForm = readCircularRes.result;
				} else {
					// 查看详情
					const circularRes = await circularDetail(id);
					state.ruleForm = circularRes.result;
				}
				break;
			case '公告详情':
				const bulletinRes = await bulletinDetail(id);
				state.ruleForm = bulletinRes.result;
				break;
			default:
				break;
		}
		loading.value = false;
	} catch (error) {
		loading.value = false;
		console.log(error);
	}
};
const route = useRoute();
const onAudit = () => {
	state.dialogVisible = true;
};
// 关闭当前页
const closePage = () => {
	state.dialogVisible = false;
  mittBus.emit('clearCache', 'auxiliaryNotice');
	// 关闭当前 tagsView
	mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
};
const examineFormRef = ref<RefType>();
// 审核提交
const onSubmit =  throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		const request = {
			reason: state.examineForm.reason,
			id: route.params.id,
			isPass:state.examineForm.isPass,
		};
		switch (noticeType.value) {
			case '通知详情':
				circularAudit(request)
					.then(() => {
						closePage();
					})
					.catch(() => {
						state.dialogVisible = false;
						loading.value = false;
					});
				break;
			case '公告详情':
				bulletinAudit(request)
					.then(() => {
						closePage();
					})
					.catch(() =>{
						state.dialogVisible = false;
						loading.value = false;
					})
		}
	})
},300);
const close = () => {
	examineFormRef.value?.resetFields();
	examineFormRef.value?.clearValidate();
};
onMounted(() => {
	noticeType.value = route.params?.tagsViewName ?? '通知详情';
	isRead.value = route.params?.isRead ?? '0';
	getDetail(route.params.id);
});
</script>
