admin.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <script setup>
  2. import { ref, computed, watch } from "vue";
  3. import { useRoute, useRouter } from "vue-router";
  4. import AdminAlertModal from "~/components/admin/AdminAlertModal.vue";
  5. import AdminModal from "~/components/admin/AdminModal.vue";
  6. const route = useRoute();
  7. const router = useRouter();
  8. // 메뉴 열림 상태 관리
  9. const openMenus = ref(["dashboard"]);
  10. // GNB 메뉴 구조
  11. const menuItems = ref([
  12. {
  13. id: "dashboard",
  14. title: "📊 대시보드",
  15. path: "/site-manager/dashboard",
  16. },
  17. {
  18. id: "admins",
  19. title: "🔑 관리자 관리",
  20. path: "/site-manager/admins",
  21. },
  22. {
  23. id: "field",
  24. title: "🗺️ 분야 및 지역관리",
  25. children: [
  26. {
  27. title: "낚시분야",
  28. path: "/site-manager/field/list",
  29. pattern: /^\/site-manager\/field\/(list|create|edit|detail)/,
  30. },
  31. {
  32. title: "지역관리",
  33. path: "/site-manager/area/list",
  34. pattern: /^\/site-manager\/area\/(list|create|edit|detail)/,
  35. },
  36. ],
  37. },
  38. {
  39. id: "fishing",
  40. title: "⚓ 선상 및 낚시터 관리",
  41. children: [
  42. {
  43. title: "선상관리",
  44. path: "/site-manager/onboard/list",
  45. pattern: /^\/site-manager\/onboard\/(list|create|edit|detail)/,
  46. },
  47. {
  48. title: "낚시터관리",
  49. path: "/site-manager/fishing/list",
  50. pattern: /^\/site-manager\/fishing\/(list|create|edit|detail)/,
  51. }
  52. ],
  53. },
  54. ]);
  55. // 메뉴 토글
  56. const toggleMenu = (menuId) => {
  57. const index = openMenus.value.indexOf(menuId);
  58. if (index > -1) {
  59. openMenus.value.splice(index, 1);
  60. } else {
  61. openMenus.value.push(menuId);
  62. }
  63. };
  64. // 현재 활성 라우트 체크 (단일 path 메뉴 — children 없는 메뉴용)
  65. const isActiveRoute = (path) => {
  66. return route.path === path;
  67. };
  68. // 서브메뉴 활성 여부 (pattern 우선, 없으면 path 정확 일치)
  69. const isSubmenuActive = (item) => {
  70. if (item?.pattern && item.pattern.test(route.path)) return true;
  71. return route.path === item?.path;
  72. };
  73. // 현재 경로에 맞는 메뉴 찾기
  74. const findCurrentMenu = () => {
  75. const currentPath = route.path;
  76. // 대시보드인 경우
  77. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  78. return { menu: null, child: null };
  79. }
  80. for (const menu of menuItems.value) {
  81. // children이 없는 단일 메뉴는 자기 자신이 매칭 대상
  82. if (!menu.children) {
  83. if (menu.path && currentPath === menu.path) {
  84. return { menu, child: { title: menu.title, path: menu.path } };
  85. }
  86. continue;
  87. }
  88. for (const child of menu.children) {
  89. // pattern이 있으면 정규식으로 매칭, 없으면 정확히 일치하는지 확인
  90. if (child.pattern && child.pattern.test(currentPath)) {
  91. return { menu, child };
  92. } else if (currentPath === child.path) {
  93. return { menu, child };
  94. }
  95. }
  96. }
  97. return { menu: null, child: null };
  98. };
  99. // 라우트 변경 시 현재 경로에 해당하는 토글만 열고 나머지는 모두 접기
  100. watch(
  101. () => route.path,
  102. () => {
  103. const { menu } = findCurrentMenu();
  104. openMenus.value = menu?.children ? [menu.id] : [];
  105. },
  106. { immediate: true }
  107. );
  108. // 페이지 타이틀 계산
  109. const pageTitle = computed(() => {
  110. const currentPath = route.path;
  111. // 대시보드
  112. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  113. return "📊 대시보드";
  114. }
  115. const { child } = findCurrentMenu();
  116. if (child) {
  117. // 상세 페이지 타이틀 처리
  118. if (currentPath.includes("/create")) {
  119. return `${child.title} 등록`;
  120. } else if (currentPath.includes("/edit/")) {
  121. return `${child.title} 수정`;
  122. } else if (currentPath.includes("/detail/")) {
  123. return `${child.title} 상세`;
  124. } else if (currentPath.includes("/print/")) {
  125. return `${child.title} 인쇄`;
  126. } else if (currentPath.includes("/print-a2")) {
  127. return `${child.title} 인쇄 (A2)`;
  128. }
  129. return child.title;
  130. }
  131. return "📊 대시보드";
  132. });
  133. // Breadcrumb 계산
  134. const breadcrumbs = computed(() => {
  135. const currentPath = route.path;
  136. const crumbs = [{ title: "대시보드", path: "/site-manager/dashboard" }];
  137. // 대시보드인 경우 Home만 표시
  138. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  139. return crumbs;
  140. }
  141. const { menu, child } = findCurrentMenu();
  142. if (menu && child) {
  143. // 메뉴 그룹 추가
  144. crumbs.push({ title: menu.title, path: null });
  145. // 현재 페이지 타이틀 추가
  146. if (currentPath.includes("/create")) {
  147. crumbs.push({ title: child.title, path: child.path });
  148. crumbs.push({ title: "등록", path: null });
  149. } else if (currentPath.includes("/edit/")) {
  150. crumbs.push({ title: child.title, path: child.path });
  151. crumbs.push({ title: "수정", path: null });
  152. } else if (currentPath.includes("/print/")) {
  153. crumbs.push({ title: child.title, path: child.path });
  154. crumbs.push({ title: "인쇄", path: null });
  155. } else if (currentPath.includes("/print-a2")) {
  156. crumbs.push({ title: child.title, path: child.path });
  157. crumbs.push({ title: "인쇄 (A2)", path: null });
  158. } else {
  159. crumbs.push({ title: child.title, path: null });
  160. }
  161. }
  162. return crumbs;
  163. });
  164. // 로그아웃 모달
  165. const showLogoutModal = ref(false);
  166. const handleLogout = () => {
  167. console.log("[Logout] 로그아웃 버튼 클릭");
  168. showLogoutModal.value = true;
  169. console.log("[Logout] showLogoutModal:", showLogoutModal.value);
  170. };
  171. const confirmLogout = () => {
  172. console.log("[Logout] 로그아웃 확인");
  173. localStorage.removeItem("admin_token");
  174. localStorage.removeItem("admin_user");
  175. router.push("/site-manager");
  176. };
  177. const closeLogoutModal = () => {
  178. console.log("[Logout] 모달 닫기");
  179. showLogoutModal.value = false;
  180. };
  181. // 정보수정 모달
  182. const showProfileModal = ref(false);
  183. const currentAdmin = ref(null);
  184. const { get } = useApi();
  185. // 알림 모달
  186. const alertModal = ref({
  187. show: false,
  188. title: "알림",
  189. message: "",
  190. type: "alert",
  191. onConfirm: null,
  192. });
  193. // 알림 모달 표시
  194. const showAlert = (message, title = "알림") => {
  195. alertModal.value = {
  196. show: true,
  197. title,
  198. message,
  199. type: "alert",
  200. onConfirm: null,
  201. };
  202. };
  203. // 알림 모달 닫기
  204. const closeAlertModal = () => {
  205. alertModal.value.show = false;
  206. };
  207. // 알림 모달 확인
  208. const handleAlertConfirm = () => {
  209. if (alertModal.value.onConfirm) {
  210. alertModal.value.onConfirm();
  211. }
  212. closeAlertModal();
  213. };
  214. // 알림 모달 취소
  215. const handleAlertCancel = () => {
  216. closeAlertModal();
  217. };
  218. // 현재 로그인한 관리자 ID 가져오기
  219. const getCurrentAdminId = () => {
  220. if (typeof window === "undefined") return null;
  221. const user = localStorage.getItem("admin_user");
  222. if (!user) return null;
  223. try {
  224. return JSON.parse(user).id;
  225. } catch {
  226. return null;
  227. }
  228. };
  229. // 대시보드로 이동
  230. const goToDashboard = () => {
  231. router.push("/site-manager/dashboard");
  232. };
  233. // 정보수정 버튼 클릭
  234. const goToProfile = async () => {
  235. const adminId = getCurrentAdminId();
  236. if (!adminId) {
  237. showAlert("로그인 정보를 찾을 수 없습니다.", "오류");
  238. return;
  239. }
  240. // 현재 관리자 정보 조회
  241. const { data, error } = await get(`/admin/${adminId}`);
  242. if (error) {
  243. showAlert("관리자 정보를 불러올 수 없습니다.", "오류");
  244. console.error("[Profile] 관리자 정보 조회 실패:", error);
  245. return;
  246. }
  247. if (data?.success && data?.data) {
  248. currentAdmin.value = data.data;
  249. showProfileModal.value = true;
  250. }
  251. };
  252. // 정보수정 모달 닫기
  253. const closeProfileModal = () => {
  254. showProfileModal.value = false;
  255. currentAdmin.value = null;
  256. };
  257. // 정보수정 완료
  258. const handleProfileSaved = (message) => {
  259. closeProfileModal();
  260. if (message) {
  261. showAlert(message, "성공");
  262. // 로컬스토리지의 사용자 정보도 업데이트
  263. if (currentAdmin.value) {
  264. localStorage.setItem("admin_user", JSON.stringify(currentAdmin.value));
  265. }
  266. }
  267. };
  268. </script>
  269. <template>
  270. <div class="admin--layout">
  271. <!-- Header -->
  272. <header class="admin--header">
  273. <div class="admin--header-left">
  274. <div class="admin--page-header">
  275. <div class="admin--breadcrumb">
  276. <span v-for="(crumb, index) in breadcrumbs" :key="index">
  277. <NuxtLink v-if="crumb.path" :to="crumb.path" class="admin--breadcrumb-link">
  278. {{ crumb.title }}
  279. </NuxtLink>
  280. <span v-else class="admin--breadcrumb-current">{{ crumb.title }}</span>
  281. <span
  282. v-if="index < breadcrumbs.length - 1"
  283. class="admin--breadcrumb-separator"
  284. >ㆍ</span
  285. >
  286. </span>
  287. </div>
  288. </div>
  289. <h1>{{ pageTitle }}</h1>
  290. </div>
  291. <div class="admin--header-right">
  292. <button class="admin--header-btn" @click="goToProfile">정보수정</button>
  293. <button
  294. type="button"
  295. class="admin--header-btn admin--header-btn-logout"
  296. @click.prevent="handleLogout"
  297. >
  298. 로그아웃
  299. </button>
  300. </div>
  301. </header>
  302. <!-- Main Content Area -->
  303. <div class="admin--content--wrapper">
  304. <!-- Sidebar GNB -->
  305. <aside class="admin--sidebar">
  306. <NuxtLink to="/site-manager" class="admin--brand" @click="goToDashboard">
  307. <div class="brand--logo">🏴‍☠️</div>
  308. <div class="brand--title">
  309. <h1>파이럿존</h1>
  310. <span>ADMIN</span>
  311. </div>
  312. </NuxtLink>
  313. <nav class="admin--gnb">
  314. <div v-for="menu in menuItems" :key="menu.id" class="admin--gnb-group">
  315. <!-- children 없는 메뉴: 토글 없이 바로 이동 -->
  316. <NuxtLink
  317. v-if="!menu.children"
  318. :to="menu.path"
  319. class="admin--gnb-title admin--gnb-title-link"
  320. :class="{ 'is-active': isActiveRoute(menu.path) }"
  321. >
  322. {{ menu.title }}
  323. </NuxtLink>
  324. <!-- children 있는 메뉴: 기존 토글 동작 -->
  325. <template v-else>
  326. <div class="admin--gnb-title" @click="toggleMenu(menu.id)">
  327. {{ menu.title }}
  328. <span
  329. class="admin--gnb-arrow"
  330. :class="{ 'is-open': openMenus.includes(menu.id) }"
  331. >
  332. </span>
  333. </div>
  334. <transition name="admin--submenu">
  335. <ul v-show="openMenus.includes(menu.id)" class="admin--gnb-submenu">
  336. <li
  337. v-for="submenu in menu.children"
  338. :key="submenu.path"
  339. class="admin--gnb-item"
  340. :class="{ 'is-active': isSubmenuActive(submenu) }"
  341. >
  342. <NuxtLink :to="submenu.path" class="admin--gnb-link">
  343. {{ submenu.title }}
  344. </NuxtLink>
  345. </li>
  346. </ul>
  347. </transition>
  348. </template>
  349. </div>
  350. </nav>
  351. </aside>
  352. <!-- Content Area -->
  353. <main class="admin--main">
  354. <!-- Page Content -->
  355. <slot />
  356. </main>
  357. </div>
  358. <!-- 로그아웃 확인 모달 -->
  359. <AdminAlertModal
  360. v-if="showLogoutModal"
  361. title="로그아웃"
  362. message="로그아웃 하시겠습니까?"
  363. type="confirm"
  364. @confirm="confirmLogout"
  365. @cancel="closeLogoutModal"
  366. @close="closeLogoutModal"
  367. />
  368. <!-- 관리자 정보수정 모달 -->
  369. <AdminModal
  370. v-if="showProfileModal"
  371. :admin="currentAdmin"
  372. @close="closeProfileModal"
  373. @saved="handleProfileSaved"
  374. />
  375. <!-- 알림 모달 -->
  376. <AdminAlertModal
  377. v-if="alertModal.show"
  378. :title="alertModal.title"
  379. :message="alertModal.message"
  380. :type="alertModal.type"
  381. @confirm="handleAlertConfirm"
  382. @cancel="handleAlertCancel"
  383. @close="closeAlertModal"
  384. />
  385. </div>
  386. </template>