admin.vue 12 KB

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