admin.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. id: "challenge",
  56. title: "🏆 챌린지 관리",
  57. path: "/site-manager/challenge/list",
  58. pattern: /^\/site-manager\/challenge\/(list|create|edit|detail)/,
  59. },
  60. {
  61. id: "quest",
  62. title: "🏅 퀘스트 관리",
  63. path: "/site-manager/quest/list",
  64. pattern: /^\/site-manager\/quest\/(list|create|edit|detail)/,
  65. },
  66. {
  67. id: "item",
  68. title: "🎁 아이템 관리",
  69. path: "/site-manager/item/list",
  70. pattern: /^\/site-manager\/item\/(list|create|edit|detail)/,
  71. },
  72. {
  73. id: "species",
  74. title: "🐟 어종 관리",
  75. children: [
  76. {
  77. title: "어종구분",
  78. path: "/site-manager/species/list",
  79. pattern: /^\/site-manager\/species\/(list|create|edit|detail)/,
  80. },
  81. {
  82. title: "챌린지 어종관리",
  83. path: "/site-manager/challenge_species/list",
  84. pattern: /^\/site-manager\/challenge_species\/(list|create|edit|detail)/,
  85. },
  86. {
  87. title: "퀘스트 어종관리",
  88. path: "/site-manager/quest_species/list",
  89. pattern: /^\/site-manager\/quest_species\/(list|create|edit|detail)/,
  90. }
  91. ],
  92. },
  93. {
  94. id: "user",
  95. title: "👥 회원 관리",
  96. path: "/site-manager/user/list",
  97. pattern: /^\/site-manager\/user\/(list|create|edit|detail)/,
  98. },
  99. ]);
  100. // 메뉴 토글
  101. const toggleMenu = (menuId) => {
  102. const index = openMenus.value.indexOf(menuId);
  103. if (index > -1) {
  104. openMenus.value.splice(index, 1);
  105. } else {
  106. openMenus.value.push(menuId);
  107. }
  108. };
  109. // 메뉴 활성 여부 (pattern 우선, 없으면 path 정확 일치)
  110. // children 없는 단일 메뉴, 서브메뉴 모두에 사용
  111. const isSubmenuActive = (item) => {
  112. if (item?.pattern instanceof RegExp && item.pattern.test(route.path)) return true;
  113. return route.path === item?.path;
  114. };
  115. // 현재 경로에 맞는 메뉴 찾기
  116. const findCurrentMenu = () => {
  117. const currentPath = route.path;
  118. // 대시보드인 경우
  119. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  120. return { menu: null, child: null };
  121. }
  122. for (const menu of menuItems.value) {
  123. // children이 없는 단일 메뉴는 자기 자신이 매칭 대상
  124. if (!menu.children) {
  125. if (menu.pattern instanceof RegExp && menu.pattern.test(currentPath)) {
  126. return { menu, child: { title: menu.title, path: menu.path } };
  127. }
  128. if (menu.path && currentPath === menu.path) {
  129. return { menu, child: { title: menu.title, path: menu.path } };
  130. }
  131. continue;
  132. }
  133. for (const child of menu.children) {
  134. // pattern이 있으면 정규식으로 매칭, 없으면 정확히 일치하는지 확인
  135. if (child.pattern instanceof RegExp && child.pattern.test(currentPath)) {
  136. return { menu, child };
  137. } else if (currentPath === child.path) {
  138. return { menu, child };
  139. }
  140. }
  141. }
  142. return { menu: null, child: null };
  143. };
  144. // 라우트 변경 시 현재 경로에 해당하는 토글만 열고 나머지는 모두 접기
  145. watch(
  146. () => route.path,
  147. () => {
  148. const { menu } = findCurrentMenu();
  149. openMenus.value = menu?.children ? [menu.id] : [];
  150. },
  151. { immediate: true }
  152. );
  153. // 페이지 타이틀 계산
  154. const pageTitle = computed(() => {
  155. const currentPath = route.path;
  156. // 대시보드
  157. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  158. return "📊 대시보드";
  159. }
  160. const { child } = findCurrentMenu();
  161. if (child) {
  162. // 상세 페이지 타이틀 처리
  163. if (currentPath.includes("/create")) {
  164. return `${child.title} 등록`;
  165. } else if (currentPath.includes("/edit/")) {
  166. return `${child.title} 수정`;
  167. } else if (currentPath.includes("/detail/")) {
  168. return `${child.title} 상세`;
  169. } else if (currentPath.includes("/print/")) {
  170. return `${child.title} 인쇄`;
  171. } else if (currentPath.includes("/print-a2")) {
  172. return `${child.title} 인쇄 (A2)`;
  173. }
  174. return child.title;
  175. }
  176. return "📊 대시보드";
  177. });
  178. // Breadcrumb 계산
  179. const breadcrumbs = computed(() => {
  180. const currentPath = route.path;
  181. const crumbs = [{ title: "대시보드", path: "/site-manager/dashboard" }];
  182. // 대시보드인 경우 Home만 표시
  183. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  184. return crumbs;
  185. }
  186. const { menu, child } = findCurrentMenu();
  187. if (!menu || !child) return crumbs;
  188. // 현재 페이지의 액션 라벨 (등록/수정/상세/인쇄 등)
  189. let subLabel = null;
  190. if (currentPath.includes("/create")) subLabel = "등록";
  191. else if (currentPath.includes("/edit/")) subLabel = "수정";
  192. else if (currentPath.includes("/detail/")) subLabel = "상세";
  193. else if (currentPath.includes("/print/")) subLabel = "인쇄";
  194. else if (currentPath.includes("/print-a2")) subLabel = "인쇄 (A2)";
  195. if (menu.children) {
  196. // 서브메뉴 있는 메뉴: 메뉴그룹 → 서브메뉴 → (액션)
  197. crumbs.push({ title: menu.title, path: null });
  198. if (subLabel) {
  199. crumbs.push({ title: child.title, path: child.path });
  200. crumbs.push({ title: subLabel, path: null });
  201. } else {
  202. crumbs.push({ title: child.title, path: null });
  203. }
  204. } else {
  205. // 단일 메뉴 (children 없음): 메뉴 → (액션) 만, 중복 없이
  206. if (subLabel) {
  207. crumbs.push({ title: menu.title, path: menu.path });
  208. crumbs.push({ title: subLabel, path: null });
  209. } else {
  210. crumbs.push({ title: menu.title, path: null });
  211. }
  212. }
  213. return crumbs;
  214. });
  215. // 로그아웃 모달
  216. const showLogoutModal = ref(false);
  217. const handleLogout = () => {
  218. console.log("[Logout] 로그아웃 버튼 클릭");
  219. showLogoutModal.value = true;
  220. console.log("[Logout] showLogoutModal:", showLogoutModal.value);
  221. };
  222. const confirmLogout = () => {
  223. console.log("[Logout] 로그아웃 확인");
  224. localStorage.removeItem("admin_token");
  225. localStorage.removeItem("admin_user");
  226. router.push("/site-manager");
  227. };
  228. const closeLogoutModal = () => {
  229. console.log("[Logout] 모달 닫기");
  230. showLogoutModal.value = false;
  231. };
  232. // 정보수정 모달
  233. const showProfileModal = ref(false);
  234. const currentAdmin = ref(null);
  235. const { get } = useApi();
  236. // 알림 모달
  237. const alertModal = ref({
  238. show: false,
  239. title: "알림",
  240. message: "",
  241. type: "alert",
  242. onConfirm: null,
  243. });
  244. // 알림 모달 표시
  245. const showAlert = (message, title = "알림") => {
  246. alertModal.value = {
  247. show: true,
  248. title,
  249. message,
  250. type: "alert",
  251. onConfirm: null,
  252. };
  253. };
  254. // 알림 모달 닫기
  255. const closeAlertModal = () => {
  256. alertModal.value.show = false;
  257. };
  258. // 알림 모달 확인
  259. const handleAlertConfirm = () => {
  260. if (alertModal.value.onConfirm) {
  261. alertModal.value.onConfirm();
  262. }
  263. closeAlertModal();
  264. };
  265. // 알림 모달 취소
  266. const handleAlertCancel = () => {
  267. closeAlertModal();
  268. };
  269. // 현재 로그인한 관리자 ID 가져오기
  270. const getCurrentAdminId = () => {
  271. if (typeof window === "undefined") return null;
  272. const user = localStorage.getItem("admin_user");
  273. if (!user) return null;
  274. try {
  275. return JSON.parse(user).id;
  276. } catch {
  277. return null;
  278. }
  279. };
  280. // 대시보드로 이동
  281. const goToDashboard = () => {
  282. router.push("/site-manager/dashboard");
  283. };
  284. // 정보수정 버튼 클릭
  285. const goToProfile = async () => {
  286. const adminId = getCurrentAdminId();
  287. if (!adminId) {
  288. showAlert("로그인 정보를 찾을 수 없습니다.", "오류");
  289. return;
  290. }
  291. // 현재 관리자 정보 조회
  292. const { data, error } = await get(`/admin/${adminId}`);
  293. if (error) {
  294. showAlert("관리자 정보를 불러올 수 없습니다.", "오류");
  295. console.error("[Profile] 관리자 정보 조회 실패:", error);
  296. return;
  297. }
  298. if (data?.success && data?.data) {
  299. currentAdmin.value = data.data;
  300. showProfileModal.value = true;
  301. }
  302. };
  303. // 정보수정 모달 닫기
  304. const closeProfileModal = () => {
  305. showProfileModal.value = false;
  306. currentAdmin.value = null;
  307. };
  308. // 정보수정 완료
  309. const handleProfileSaved = (message) => {
  310. closeProfileModal();
  311. if (message) {
  312. showAlert(message, "성공");
  313. // 로컬스토리지의 사용자 정보도 업데이트
  314. if (currentAdmin.value) {
  315. localStorage.setItem("admin_user", JSON.stringify(currentAdmin.value));
  316. }
  317. }
  318. };
  319. </script>
  320. <template>
  321. <div class="admin--layout">
  322. <!-- Header -->
  323. <header class="admin--header">
  324. <div class="admin--header-left">
  325. <div class="admin--page-header">
  326. <div class="admin--breadcrumb">
  327. <span v-for="(crumb, index) in breadcrumbs" :key="index">
  328. <NuxtLink v-if="crumb.path" :to="crumb.path" class="admin--breadcrumb-link">
  329. {{ crumb.title }}
  330. </NuxtLink>
  331. <span v-else class="admin--breadcrumb-current">{{ crumb.title }}</span>
  332. <span
  333. v-if="index < breadcrumbs.length - 1"
  334. class="admin--breadcrumb-separator"
  335. >ㆍ</span
  336. >
  337. </span>
  338. </div>
  339. </div>
  340. <h1>{{ pageTitle }}</h1>
  341. </div>
  342. <div class="admin--header-right">
  343. <button class="admin--header-btn" @click="goToProfile">정보수정</button>
  344. <button
  345. type="button"
  346. class="admin--header-btn admin--header-btn-logout"
  347. @click.prevent="handleLogout"
  348. >
  349. 로그아웃
  350. </button>
  351. </div>
  352. </header>
  353. <!-- Main Content Area -->
  354. <div class="admin--content--wrapper">
  355. <!-- Sidebar GNB -->
  356. <aside class="admin--sidebar">
  357. <NuxtLink to="/site-manager" class="admin--brand" @click="goToDashboard">
  358. <div class="brand--logo">🏴‍☠️</div>
  359. <div class="brand--title">
  360. <h1>파이럿존</h1>
  361. <span>ADMIN</span>
  362. </div>
  363. </NuxtLink>
  364. <nav class="admin--gnb">
  365. <div v-for="menu in menuItems" :key="menu.id" class="admin--gnb-group">
  366. <!-- children 없는 메뉴: 토글 없이 바로 이동 -->
  367. <NuxtLink
  368. v-if="!menu.children"
  369. :to="menu.path"
  370. class="admin--gnb-title admin--gnb-title-link"
  371. :class="{ 'is-active': isSubmenuActive(menu) }"
  372. >
  373. {{ menu.title }}
  374. </NuxtLink>
  375. <!-- children 있는 메뉴: 기존 토글 동작 -->
  376. <template v-else>
  377. <div class="admin--gnb-title" @click="toggleMenu(menu.id)">
  378. {{ menu.title }}
  379. <span
  380. class="admin--gnb-arrow"
  381. :class="{ 'is-open': openMenus.includes(menu.id) }"
  382. >
  383. </span>
  384. </div>
  385. <transition name="admin--submenu">
  386. <ul v-show="openMenus.includes(menu.id)" class="admin--gnb-submenu">
  387. <li
  388. v-for="submenu in menu.children"
  389. :key="submenu.path"
  390. class="admin--gnb-item"
  391. :class="{ 'is-active': isSubmenuActive(submenu) }"
  392. >
  393. <NuxtLink :to="submenu.path" class="admin--gnb-link">
  394. {{ submenu.title }}
  395. </NuxtLink>
  396. </li>
  397. </ul>
  398. </transition>
  399. </template>
  400. </div>
  401. </nav>
  402. </aside>
  403. <!-- Content Area -->
  404. <main class="admin--main">
  405. <!-- Page Content -->
  406. <slot />
  407. </main>
  408. </div>
  409. <!-- 로그아웃 확인 모달 -->
  410. <AdminAlertModal
  411. v-if="showLogoutModal"
  412. title="로그아웃"
  413. message="로그아웃 하시겠습니까?"
  414. type="confirm"
  415. @confirm="confirmLogout"
  416. @cancel="closeLogoutModal"
  417. @close="closeLogoutModal"
  418. />
  419. <!-- 관리자 정보수정 모달 -->
  420. <AdminModal
  421. v-if="showProfileModal"
  422. :admin="currentAdmin"
  423. @close="closeProfileModal"
  424. @saved="handleProfileSaved"
  425. />
  426. <!-- 알림 모달 -->
  427. <AdminAlertModal
  428. v-if="alertModal.show"
  429. :title="alertModal.title"
  430. :message="alertModal.message"
  431. :type="alertModal.type"
  432. @confirm="handleAlertConfirm"
  433. @cancel="handleAlertCancel"
  434. @close="closeAlertModal"
  435. />
  436. </div>
  437. </template>