index.vue 6.8 KB

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