admin.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/fishing/list",
  45. pattern: /^\/site-manager\/fishing\/list|create|edit/,
  46. },
  47. {
  48. title: "낚시터관리",
  49. path: "/site-manager/fishing/event",
  50. pattern: /^\/site-manager\/fishing\/event/,
  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("/print/")) {
  123. return `${child.title} 인쇄`;
  124. } else if (currentPath.includes("/print-a2")) {
  125. return `${child.title} 인쇄 (A2)`;
  126. }
  127. return child.title;
  128. }
  129. return "📊 대시보드";
  130. });
  131. // Breadcrumb 계산
  132. const breadcrumbs = computed(() => {
  133. const currentPath = route.path;
  134. const crumbs = [{ title: "대시보드", path: "/site-manager/dashboard" }];
  135. // 대시보드인 경우 Home만 표시
  136. if (currentPath === "/site-manager/dashboard" || currentPath === "/site-manager") {
  137. return crumbs;
  138. }
  139. const { menu, child } = findCurrentMenu();
  140. if (menu && child) {
  141. // 메뉴 그룹 추가
  142. crumbs.push({ title: menu.title, path: null });
  143. // 현재 페이지 타이틀 추가
  144. if (currentPath.includes("/create")) {
  145. crumbs.push({ title: child.title, path: child.path });
  146. crumbs.push({ title: "등록", path: null });
  147. } else if (currentPath.includes("/edit/")) {
  148. crumbs.push({ title: child.title, path: child.path });
  149. crumbs.push({ title: "수정", path: null });
  150. } else if (currentPath.includes("/print/")) {
  151. crumbs.push({ title: child.title, path: child.path });
  152. crumbs.push({ title: "인쇄", path: null });
  153. } else if (currentPath.includes("/print-a2")) {
  154. crumbs.push({ title: child.title, path: child.path });
  155. crumbs.push({ title: "인쇄 (A2)", path: null });
  156. } else {
  157. crumbs.push({ title: child.title, path: null });
  158. }
  159. }
  160. return crumbs;
  161. });
  162. // 로그아웃 모달
  163. const showLogoutModal = ref(false);
  164. const handleLogout = () => {
  165. console.log("[Logout] 로그아웃 버튼 클릭");
  166. showLogoutModal.value = true;
  167. console.log("[Logout] showLogoutModal:", showLogoutModal.value);
  168. };
  169. const confirmLogout = () => {
  170. console.log("[Logout] 로그아웃 확인");
  171. localStorage.removeItem("admin_token");
  172. localStorage.removeItem("admin_user");
  173. router.push("/site-manager");
  174. };
  175. const closeLogoutModal = () => {
  176. console.log("[Logout] 모달 닫기");
  177. showLogoutModal.value = false;
  178. };
  179. // 정보수정 모달
  180. const showProfileModal = ref(false);
  181. const currentAdmin = ref(null);
  182. const { get } = useApi();
  183. // 알림 모달
  184. const alertModal = ref({
  185. show: false,
  186. title: "알림",
  187. message: "",
  188. type: "alert",
  189. onConfirm: null,
  190. });
  191. // 알림 모달 표시
  192. const showAlert = (message, title = "알림") => {
  193. alertModal.value = {
  194. show: true,
  195. title,
  196. message,
  197. type: "alert",
  198. onConfirm: null,
  199. };
  200. };
  201. // 알림 모달 닫기
  202. const closeAlertModal = () => {
  203. alertModal.value.show = false;
  204. };
  205. // 알림 모달 확인
  206. const handleAlertConfirm = () => {
  207. if (alertModal.value.onConfirm) {
  208. alertModal.value.onConfirm();
  209. }
  210. closeAlertModal();
  211. };
  212. // 알림 모달 취소
  213. const handleAlertCancel = () => {
  214. closeAlertModal();
  215. };
  216. // 현재 로그인한 관리자 ID 가져오기
  217. const getCurrentAdminId = () => {
  218. if (typeof window === "undefined") return null;
  219. const user = localStorage.getItem("admin_user");
  220. if (!user) return null;
  221. try {
  222. return JSON.parse(user).id;
  223. } catch {
  224. return null;
  225. }
  226. };
  227. // 대시보드로 이동
  228. const goToDashboard = () => {
  229. router.push("/site-manager/dashboard");
  230. };
  231. // 정보수정 버튼 클릭
  232. const goToProfile = async () => {
  233. const adminId = getCurrentAdminId();
  234. if (!adminId) {
  235. showAlert("로그인 정보를 찾을 수 없습니다.", "오류");
  236. return;
  237. }
  238. // 현재 관리자 정보 조회
  239. const { data, error } = await get(`/admin/${adminId}`);
  240. if (error) {
  241. showAlert("관리자 정보를 불러올 수 없습니다.", "오류");
  242. console.error("[Profile] 관리자 정보 조회 실패:", error);
  243. return;
  244. }
  245. if (data?.success && data?.data) {
  246. currentAdmin.value = data.data;
  247. showProfileModal.value = true;
  248. }
  249. };
  250. // 정보수정 모달 닫기
  251. const closeProfileModal = () => {
  252. showProfileModal.value = false;
  253. currentAdmin.value = null;
  254. };
  255. // 정보수정 완료
  256. const handleProfileSaved = (message) => {
  257. closeProfileModal();
  258. if (message) {
  259. showAlert(message, "성공");
  260. // 로컬스토리지의 사용자 정보도 업데이트
  261. if (currentAdmin.value) {
  262. localStorage.setItem("admin_user", JSON.stringify(currentAdmin.value));
  263. }
  264. }
  265. };
  266. </script>
  267. <template>
  268. <div class="admin--layout">
  269. <!-- Header -->
  270. <header class="admin--header">
  271. <div class="admin--header-left">
  272. <div class="admin--page-header">
  273. <div class="admin--breadcrumb">
  274. <span v-for="(crumb, index) in breadcrumbs" :key="index">
  275. <NuxtLink v-if="crumb.path" :to="crumb.path" class="admin--breadcrumb-link">
  276. {{ crumb.title }}
  277. </NuxtLink>
  278. <span v-else class="admin--breadcrumb-current">{{ crumb.title }}</span>
  279. <span
  280. v-if="index < breadcrumbs.length - 1"
  281. class="admin--breadcrumb-separator"
  282. >ㆍ</span
  283. >
  284. </span>
  285. </div>
  286. </div>
  287. <h1>{{ pageTitle }}</h1>
  288. </div>
  289. <div class="admin--header-right">
  290. <button class="admin--header-btn" @click="goToProfile">정보수정</button>
  291. <button
  292. type="button"
  293. class="admin--header-btn admin--header-btn-logout"
  294. @click.prevent="handleLogout"
  295. >
  296. 로그아웃
  297. </button>
  298. </div>
  299. </header>
  300. <!-- Main Content Area -->
  301. <div class="admin--content--wrapper">
  302. <!-- Sidebar GNB -->
  303. <aside class="admin--sidebar">
  304. <NuxtLink to="/site-manager" class="admin--brand" @click="goToDashboard">
  305. <div class="brand--logo">🏴‍☠️</div>
  306. <div class="brand--title">
  307. <h1>파이럿존</h1>
  308. <span>ADMIN</span>
  309. </div>
  310. </NuxtLink>
  311. <nav class="admin--gnb">
  312. <div v-for="menu in menuItems" :key="menu.id" class="admin--gnb-group">
  313. <!-- children 없는 메뉴: 토글 없이 바로 이동 -->
  314. <NuxtLink
  315. v-if="!menu.children"
  316. :to="menu.path"
  317. class="admin--gnb-title admin--gnb-title-link"
  318. :class="{ 'is-active': isActiveRoute(menu.path) }"
  319. >
  320. {{ menu.title }}
  321. </NuxtLink>
  322. <!-- children 있는 메뉴: 기존 토글 동작 -->
  323. <template v-else>
  324. <div class="admin--gnb-title" @click="toggleMenu(menu.id)">
  325. {{ menu.title }}
  326. <span
  327. class="admin--gnb-arrow"
  328. :class="{ 'is-open': openMenus.includes(menu.id) }"
  329. >
  330. </span>
  331. </div>
  332. <transition name="admin--submenu">
  333. <ul v-show="openMenus.includes(menu.id)" class="admin--gnb-submenu">
  334. <li
  335. v-for="submenu in menu.children"
  336. :key="submenu.path"
  337. class="admin--gnb-item"
  338. :class="{ 'is-active': isSubmenuActive(submenu) }"
  339. >
  340. <NuxtLink :to="submenu.path" class="admin--gnb-link">
  341. {{ submenu.title }}
  342. </NuxtLink>
  343. </li>
  344. </ul>
  345. </transition>
  346. </template>
  347. </div>
  348. </nav>
  349. </aside>
  350. <!-- Content Area -->
  351. <main class="admin--main">
  352. <!-- Page Content -->
  353. <slot />
  354. </main>
  355. </div>
  356. <!-- 로그아웃 확인 모달 -->
  357. <AdminAlertModal
  358. v-if="showLogoutModal"
  359. title="로그아웃"
  360. message="로그아웃 하시겠습니까?"
  361. type="confirm"
  362. @confirm="confirmLogout"
  363. @cancel="closeLogoutModal"
  364. @close="closeLogoutModal"
  365. />
  366. <!-- 관리자 정보수정 모달 -->
  367. <AdminModal
  368. v-if="showProfileModal"
  369. :admin="currentAdmin"
  370. @close="closeProfileModal"
  371. @saved="handleProfileSaved"
  372. />
  373. <!-- 알림 모달 -->
  374. <AdminAlertModal
  375. v-if="alertModal.show"
  376. :title="alertModal.title"
  377. :message="alertModal.message"
  378. :type="alertModal.type"
  379. @confirm="handleAlertConfirm"
  380. @cancel="handleAlertCancel"
  381. @close="closeAlertModal"
  382. />
  383. </div>
  384. </template>