<template>
	<page-meta root-font-size="system" />
	<view class="work-container">
		<!-- 宫格组件 -->
		<view v-if="processList.fList.length != 0">
			<view v-for="(type, typeIndex) in processList.ftypeList" :key="typeIndex">
				<uni-section :title="type.typeName" titleFontSize="1.3rem" type="line"></uni-section>
				<view class="grid-body">
					<uni-grid :column="uni_grid_column" :showBorder="false"  >
						<view v-for="(item, index) in processList.fList" :index="index" :key="index" >
							<view v-if="type.typeId === item.typeId.typeId" @click="changeProcessGrid(item)">
								<uni-grid-item >
									<view class="grid-item-box">
										<text class="ygoa_work_icon"
											:class="item.modelPicture == 'default.png' ? 'icon-outsourcing' : item.modelPicture"></text>
										<view class="modelName">
										<text class="text">{{ item.modelName }}</text>
										</view>
									</view>
								</uni-grid-item>
							</view>
						</view>
					</uni-grid>
				</view>
			</view>
		</view>
		<uni-section title="考勤管理" titleFontSize="1.3rem" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="uni_grid_column" :showBorder="false" @change="changeAttendanceGrid">
				<uni-grid-item :index="1">
					<view class="grid-item-box">
						<text class="ygoa_icon icon-checkIn"></text>
						<text class="text">考勤</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="2">
					<view class="grid-item-box">
						<text class="ygoa_icon icon-location"></text>
						<text class="text">打卡</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		<view v-if="diaryShow">
			<uni-section title="日报周报" titleFontSize="1.3rem" type="line"></uni-section>
			<view class="grid-body">
				<uni-grid :column="uni_grid_column" :showBorder="false" @change="changeDiaryGrid">
					<uni-grid-item :index="1">
						<view class="grid-item-box">
							<text class="ygoa-icon icon-diary"></text>
							<text class="text">日报</text>
						</view>
					</uni-grid-item>
					<!-- <uni-grid-item :index="2">
						<view class="grid-item-box">
							<text class="ygoa-icon icon-diary"></text>
							<text class="text">日报</text>
						</view>
					</uni-grid-item> -->
				</uni-grid>
			</view>
		</view>
		<uni-section title="系统工具" titleFontSize="1.3rem" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="uni_grid_column" :showBorder="false" @change="changeToolsGrid">
				<uni-grid-item :index="1">
					<view class="grid-item-box">
						<text class="ygoa_icon icon-calculator"></text>
						<text class="text">计算器</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="2">
					<view class="grid-item-box">
						<text class="ygoa_icon icon-clear-cache"></text>
						<text class="text">清除缓存</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		<uni-section title="房屋管家" titleFontSize="1.3rem" v-if="homesysShow" type="line"></uni-section>
		<view class="grid-body" v-if="homesysShow">
			<uni-grid :column="uni_grid_column" :showBorder="false" @change="changeHomesys">
				<uni-grid-item v-for="(home, index) in homesysList" :index="index" v-if="homesysShow" :key="index">
					<view class="grid-item-box">
						<text class="ygoa_work_icon icon-zhinengjiaju"></text>
						<text class="text">
							{{ home.room_name || home.room_url.split('/').pop() }}
						</text>
					</view>
				</uni-grid-item>
				<uni-grid-item :index="homesysList.length + 1" v-if="userStore.user.userName == 'yzadmin'">
					<view class="grid-item-box">
						<text class="ygoa_work_icon icon-zhinengjiaju"></text>
						<text class="text">房屋管理</text>
					</view>
				</uni-grid-item>
				<!-- <uni-grid-item :index="homesysList.length + 2" v-if="userStore.user.userName == 'yzadmin'">
					<view class="grid-item-box">
						<text class="ygoa_work_icon icon-zhinengjiaju"></text>
						<text class="text">用户管理</text>
					</view>
				</uni-grid-item> -->
				<uni-grid-item :index="homesysList.length + 3" v-if="userStore.user.userName == 'yzadmin'">
					<view class="grid-item-box">
						<text class="ygoa_work_icon icon-zhinengjiaju"></text>
						<text class="text">用户管理</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		<!-- 计算器弹出层 -->
		<view class="popup_container">
			<cui-calculator ref="calculatorPopup" size="normal"></cui-calculator>
		</view>
		<!-- 清除缓存弹出层 -->
		<!-- <view class="clear_cache_popup_container">
			<uni-popup ref="clearCacheDialog" type="dialog">
				<uni-popup-dialog title="确认清除缓存" @confirm="clearCache"></uni-popup-dialog>
			</uni-popup>
		</view> -->
	</view>
</template>

<script setup lang="ts">
import cuiCalculator from "@/components/cui-calculator/cui-calculator.vue"
import { onMounted, ref } from "vue"
import $tab from "@/plugins/tab.js"
import { useUserStore } from '@/store/user.js'
import { getProcessList, roomUserLogin, getUserRoomByUser } from '@/api/work.js'
import { clearCache } from '@/utils/ygoa.js'
import { onShow } from '@dcloudio/uni-app'
import config from '@/config.js';
const userStore = useUserStore()

let sysFontSize = 0
const uni_grid_column = ref(4)

const diaryShow = ref(false)
if(config.companyCode && config.companyCode == 'yg'){
	diaryShow.value = true;
}else{
	diaryShow.value = false;
}
const homesysShow = ref(false)
const homesysList = ref([])
function getUserRoom() {
	getUserRoomByUser(userStore.user.userName).then((res) => {
		console.log('getUserRoomByUser', res)
		const hasRoom = res.length != 0
		if (hasRoom) {
			homesysList.value = res
			homesysShow.value = true
			console.log('getUserRoomByUser', homesysList.value)
		}
	})
}
// 点击房屋管家
function changeHomesys(e) { 
	const roomIndex = e.detail.index
	const roomListLength = homesysList.value.length
	// if (roomIndex > roomListLength) {
		switch (roomIndex) {
			case roomListLength + 1: 
				$tab.navigateTo('/pages/work/homesysSetting/index')
				break
			case roomListLength + 2: 
				$tab.navigateTo('/pages/work/homesysSetting/user')
				break
			case roomListLength + 3: 
				$tab.navigateTo('/pages/work/homesysSetting/userList')
				break
			default:
				$tab.navigateTo('/pages/work/homesys/index?homeCode=' + homesysList.value[roomIndex].room_url)
		}
	// } else {
	// 	$tab.navigateTo('/pages/work/homesys/index?homeCode=' + homesysList.value[roomIndex].room_url)
	// }
}
onMounted(() => {
	getUserRoom()
})
onMounted(() => {
	sysFontSize = uni.getAppBaseInfo().fontSizeSetting
	if (sysFontSize > 20) uni_grid_column.value = 3
	initProcessList()
})

onShow(()=>{
		uni.$emit('showTabBarBadge')
})
// 流程列表
const processList = ref({
	fList: [],
	ftypeList: [
		{
			typeName: ''
		}
	],
})
// 获取流程宫格数据
function initProcessList() {
	const staffId = userStore.user.useId
	const unitId = userStore.user.unitId
	getProcessList(staffId, unitId).then(res => {
		processList.value = res.returnParams // 设置processList
	})
}
// 点击流程宫格
function changeProcessGrid(item) { 
	// console.log('changeProcessGrid', e);
	// const { modelName, modelId, control } = processList.value.fList[e.detail.index]
	const { modelName, modelId, control } = item
	// const modelName = item.value.modelName
	// const modelId = item.value.modelId
	// const control = item.value.control
	// 跳转流程申请页面
	$tab.navigateTo('/pages/work/edit/index?modelName=' + modelName + '&modelId=' + modelId + '&control=' + control)
}
// 点击考勤宫格
function changeAttendanceGrid(e) {
	if (1 == e.detail.index) { // 考勤
		$tab.navigateTo('/pages/mine/checkIn/checkIn')
	}
	if (2 == e.detail.index) { // 打卡
		$tab.navigateTo('/pages/mine/clockIn/clockIn')
	}
}
// 点击日报宫格
function changeDiaryGrid(e) { 
	if (1 == e.detail.index) {
		wx.navigateToMiniProgram({
			appId: 'wxd45c635d754dbf59',
			// path: `pages/detail/detail?url=https://docs.qq.com/sheet/DUmJRc2NYTmx1d3Nr`,
		})
		// wx.navigateToMiniProgram({
		// 	appId: 'wxde4bf149a4c88707',
		// 	path: `pages/login`
		// })
		return
	}
	if (2 == e.detail.index) {
		$tab.navigateTo('/pages/work/diary/index')
		return
	}
}
// 点击工具宫格
function changeToolsGrid(e) { 
	switch (e.detail.index) {
		case 1:
			openCalculatorPopup() // 打开计算器弹出层
			break
		case 2:
			clearCache()
			// openClearCachePopup() // 打开清除缓存弹出层
			break
		default:
	}
}
// 计算器
const calculatorPopup = ref(null)
function openCalculatorPopup() { // 打开计算器弹出层
	calculatorPopup.value.open()
}
// 清除缓存
// const clearCacheDialog = ref(null)
// function openClearCachePopup() { // 打开清除缓存弹出层
// 	clearCacheDialog.value.open()
// }
// const userStore = useUserStore()

</script>

<style lang="scss">
// @import "@/static/font/ygoa/iconfont.css";

.ygoa_icon, .ygoa_work_icon {
	font-size: calc(80rpx + 1.5*(1rem - 16px));
}
.icon_size {
	font-size: calc(80rpx + 1.5*(1rem - 16px));
}

/* #ifndef APP-NVUE */
page {
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	background-color: #fff;
	min-height: 100%;
	height: auto;
}

view {
	font-size: calc(14px + 1.5*(1rem - 16px));
	line-height: inherit;
}

/* #endif */

.text {
	text-align: center;
	font-size: calc(26rpx + 1.5*(1rem - 16px));
	margin-top: 10rpx;
}
.modelName {
	max-width: 120rpx;
	text-align: center;
}
.work-container {
	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}
	
	// .uni-margin-wrap {
	// 	width: 690rpx;
	// 	width: 100%;
	// }
	
	.swiper {
		height: 300rpx;
	}
	
	.swiper-box {
		height: 150px;
	}
	
	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}
	
	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}
	
		.image {
			width: 100%;
		}
	}
}

</style>