index.vue 5.7 KB

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