list.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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: 40%;">분야</th>
  32. <th>지역명</th>
  33. <th>선상명</th>
  34. <th>제휴업체</th>
  35. <th>상태</th>
  36. <th>등록일</th>
  37. <th style="width: 120px;">관리</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="!fields || fields.length === 0">
  45. <td colspan="6" class="admin--table-empty">등록된 선상이 없습니다.</td>
  46. </tr>
  47. <tr
  48. v-else
  49. v-for="(field, index) in fields"
  50. :key="field.id"
  51. class="admin--table-row-clickable"
  52. @click="goToDetail(field.id)"
  53. >
  54. <td class="date">{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  55. <td class="admin--table-title">{{ field.name }}</td>
  56. <td class="color--yellow">{{ field.weight }}</td>
  57. <td class="date">{{ formatDate(field.created_at) }}</td>
  58. <td>
  59. <span :class="['admin--badge', getStatusBadgeClass(field.status_YN)]">
  60. {{ getStatusLabel(field.status_YN) }}
  61. </span>
  62. </td>
  63. <td>
  64. <div class="admin--table-actions">
  65. <button class="admin--btn-small admin--btn-blue" @click.stop="goToEdit(field.id)">
  66. 수정
  67. </button>
  68. </div>
  69. </td>
  70. </tr>
  71. </tbody>
  72. </table>
  73. </div>
  74. <!-- 페이지네이션 -->
  75. <div v-if="totalPages > 1" class="admin--pagination">
  76. <button
  77. class="admin--pagination-btn"
  78. :disabled="currentPage === 1"
  79. @click="changePage(currentPage - 1)"
  80. >
  81. 이전
  82. </button>
  83. <button
  84. v-for="page in visiblePages"
  85. :key="page"
  86. class="admin--pagination-btn"
  87. :class="{ 'is-active': page === currentPage }"
  88. @click="changePage(page)"
  89. >
  90. {{ page }}
  91. </button>
  92. <button
  93. class="admin--pagination-btn"
  94. :disabled="currentPage === totalPages"
  95. @click="changePage(currentPage + 1)"
  96. >
  97. 다음
  98. </button>
  99. </div>
  100. </div>
  101. </template>
  102. <script setup>
  103. import { ref, computed, onMounted } from "vue";
  104. import { useRouter } from "vue-router";
  105. definePageMeta({
  106. layout: "admin",
  107. middleware: ["auth"],
  108. });
  109. const router = useRouter();
  110. const { get } = useApi();
  111. const isLoading = ref(false);
  112. const fields = ref([]);
  113. const currentPage = ref(1);
  114. const perPage = ref(10);
  115. const totalCount = ref(0);
  116. const totalPages = ref(0);
  117. const searchQuery = ref("");
  118. const filterStatus = ref("");
  119. // 보이는 페이지 번호 계산
  120. const visiblePages = computed(() => {
  121. const pages = [];
  122. const maxVisible = 5;
  123. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  124. let end = Math.min(totalPages.value, start + maxVisible - 1);
  125. if (end - start < maxVisible - 1) {
  126. start = Math.max(1, end - maxVisible + 1);
  127. }
  128. for (let i = start; i <= end; i++) {
  129. pages.push(i);
  130. }
  131. return pages;
  132. });
  133. // 데이터 로드
  134. const loadFields = async () => {
  135. isLoading.value = true;
  136. const params = {
  137. page: currentPage.value,
  138. per_page: perPage.value,
  139. };
  140. if (searchQuery.value) params.search = searchQuery.value;
  141. if (filterStatus.value) params.status = filterStatus.value;
  142. const { data, error } = await get("/field/list", { params });
  143. if (error) {
  144. console.error("[FieldList] 목록 로드 실패:", error);
  145. fields.value = [];
  146. totalCount.value = 0;
  147. totalPages.value = 0;
  148. } else if (data?.success && data?.data) {
  149. fields.value = data.data.items || [];
  150. totalCount.value = data.data.total || 0;
  151. totalPages.value = data.data.total_pages || 0;
  152. }
  153. isLoading.value = false;
  154. };
  155. // 검색
  156. const onSearch = () => {
  157. currentPage.value = 1;
  158. loadFields();
  159. };
  160. // 검색 초기화
  161. const resetSearch = () => {
  162. searchQuery.value = "";
  163. filterStatus.value = "";
  164. currentPage.value = 1;
  165. loadFields();
  166. };
  167. // 페이지 변경
  168. const changePage = (page) => {
  169. if (page < 1 || page > totalPages.value) return;
  170. currentPage.value = page;
  171. loadFields();
  172. window.scrollTo({ top: 0, behavior: "smooth" });
  173. };
  174. // 이동
  175. const goToCreate = () => router.push("/site-manager/field/create");
  176. const goToDetail = (id) => router.push(`/site-manager/field/detail/${id}`);
  177. const goToEdit = (id) => router.push(`/site-manager/field/edit/${id}`);
  178. // 상태 라벨 / 뱃지 클래스
  179. const getStatusLabel = (status) => (status === "Y" ? "사용중" : "미사용");
  180. const getStatusBadgeClass = (status) =>
  181. status === "Y" ? "admin--badge-active" : "admin--badge-ended";
  182. // 날짜 포맷
  183. const formatDate = (dateString) => {
  184. if (!dateString) return "-";
  185. const date = new Date(dateString.replace(" ", "T"));
  186. if (isNaN(date.getTime())) return dateString;
  187. return date.toLocaleDateString("ko-KR", {
  188. year: "numeric",
  189. month: "2-digit",
  190. day: "2-digit",
  191. });
  192. };
  193. onMounted(() => {
  194. loadFields();
  195. });
  196. </script>