<route lang="json5" type="page">
	{
	layout: 'default',
	style: {
	navigationStyle: 'custom',
	navigationBarTitleText: '三册',
	},
	}
</route>
<template>
	<view :class="{'gray': appStore.isGray == 1 }">
		<wd-navbar left-text="返回" left-arrow
			custom-style="padding-top: var(--status-bar-height, 0); background-image: linear-gradient(to right, #1890ff, #096dd9); color: #fff;"
			@click-left="handleClickLeft">
		</wd-navbar>
		<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>
	</view>
</template>

<script setup>
	import {
		ref,
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useAppStore
	} from '@/store';
	import {
		useUserStore
	} from '@/store/user';
	import {
		getListApi,
		getJobDesIdByDepAndName,
		getPathByInsflagQuote,
		queryByIdApi
	} from '@/api/pages/sc';
	import {
		useColPickerData
	} from '@/hooks/useColPickerData'
	import Base64 from '@/utils/code';
	import {
		getEnvBaseUrl
	} from '@/utils/index'
	const {
		colPickerData,
		findChildrenByCode
	} = useColPickerData()

	const appStore = useAppStore();
	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 handleClickLeft = () => {
		uni.navigateBack();
	};
	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}`
		});
	};
	
	// 判断是否是 H5 环境
	const isH5 = () => {
	  // #ifdef H5
	  return true;
	  // #endif
	  // #ifndef H5
	  return false;
	  // #endif
	}

	onLoad(() => {
		getList();
		//#ifdef H5 || MP-WEIXIN
		ifH5 = true;
		//#endif
	});
</script>
<style lang="scss">
	#bdIframe {
		position: absolute;
		top: 10;
		left: 0;
		width: 100%;
		height: 100%;
	}
</style>