<template>

    <header>
        <div class="header-container w1500">
            <div class="logo-wrap">
                <router-link :to="{ path: '/' }" class="logo"><img :src="logo" alt=""></router-link>
            </div>
            <div class="nav-wrap">
                <nav>
                    <ul>
                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_ADMIN'"
                            @click="updateMenuStats('MENU_00000001')">기록물
                            <div class="submenu">
                                <p>• <router-link :to="{ path: '/PicHistorySearch.page' }" >사진 기록물</router-link></p>
                                <div class="hr"></div>
                                <p>• <router-link :to="{ path: '/VideoHistorySearch.page' }" >영상 기록물</router-link></p>
                            </div>
                        </li>
                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_ADMIN'"
                            @click="updateMenuStats('MENU_00000004')">언론에서 바라본 구미시
                            <div class="submenu">
                                <p>• <router-link :to="{ path: '/MediaVideoSearch.page' }" >미디어 영상</router-link></p>
                                <div class="hr"></div>
                                <p>• <router-link :to="{ path: '/NewsReleaseSearch.page' }" >보도자료</router-link></p>
                            </div>
                        </li>
                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_ADMIN'"
                            @click="updateMenuStats('MENU_00000007')"><router-link :to="{ path: '/MemberManagement.page' }" >회원관리</router-link></li>
                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_ADMIN'"
                            @click="updateMenuStats('MENU_00000008')"><router-link :to="{ path: '/CategoryManagement.page' }" >카테고리 관리</router-link></li>

                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_USER'"
                            @click="updateMenuStats('MENU_00000001')">기록물</li>
                        <li v-if="$store.state.roles[0]?.authority === 'ROLE_USER'"
                            @click="updateMenuStats('MENU_00000004')">언론에서 바라본 구미시</li>
                    </ul>
                </nav>
            </div>
            <div class="auth-area">
                <ul v-if="$store.state.userId != null">
                    <li><img src="../../resources/images/icon/user-settings-line.png" alt="">
                        <router-link :to="{ path: '/MyInfo.page' }">{{ $store.state.userNm }}</router-link>
                    </li>
                    <li>
                        <div class="line"></div>
                    </li>
                    <li><img src="../../resources/images/icon/logout-box-line.png" alt="">
                        <a href="#" @click.prevent="logout">로그아웃</a>
                    </li>
                </ul>
                <a href="#" class="all-menu"><img src="../../resources/images/allmenu.png" alt=""></a>
            </div>
        </div>
    </header>
</template>
<script>
import { logOutProc } from "../../resources/api/user"
import { updateStatsByMenuId } from "../../resources/api/main"
export default {
  data() {
    return {
      // Define the image sources
      logo: 'client/resources/images/logo.png',
    };
  },
  methods: {
    async updateMenuStats(menuId) {
      try {
        const response = await updateStatsByMenuId(menuId);
        if (response.status === 200) {
          console.log(`메뉴 ID ${menuId} 통계 업데이트 성공`);
        }
      } catch (error) {
        console.error(`메뉴 ID ${menuId} 통계 업데이트 중 오류:`, error);
      }
    },
    logout() {
      // 백엔드 로그아웃 API 호출
      logOutProc()
        .then(() => {
          console.log('로그아웃 성공 - 서버 측 쿠키 삭제 완료');
          this.$store.commit('setStoreReset'); // 로그아웃 성공 후 스토어 초기화
          this.$router.push({ path: '/Login.page' }); // 로그인 페이지로 리다이렉트
        })
        .catch(err => {
          console.error('로그아웃 처리 중 오류:', err);
          this.$store.commit('setStoreReset'); // 오류가 있어도 스토어는 초기화
          this.$router.push({ path: '/Login.page' }); // 로그인 페이지로 리다이렉트
        });
    },

  },
  watch: {},
  computed: {},
  components: {},
  mounted() { },
};
</script>