list.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. v-if="totalPages > 2"
  76. class="admin--pagination-btn"
  77. :disabled="currentPage === 1"
  78. @click="changePage(1)"
  79. title="처음"
  80. >
  81. ◀◀
  82. </button>
  83. <button
  84. class="admin--pagination-btn"
  85. :disabled="currentPage === 1"
  86. @click="changePage(currentPage - 1)"
  87. title="이전"
  88. >
  89. </button>
  90. <button
  91. v-for="page in visiblePages"
  92. :key="page"
  93. class="admin--pagination-btn"
  94. :class="{ 'is-active': page === currentPage }"
  95. @click="changePage(page)"
  96. >
  97. {{ page }}
  98. </button>
  99. <button
  100. class="admin--pagination-btn"
  101. :disabled="currentPage === totalPages"
  102. @click="changePage(currentPage + 1)"
  103. title="다음"
  104. >
  105. </button>
  106. <button
  107. v-if="totalPages > 2"
  108. class="admin--pagination-btn"
  109. :disabled="currentPage === totalPages"
  110. @click="changePage(totalPages)"
  111. title="끝"
  112. >
  113. ▶▶
  114. </button>
  115. </div>
  116. </div>
  117. </template>
  118. <script setup>
  119. import { ref, computed, onMounted } from "vue";
  120. import { useRouter } from "vue-router";
  121. definePageMeta({
  122. layout: "admin",
  123. middleware: ["auth"],
  124. });
  125. const router = useRouter();
  126. const { get } = useApi();
  127. const isLoading = ref(false);
  128. const fields = ref([]);
  129. const currentPage = ref(1);
  130. const perPage = ref(10);
  131. const totalCount = ref(0);
  132. const totalPages = ref(0);
  133. const searchQuery = ref("");
  134. const filterStatus = ref("");
  135. // 보이는 페이지 번호 계산
  136. const visiblePages = computed(() => {
  137. const pages = [];
  138. const maxVisible = 5;
  139. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  140. let end = Math.min(totalPages.value, start + maxVisible - 1);
  141. if (end - start < maxVisible - 1) {
  142. start = Math.max(1, end - maxVisible + 1);
  143. }
  144. for (let i = start; i <= end; i++) {
  145. pages.push(i);
  146. }
  147. return pages;
  148. });
  149. // 데이터 로드
  150. const loadFields = async () => {
  151. isLoading.value = true;
  152. const params = {
  153. page: currentPage.value,
  154. per_page: perPage.value,
  155. };
  156. if (searchQuery.value) params.search = searchQuery.value;
  157. if (filterStatus.value) params.status = filterStatus.value;
  158. const { data, error } = await get("/field/list", { params });
  159. if (error) {
  160. console.error("[FieldList] 목록 로드 실패:", error);
  161. fields.value = [];
  162. totalCount.value = 0;
  163. totalPages.value = 0;
  164. } else if (data?.success && data?.data) {
  165. fields.value = data.data.items || [];
  166. totalCount.value = data.data.total || 0;
  167. totalPages.value = data.data.total_pages || 0;
  168. }
  169. isLoading.value = false;
  170. };
  171. // 검색
  172. const onSearch = () => {
  173. currentPage.value = 1;
  174. loadFields();
  175. };
  176. // 검색 초기화
  177. const resetSearch = () => {
  178. searchQuery.value = "";
  179. filterStatus.value = "";
  180. currentPage.value = 1;
  181. loadFields();
  182. };
  183. // 페이지 변경
  184. const changePage = (page) => {
  185. if (page < 1 || page > totalPages.value) return;
  186. currentPage.value = page;
  187. loadFields();
  188. window.scrollTo({ top: 0, behavior: "smooth" });
  189. };
  190. // 이동
  191. const goToCreate = () => router.push("/site-manager/field/create");
  192. const goToDetail = (id) => router.push(`/site-manager/field/detail/${id}`);
  193. const goToEdit = (id) => router.push(`/site-manager/field/edit/${id}`);
  194. // 상태 라벨 / 뱃지 클래스
  195. const getStatusLabel = (status) => (status === "Y" ? "사용중" : "미사용");
  196. const getStatusBadgeClass = (status) =>
  197. status === "Y" ? "admin--badge-active" : "admin--badge-ended";
  198. // 날짜 포맷
  199. const formatDate = (dateString) => {
  200. if (!dateString) return "-";
  201. const date = new Date(dateString.replace(" ", "T"));
  202. if (isNaN(date.getTime())) return dateString;
  203. return date.toLocaleDateString("ko-KR", {
  204. year: "numeric",
  205. month: "2-digit",
  206. day: "2-digit",
  207. });
  208. };
  209. onMounted(() => {
  210. loadFields();
  211. });
  212. </script>