<template>
    <header class="admin-header">
        <div class="title-zone">
            <div class="page-title">
                <p>{{ pgNm }} </p>
            </div>
            <Breadcrumb v-if="$route.path !== this.$filters.ctxPath('/adm/main.page') && $route.path !== this.$filters.ctxPath('/main.page')" />
        </div>
        <div class="info-wrap">
            <div class="info">
                <p class="info-name">{{ mbrNm }} 님</p>
                <ul class="info-btn">
                    <li><button class="btn-txt xs ico-before ico-logout"  @click="fnlogOut" title="로그아웃">로그아웃</button></li>
                    <li><button class="btn-txt xsm ico-before ico-go"  @click="fnSiteMove" title="사이트이동">사이트이동</button></li>
                    <li><button class="btn-txt xsm ico-before ico-reset"  @click="cacheClean()" title="변경사항 적용">변경사항 적용</button></li>
                </ul>
            </div>
        </div>
    </header>
</template>

<script>
import queryParams from '../../resources/js/queryParams';
import { mapActions } from "vuex";
import { cacheReSet } from "../../resources/api/cacheReSet";
import Breadcrumb from "../component/Breadcrumb/Breadcrumb.vue";

export default {
    mixins: [queryParams],
    components: {
        'Breadcrumb':Breadcrumb
    },
    data() {
        return {
            mbrNm: this.$store.state.mbrNm,
        }
    },
    created() {
    },
    methods: {
        // 사이트 이동
        fnSiteMove() {
            this.$router.push({
                path: this.$filters.ctxPath("/"),
            });
        },
        ...mapActions(["logout"]),
        // 로그 아웃
        async fnlogOut() {
            await this.logout();
            // this.$router.push({
            //     path: this.$filters.ctxPath("/login.page"),
            // });
        },
        // 캐시 초기화
        async cacheClean() {
            if (!confirm("변경사항을 적용하시겠습니까?")) return;
            const res = await cacheReSet();
            alert(res.data.message);
        },

    },
    watch: {
        '$store.state.menu'(newValue) {
            if(newValue) {
                this.pgNm = newValue.menuNm
            }
        },
        'this.$route.path'(newValue) {
            if(newValue) {
            }
        },

    },
    computed: {
        pgNm() {
            const route = this.$route;
            const storeMenu = this.$store.state.menu;

            if (route?.meta?.korName) {
                if (route.path === '/adm/main.page') {
                    return "홈";
                }

                // if (route.meta.typeId.includes("BBS_MNG")) {
                //     const matchedMenu = store.state.flatMenuList.find(menu => menu.menuTypeCtgry === route.meta.typeId);
                //     if (matchedMenu) {
                //         return matchedMenu.menuNm;
                //     }
                // }

                if (storeMenu?.menuNm) {
                    return storeMenu.menuNm;
                } else {
                    return route.meta.korName;
                }
            }

            return "홈";
            // return store.state.menu && store.state.menu.menuNm ? store.state.menu.menuNm : "홈";
        }
    },
    mounted() {

    }
}
</script>