list.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <div class="admin--field-list">
  3. <!-- 상단 검색/액션 영역 -->
  4. <div class="admin--search-box type2">
  5. <div class="admin--search--inner--box">
  6. <div class="admin--search-form">
  7. <select v-model="filterStatus" @change="onSearch" class="admin--form-select admin--search-select">
  8. <option value="">전체</option>
  9. <option value="Y">모집중</option>
  10. <option value="N">진행중</option>
  11. <option value="">종료</option>
  12. </select>
  13. <input
  14. v-model="searchQuery"
  15. type="text"
  16. placeholder="챌린지명으로 검색"
  17. @keyup.enter="onSearch"
  18. class="admin--form-input admin--search-input"
  19. />
  20. <button @click="onSearch" class="admin--btn-small admin--btn-small-primary">검색</button>
  21. <button @click="resetSearch" class="admin--btn-small admin--btn-small-secondary">초기화</button>
  22. </div>
  23. <div class="admin--search-actions">
  24. <button class="admin--btn-add" @click="goToCreate">+ 새 챌린지 등록</button>
  25. </div>
  26. </div>
  27. <div class="admin--search--inner--box">
  28. <div class="admin--search-form">
  29. <DatePicker v-model="startDate" placeholder="📅 YYYY-MM-DD" />
  30. <span class="admin--date-separator">-</span>
  31. <DatePicker v-model="endDate" placeholder="📅 YYYY-MM-DD" />
  32. <div class="admin--quick-range">
  33. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('today')">오늘</button>
  34. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('7d')">7일</button>
  35. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('15d')">15일</button>
  36. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('1m')">1개월</button>
  37. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('3m')">3개월</button>
  38. <button type="button" class="admin--btn-small admin--btn-small-secondary range--btn" @click="setRange('1y')">1년</button>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- 테이블 -->
  44. <p class="admin--table--count">전체 42ㆍ모집중 12ㆍ진행중 19ㆍ종료 12</p>
  45. <div class="admin--table-wrapper">
  46. <table class="admin--table fishing--table">
  47. <thead>
  48. <tr>
  49. <th style="width: 40px;">번호</th>
  50. <th>챌린지명</th>
  51. <th style="width: 100px;">상태</th>
  52. <th style="width: 100px;">라운드</th>
  53. <th style="width: 100px;">모집/전체</th>
  54. <th style="">기간</th>
  55. <th style="width: 120px;">액션</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. <tr v-if="isLoading">
  60. <td colspan="7" class="admin--table-loading">데이터를 불러오는 중...</td>
  61. </tr>
  62. <tr v-else-if="!spots || spots.length === 0">
  63. <td colspan="7" class="admin--table-empty">등록된 챌린지가 없습니다.</td>
  64. </tr>
  65. <tr
  66. v-else
  67. v-for="(item, index) in spots"
  68. :key="item.id"
  69. class="admin--table-row-clickable"
  70. @click="goToDetail(item.id)"
  71. >
  72. <td class="date">{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  73. <td>{{ item.field_name || "-" }}</td>
  74. <td>{{ item.area_name || "-" }}</td>
  75. <td class="admin--table-title">{{ item.name }}</td>
  76. <td>{{ item.address || "-" }}</td>
  77. <td>{{ item.fish_species || "-" }}</td>
  78. <td>{{ item.operating_hours || "-" }}</td>
  79. <td>
  80. <span :class="['admin--badge', item.partnership_YN === 'Y' ? 'admin--badge-active' : 'admin--badge-ended']">
  81. {{ item.partnership_YN === "Y" ? "제휴" : "비제휴" }}
  82. </span>
  83. </td>
  84. <td>
  85. <span :class="['admin--badge', getStatusBadgeClass(item.status_YN)]">
  86. {{ getStatusLabel(item.status_YN) }}
  87. </span>
  88. </td>
  89. <td class="date">{{ formatDate(item.created_at) }}</td>
  90. <td>
  91. <div class="admin--table-actions">
  92. <button class="admin--btn-small admin--btn-blue" @click.stop="goToEdit(item.id)">
  93. 수정
  94. </button>
  95. </div>
  96. </td>
  97. </tr>
  98. </tbody>
  99. </table>
  100. </div>
  101. <!-- 페이지네이션 -->
  102. <div v-if="totalPages > 1" class="admin--pagination">
  103. <button
  104. v-if="totalPages > 2"
  105. class="admin--pagination-btn"
  106. :disabled="currentPage === 1"
  107. @click="changePage(1)"
  108. title="처음"
  109. >
  110. ◀◀
  111. </button>
  112. <button
  113. class="admin--pagination-btn"
  114. :disabled="currentPage === 1"
  115. @click="changePage(currentPage - 1)"
  116. title="이전"
  117. >
  118. </button>
  119. <button
  120. v-for="page in visiblePages"
  121. :key="page"
  122. class="admin--pagination-btn"
  123. :class="{ 'is-active': page === currentPage }"
  124. @click="changePage(page)"
  125. >
  126. {{ page }}
  127. </button>
  128. <button
  129. class="admin--pagination-btn"
  130. :disabled="currentPage === totalPages"
  131. @click="changePage(currentPage + 1)"
  132. title="다음"
  133. >
  134. </button>
  135. <button
  136. v-if="totalPages > 2"
  137. class="admin--pagination-btn"
  138. :disabled="currentPage === totalPages"
  139. @click="changePage(totalPages)"
  140. title="끝"
  141. >
  142. ▶▶
  143. </button>
  144. </div>
  145. </div>
  146. </template>
  147. <script setup>
  148. import { ref, computed, onMounted } from "vue";
  149. import { useRouter } from "vue-router";
  150. import DatePicker from "~/components/admin/DatePicker.vue";
  151. definePageMeta({
  152. layout: "admin",
  153. middleware: ["auth"],
  154. });
  155. const router = useRouter();
  156. const { get } = useApi();
  157. const isLoading = ref(false);
  158. const spots = ref([]);
  159. const currentPage = ref(1);
  160. const perPage = ref(10);
  161. const totalCount = ref(0);
  162. const totalPages = ref(0);
  163. const searchField = ref(""); // '', field, area, name
  164. const searchQuery = ref("");
  165. const filterPartnership = ref(""); // '', Y, N
  166. const filterStatus = ref(""); // '', Y, N
  167. const startDate = ref(""); // YYYY-MM-DD
  168. const endDate = ref(""); // YYYY-MM-DD
  169. // YYYY-MM-DD 포맷터
  170. const toYMD = (d) => {
  171. const y = d.getFullYear();
  172. const m = String(d.getMonth() + 1).padStart(2, "0");
  173. const day = String(d.getDate()).padStart(2, "0");
  174. return `${y}-${m}-${day}`;
  175. };
  176. // 빠른 기간 선택 (오늘 기준)
  177. const setRange = (kind) => {
  178. const today = new Date();
  179. const end = toYMD(today);
  180. const startDt = new Date();
  181. switch (kind) {
  182. case "today":
  183. break;
  184. case "7d":
  185. startDt.setDate(startDt.getDate() - 7);
  186. break;
  187. case "15d":
  188. startDt.setDate(startDt.getDate() - 15);
  189. break;
  190. case "1m":
  191. startDt.setMonth(startDt.getMonth() - 1);
  192. break;
  193. case "3m":
  194. startDt.setMonth(startDt.getMonth() - 3);
  195. break;
  196. case "1y":
  197. startDt.setFullYear(startDt.getFullYear() - 1);
  198. break;
  199. }
  200. startDate.value = toYMD(startDt);
  201. endDate.value = end;
  202. onSearch();
  203. };
  204. // 보이는 페이지 번호 계산
  205. const visiblePages = computed(() => {
  206. const pages = [];
  207. const maxVisible = 5;
  208. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  209. let end = Math.min(totalPages.value, start + maxVisible - 1);
  210. if (end - start < maxVisible - 1) {
  211. start = Math.max(1, end - maxVisible + 1);
  212. }
  213. for (let i = start; i <= end; i++) {
  214. pages.push(i);
  215. }
  216. return pages;
  217. });
  218. // 데이터 로드
  219. const loadSpots = async () => {
  220. isLoading.value = true;
  221. const params = {
  222. page: currentPage.value,
  223. per_page: perPage.value,
  224. };
  225. if (searchQuery.value) {
  226. params.search = searchQuery.value;
  227. if (searchField.value) params.search_field = searchField.value;
  228. }
  229. if (filterPartnership.value) params.partnership = filterPartnership.value;
  230. if (filterStatus.value) params.status = filterStatus.value;
  231. if (startDate.value) params.start_date = startDate.value;
  232. if (endDate.value) params.end_date = endDate.value;
  233. const { data, error } = await get("/challenge/list", { params });
  234. if (error) {
  235. console.error("[ChallengeList] 목록 로드 실패:", error);
  236. spots.value = [];
  237. totalCount.value = 0;
  238. totalPages.value = 0;
  239. } else if (data?.success && data?.data) {
  240. spots.value = data.data.items || [];
  241. totalCount.value = data.data.total || 0;
  242. totalPages.value = data.data.total_pages || 0;
  243. }
  244. isLoading.value = false;
  245. };
  246. // 검색
  247. const onSearch = () => {
  248. currentPage.value = 1;
  249. loadSpots();
  250. };
  251. // 검색 초기화
  252. const resetSearch = () => {
  253. searchField.value = "";
  254. searchQuery.value = "";
  255. filterPartnership.value = "";
  256. filterStatus.value = "";
  257. startDate.value = "";
  258. endDate.value = "";
  259. currentPage.value = 1;
  260. loadSpots();
  261. };
  262. // 페이지 변경
  263. const changePage = (page) => {
  264. if (page < 1 || page > totalPages.value) return;
  265. currentPage.value = page;
  266. loadSpots();
  267. window.scrollTo({ top: 0, behavior: "smooth" });
  268. };
  269. // 이동
  270. const goToCreate = () => router.push("/site-manager/challenge/create");
  271. const goToDetail = (id) => router.push(`/site-manager/challenge/detail/${id}`);
  272. const goToEdit = (id) => router.push(`/site-manager/challenge/edit/${id}`);
  273. // 상태 라벨 / 뱃지 클래스
  274. const getStatusLabel = (status) => (status === "Y" ? "사용중" : "미사용");
  275. const getStatusBadgeClass = (status) =>
  276. status === "Y" ? "admin--badge-active" : "admin--badge-ended";
  277. // 날짜 포맷
  278. const formatDate = (dateString) => {
  279. if (!dateString) return "-";
  280. const date = new Date(dateString.replace(" ", "T"));
  281. if (isNaN(date.getTime())) return dateString;
  282. return date.toLocaleDateString("ko-KR", {
  283. year: "numeric",
  284. month: "2-digit",
  285. day: "2-digit",
  286. });
  287. };
  288. onMounted(() => {
  289. loadSpots();
  290. });
  291. </script>