list.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. class="admin--pagination-btn"
  68. :disabled="currentPage === 1"
  69. @click="changePage(currentPage - 1)"
  70. >
  71. 이전
  72. </button>
  73. <button
  74. v-for="page in visiblePages"
  75. :key="page"
  76. class="admin--pagination-btn"
  77. :class="{ 'is-active': page === currentPage }"
  78. @click="changePage(page)"
  79. >
  80. {{ page }}
  81. </button>
  82. <button
  83. class="admin--pagination-btn"
  84. :disabled="currentPage === totalPages"
  85. @click="changePage(currentPage + 1)"
  86. >
  87. 다음
  88. </button>
  89. </div>
  90. </div>
  91. </template>
  92. <script setup>
  93. import { ref, computed, onMounted } from "vue";
  94. import { useRouter } from "vue-router";
  95. definePageMeta({
  96. layout: "admin",
  97. middleware: ["auth"],
  98. });
  99. const router = useRouter();
  100. const { get } = useApi();
  101. const isLoading = ref(false);
  102. const fields = ref([]);
  103. const currentPage = ref(1);
  104. const perPage = ref(10);
  105. const totalCount = ref(0);
  106. const totalPages = ref(0);
  107. const searchQuery = ref("");
  108. // 보이는 페이지 번호 계산
  109. const visiblePages = computed(() => {
  110. const pages = [];
  111. const maxVisible = 5;
  112. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  113. let end = Math.min(totalPages.value, start + maxVisible - 1);
  114. if (end - start < maxVisible - 1) {
  115. start = Math.max(1, end - maxVisible + 1);
  116. }
  117. for (let i = start; i <= end; i++) {
  118. pages.push(i);
  119. }
  120. return pages;
  121. });
  122. // 데이터 로드
  123. const loadFields = async () => {
  124. isLoading.value = true;
  125. const params = {
  126. page: currentPage.value,
  127. per_page: perPage.value,
  128. };
  129. if (searchQuery.value) params.search = searchQuery.value;
  130. const { data, error } = await get("/area/list", { params });
  131. if (error) {
  132. console.error("[AreaList] 목록 로드 실패:", error);
  133. fields.value = [];
  134. totalCount.value = 0;
  135. totalPages.value = 0;
  136. } else if (data?.success && data?.data) {
  137. fields.value = data.data.items || [];
  138. totalCount.value = data.data.total || 0;
  139. totalPages.value = data.data.total_pages || 0;
  140. }
  141. isLoading.value = false;
  142. };
  143. // 검색
  144. const onSearch = () => {
  145. currentPage.value = 1;
  146. loadFields();
  147. };
  148. // 검색 초기화
  149. const resetSearch = () => {
  150. searchQuery.value = "";
  151. currentPage.value = 1;
  152. loadFields();
  153. };
  154. // 페이지 변경
  155. const changePage = (page) => {
  156. if (page < 1 || page > totalPages.value) return;
  157. currentPage.value = page;
  158. loadFields();
  159. window.scrollTo({ top: 0, behavior: "smooth" });
  160. };
  161. // 이동
  162. const goToCreate = () => router.push("/site-manager/area/create");
  163. const goToDetail = (id) => router.push(`/site-manager/area/detail/${id}`);
  164. const goToEdit = (id) => router.push(`/site-manager/area/edit/${id}`);
  165. // 날짜 포맷
  166. const formatDate = (dateString) => {
  167. if (!dateString) return "-";
  168. const date = new Date(dateString.replace(" ", "T"));
  169. if (isNaN(date.getTime())) return dateString;
  170. return date.toLocaleDateString("ko-KR", {
  171. year: "numeric",
  172. month: "2-digit",
  173. day: "2-digit",
  174. });
  175. };
  176. onMounted(() => {
  177. loadFields();
  178. });
  179. </script>