<template>
	<el-dialog v-model="state.dialogVisible" width="70%" draggable title="随手拍信息配置详情" append-to-body destroy-on-close>
		<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" :disabled="loading" scroll-to-error>
			<el-text tag="h2" size="large" style="text-align: center">{{ state.ruleForm.title }}</el-text>
			<el-divider />
			<p style="text-align: center" class="mb10">
				<span class="mr5">创建时间：{{ formatDate(state.ruleForm.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
				<span class="mr5">类别：{{ state.ruleForm.bulletinTypeName }}</span>
				<span class="mr5">编号：{{ state.ruleForm.no }}</span>
				<span class="mr5">创建人：{{ state.ruleForm.creatorName }}</span>
				<span class="mr5">已被阅读：{{ state.ruleForm.readedNum }}</span>
				<span class="mr5">所属部门：{{ state.ruleForm.departmentName }}</span>
				<span class="mr5">审核状态：{{ state.ruleForm.bulletinStateTxt }}</span>
				<span>来源方式：{{ state.ruleForm.sourceMode === '1' ? '自建' : '外部' }}</span>
			</p>
			<p style="text-align: center" class="mb10">
				<span class="mr5">是否公开：{{ state.ruleForm.isOpen ? '是' : '否' }}</span>
				<span class="mr5">是否加粗：{{ state.ruleForm.isBold ? '是' : '否' }}</span>
				<span class="mr5">网站公开：{{ state.ruleForm.isOpenWebsite ? '是' : '否' }}</span>
				<span class="mr5">微博公开：{{ state.ruleForm.isWeibo ? '是' : '否' }}</span>
				<span class="mr5">微信公开：{{ state.ruleForm.isWeChat ? '是' : '否' }}</span>
				<span class="mr5">是否置顶：{{ state.ruleForm.isTop ? '是' : '否' }}</span>
				<span>是否飘窗：{{ state.ruleForm.isPopup ? '是' : '否' }}</span>
			</p>
			<p style="text-align: center">
				<span class="mr5">结束时间：{{ formatDate(state.ruleForm.bulletinTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
				<span>来源单位：{{ state.ruleForm.sourceOrgName }}</span>
			</p>
			<el-divider />
			<div v-html="state.ruleForm.content"></div>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button
					type="primary"
					@click="onAudit"
					:loading="loading"
					v-if="state.ruleForm.bulletinState === 1"
					v-auth="'snapshot:info:sspConfig:audit'"
					>审 批</el-button
				>
			</span>
		</template>
	</el-dialog>
	<el-dialog v-model="state.dialogVisibleAudit" width="500px" draggable title="审批" @close="close" destroy-on-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: false, 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.dialogVisibleAudit = false" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(examineFormRef)" :loading="loading">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx" name="noticeDetail">
import { reactive, ref } from 'vue';
import { auditSnapshotBulletin, getSnapshotBulletinDetail } from '@/api/snapshot/info';
import { formatDate } from '@/utils/formatTime';
import { ElMessage, FormInstance } from 'element-plus';
import { circularAudit } from '@/api/auxiliary/notice';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {},
	searchContent: '', // 搜索内容
	dialogVisibleAudit: false,
	examineForm: {
		isPass: true,
		reason: '',
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();

const openDialog = async (id: string) => {
	loading.value = true;
	state.dialogVisible = true;
	try {
		const { result } = await getSnapshotBulletinDetail(id);
		state.ruleForm = result ?? {};
		loading.value = false;
	} catch (error) {
		console.log(error);
		loading.value = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 审批
const onAudit = () => {
	state.dialogVisibleAudit = true;
};
const examineFormRef = ref<RefType>();
const close = () => {
	examineFormRef.value?.resetFields();
	examineFormRef.value?.clearValidate();
};
// 审核提交
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		const request = {
			reason: state.examineForm.reason,
			id: state.ruleForm.id,
			isPass: state.examineForm.isPass,
		};
		auditSnapshotBulletin(request)
			.then(() => {
				loading.value = false;
				closeDialog();
				state.dialogVisibleAudit = false;
				emit('updateList');
				ElMessage.success('审核完成');
			})
			.catch(() => {
				state.dialogVisibleAudit = false;
				loading.value = false;
			});
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
