<template>
    <el-container class="hc-layout-box" :class="[!isNullES(isLayout) && isLayout === 'no' ? 'is-no-layout' : '']">
        <div v-if="appTheme === 'dark'" class="hc-app-bg-box">
            <img :src="appViewBg" alt="">
        </div>
        <el-header class="hc-layout-header">
            <div class="hc-layout-header-logo" :style="`width: ${isCollapse ? '0px' : '200px'};`" @click="logoClick">
                <!-- <img id="logo-icon" :src="appLogoIcon" alt=""> -->
                <img v-show="!isCollapse" id="logo-name" :src="appLogoName" alt="">
            </div>
            <div class="header-top-collapse-bar" @click="collapseChange">
                <HcIcon v-if="isCollapse" name="menu-unfold" />
                <HcIcon v-else name="menu-fold" />
            </div>
            <div class="header-top-menu-bar">
                <HcTopMenuBar @load="topMenuLoad" @change="topMenuChange" />
            </div>
            <div class="header-content-bar">
                <HcCascader v-if="userRoleId !== website.role_id" @send="cascaderSend" @change="cascaderChange" />
                <div v-else class="hc-header-project-name-box" @click="userProjectClick">
                    <HcIcon name="arrow-right" class="project-icon" />
                    <div class="truncate">{{ projectInfoData?.projectName ?? '请先选择项目' }}</div>
                </div>
                <HelpInfoBar />
                <ConfigBar />
                <UserInfoBar @load="userInfoLoad" />
            </div>
        </el-header>
        <el-container class="hc-layout-container">
            <el-aside v-if="isAsideMenu" class="hc-layout-aside" :class="[isCollapse ? 'is-collapse' : '']" :width="isCollapse ? '90px' : '200px'">
                <MenuBar :collapse="isCollapse" :cur="menuBarKey" :datas="menuBarData" :msg-count="msgCount" @change="menuBarChange" />
            </el-aside>
            <el-main class="hc-layout-main">
                <div class="hc-router-menu-bar">
                    <RouterMenu @load="routerMenuLoad" />
                </div>
                <div id="hc-main-box" class="hc-main-page">
                    <div class="hc-main-body">
                        <router-view v-if="reloadRouter" v-slot="{ Component }">
                            <transition name="fade-transform">
                                <keep-alive :max="10" exclude="HcDataV">
                                    <component :is="Component" :msg-count="msgCount" :msg-change="msgChange" />
                                </keep-alive>
                            </transition>
                        </router-view>
                    </div>
                </div>
            </el-main>
        </el-container>
    </el-container>
</template>

<script setup>
import { nextTick, onMounted, ref, watch } from 'vue'
import { useAppStore } from '~src/store'
import { useRoute, useRouter } from 'vue-router'
import { initButtons } from '~sto/app'
import HcSocket from '~src/plugins/HcSocket'
import { getObjValue, isNullES } from 'js-fast-way'
import website from '~src/config'

//初始组合式
const router = useRouter()
const useRoutes = useRoute()
const store = useAppStore()

const reloadRouter = ref(true)
const userRoleId = ref(store.getRoleId)
const projectInfoData = ref(store.getProjectInfo)

//子组件
import HcTopMenuBar from './modules/HcTopMenu.vue'
import HcCascader from './modules/Cascader.vue'
import UserInfoBar from './modules/UserInfoBar.vue'
import HelpInfoBar from './modules/HelpInfoBar.vue'
import ConfigBar from './modules/ConfigBar.vue'
import RouterMenu from './modules/RouterMenu.vue'
import MenuBar from '~src/layout/modules/MenuBar.vue'
import appViewBg from '~src/assets/view/bg.png'

// logo
const appLogoName = ref(store.getLogoName)
const appTheme = ref(store.getTheme)

//菜单数据
const menuBarKey = ref('')
const menuBarData = ref([])
const isLayout = ref('')

//渲染完成
onMounted(() => {
    const layout = useRoutes?.query?.layout, layout2 = store.isLayout
    isLayout.value = layout ?? layout2
    initButtons()
})

//监听layout
watch(() => [
    useRoutes?.query?.layout,
    store.isLayout,
], ([layout, layout2]) => {
    isLayout.value = layout ?? layout2
}, { deep: true })

//监听
watch(() => store.getTheme, (theme) => {
    appTheme.value = theme
})

//路由信息
const routerMenuLoad = ({ key }) => {
    menuBarKey.value = key
}

// 是否折叠
const isCollapse = ref(false)
const collapseChange = () => {
    const bool = !isCollapse.value
    isCollapse.value = bool
    store.setCollapse(bool)
}

//顶部菜单导航
const isAsideMenu = ref(true)
const topMenuLoad = () => {
    isAsideMenu.value = false
}

//顶部菜单导航被点击
const topMenuChange = (data) => {
    if (!isNullES(data)) {
        menuBarData.value = data
        isAsideMenu.value = true
    }
}

//菜单被点击
const menuBarChange = ({ code }) => {
    menuBarKey.value = code
    router.push({ name: code })
}

//消息数量
const msgCount = ref({
    allCount: 0,
    taskCount: 0,
    messageCount: 0,
    messageCount_1: 0,
    messageCount_2: 0,
    messageCount_3: 0,
    messageCount_4: 0,
    messageCount_5: 0,
})
const msgChange = ref(0)

//用户信息
const userInfoLoad = ({ user_id }) => {
    HcSocket.create(user_id, (data) => {
        msgCount.value = getObjValue(data)
        msgChange.value++
    })
}

//项目合同段的ID
const cascaderSend = ({ projectId, contractId }) => {
    HcSocket.send(projectId + ',' + contractId)
}

// 项目切换
const cascaderChange = () => {
    reloadRouter.value = false
    nextTick(() => {
        reloadRouter.value = true
    })
}

//首页
const logoClick = () => {
    router.push({ name: 'datav' })
}

const userProjectClick = () => {
    router.push({ path: '/user/project' })
}
</script>

<style lang="scss">
@import "./index.scss";
</style>
