list.vue 6.3 KB

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