index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <div class="admin--board-list">
  3. <!-- 검색 영역 -->
  4. <div class="admin--search-box">
  5. <div class="admin--search-form">
  6. <select v-model="searchType" class="admin--form-select admin--search-select">
  7. <option value="title">제목</option>
  8. <option value="name">이름</option>
  9. <option value="content">내용</option>
  10. </select>
  11. <input
  12. v-model="searchKeyword"
  13. type="text"
  14. class="admin--form-input admin--search-input"
  15. placeholder="검색어를 입력하세요"
  16. @keyup.enter="handleSearch"
  17. />
  18. <button class="admin--btn admin--btn-primary" @click="handleSearch">검색</button>
  19. <button class="admin--btn admin--btn-secondary" @click="handleReset">
  20. 초기화
  21. </button>
  22. </div>
  23. <div class="admin--search-actions">
  24. <button class="admin--btn admin--btn-primary" @click="goToCreate">+ 등록</button>
  25. </div>
  26. </div>
  27. <!-- 테이블 -->
  28. <div class="admin--table-wrapper">
  29. <table class="admin--table">
  30. <thead>
  31. <tr>
  32. <th>NO</th>
  33. <th>제목</th>
  34. <th>이름</th>
  35. <th>등록일</th>
  36. <th>조회</th>
  37. <th>관리</th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. <tr v-if="isLoading">
  42. <td colspan="6" class="admin--table-loading">데이터를 불러오는 중...</td>
  43. </tr>
  44. <tr v-else-if="!posts || posts.length === 0">
  45. <td colspan="6" class="admin--table-empty">등록된 게시물이 없습니다.</td>
  46. </tr>
  47. <tr v-else v-for="(post, index) in posts" :key="post.id">
  48. <td>
  49. {{
  50. post.is_notice
  51. ? "공지"
  52. : totalCount - ((currentPage - 1) * perPage + index)
  53. }}
  54. </td>
  55. <td class="admin--table-title">{{ post.title }}</td>
  56. <td>{{ post.name }}</td>
  57. <td>{{ formatDate(post.created_at) }}</td>
  58. <td>{{ post.views }}</td>
  59. <td>
  60. <div class="admin--table-actions">
  61. <button
  62. class="admin--btn-small admin--btn-small-primary"
  63. @click="goToEdit(post.id)"
  64. >
  65. 수정
  66. </button>
  67. <button
  68. class="admin--btn-small admin--btn-small-danger"
  69. @click="handleDelete(post.id)"
  70. >
  71. 삭제
  72. </button>
  73. </div>
  74. </td>
  75. </tr>
  76. </tbody>
  77. </table>
  78. </div>
  79. <!-- 페이지네이션 -->
  80. <div v-if="totalPages > 1" class="admin--pagination">
  81. <button
  82. class="admin--pagination-btn"
  83. :disabled="currentPage === 1"
  84. @click="changePage(currentPage - 1)"
  85. >
  86. 이전
  87. </button>
  88. <button
  89. v-for="page in visiblePages"
  90. :key="page"
  91. class="admin--pagination-btn"
  92. :class="{ 'is-active': page === currentPage }"
  93. @click="changePage(page)"
  94. >
  95. {{ page }}
  96. </button>
  97. <button
  98. class="admin--pagination-btn"
  99. :disabled="currentPage === totalPages"
  100. @click="changePage(currentPage + 1)"
  101. >
  102. 다음
  103. </button>
  104. </div>
  105. </div>
  106. </template>
  107. <script setup>
  108. import { ref, computed, onMounted } from "vue";
  109. import { useRouter } from "vue-router";
  110. definePageMeta({ layout: "admin", middleware: ["auth"] });
  111. const router = useRouter();
  112. const { get, del } = useApi();
  113. const isLoading = ref(false);
  114. const posts = ref([]);
  115. const searchType = ref("title");
  116. const searchKeyword = ref("");
  117. const currentPage = ref(1);
  118. const perPage = ref(10);
  119. const totalCount = ref(0);
  120. const totalPages = ref(0);
  121. const visiblePages = computed(() => {
  122. const pages = [];
  123. const maxVisible = 5;
  124. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  125. let end = Math.min(totalPages.value, start + maxVisible - 1);
  126. if (end - start < maxVisible - 1) start = Math.max(1, end - maxVisible + 1);
  127. for (let i = start; i <= end; i++) pages.push(i);
  128. return pages;
  129. });
  130. const loadPosts = async () => {
  131. isLoading.value = true;
  132. const params = { page: currentPage.value, per_page: perPage.value };
  133. if (searchKeyword.value) {
  134. params.search_type = searchType.value;
  135. params.search_keyword = searchKeyword.value;
  136. }
  137. const { data, error } = await get("/board/event", params);
  138. console.log("[EventBoard] API 응답:", { data, error });
  139. // API 응답: { success: true, data: { items, total }, message }
  140. if (data?.success && data?.data) {
  141. posts.value = data.data.items || [];
  142. totalCount.value = data.data.total || 0;
  143. totalPages.value = Math.ceil(totalCount.value / perPage.value);
  144. console.log("[EventBoard] 로드 성공:", posts.value.length);
  145. }
  146. isLoading.value = false;
  147. };
  148. const handleSearch = () => {
  149. currentPage.value = 1;
  150. loadPosts();
  151. };
  152. const handleReset = () => {
  153. searchType.value = "title";
  154. searchKeyword.value = "";
  155. currentPage.value = 1;
  156. loadPosts();
  157. };
  158. const changePage = (page) => {
  159. if (page < 1 || page > totalPages.value) return;
  160. currentPage.value = page;
  161. loadPosts();
  162. };
  163. const goToCreate = () => router.push("/admin/board/event/create");
  164. const goToEdit = (id) => router.push(`/admin/board/event/edit/${id}`);
  165. const handleDelete = async (id) => {
  166. if (!confirm("정말 삭제하시겠습니까?")) return;
  167. const { error } = await del(`/board/event/${id}`);
  168. if (error) alert("삭제에 실패했습니다.");
  169. else {
  170. alert("삭제되었습니다.");
  171. loadPosts();
  172. }
  173. };
  174. const formatDate = (dateString) => {
  175. if (!dateString) return "-";
  176. const date = new Date(dateString);
  177. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(
  178. 2,
  179. "0"
  180. )}.${String(date.getDate()).padStart(2, "0")}`;
  181. };
  182. onMounted(() => loadPosts());
  183. </script>