index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-small admin--btn-small-primary" @click="handleSearch">검색</button>
  19. <button class="admin--btn-small admin--btn-small-secondary" @click="handleReset">
  20. 초기화
  21. </button>
  22. </div>
  23. <div class="admin--search-actions">
  24. <button class="admin--btn-small admin--btn-small-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="!posts || posts.length === 0">
  42. <td colspan="6" class="admin--table-empty">등록된 게시물이 없습니다.</td>
  43. </tr>
  44. <tr v-else v-for="(post, index) in posts" :key="post.id">
  45. <td>
  46. {{
  47. (post.is_notice === 1 || post.is_notice === '1')
  48. ? "공지"
  49. : totalCount - ((currentPage - 1) * perPage + index)
  50. }}
  51. </td>
  52. <td class="admin--table-title">{{ post.title }}</td>
  53. <td>{{ post.name }}</td>
  54. <td>{{ formatDate(post.created_at) }}</td>
  55. <td>{{ post.views }}</td>
  56. <td>
  57. <div class="admin--table-actions">
  58. <button
  59. class="admin--btn-small admin--btn-small-primary"
  60. @click="goToEdit(post.id)"
  61. >
  62. 수정
  63. </button>
  64. <button
  65. class="admin--btn-small admin--btn-small-danger"
  66. @click="handleDelete(post.id)"
  67. >
  68. 삭제
  69. </button>
  70. </div>
  71. </td>
  72. </tr>
  73. </tbody>
  74. </table>
  75. </div>
  76. <!-- 페이지네이션 -->
  77. <div v-if="totalPages > 1" class="admin--pagination">
  78. <button
  79. class="admin--pagination-btn"
  80. :disabled="currentPage === 1"
  81. @click="changePage(1)"
  82. title="처음"
  83. >
  84. </button>
  85. <button
  86. class="admin--pagination-btn"
  87. :disabled="currentPage === 1"
  88. @click="changePage(currentPage - 1)"
  89. title="이전"
  90. >
  91. </button>
  92. <button
  93. v-for="page in visiblePages"
  94. :key="page"
  95. class="admin--pagination-btn"
  96. :class="{ 'is-active': page === currentPage }"
  97. @click="changePage(page)"
  98. >
  99. {{ page }}
  100. </button>
  101. <button
  102. class="admin--pagination-btn"
  103. :disabled="currentPage === totalPages"
  104. @click="changePage(currentPage + 1)"
  105. title="다음"
  106. >
  107. </button>
  108. <button
  109. class="admin--pagination-btn"
  110. :disabled="currentPage === totalPages"
  111. @click="changePage(totalPages)"
  112. title="끝"
  113. >
  114. </button>
  115. </div>
  116. </div>
  117. </template>
  118. <script setup>
  119. import { ref, computed, onMounted } from "vue";
  120. import { useRouter } from "vue-router";
  121. definePageMeta({ layout: "admin", middleware: ["auth"] });
  122. const router = useRouter();
  123. const { get, del } = useApi();
  124. const posts = ref([]);
  125. const searchType = ref("title");
  126. const searchKeyword = ref("");
  127. const currentPage = ref(1);
  128. const perPage = ref(10);
  129. const totalCount = ref(0);
  130. const totalPages = ref(0);
  131. const visiblePages = computed(() => {
  132. const pages = [];
  133. const maxVisible = 5;
  134. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  135. let end = Math.min(totalPages.value, start + maxVisible - 1);
  136. if (end - start < maxVisible - 1) start = Math.max(1, end - maxVisible + 1);
  137. for (let i = start; i <= end; i++) pages.push(i);
  138. return pages;
  139. });
  140. const loadPosts = async () => {
  141. const params = { page: currentPage.value, per_page: perPage.value };
  142. if (searchKeyword.value) {
  143. params.search_type = searchType.value;
  144. params.search_keyword = searchKeyword.value;
  145. }
  146. const { data, error } = await get("/board/news", { params });
  147. console.log("[NewsBoard] API 응답:", { data, error });
  148. // API 응답: { success: true, data: { items, total }, message }
  149. if (data?.success && data?.data) {
  150. posts.value = data.data.items || [];
  151. totalCount.value = data.data.total || 0;
  152. totalPages.value = Math.ceil(totalCount.value / perPage.value);
  153. console.log("[NewsBoard] 로드 성공:", posts.value.length);
  154. }
  155. };
  156. const handleSearch = () => {
  157. currentPage.value = 1;
  158. loadPosts();
  159. };
  160. const handleReset = () => {
  161. searchType.value = "title";
  162. searchKeyword.value = "";
  163. currentPage.value = 1;
  164. loadPosts();
  165. };
  166. const changePage = (page) => {
  167. if (page < 1 || page > totalPages.value) return;
  168. currentPage.value = page;
  169. loadPosts();
  170. window.scrollTo({ top: 0, behavior: 'smooth' });
  171. };
  172. const goToCreate = () => router.push("/site-manager/board/news/create");
  173. const goToEdit = (id) => router.push(`/site-manager/board/news/edit/${id}`);
  174. const handleDelete = async (id) => {
  175. if (!confirm("정말 삭제하시겠습니까?")) return;
  176. const { error } = await del(`/board/news/${id}`);
  177. if (error) alert("삭제에 실패했습니다.");
  178. else {
  179. alert("삭제되었습니다.");
  180. loadPosts();
  181. }
  182. };
  183. const formatDate = (dateString) => {
  184. if (!dateString) return "-";
  185. const date = new Date(dateString);
  186. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(
  187. 2,
  188. "0"
  189. )}.${String(date.getDate()).padStart(2, "0")}`;
  190. };
  191. onMounted(() => loadPosts());
  192. </script>
  193. <style scoped>
  194. /* 검색 영역 input/select 스타일 통일 */
  195. .admin--search-box .admin--form-input,
  196. .admin--search-box .admin--search-input,
  197. .admin--search-box .admin--form-select,
  198. .admin--search-box .admin--search-select {
  199. border: 1px solid var(--admin-border-color) !important;
  200. border-radius: 4px;
  201. height: 33px !important;
  202. padding: 6px 14px !important;
  203. font-size: 13px !important;
  204. }
  205. </style>