list.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="admin--field-list">
  3. <!-- 상단 검색/액션 영역 -->
  4. <div class="admin--search-box">
  5. <div class="admin--search-form">
  6. <!-- <select v-model="filterStatus" @change="onSearch" class="admin--form-select admin--search-select">
  7. <option value="">전체 상태</option>
  8. <option value="Y">사용중</option>
  9. <option value="N">미사용</option>
  10. </select> -->
  11. <input
  12. v-model="searchQuery"
  13. type="text"
  14. placeholder="지역명으로 검색"
  15. @keyup.enter="onSearch"
  16. class="admin--form-input admin--search-input"
  17. />
  18. <button @click="onSearch" class="admin--btn-small admin--btn-small-primary">검색</button>
  19. <button @click="resetSearch" class="admin--btn-small admin--btn-small-secondary">초기화</button>
  20. </div>
  21. <div class="admin--search-actions">
  22. <button class="admin--btn-add" @click="goToCreate">+ 새 지역 추가</button>
  23. </div>
  24. </div>
  25. <!-- 테이블 -->
  26. <div class="admin--table-wrapper">
  27. <table class="admin--table">
  28. <thead>
  29. <tr>
  30. <th style="width: 80px;">번호</th>
  31. <th style="width: 60%;">지역</th>
  32. <th>등록일</th>
  33. <th style="width: 120px;">관리</th>
  34. </tr>
  35. </thead>
  36. <tbody>
  37. <tr v-if="isLoading">
  38. <td colspan="6" class="admin--table-loading">데이터를 불러오는 중...</td>
  39. </tr>
  40. <tr v-else-if="!fields || fields.length === 0">
  41. <td colspan="6" class="admin--table-empty">등록된 지역이 없습니다.</td>
  42. </tr>
  43. <tr
  44. v-else
  45. v-for="(field, index) in fields"
  46. :key="field.id"
  47. class="admin--table-row-clickable"
  48. @click="goToDetail(field.id)"
  49. >
  50. <td class="date">{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  51. <td class="admin--table-title">{{ field.name }}</td>
  52. <td class="date">{{ formatDate(field.created_at) }}</td>
  53. <td>
  54. <div class="admin--table-actions">
  55. <button class="admin--btn-small admin--btn-blue" @click.stop="goToEdit(field.id)">
  56. 수정
  57. </button>
  58. </div>
  59. </td>
  60. </tr>
  61. </tbody>
  62. </table>
  63. </div>
  64. <!-- 페이지네이션 -->
  65. <div v-if="totalPages > 1" class="admin--pagination">
  66. <button
  67. v-if="totalPages > 2"
  68. class="admin--pagination-btn"
  69. :disabled="currentPage === 1"
  70. @click="changePage(1)"
  71. title="처음"
  72. >
  73. ◀◀
  74. </button>
  75. <button
  76. class="admin--pagination-btn"
  77. :disabled="currentPage === 1"
  78. @click="changePage(currentPage - 1)"
  79. title="이전"
  80. >
  81. </button>
  82. <button
  83. v-for="page in visiblePages"
  84. :key="page"
  85. class="admin--pagination-btn"
  86. :class="{ 'is-active': page === currentPage }"
  87. @click="changePage(page)"
  88. >
  89. {{ page }}
  90. </button>
  91. <button
  92. class="admin--pagination-btn"
  93. :disabled="currentPage === totalPages"
  94. @click="changePage(currentPage + 1)"
  95. title="다음"
  96. >
  97. </button>
  98. <button
  99. v-if="totalPages > 2"
  100. class="admin--pagination-btn"
  101. :disabled="currentPage === totalPages"
  102. @click="changePage(totalPages)"
  103. title="끝"
  104. >
  105. ▶▶
  106. </button>
  107. </div>
  108. </div>
  109. </template>
  110. <script setup>
  111. import { ref, computed, onMounted } from "vue";
  112. import { useRouter } from "vue-router";
  113. definePageMeta({
  114. layout: "admin",
  115. middleware: ["auth"],
  116. });
  117. const router = useRouter();
  118. const { get } = useApi();
  119. const isLoading = ref(false);
  120. const fields = ref([]);
  121. const currentPage = ref(1);
  122. const perPage = ref(10);
  123. const totalCount = ref(0);
  124. const totalPages = ref(0);
  125. const searchQuery = ref("");
  126. // 보이는 페이지 번호 계산
  127. const visiblePages = computed(() => {
  128. const pages = [];
  129. const maxVisible = 5;
  130. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  131. let end = Math.min(totalPages.value, start + maxVisible - 1);
  132. if (end - start < maxVisible - 1) {
  133. start = Math.max(1, end - maxVisible + 1);
  134. }
  135. for (let i = start; i <= end; i++) {
  136. pages.push(i);
  137. }
  138. return pages;
  139. });
  140. // 데이터 로드
  141. const loadFields = async () => {
  142. isLoading.value = true;
  143. const params = {
  144. page: currentPage.value,
  145. per_page: perPage.value,
  146. };
  147. if (searchQuery.value) params.search = searchQuery.value;
  148. const { data, error } = await get("/area/list", { params });
  149. if (error) {
  150. console.error("[AreaList] 목록 로드 실패:", error);
  151. fields.value = [];
  152. totalCount.value = 0;
  153. totalPages.value = 0;
  154. } else if (data?.success && data?.data) {
  155. fields.value = data.data.items || [];
  156. totalCount.value = data.data.total || 0;
  157. totalPages.value = data.data.total_pages || 0;
  158. }
  159. isLoading.value = false;
  160. };
  161. // 검색
  162. const onSearch = () => {
  163. currentPage.value = 1;
  164. loadFields();
  165. };
  166. // 검색 초기화
  167. const resetSearch = () => {
  168. searchQuery.value = "";
  169. currentPage.value = 1;
  170. loadFields();
  171. };
  172. // 페이지 변경
  173. const changePage = (page) => {
  174. if (page < 1 || page > totalPages.value) return;
  175. currentPage.value = page;
  176. loadFields();
  177. window.scrollTo({ top: 0, behavior: "smooth" });
  178. };
  179. // 이동
  180. const goToCreate = () => router.push("/site-manager/area/create");
  181. const goToDetail = (id) => router.push(`/site-manager/area/detail/${id}`);
  182. const goToEdit = (id) => router.push(`/site-manager/area/edit/${id}`);
  183. // 날짜 포맷
  184. const formatDate = (dateString) => {
  185. if (!dateString) return "-";
  186. const date = new Date(dateString.replace(" ", "T"));
  187. if (isNaN(date.getTime())) return dateString;
  188. return date.toLocaleDateString("ko-KR", {
  189. year: "numeric",
  190. month: "2-digit",
  191. day: "2-digit",
  192. });
  193. };
  194. onMounted(() => {
  195. loadFields();
  196. });
  197. </script>