<template>
	<div class="table-box">
		<ProTable
			ref="proTable"
			title="我发布的"
			:columns="columns"
			:requestApi="getTableList"
			:dataCallback="dataCallback"
			:tool-button="false"
			:pagination="true"
			background
			:isShowSearch="false"
		>
			<template #operation="{ row }">
				<el-button type="primary" link @click="onLook(row)">
					<img src="@/assets/images/tableIcon/look.png" alt="" class="configureIcon" />
					<span>查看</span>
				</el-button>
				<el-button type="primary" link @click="handleEditItem(row)">
					<img src="@/assets/images/tableIcon/updateIcon.png" alt="" class="configureIcon" />
					<span>编辑</span>
				</el-button>
				<el-button type="danger" link :icon="Delete" @click="handleDeleteItem(row)">删除</el-button>
			</template>
		</ProTable>
		<el-dialog v-model="visible" :destroy-on-close="true" title="查看通知">
			<div>
				<span class="flx-center titleText">{{ rowList.title }}</span>
			</div>
			<div class="text-12 flx-center">
				<span>撰稿人：{{ rowList.createByName }}</span>
				<span class="time">时间:{{ rowList.createTime }}</span>
			</div>
			<div class="content text-12" v-html="rowList.content"></div>
			<div class="littleTitle list">附件列表</div>
			<el-table
				:data="annexFiles"
				border
				class="el-table"
				:header-cell-style="{ backgroundColor: '#e1eeff', textAlign: 'center' }"
				:cell-style="{ textAlign: 'center' }"
			>
				<el-table-column type="index" label="序号" width="200" />
				<el-table-column prop="fileName" label="附件名称" />
				<el-table-column label="操作" width="200">
					<template #default="{ row }">
						<el-button type="primary" link size="small" @click="addAnnexFile(row)">查看</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="littleTitle list">接收人员列表</div>
			<el-table
				:data="rowList.accetpList"
				class="el-table"
				max-height="200"
				:header-cell-style="{ textAlign: 'center', boxShadow: 'inset 0px -1px 0px 0px #EBEEF5' }"
				:cell-style="{ textAlign: 'center' }"
			>
				<el-table-column prop="acceptName" label="名称" />
				<el-table-column prop="isRead" label="阅读状态">
					<template #default="{ row }">
						{{ row.isRead === 1 ? "已读" : "未读" }}
					</template>
				</el-table-column>
				<el-table-column prop="readTime" label="查看时间" />
			</el-table>
			<template #footer>
				<!-- <el-button type="primary" v-if="!store.isManager" @click="onKnow">我已知晓</el-button> -->
				<el-button type="primary" @click="visible = false">关闭</el-button>
			</template>
		</el-dialog>
		<el-dialog v-model="fileVisible" title="查看附件" width="30%" show-close>
			<el-table
				:data="current"
				border
				class="el-table"
				:header-cell-style="{ backgroundColor: '#f5f7fa', textAlign: 'center' }"
				:cell-style="{ textAlign: 'center' }"
			>
				<el-table-column prop="fileName" label="文件名称" />
				<el-table-column label="操作" width="200">
					<template #default="{ row }">
						<el-button type="primary" link size="small" @click="ondownLoad(row)">下载</el-button>
					</template>
				</el-table-column>
			</el-table>
			<template #footer>
				<span class="dialog-footer">
					<el-button type="primary" @click="fileVisible = false"> 关闭 </el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="governmentMyposted">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { ColumnProps } from "@/components/ProTable/interface";
import { useDownload } from "@/hooks/useDownload";
import { Delete } from "@element-plus/icons-vue";
import { jxj_User } from "@/api/types";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import { noticemyPostPage, noticedelMyPost, noticeDetailMyPost, noticeReadMyPost } from "@/api/modules/common";
import { exportApp } from "@/api/modules/jxjview";
import { GlobalStore } from "@/stores";
import { useRoute, useRouter } from "vue-router";
import type { Options } from "@/views/goverment/approve/company/components/unit-table.vue";
import type { UploadFile } from "@/components/FilesUpload/FilesUpload.vue";

type AnnexFilesOptions = Options & {
	files: UploadFile[];
};

const store = GlobalStore();
const router = useRouter();

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
const baseUrl = import.meta.env.VITE_API_URL;
const visible = ref(false);
const fileVisible = ref(false);
const rowList = ref({
	title: "",
	createByName: "",
	createTime: "",
	content: "",
	accetpList: {}
});
const rowNoticeId = ref("");

const annexFiles = ref<AnnexFilesOptions[]>([]);
const current = ref<AnnexFilesOptions>({} as AnnexFilesOptions);

// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },

	// 多级 prop
	{ prop: "title", label: "通知标题", search: { el: "input" } },
	{ prop: "content", label: "通知内容", search: { el: "input" } },
	{ prop: "createTime", label: "发布时间" },
	{ prop: "operation", label: "操作", fixed: "right" }
];

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，那么你可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
	// console.log(data);
	return {
		list: data.records,
		total: Number(data.total),
		pageNo: Number(data.current),
		pageSize: Number(data.size)
	};
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
	let newParams = JSON.parse(JSON.stringify(params));
	return noticemyPostPage(newParams);
};

const onLook = async row => {
	const { result } = await noticeDetailMyPost({ noticeId: row.noticeId });
	rowList.value = result || [];
	annexFiles.value = result.annexFileList;
	rowList.value.accetpList = result.acceptList;
	visible.value = true;
	rowNoticeId.value = row.noticeId;
};

// 修改数据按钮
const handleEditItem = async (row: any) => {
	const { result } = await noticeDetailMyPost({ noticeId: row.noticeId });
	store.Message = result;
	router.push({ path: "/goverment/notice/newNotification/index", query: { noticeId: row.noticeId } });
};

// 删除用户信息
const handleDeleteItem = async (params: jxj_User.ResUserList) => {
	await useHandleData(noticedelMyPost, { noticeId: params.noticeId }, `删除【${params.title}】`);
	proTable.value.getTableList();
};

const addAnnexFile = (row: AnnexFilesOptions) => {
	fileVisible.value = true;
	current.value = [row];
};

const ondownLoad = row => {
	ElMessageBox.confirm("确认下载数据?", "温馨提示", { type: "warning" }).then(() => {
		if (!row.fileName) {
			ElMessage.error("当前没有文件，请先上传文件");
		} else {
			window.open(baseUrl + "/xmgl/file/preview?fileUrl=" + row.fileUrl);
		}
	});
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>
