<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>
				<el-form-item label="通知类型" prop="BulletinTypeId">
					<el-select v-model="state.queryParams.BulletinTypeId" placeholder="请选择通知类型">
						<el-option v-for="item in bulletinType" :value="item.id" :key="item.id" :label="item.status" />
					</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>
				<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-waves v-auth="'auxiliary:notice:add'">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
				<el-button type="primary" @click="onNoticeDelete" v-waves v-auth="'auxiliary:notice:delete'" :disabled="!multipleSelection.length">
					<SvgIcon name="ele-Delete" 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="commonTypeText" label="通知类型" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="通知状态" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="通知标题" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="文档编码" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="必须阅读" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="已阅读数" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" 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 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 prop="commonTypeText" label="来源单位" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="已阅读次数" show-overflow-tooltip></el-table-column>
				</template>
				<!--  公告  -->
				<template v-if="listType === '1'">
					<el-table-column prop="commonTypeText" label="公告类型" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="公告状态" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="公告标题" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="文档编码" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" label="已阅次数" show-overflow-tooltip></el-table-column>
					<el-table-column prop="commonTypeText" 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 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 prop="commonTypeText" label="来源单位" show-overflow-tooltip></el-table-column>
				</template>
				<el-table-column label="操作" width="120" fixed="right" align="center">
					<template #default="{ row }">
						<el-button link type="primary" @click="updateNotice(row)" v-auth="'auxiliary:notice:edit'" title="修改通知公告"> 修改 </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>
		<!--  常用意见新增  -->
		<notice-add ref="noticeAddRef" @updateList="queryList" />
		<!--  常用意见编辑  -->
		<notice-edit ref="noticeEditRef" @updateList="queryList" />
		<!-- 流程审批 -->
		<process-audit ref="processAuditRef" @orderProcessSuccess="queryList" />
	</div>
</template>

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

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

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Title: '',
		BulletinTimeStart: '', // 创建时间 开始
		BulletinTimeEnd: '', // 创建时间 结束
		exTime: [], // 办理期限
		BulletinTypeId: '', //通知类型
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const bulletinType = 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] = '';
	}
};
// 甄别时间
const timeStartChangeCr = (val: string[]) => {
	handleTimeChange(val, 'BulletinTimeStart', 'BulletinTimeEnd');
};
const getBaseData = async () => {
	try {
    let res = null;
    switch (listType.value){
      case '0':
        res = await circularBaseData();
        bulletinType.value = res.result ?? [];
        break;
      case '1':
        res = await bulletinBaseData();
        bulletinType.value = res.result ?? [];
        break;
    }
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
  getBaseData();
	state.loading = true;
	if (!auth('auxiliary:notice:query')) ElMessage.error('抱歉，您没有权限获取通知公告列表！');
	else {
		switch (listType.value) {
			case '0':
        circularList(state.queryParams)
					.then((res) => {
						state.loading = false;
						state.tableData = res.result.items ?? [];
						state.total = res.result.total ?? 0;
					})
					.finally(() => {
						state.loading = false;
					});
				break;
			case '1':
				bulletinList(state.queryParams)
					.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();
	queryList();
};
// 通知公告新增
const noticeAddRef = ref<RefType>(); // 通知公告新增
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 noticeEditRef = ref<RefType>(); // 修改通知公告
const updateNotice = (row: any) => {
	noticeEditRef.value.openDialog(row);
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
	multipleSelection.value = val;
};
// 删除通知公告
const onNoticeDelete = () => {
	const contents = multipleSelection.value.map((item: any) => item.content).join('、');
	const ids = multipleSelection.value.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定要删除：【${contents}】的通知公告，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			deleteCommon({ ids }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>

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