<template>
	<div class="system-config-notice-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.type" @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="Keyword">
					<el-input v-model="state.queryParams.keyword" placeholder="标题" clearable @keyup.enter="queryList" style="width: 250px" />
				</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="'system:config:notice:add'">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
				<el-button type="primary" @click="onNoticeDelete" v-waves v-auth="'system:config: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="state.queryParams.type === '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="state.queryParams.type === '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="'system:config: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" />
	</div>
</template>

<script lang="ts" setup name="systemConfigNotice">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { commonEnum, shortcuts } from '/@/utils/constants';
import { formatDate } from '/@/utils/formatTime';
import { auth } from '/@/utils/authFunction';
import { commonBaseData, publicCommonList, deleteCommon } from '/@/api/system/commonAdvice';

// 引入组件
const NoticeAdd = defineAsyncComponent(() => import('/@/views/system/config/notice/component/Notice-add.vue')); // 公告通知新增
const NoticeEdit = defineAsyncComponent(() => import('/@/views/system/config/notice/component/Notice-edit.vue')); // 公告通知编辑

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		type: '0', // 常用意见类型
		CreationTimeStart: '', // 创建时间 开始
		CreationTimeEnd: '', // 创建时间 结束
		exTime: [], // 办理期限
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const commonType = ref<EmptyArrayType>([]); // 常用意见类型
const openState = ref<EmptyArrayType>([
	{
		key: true,
		value: '公开常用意见',
	},
	{
		key: false,
		value: '个人常用意见',
	},
]); // 是否公开
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, 'CreationTimeStart', 'CreationTimeEnd');
};
const getBaseData = async () => {
	try {
		const res = await commonBaseData();
		commonType.value = res.result?.commonType ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	if (!auth('system:config:notice:query')) ElMessage.error('抱歉，您没有权限获取通知公告列表！');
	else {
		publicCommonList(state.queryParams)
			.then((res) => {
				state.loading = false;
				state.tableData = res.result.items ?? [];
				state.total = res.result.total ?? 0;
			})
			.finally(() => {
				state.loading = false;
			});
	}
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 通知公告新增
const noticeAddRef = ref<RefType>(); // 通知公告新增
const onNoticeAdd = () => {
	noticeAddRef.value.openDialog(state.queryParams.type);
};
// 修改通知公告
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(() => {
	getBaseData();
	queryList();
});
</script>

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