list.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div class="admin--branch-list">
  3. <!-- 상단 버튼 -->
  4. <div class="admin--search-box">
  5. <div class="admin--search-form"></div>
  6. <div class="admin--search-actions">
  7. <button class="admin--btn admin--btn-primary" @click="goToCreate">
  8. + 지점 등록
  9. </button>
  10. </div>
  11. </div>
  12. <!-- 테이블 -->
  13. <div class="admin--table-wrapper">
  14. <table class="admin--table">
  15. <thead>
  16. <tr>
  17. <th>NO</th>
  18. <th>지점명</th>
  19. <th>대표번호</th>
  20. <th>주소</th>
  21. <th>관리</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. <tr v-if="isLoading">
  26. <td colspan="5" class="admin--table-loading">
  27. 데이터를 불러오는 중...
  28. </td>
  29. </tr>
  30. <tr v-else-if="!branches || branches.length === 0">
  31. <td colspan="5" class="admin--table-empty">
  32. 등록된 지점이 없습니다.
  33. </td>
  34. </tr>
  35. <tr v-else v-for="(branch, index) in branches" :key="branch.id">
  36. <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  37. <td class="admin--table-title">{{ branch.name }}</td>
  38. <td>{{ branch.main_phone }}</td>
  39. <td>{{ branch.address }}</td>
  40. <td>
  41. <div class="admin--table-actions">
  42. <button
  43. class="admin--btn-small admin--btn-small-primary"
  44. @click="goToEdit(branch.id)"
  45. >
  46. 수정
  47. </button>
  48. <button
  49. class="admin--btn-small admin--btn-small-danger"
  50. @click="deleteBranch(branch.id)"
  51. >
  52. 삭제
  53. </button>
  54. </div>
  55. </td>
  56. </tr>
  57. </tbody>
  58. </table>
  59. </div>
  60. <!-- 페이지네이션 -->
  61. <div v-if="totalPages > 1" class="admin--pagination">
  62. <button
  63. class="admin--pagination-btn"
  64. :disabled="currentPage === 1"
  65. @click="changePage(currentPage - 1)"
  66. >
  67. 이전
  68. </button>
  69. <button
  70. v-for="page in visiblePages"
  71. :key="page"
  72. class="admin--pagination-btn"
  73. :class="{ 'is-active': page === currentPage }"
  74. @click="changePage(page)"
  75. >
  76. {{ page }}
  77. </button>
  78. <button
  79. class="admin--pagination-btn"
  80. :disabled="currentPage === totalPages"
  81. @click="changePage(currentPage + 1)"
  82. >
  83. 다음
  84. </button>
  85. </div>
  86. </div>
  87. </template>
  88. <script setup>
  89. import { ref, computed, onMounted } from 'vue'
  90. import { useRouter } from 'vue-router'
  91. definePageMeta({
  92. layout: 'admin',
  93. middleware: ['auth']
  94. })
  95. const router = useRouter()
  96. const { get, del } = useApi()
  97. const isLoading = ref(false)
  98. const branches = ref([])
  99. const currentPage = ref(1)
  100. const perPage = ref(10)
  101. const totalCount = ref(0)
  102. const totalPages = ref(0)
  103. // 보이는 페이지 번호 계산
  104. const visiblePages = computed(() => {
  105. const pages = []
  106. const maxVisible = 5
  107. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
  108. let end = Math.min(totalPages.value, start + maxVisible - 1)
  109. if (end - start < maxVisible - 1) {
  110. start = Math.max(1, end - maxVisible + 1)
  111. }
  112. for (let i = start; i <= end; i++) {
  113. pages.push(i)
  114. }
  115. return pages
  116. })
  117. // 데이터 로드
  118. const loadBranches = async () => {
  119. isLoading.value = true
  120. const params = {
  121. page: currentPage.value,
  122. per_page: perPage.value
  123. }
  124. const { data, error } = await get('/branch/list', params)
  125. console.log('[BranchList] API 응답:', { data, error })
  126. // API 응답: { success: true, data: { items, total }, message }
  127. if (data?.success && data?.data) {
  128. branches.value = data.data.items || []
  129. totalCount.value = data.data.total || 0
  130. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  131. console.log('[BranchList] 로드 성공:', branches.value.length)
  132. }
  133. isLoading.value = false
  134. }
  135. // 페이지 변경
  136. const changePage = (page) => {
  137. if (page < 1 || page > totalPages.value) return
  138. currentPage.value = page
  139. loadBranches()
  140. }
  141. // 지점 등록 페이지로 이동
  142. const goToCreate = () => {
  143. router.push('/admin/branch/create')
  144. }
  145. // 지점 수정 페이지로 이동
  146. const goToEdit = (id) => {
  147. router.push(`/admin/branch/edit/${id}`)
  148. }
  149. // 지점 삭제
  150. const deleteBranch = async (id) => {
  151. if (!confirm('정말 삭제하시겠습니까?')) return
  152. const { data, error } = await del(`/branch/${id}`)
  153. if (error || !data?.success) {
  154. alert(error?.message || data?.message || '삭제에 실패했습니다.')
  155. } else {
  156. alert(data.message || '지점이 삭제되었습니다.')
  157. loadBranches()
  158. }
  159. }
  160. onMounted(() => {
  161. loadBranches()
  162. })
  163. </script>
  164. <style scoped>
  165. .admin--search-actions .admin--btn-primary {
  166. background: var(--admin-accent-primary);
  167. color: white;
  168. border-color: var(--admin-accent-primary);
  169. font-weight: 500;
  170. padding: 8px 18px;
  171. font-size: 13px;
  172. border-radius: 8px;
  173. transition: all 0.3s ease;
  174. }
  175. .admin--search-actions .admin--btn-primary:hover {
  176. background: var(--admin-accent-hover);
  177. border-color: var(--admin-accent-hover);
  178. transform: translateY(-1px);
  179. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  180. }
  181. </style>