<script setup lang="ts">
import rfdc from "rfdc";
import type {blogMenu} from "~/drizzle/schema";
const clone = rfdc()
const niMessage = useNiMessage()

definePageMeta({
    layout: 'home',
    pageTransition: {
        name: 'slide',
        mode: 'out-in',
    },
})
// 菜单数据类型
type InsertBlogMenu = typeof blogMenu.$inferInsert;
// 菜单原始数据
const originalBlogMenuLis = shallowRef([])
// 根据pid存储列表Map
const blogMenuPidMap = ref(new Map)
// 根据id存储对象Map
const blogMenuIdMap = ref(new Map)
// blog渲染列表
const blogMenuList = ref<InsertBlogMenu[]>([]);
// blog菜单对象
const blogMenuObject = ref<InsertBlogMenu>(null);
// 菜单折叠
const menuCollapseStatus = ref(true)
// blog信息弹窗状态
const blogInfoPopupStatus = ref(false)

// 格式化菜单数据
const formatBlogMenuListToPidMap = (blogMenuList: Array<InsertBlogMenu>) => {
    const idMap = new Map()
    const pidMap = new Map()
    const list = clone(blogMenuList)
    const pidObj: Record<string, InsertBlogMenu[]> = {};
    for(const menuItem of list){
        if(pidObj[menuItem.pid]){
            pidObj[menuItem.pid].push(menuItem)
        }else{
            pidObj[menuItem.pid] = [menuItem]
        }
        if(menuItem.pid == 0){
            menuItem.rank = 0;
        }
    }
    for(const menuItem of list){
        const id = menuItem.id;
        if(pidObj[id]){
            menuItem.childrenLength = pidObj[id].length
        }
        idMap.set(id, menuItem)
    }
    Object.keys(pidObj).forEach(key => {
        pidMap.set(key, pidObj[key]);
    })
    blogMenuPidMap.value = pidMap
    blogMenuIdMap.value = idMap
}
// 获取blog菜单数据
const getMenuListFetch = async () => {
    const { data, error } = await useFetch('/api/blog/blogMenu');
    if (error.value) {
        throw error.value;
    }
    originalBlogMenuLis.value = data.value;
    formatBlogMenuListToPidMap(data.value);
    blogMenuList.value = blogMenuPidMap.value.get('0')
}
// 展开/关闭子菜单事件
const handleCollapseChildrenEvent = (item, index) => {
    if(item.open == true){
        blogMenuIdMap.value.get(item.id).open = false;
        blogMenuList.value.splice(index + 1, item.childrenLength);
    }else{
        blogMenuIdMap.value.get(item.id).open = true;
        const childrenList = blogMenuPidMap.value.get(item.id)
        blogMenuList.value.splice(index + 1, 0, ...childrenList.map(i => {
            i.rank = item.rank + 1;
            return i
        }));
    }
}
// 新增blog事件
const handleCreateBlogMenuItemEvent = (pid: string) => {
    blogMenuObject.value = {
        pid,
        name: '',
        desc: ''
    }
    blogInfoPopupStatus.value = true;
}
// 确认新增
const handleCreateBlogMenuItemAck = async () => {
    consola.info(blogMenuObject.value)
    const resd= await $fetch('/api/blog/blogMenu',{
        method: 'POST',
        body: blogMenuObject.value
    });
    consola.info(resd);
}

// SSR运行
await getMenuListFetch()
onMounted(() => {
    consola.log(blogMenuList.value)
})
</script>

<template>
    <div class="homeBlog">
        <HomeBlogMenu
            class="left"
            v-model:status="menuCollapseStatus"
            :blogMenuList
            @createBlogMenu="handleCreateBlogMenuItemEvent"
            @collapseChildren="handleCollapseChildrenEvent"></HomeBlogMenu>
        <div class="main">
            <header>
                <div class="menuCollapse a11 allCenter point" :class="menuCollapseStatus && 'hide'">
                    <div class="star-blogIconFont" @click="menuCollapseStatus = !menuCollapseStatus">&#xe67f;</div>
                </div>
            </header>
            <main>
            </main>
        </div>
        <div class="right"></div>
        <NiPopup v-model:status="blogInfoPopupStatus">
            <template #header>博客菜单</template>
            <NiForm :data="blogMenuObject">
                <NiFormItem label="博客名称" name="name" label-width="6">
                    <NiInput v-model="blogMenuObject.name"></NiInput>
                </NiFormItem>
                <NiFormItem label="博客描述" name="name" label-width="6">
                    <NiInput v-model="blogMenuObject.desc"></NiInput>
                </NiFormItem>
                <div>{{ blogMenuObject.name }}</div>
                <div>{{ blogMenuObject.desc }}</div>
            </NiForm>
            <template #footer>
                <NiButton @click="blogInfoPopupStatus = false">取消</NiButton>
                <NiButton type="primary" @click="handleCreateBlogMenuItemAck">确认</NiButton>
            </template>
        </NiPopup>
    </div>
</template>

<style scoped lang="scss">
.homeBlog {
    position: relative;
    display: flex;
    width: 100%;
    height: 100%;
    overflow: hidden;
    & > div.left{
        position: relative;
        height: 100%;
        flex-shrink: 0;
        background: var(--blog-menu-background-color);
        border-right: 1px solid var(--bg-color-be);
    }
    & > div.right{
        position: relative;
        height: 100%;
        overflow: auto;
        flex-shrink: 0;
    }
    & > div.main{
        position: relative;
        height: 100%;
        overflow: auto;
        flex: 1;
        & > header{
            position: relative;
            display: flex;
            height: 3rem;
            border-bottom: 1px solid var(--bg-color-be);

            & > div.menuCollapse{
                position: relative;
                height: 100%;
                transition: width .3s;
                width: 3rem;
                overflow: hidden;
                &:hover & > div{
                    color: var(--font-color-top3-h1);
                }
                &.hide{
                    width: 0;
                }

                & > div{
                    font-size: 1.3rem;
                    color: var(--font-color-top3-h2);
                    transition: color .5s, opacity .5s;
                }
            }
        }
    }
}
@media (max-width: 767px){

}
@media (min-width: 768px){

}
</style>
