index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div class="admin--advisor-list">
  3. <!-- 검색 영역 -->
  4. <div class="admin--search-box">
  5. <div class="admin--search-form">
  6. <select v-model="searchType" class="admin--form-select admin--search-select">
  7. <option value="all">전체</option>
  8. <option value="name">이름</option>
  9. <option value="service_center">서비스센터</option>
  10. </select>
  11. <input
  12. v-model="searchKeyword"
  13. type="text"
  14. class="admin--form-input admin--search-input"
  15. placeholder="검색어를 입력하세요"
  16. @keyup.enter="handleSearch"
  17. />
  18. <button class="admin--btn admin--btn-primary" @click="handleSearch">검색</button>
  19. <button class="admin--btn admin--btn-secondary" @click="handleReset">
  20. 초기화
  21. </button>
  22. </div>
  23. <div class="admin--search-actions">
  24. <button class="admin--btn admin--btn-primary" @click="goToCreate">
  25. + 어드바이저 등록
  26. </button>
  27. </div>
  28. </div>
  29. <!-- 테이블 -->
  30. <div class="admin--table-wrapper">
  31. <table class="admin--table">
  32. <thead>
  33. <tr>
  34. <th>NO</th>
  35. <th>사진</th>
  36. <th>서비스센터</th>
  37. <th>이름</th>
  38. <th>대표번호</th>
  39. <th>핸드폰</th>
  40. <th>관리</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <tr v-if="isLoading">
  45. <td colspan="7" class="admin--table-loading">데이터를 불러오는 중...</td>
  46. </tr>
  47. <tr v-else-if="!advisors || advisors.length === 0">
  48. <td colspan="7" class="admin--table-empty">등록된 어드바이저가 없습니다.</td>
  49. </tr>
  50. <tr v-else v-for="(advisor, index) in advisors" :key="advisor.id">
  51. <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  52. <td>
  53. <div class="admin--table-photo">
  54. <img
  55. v-if="advisor.photo_url"
  56. :src="advisor.photo_url"
  57. :alt="advisor.name"
  58. />
  59. <div v-else class="admin--table-photo-empty">사진없음</div>
  60. </div>
  61. </td>
  62. <td>{{ advisor.service_center_name }}</td>
  63. <td class="admin--table-title">{{ advisor.name }}</td>
  64. <td>{{ advisor.main_phone }}</td>
  65. <td>{{ advisor.mobile }}</td>
  66. <td>
  67. <div class="admin--table-actions">
  68. <button
  69. class="admin--btn-small admin--btn-small-primary"
  70. @click="goToEdit(advisor.id)"
  71. >
  72. 수정
  73. </button>
  74. <button
  75. class="admin--btn-small admin--btn-small-danger"
  76. @click="handleDelete(advisor.id)"
  77. >
  78. 삭제
  79. </button>
  80. </div>
  81. </td>
  82. </tr>
  83. </tbody>
  84. </table>
  85. </div>
  86. <!-- 페이지네이션 -->
  87. <div v-if="totalPages > 1" class="admin--pagination">
  88. <button
  89. class="admin--pagination-btn"
  90. :disabled="currentPage === 1"
  91. @click="changePage(currentPage - 1)"
  92. >
  93. 이전
  94. </button>
  95. <button
  96. v-for="page in visiblePages"
  97. :key="page"
  98. class="admin--pagination-btn"
  99. :class="{ 'is-active': page === currentPage }"
  100. @click="changePage(page)"
  101. >
  102. {{ page }}
  103. </button>
  104. <button
  105. class="admin--pagination-btn"
  106. :disabled="currentPage === totalPages"
  107. @click="changePage(currentPage + 1)"
  108. >
  109. 다음
  110. </button>
  111. </div>
  112. </div>
  113. </template>
  114. <script setup>
  115. import { ref, computed, onMounted } from "vue";
  116. import { useRouter } from "vue-router";
  117. definePageMeta({
  118. layout: "admin",
  119. middleware: ["auth"],
  120. });
  121. const router = useRouter();
  122. const { get, del } = useApi();
  123. const isLoading = ref(false);
  124. const advisors = ref([]);
  125. const searchType = ref("all");
  126. const searchKeyword = ref("");
  127. const currentPage = ref(1);
  128. const perPage = ref(10);
  129. const totalCount = ref(0);
  130. const totalPages = ref(0);
  131. const visiblePages = computed(() => {
  132. const pages = [];
  133. const maxVisible = 5;
  134. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  135. let end = Math.min(totalPages.value, start + maxVisible - 1);
  136. if (end - start < maxVisible - 1) {
  137. start = Math.max(1, end - maxVisible + 1);
  138. }
  139. for (let i = start; i <= end; i++) {
  140. pages.push(i);
  141. }
  142. return pages;
  143. });
  144. const loadAdvisors = async () => {
  145. isLoading.value = true;
  146. const params = {
  147. page: currentPage.value,
  148. per_page: perPage.value,
  149. };
  150. if (searchKeyword.value) {
  151. params.search_type = searchType.value;
  152. params.search_keyword = searchKeyword.value;
  153. }
  154. const { data, error } = await get("/staff/advisor", params);
  155. console.log("[Advisor] API 응답:", { data, error });
  156. // API 응답: { success: true, data: { items, total }, message }
  157. if (data?.success && data?.data) {
  158. advisors.value = data.data.items || [];
  159. totalCount.value = data.data.total || 0;
  160. totalPages.value = Math.ceil(totalCount.value / perPage.value);
  161. console.log("[Advisor] 로드 성공:", advisors.value.length);
  162. }
  163. isLoading.value = false;
  164. };
  165. const handleSearch = () => {
  166. currentPage.value = 1;
  167. loadAdvisors();
  168. };
  169. const handleReset = () => {
  170. searchType.value = "all";
  171. searchKeyword.value = "";
  172. currentPage.value = 1;
  173. loadAdvisors();
  174. };
  175. const changePage = (page) => {
  176. if (page < 1 || page > totalPages.value) return;
  177. currentPage.value = page;
  178. loadAdvisors();
  179. };
  180. const goToCreate = () => {
  181. router.push("/admin/staff/advisor/create");
  182. };
  183. const goToEdit = (id) => {
  184. router.push(`/admin/staff/advisor/edit/${id}`);
  185. };
  186. const handleDelete = async (id) => {
  187. if (!confirm("정말 삭제하시겠습니까?")) return;
  188. const { error } = await del(`/staff/advisor/${id}`);
  189. if (error) {
  190. alert("삭제에 실패했습니다.");
  191. } else {
  192. alert("삭제되었습니다.");
  193. loadAdvisors();
  194. }
  195. };
  196. onMounted(() => {
  197. loadAdvisors();
  198. });
  199. </script>