<template>
	<view class="backgroundContainer">
		<!-- 左侧菜单 -->
		<leftcontent :list="arrlist" @navurl="navurl"></leftcontent>
		<!-- 主页 -->
		<index :isShow="menuIndex==0" v-if="swipedex==0" @swip="swip"/>
		<!-- 户嘱页 -->
		<nurse :isshow="menuIndex==1" v-if="swipedex==1&&uni.getStorageSync('elderId')&&uni.getStorageSync('nuId')" />
		<!-- 医嘱 -->
		<doctorask v-if="swipedex==2&&uni.getStorageSync('elderId')&&uni.getStorageSync('nuId')" />
		<!-- 设备页 -->
		<logistics :isshow="menuIndex==3" v-if="swipedex==3&&uni.getStorageSync('elderId')&&uni.getStorageSync('nuId')" />
		<equipment :isShow="menuIndex==5" v-if="swipedex==5" />
		<!-- 医嘱页 -->
		<!-- <logistics :isShow="menuIndexshowfourth" v-if="menuIndex==3" /> -->
		<requestform :isShow="menuIndex==4" v-if="swipedex==4" />
		
		<!-- 没有数据 -->
		<view class="nomessageclass" :style="messageopit?{opacity: `1`}:{opacity: `0`}"
			v-if="(menuIndex == 1 || menuIndex == 2) && (!uni.getStorageSync('elderId') ||!uni.getStorageSync('nuId'))">
			<defaultr cont="暂无老人入住"></defaultr>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, onBeforeUnmount, nextTick } from 'vue';
	import { queryPadPageList ,getPermissionList } from '@/pages/watch/api/lunpan.js'
	import index from "./component/index.vue"
	import equipment from "./component/equipment.vue"
	import logistics from "./component/nurse/logistics.vue"
	import requestform from "./component/pleasetake/takehome.vue"
	import doctorask from "./component/doctorask/doctorask.vue"
	import nurse from "./component/nurse/index.vue"
	import { onShow } from '@dcloudio/uni-app';
	import { getServiceTree, getNcPackagelist } from './component/nurse/api.js'
	import leftcontent from "./component/leftcontent/leftcontent.vue"
	import defaultr from '@/pages/procurement/components/default.vue';

	const listarr = ref([]);
	onMounted(() => {
		menuIndex.value = 0;
		swipedex.value = 0;
		nextTick(() => swipedex.value = 0)

		getServiceTree().then((res : any) => {
			//缓存护嘱菜单
			uni.setStorageSync("saveTree", res)
		})
		getNcPackagelist().then((res : any) => {
			//缓存指令包
			uni.setStorageSync("Packagelist", res.result)
		})
		getPermissionList({employessId:uni.getStorageSync('userInfo').employessId}).then(res=>{
			listarr.value = res.result;
		})
	})

	// 通用的生成函数
	function genPaths(base, prefix, count, ext = 'png', startIndex = 0, pad = false) {
		return Array.from({ length: count }, (_, i) => {
			const idx = pad
				? String(i + startIndex).padStart(2, '0')
				: i + startIndex
			return `${base}/${prefix}${idx}.${ext}`
		})
	}
	const tabbrarr = ref([
		{ name: '护理矩阵', url: '/static/shouye/sy/n0.png', urls: '/static/shouye/sy/n1.png' ,type:'hldy_hljz'},
		{ name: '医疗矩阵', url: '/static/shouye/sy/y0.png', urls: '/static/shouye/sy/y1.png' ,type:'hldy_yljz'},
		{ name: '后勤矩阵', url: '/static/shouye/sy/l0.png', urls: '/static/shouye/sy/l1.png' ,type:'hldy_xzjz'},
		{ name: '请领', url: '/static/shouye/sy/l0.png', urls: '/static/shouye/sy/l1.png' ,type:'hldy_ql'},
	])
	const navurl = (e,v) => {
		swipedex.value = -1;
		switch (v.type){
			case 'hldy':
			swipedex.value = 0;
			setTimeout(()=>{
				menuIndex.value = 0;
			},100)
			console.log('护理单元')
				break;
			case 'hldy_hljz':
			swipedex.value = 1;
			setTimeout(()=>{
				menuIndex.value =1;
			},100)
			console.log('护理矩阵')
				break;
			case 'hldy_yljz':
			swipedex.value = 2;
			setTimeout(()=>{
				menuIndex.value = 2;
			},100)
			console.log('医疗矩阵')
				break;
			case 'hldy_xzjz':
			swipedex.value = 3;
			setTimeout(()=>{
				menuIndex.value = 3;
			},100)
			console.log('行政矩阵')
				break;
			case 'kf':
			console.log('库房')
				break;
			case 'kf_cg':
			console.log('采购')
				break;
			case 'kf_jh':
			console.log('拣货')
				break;
			case 'kf_wj':
			console.log('完结')
				break;
			case 'hldy_ql':
			swipedex.value = 4;
			setTimeout(()=>{
				menuIndex.value = 4;
			},100)
			console.log('请领')
				break;
			case 'kf_th':
			console.log('退货')
				break;
			case 'kf_pd':
			console.log('盘点')
				break;
			case 'wl':
			swipedex.value = 5;
			setTimeout(()=>{
				menuIndex.value = 5;
			},100)
			console.log('物联')
				break;
			case 'back':
			uni.navigateBack()
			console.log('物联')
				break;
				 
			default:
				break;
		}
		// 
	 
	}
	const arrlist = ref([
		{ name: '首页', url: '/static/shouye/sy/h0.png', urls: '/static/shouye/sy/h1.png' ,type:'hldy'}
	])
	const swip = ()=>{
		let nuName = uni.getStorageSync('nuName')
		queryPadPageList({'pageNo':-1,'nuName':nuName}).then((res => {
			console.log(res.result.records)
			let arr = res.result.records[0].permissionList;
			arrlist.value = [{ name: '首页', url: '/static/shouye/sy/h0.png', urls: '/static/shouye/sy/h1.png' ,type:'hldy'}];
			let tbr = [];
			if(arr){
				arr.forEach((v,i)=>{
					let obj = listarr.value.find(item=>
						item.menuCode == v.menuCode
					);
					if(obj){
						tbr.push(v);
					}
				})
				tbr.forEach(item=>{
					let obj = tabbrarr.value.find(r=>
						r.type == item.menuCode
					);
					if(obj){
						arrlist.value.push(obj);
						console.log(arrlist.value)
					}
				}) 
			}
			
			let back =[
				{ name: '智慧物联', url: '/static/shouye/sy/g0.png', urls: '/static/shouye/sy/g1.png' ,type:'wl'},
				{ name: '返回', url: '/static/shouye/sy/f0.png', urls: '/static/shouye/sy/f1.png' ,type:'back'}
			]
			arrlist.value.push(...back);
			navurl(0,arrlist.value[0])
		}))
	}
	// 当前选中的菜单索引
	const menuIndex = ref<number>(-1);
	const swipedex = ref<number>(-1);
	const menuIndexshow = ref<boolean>(false);
	const menuIndexshowsecond = ref<boolean>(false);
	const menuIndexshowfourth = ref<boolean>(false);
	const menuIndexshowfifth = ref<boolean>(false);
	const messageopit = ref<boolean>(false);

	// 变更菜单
	const changeMenu = (index : number) => {
		if (index === menuIndex.value) {
			return
		}
		menuIndex.value = index;
		menuIndexshow.value = false
		menuIndexshowsecond.value = false
		menuIndexshowfourth.value = false;
		menuIndexshowfifth.value = false
		if (index === arrlist.value.length-1) {
			uni.navigateBack()
			return
		}
		setTimeout(() => {
			switch (index) {
				case 0:
					menuIndexshow.value = true
					break;
				case 1:
					menuIndexshowsecond.value = true
					nomesssageshow()
					break;
				case 2:

					nomesssageshow()
					break;
				case 3:
					menuIndexshowfourth.value = true
					break;
				case 4:
					menuIndexshowfifth.value = true;
					break;
				case 5:
					menuIndexshowfourth.value = true
					break;
					
				default:
			}
		}, 50)
	};
	const clientX = ref(0);
	const clientY = ref(0);
	const savename = ref("")
	const canmove = ref(true)

	// 生命周期钩子
	onMounted(() => {

		//首次加载和跳转回来需要重新做个动画
		setTimeout(() => {
			changeMenu(menuIndex.value)
		}, 50)

		menuIndexshow.value = false
		setTimeout(() => {
			menuIndexshow.value = true
		}, 50)

	});

	const nomesssageshow = () => {
		messageopit.value = false;
		setTimeout(() => {
			messageopit.value = true;
		}, 200)
	}
</script>

<style lang="less" scoped>
	.backgroundContainer {
		display: flex;
		position: relative;
		width: 100%;
		height: 100vh;
		background-color: #eff0f4;
		overflow: hidden;
		z-index: 12;
	}

	.nomessageclass {
		width: 100vw;
		transition: opacity 1s ease;
	}
</style>