<template>
	<div class="system-config-notice-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="listType" @tab-change="queryList">
				<el-tab-pane name="0" label="通知"></el-tab-pane>
				<el-tab-pane name="1" label="公告"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt10">
				<el-form-item label="标题" prop="Title">
					<el-input v-model="state.queryParams.Title" placeholder="标题" clearable @keyup.enter="queryList" style="width: 250px" />
				</el-form-item>
				<template v-if="listType === '0'">
					<el-form-item label="通知类型" prop="CircularTypeId">
						<el-select v-model="state.queryParams.CircularTypeId" placeholder="请选择通知类型">
							<el-option v-for="item in bulletinType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
					<el-form-item label="通知时间" prop="exTime">
						<el-date-picker
							v-model="state.queryParams.exTime"
							type="datetimerange"
							unlink-panels
							range-separator="至"
							start-placeholder="开始时间"
							end-placeholder="结束时间"
							:shortcuts="shortcuts"
							@change="timeStartChangeCr"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
						/>
					</el-form-item>
				</template>
				<template v-else>
					<el-form-item label="公告类型" prop="BulletinTypeId">
						<el-select v-model="state.queryParams.BulletinTypeId" placeholder="请选择公告类型">
							<el-option v-for="item in circularType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
						</el-select>
					</el-form-item>
					<el-form-item label="公告时间" prop="Time">
						<el-date-picker
							v-model="state.queryParams.Time"
							type="datetimerange"
							unlink-panels
							range-separator="至"
							start-placeholder="开始时间"
							end-placeholder="结束时间"
							:shortcuts="shortcuts"
							@change="timeStartChange"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
						/>
					</el-form-item>
				</template>

				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
			<div class="mb20">
				<el-button type="primary" @click="onNoticeAdd" v-if="listType === '0'" v-auth="'auxiliary:notice:add'" title="新增通知">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
        <el-button type="primary" @click="onNoticeAdd" v-else v-auth="'auxiliary:bulletin:add'" title="新增公告">
          <SvgIcon name="ele-Plus" class="mr5" />新增
        </el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id" ref="multipleTableRef" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" />
				<!--  通知  -->
				<template v-if="listType === '0'">
					<el-table-column prop="circularTypeName" label="通知类型" show-overflow-tooltip></el-table-column>
					<el-table-column prop="circularStateText" label="通知状态" show-overflow-tooltip></el-table-column>
					<el-table-column prop="title" label="通知标题" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="必须阅读" show-overflow-tooltip>
						<template #default="{ row }">
							{{ row.isMustRead ? '是' : '否' }}
						</template>
					</el-table-column>
					<el-table-column prop="commonTypeText" label="已阅读数" show-overflow-tooltip>
						<template #default="{ row }">
							<el-popover placement="right" :width="400" trigger="click">
								<template #reference>
									<el-button link type="primary">{{ row.readedNum + '/' + row.needReadNum }}</el-button>
								</template>
								<el-table :data="row.circularReadGroups" v-loading="state.loading" row-key="id" ref="multipleTableRef">
									<el-table-column prop="userName" label="姓名" show-overflow-tooltip v-if="row.circularType === 1"></el-table-column>
									<el-table-column prop="orgName" label="部门名称" show-overflow-tooltip v-if="row.circularType === 2"></el-table-column>
									<el-table-column prop="isRead" label="阅读状态" show-overflow-tooltip>
										<template #default="{ row }">
											{{ row.isRead ? '已读' : '未读' }}
										</template>
									</el-table-column>
									<el-table-column prop="isTimeOut" label="超时" show-overflow-tooltip>
										<template #default="{ row }">
											{{ row.isTimeOut ? '已超时' : '未超时' }}
										</template>
									</el-table-column>
								</el-table>
							</el-popover>
						</template>
					</el-table-column>
					<el-table-column prop="circularTypeText" label="通知对象" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="通知时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column label="失效时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.lostEfficacyTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="sourceOrgName" label="来源单位" show-overflow-tooltip></el-table-column>
				</template>
				<!--  公告  -->
				<template v-if="listType === '1'">
					<el-table-column prop="bulletinTypeName" label="公告类型" show-overflow-tooltip></el-table-column>
					<el-table-column prop="bulletinStateText" label="公告状态" show-overflow-tooltip></el-table-column>
					<el-table-column prop="title" label="公告标题" show-overflow-tooltip></el-table-column>
					<el-table-column prop="readedNum" label="已阅次数" show-overflow-tooltip></el-table-column>
					<el-table-column prop="pushRangesText" label="公告范围" show-overflow-tooltip width="300"></el-table-column>
					<el-table-column prop="commonTypeText" label="通知时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column label="失效时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.loseEfficacyTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="creatorOrgName" label="来源单位" show-overflow-tooltip></el-table-column>
				</template>
				<el-table-column label="操作" width="140" fixed="right" align="center">
					<template #default="{ row }">
						<el-button link type="primary" @click="onDetail(row)" v-auth="'auxiliary:notice:detail'"> 查看详情 </el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>
		<!-- 流程审批 -->
		<process-audit ref="processAuditRef" @orderProcessSuccess="queryList" />
		<!--  编辑通知公告  -->
		<notice-edit ref="noticeEditRef" />
	</div>
</template>

<script lang="ts" setup name="auxiliaryNotice">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, FormInstance } from 'element-plus';
import { shortcuts } from '/@/utils/constants';
import { formatDate } from '/@/utils/formatTime';
import { auth } from '/@/utils/authFunction';
import { bulletinList, circularList, bulletinBaseData, circularBaseData } from '/@/api/auxiliary/notice';

// 引入组件
const ProcessAudit = defineAsyncComponent(() => import('/@/components/ProcessAudit/index.vue')); // 流程审批
const NoticeEdit = defineAsyncComponent(() => import('/@/views/auxiliary/notice/component/Notice-edit.vue')); // 通知公告编辑

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Title: null,
		BulletinTimeStart: null, // 创建时间 开始
		BulletinTimeEnd: null, // 创建时间 结束
		CircularTimeStart: null,
		CircularTimeEnd: null,
		exTime: [],
		Time: [],
		BulletinTypeId: null, //通知类型
		CircularTypeId: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const bulletinType = ref<EmptyArrayType>([]); // 通知类型
const circularType = ref<EmptyArrayType>([]); // 通知类型
const listType = ref<string>('0'); // 列表类型
const handleTimeChange = (val: string[], startKey: string, endKey: string) => {
	if (val) {
		state.queryParams[startKey] = val[0];
		state.queryParams[endKey] = val[1];
	} else {
		state.queryParams[startKey] = '';
		state.queryParams[endKey] = '';
	}
	queryList();
};
// 甄别时间
const timeStartChangeCr = (val: string[]) => {
	handleTimeChange(val, 'CircularTimeStart', 'CircularTimeEnd');
};
const timeStartChange = (val: string[]) => {
	handleTimeChange(val, 'BulletinTimeStart', 'BulletinTimeEnd');
};
const getBaseData = async () => {
	try {
		const resCircularType = await circularBaseData();
		circularType.value = resCircularType.result?.circularType ?? [];
		const res = await bulletinBaseData();
		bulletinType.value = res.result.bulletinType ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	switch (listType.value) {
		case '0': //通知
			if (!auth('auxiliary:notice:query')) ElMessage.error('抱歉，您没有权限获取通知公告列表！');
			else {
				const requestCircular = {
					title: state.queryParams.Title,
					CircularTypeId: state.queryParams.CircularTypeId,
					CircularTimeStart: state.queryParams.CircularTimeStart,
					CircularTimeEnd: state.queryParams.CircularTimeEnd,
				};
				circularList(requestCircular)
					.then((res) => {
						state.loading = false;
						state.tableData = res.result.items ?? [];
						state.total = res.result.total ?? 0;
					})
					.finally(() => {
						state.loading = false;
					});
			}
			break;
		case '1': // 公告
			if (!auth('auxiliary:bulletin:query')) ElMessage.error('抱歉，您没有权限获取通知公告列表！');
			else {
				const requestBulletin = {
					title: state.queryParams.Title,
					BulletinTypeId: state.queryParams.BulletinTypeId,
					BulletinTimeStart: state.queryParams.BulletinTimeStart,
					BulletinTimeEnd: state.queryParams.BulletinTimeEnd,
				};
				bulletinList(requestBulletin)
					.then((res) => {
						state.loading = false;
						state.tableData = res.result.items ?? [];
						state.total = res.result.total ?? 0;
					})
					.finally(() => {
						state.loading = false;
					});
			}
			break;
	}
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.BulletinTimeStart = null;
	state.queryParams.BulletinTimeEnd = null;
	state.queryParams.CircularTimeStart = null;
	state.queryParams.CircularTimeEnd = null;
	queryList();
};
// 通知公告新增
const processAuditRef = ref<RefType>(); // 处理流程
const onNoticeAdd = () => {
	const processType = listType.value === '0' ? '新增通知' : '新增公告';
	const annexName = listType.value === '0' ? '通知附件' : '公告附件';
	const params = {
		id: '',
		processType,
		orderDetail: {},
		extra: {
			dialogTitle: processType,
			inputPlaceholder: '办理意见',
			annexName,
		},
	};
	processAuditRef.value.openDialog(params);
};
// 查看详情
const router = useRouter();
const onDetail = (row: any) => {
	const tagsViewName = listType.value === '0' ? '通知详情' : '公告详情';
	router.push({
		name: 'auxiliaryNoticeDetail',
		params: {
			id: row.id,
			isRead: 0,
			tagsViewName,
		},
	});
};
// 编辑
const noticeEditRef = ref<RefType>(); // 通知公告编辑
const onEdit = (row: any) => {
	noticeEditRef.value.openDialog(row, listType.value);
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
	multipleSelection.value = val;
};
// 页面加载时
onMounted(() => {
	getBaseData();
	queryList();
});
</script>

<style lang="scss" scoped>
.system-config-notice-container {
}
</style>
