<template>
	<view class="index-page">
		<u-navbar back-icon-color='#fff' back-icon-name=''
			:background="{ background: 'rgba(64, 158,255,'+bgOpacity+')'}" back-text="" title-color='#f0f0f0'
			:immersive='true' :border-bottom='false' title="首页">
		</u-navbar>
		<view class="top">
			<image src="../static/images/mobile_bg.png" class="bg" mode=""></image>
			<view class="top-box">
				<view class="number">
					<view class="number-item">
						<view>{{counts.allCount || 0}}</view>
						<view>设备总数</view>
					</view>
					<view class="number-item">
						<view>{{counts.breakDownCount || 0}}</view>
						<view>故障中设备</view>
					</view>
					<view class="number-item">
						<view>{{counts.repairCount || 0}}</view>
						<view>已报修设备</view>
					</view>
				</view>
				<view class="status">
					<view class="status-item">
						<view>{{counts1.gongdan || 0}}</view>
						<view>待接单</view>
					</view>
					<view class="status-item">
						<view>{{counts1.yanzheng || 0}}</view>
						<view>待验证</view>
					</view>
					<view class="status-item">
						<view>{{counts1.shenpi || 0}}</view>
						<view>待审核</view>
					</view>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="" v-if="deviceMenus.length>0">
				<view class="title">设备维护</view>

				<u-row gutter="16" style="padding:0px 20rpx ;">
					<u-col span="3" v-for="(item,index) in store.deviceMenus" :key='index'>
						<view class="icon-item" @click="open('/pages/deviceReport/index?type=DEVICE')"
							v-if="item.path =='deviceRequest'">
							<image src="../static/images/icon4.svg" mode=""></image>
							<view>设备报修</view>
						</view>
						<view class="icon-item" @click="open('/pages/repairOrder/index?type=DEVICE')"
							v-if="item.path =='deviceMaintenanceJob'">
							<image src="../static/images/icon5.svg" mode=""></image>
							<view>维修工单</view>
						</view>
						<view class="icon-item" @click="open('/pages/overhaulOrder/index?type=DEVICE')"
							v-if="item.path =='deviceInspectionJob'">
							<image src="../static/images/icon3.svg" mode=""></image>
							<view>检修工单</view>
						</view>
						<view class="icon-item" @click="open('/pages/spotCheckOrder/addForm?type=DEVICE')"
							v-if="item.path =='deviceSpotInspectionRecord'">
							<image src="../static/images/icon2.svg" mode=""></image>
							<view>点检工单</view>
						</view>
						<view class="icon-item" @click="open('/pages/upkeepOrder/index?type=DEVICE')"
							v-if="item.path =='deviceMaintainJob'">
							<image src="../static/images/icon1.svg" mode=""></image>
							<view>保养工单</view>
						</view>
					</u-col>
				</u-row>

			</view>

			<view class="" v-if="moldMenus.length>0">
				<view class="title">模具维护</view>
				<u-row gutter="16">
					<u-col span="3" v-for="(item,index) in moldMenus" :key='index'>
						<view class="icon-item" @click="open('/pages/deviceReport/index?type=MOLD')"
							v-if="item.path =='moldRequest'">
							<image src="../static/images/icon4.svg" mode=""></image>
							<view>模具报修</view>
						</view>
						<view class="icon-item" @click="open('/pages/repairOrder/index?type=MOLD')"
							v-if="item.path =='moldMaintenanceJob'">
							<image src="../static/images/icon5.svg" mode=""></image>
							<view>维修工单</view>
						</view>
						<view class="icon-item" @click="open('/pages/overhaulOrder/index?type=MOLD')"
							v-if="item.path =='moldInspectionJob'">
							<image src="../static/images/icon2.svg" mode=""></image>
							<view>检修工单</view>
						</view>
						<view class="icon-item" @click="open('/pages/upkeepOrder/index?type=MOLD')"
							v-if="item.path =='moldMaintainJob'">
							<image src="../static/images/icon1.svg" mode=""></image>
							<view>保养工单</view>
						</view>
					</u-col>
				</u-row>
			</view>

			<view class="" v-if="thchMenus.length>0">
				<view class="title">备件管理</view>
				<u-row gutter="16">
					<u-col span="3" v-for="(item,index) in thchMenus" :key='index'>
						<view class="icon-item" @click="open('/pages/sparePartsApplication/index?from=2')"
							v-if="item.path =='itemRequest'">
							<image src="../static/images/icon6.png" mode=""></image>
							<view>领用申请</view>
						</view>
						<view class="icon-item" @click="open('/pages/sparePartsApplicationApprove/index?from=3')"
							v-if="item.path =='itemApprove'">
							<image src="../static/images/icon7.png" mode=""></image>
							<view>领用申请审批 </view>
						</view>
						<view class="icon-item"
							@click="open('/pages/sparePartsServiceWorkOrderList/addForm?type=DEVICE')"
							v-if="item.path =='itemMaintenanceRecord'">
							<image src="../static/images/icon5.svg" mode=""></image>
							<view>维修工单</view>
						</view>
					</u-col>
				</u-row>
			</view>

		</view>
	</view>
</template>

<script setup lang="ts">
	import {
		onShow,
		onPageScroll,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getCounts,
		getToDoCountsByUser
	} from '@/api/index'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		getAccessToken
	} from '@/utils/auth'
	import { storeToRefs } from 'pinia'
	import { useCountStore } from '@/store'

	const { proxy } = getCurrentInstance()
	// 获取自定义的store
	const store = useCountStore()

	const bgOpacity = ref(0)

	const counts = ref({
		allCount: "",
		breakDownCount: '',
		repairCount: ''
	})
	const counts1 = ref({
		gongdan: '',
		yanzheng: '',
		shenpi: ''
	})
	const deviceMenus = ref([])
	const moldMenus = ref([])
	const thchMenus = ref([])

	// 获取顶部数字
	function getCounts1() {
		getCounts().then(res => {
			counts.value = res.data
		}).catch(() => { })
	}
	function getToDoCountsByUser1() {
		getToDoCountsByUser().then(res => {
			counts1.value = res.data
		}).catch(() => { })
	}
	function open(url) {
		proxy.$tab.navigateTo(url)
		
	}
	onLoad(async()=>{
		// 解决onLaunch和onLoad异步问题
		await proxy.$onLaunched;
		deviceMenus.value = store.deviceMenus
		moldMenus.value = store.moldMenus
		thchMenus.value = store.thchMenus
	})
	onShow(async () => {
		if (getAccessToken()) {
			getCounts1()
			getToDoCountsByUser1()
		}
	})
	onPageScroll((e) => {
		if (e.scrollTop > 0) {
			bgOpacity.value = e.scrollTop / 200;
		} else {
			bgOpacity.value = 0
		}
	})
</script>
<style scoped lang="scss">
	.index-content {
		background-color: white;
		min-height: 100vh;
	}

	.index-page {
		background: white;
	}

	.bg {
		width: 100%;
	}

	.top {
		position: relative;

		image {
			height: calc(var(--status-bar-height) + 410rpx);
		}
	}

	.top-box {
		position: absolute;
		width: 100%;
		bottom: 0px;

		.number {
			padding: 30rpx 80rpx 50rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.number-item {
				text-align: center;

				view {
					&:nth-child(1) {
						color: white;
						font-size: 40rpx;
						font-weight: bold;
					}

					&:nth-child(2) {
						color: rgba(255, 255, 255, 0.7);
						font-size: 28rpx;
						margin-top: 10rpx;
					}
				}
			}
		}

		.status {
			margin: 0px 40rpx;
			padding: 40rpx 0rpx;
			background: white;
			border-radius: 20rpx 20rpx 0px 0px;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.status-item {
				text-align: center;
				position: relative;
				flex: 1;

				view {
					&:nth-child(1) {
						color: black;
						font-size: 40rpx;
						font-weight: bold;
					}

					&:nth-child(2) {
						color: rgba(0, 0, 0, 0.5);
						font-size: 28rpx;
						margin-top: 10rpx;
					}
				}

				&::after {
					content: "";
					border-right: 1px solid #dedede;
					position: absolute;
					height: 60rpx;
					right: 0px;
					top: 50%;
					margin-top: -30rpx;

				}

				&:nth-last-child(1)::after {
					border: none;
				}
			}
		}
	}

	.box {
		.title {
			font-size: 32rpx;
			font-weight: bold;
			padding: 50rpx 50rpx 30rpx;
		}

		.icon-item {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			margin: 20rpx 0px;

			image {
				width: 80rpx;
				height: 80rpx;
			}

			view {
				margin-top: 20rpx;
				color: #999999;
			}
		}
	}
</style>