<route lang="json5" type="page">
	{
	layout: 'default',
	style: {
	navigationStyle: 'custom',
	navigationBarTitleText: '三册',
	},
	}
</route>
<template>
	<PageLayout navTitle="三册查看">
		<wd-col-picker label="选择目录" v-model="selectedValue" :columns="dataSource" :column-change="columnChange"
			@confirm="handleConfirm" :display-format="displayFormat"></wd-col-picker>
		<iframe id="bdIframe" :src="fileUrl" ref="bdIframe" style="border: none;" class="iframe" v-if="fileUrl" />
		<view v-else @click="onlinePreview(item.path)" v-for="(item, i) in fileUrl2" style="padding: 15px 0 0 0">
			<wd-card :title="item.name"></wd-card>
		</view>
	</PageLayout>
</template>

<script setup>
	import {
		ref,
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useUserStore
	} from '@/store/user';
	import {
		getListApi,
		getJobDesIdByDepAndName,
		getPathByInsflagQuote,
		queryByIdApi
	} from '@/api/pages/sc';
	import Base64 from '@/utils/code';
	import {
		getEnvBaseUrl
	} from '@/utils/index'

	const data = ref([])
	const dataSource = ref([])
	const selectedValue = ref([]);
	const loading = ref(false);
	const path = ref('')
	const url = ref('')
	const fileUrl = ref('')
	const fileUrl2 = ref([])
	const columnChange = ({
		selectedItem,
		resolve,
		finish
	}) => {
		const areaData = findChildrenByCode(data.value, selectedItem.value)
		if (areaData && areaData.length) {
			resolve(
				areaData.map((item) => {
					return {
						value: item.value,
						label: item.text
					}
				})
			)
		} else {
			finish()
		}
	}

	const handleConfirm = ({
		value
	}) => {
		let base64 = new Base64();
		queryByIdApi(value[value.length - 1]).then(res => {
			let url = res.result.path
			let quote = res.result.quote
			let insFlag = res.result.insFlag
			if (url) {
				fileUrl.value = 'https://36.112.48.190/preview/onlinePreview?url=' + encodeURIComponent(base64
					.encode(getEnvBaseUrl() + '/sys/common/static/' + url))
			} else if (/^-?\d+$/.test(quote) && !insFlag) {
				getJobDesIdByDepAndName({
					jobname: quote,
					jobdep: useUserStore().userInfo.orgCode
				}).then((res) => {
					fileUrl.value = getEnvBaseUrl() + '/jmreport/view/929517863011811328?id=' + res
						.id +
						"&token=" + useUserStore().userInfo.token;
				})
			} else if (insFlag) {
				getPathByInsflagQuote({
					insFlag: insFlag,
					quote: quote
				}).then(res => {
					fileUrl.value = ''
					fileUrl2.value = res;
				})
			} else {
				fileUrl.value = ''
				fileUrl2.value = ''
			}
		})
	}

	const displayFormat = (selectedItems) => {
		if (selectedItems.length > 0) {
			return selectedItems[selectedItems.length - 1].label
		}
	}

	const getList = () => {
		getListApi(useUserStore().userInfo.orgCode).then(res => {
			data.value = res.result
			dataSource.value = [data.value.map(item => {
				return {
					value: item.value,
					label: item.text
				}
			})]
		});
	}

	const onlinePreview = (e) => {
		uni.navigateTo({
			url: `/pages/onlinePreview/${path}?data=${e}`
		});
	};

	const findChildrenByCode = (data, code) => {
		if (!code) {
			return data
		}
		for (const item of data) {
			if (item.value === code) {
				return item.children || null
			}
			if (item.children) {
				const childrenResult = findChildrenByCode(item.children, code)
				if (childrenResult) {
					return childrenResult
				}
			}
		}
		return null
	}

	// 判断是否是 H5 环境
	const isH5 = () => {
		// #ifdef H5
		return true;
		// #endif
		// #ifndef H5
		return false;
		// #endif
	}

	onLoad(() => {
		getList();
	});
</script>
<style lang="scss">
	#bdIframe {
		position: absolute;
		top: 10;
		left: 0;
		width: 100%;
		height: 100%;
	}
</style>