<template>
	<view>
		<u-cell-group :border="false">
			<u-cell :border="false">
				<u--text slot="title" :text="`标题：${notication.title}`"   ></u--text>
			</u-cell>
			<u-cell :border="false">
				<u--text slot="title" type="info" :text="`发布者：${notication.createBy.name}，类型：${$dictUtils.getDictLabel('oa_notify_type', notication.type ,'')}`"></u--text>
			</u-cell>
			<u-cell>
				<u--text slot="title" type="info" :text="`发布时间: ${notication.createTime}`"></u--text>
			</u-cell>
		</u-cell-group>
		<view class="padding bg-white">
			<u-parse :content="notication.content"></u-parse>
		</view>
		<u-cell-group :border="false">
			<u-cell :border="false">
				<u--text slot="title" :text="`附件`"></u--text>
			</u-cell>
			<u-cell v-for="(file, index) in fileArray" :key="index" :border="false">
				<u--text slot="title" type="info" :text="getFileName(file)" @click="downloadFile(file)"></u--text>
			</u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import fileService from "@/api/file/fileService"
	import notifyService from "@/api/notify/notifyService";
	export default {
		data() {
			return {
				notication: {
					title: '',
					createTime: '',
					createBy: {
						name: ''
					}
				}
			}
		},
		computed: {
			fileArray() {
				// 拆分字符串，以逗号为分隔符
				return this.notication.files.split(',');
			}
		},
		onLoad (option) {
			notifyService.query({isSelf:true, id:option.id}).then((data)=>{
				this.notication = data
			});
		},
		methods: {
			getFileName(file) {
				// 通过字符串处理方法提取出文件名部分
				const nameIndex = file.indexOf('name=');
				if (nameIndex !== -1) {
					return decodeURIComponent(file.slice(nameIndex + 5)); // +5 是为了跳过 'name=' 部分
				}
				return file;
			},
			downloadFile(file) {
				if (file) {

					const paramsString = file.split('?')[1];
					const paramsArray = paramsString.split('&');

					let uploadPath = '';
					let name = '';

					// 遍历参数数组并提取参数值
					paramsArray.forEach(param => {
						const [key, value] = param.split('=');
						if (key === 'uploadPath') {
							uploadPath = value;
						} else if (key === 'name') {
							name = value;
						}
					});

					// 使用 uni.downloadFile 下载文件
					fileService.downloadUrl({
						uploadPath: uploadPath,
						name: name,
					}).then((data) => {
						// console.log('data', data)
						// window.open(data, '_blank'); // 在新标签页或新窗口打开文件
						// 在新窗口打开下载链接
						window.location.href = data;
					})

				}
			}
		}
	}
</script>

<style>

</style>
