index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <div class="admin--popup-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="title">제목</option>
  8. <option value="status">상태</option>
  9. </select>
  10. <input
  11. v-model="searchKeyword"
  12. type="text"
  13. class="admin--form-input admin--search-input"
  14. placeholder="검색어를 입력하세요"
  15. @keyup.enter="handleSearch"
  16. >
  17. <button class="admin--btn admin--btn-primary" @click="handleSearch">
  18. 검색
  19. </button>
  20. <button class="admin--btn admin--btn-secondary" @click="handleReset">
  21. 초기화
  22. </button>
  23. </div>
  24. <div class="admin--search-actions">
  25. <button class="admin--btn admin--btn-primary" @click="goToCreate">
  26. + 팝업 등록
  27. </button>
  28. </div>
  29. </div>
  30. <!-- 테이블 -->
  31. <div class="admin--table-wrapper">
  32. <table class="admin--table">
  33. <thead>
  34. <tr>
  35. <th>NO</th>
  36. <th>출력형태</th>
  37. <th>제목</th>
  38. <th>시작일</th>
  39. <th>종료일</th>
  40. <th>상태</th>
  41. <th>관리</th>
  42. </tr>
  43. </thead>
  44. <tbody>
  45. <tr v-if="isLoading">
  46. <td colspan="7" class="admin--table-loading">
  47. 데이터를 불러오는 중...
  48. </td>
  49. </tr>
  50. <tr v-else-if="!popups || popups.length === 0">
  51. <td colspan="7" class="admin--table-empty">
  52. 등록된 팝업이 없습니다.
  53. </td>
  54. </tr>
  55. <tr v-else v-for="(popup, index) in popups" :key="popup.id">
  56. <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  57. <td>
  58. <span class="admin--badge" :class="`admin--badge-${popup.type}`">
  59. {{ popup.type === 'html' ? 'HTML' : '단일페이지' }}
  60. </span>
  61. </td>
  62. <td class="admin--table-title">{{ popup.title }}</td>
  63. <td>{{ formatDate(popup.start_date) }}</td>
  64. <td>{{ formatDate(popup.end_date) }}</td>
  65. <td>
  66. <span class="admin--badge" :class="getStatusClass(popup)">
  67. {{ getStatusText(popup) }}
  68. </span>
  69. </td>
  70. <td>
  71. <div class="admin--table-actions">
  72. <button
  73. class="admin--btn-small admin--btn-small-primary"
  74. @click="goToEdit(popup.id)"
  75. >
  76. 수정
  77. </button>
  78. <button
  79. class="admin--btn-small admin--btn-small-danger"
  80. @click="handleDelete(popup.id)"
  81. >
  82. 삭제
  83. </button>
  84. </div>
  85. </td>
  86. </tr>
  87. </tbody>
  88. </table>
  89. </div>
  90. <!-- 페이지네이션 -->
  91. <div v-if="totalPages > 1" class="admin--pagination">
  92. <button
  93. class="admin--pagination-btn"
  94. :disabled="currentPage === 1"
  95. @click="changePage(currentPage - 1)"
  96. >
  97. 이전
  98. </button>
  99. <button
  100. v-for="page in visiblePages"
  101. :key="page"
  102. class="admin--pagination-btn"
  103. :class="{ 'is-active': page === currentPage }"
  104. @click="changePage(page)"
  105. >
  106. {{ page }}
  107. </button>
  108. <button
  109. class="admin--pagination-btn"
  110. :disabled="currentPage === totalPages"
  111. @click="changePage(currentPage + 1)"
  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, del } = useApi()
  127. const isLoading = ref(false)
  128. const popups = ref([])
  129. const searchType = ref('title')
  130. const searchKeyword = ref('')
  131. const currentPage = ref(1)
  132. const perPage = ref(10)
  133. const totalCount = ref(0)
  134. const totalPages = ref(0)
  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 loadPopups = async () => {
  151. isLoading.value = true
  152. const params = {
  153. page: currentPage.value,
  154. per_page: perPage.value
  155. }
  156. if (searchKeyword.value) {
  157. params.search_type = searchType.value
  158. params.search_keyword = searchKeyword.value
  159. }
  160. const { data, error } = await get('/basic/popup', params)
  161. console.log('[Popup List] API 응답:', { data, error })
  162. // API 응답: { success: true, data: { items, total, page }, message }
  163. if (data?.success && data?.data) {
  164. popups.value = data.data.items || []
  165. totalCount.value = data.data.total || 0
  166. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  167. console.log('[Popup List] 로드 성공:', {
  168. items: popups.value.length,
  169. total: totalCount.value,
  170. pages: totalPages.value
  171. })
  172. } else {
  173. console.log('[Popup List] 데이터 없음 또는 에러')
  174. }
  175. isLoading.value = false
  176. }
  177. // 검색
  178. const handleSearch = () => {
  179. currentPage.value = 1
  180. loadPopups()
  181. }
  182. // 초기화
  183. const handleReset = () => {
  184. searchType.value = 'title'
  185. searchKeyword.value = ''
  186. currentPage.value = 1
  187. loadPopups()
  188. }
  189. // 페이지 변경
  190. const changePage = (page) => {
  191. if (page < 1 || page > totalPages.value) return
  192. currentPage.value = page
  193. loadPopups()
  194. }
  195. // 등록 페이지로 이동
  196. const goToCreate = () => {
  197. router.push('/admin/basic/popup/create')
  198. }
  199. // 수정 페이지로 이동
  200. const goToEdit = (id) => {
  201. router.push(`/admin/basic/popup/edit/${id}`)
  202. }
  203. // 삭제
  204. const handleDelete = async (id) => {
  205. if (!confirm('정말 삭제하시겠습니까?')) return
  206. const { data, error } = await del(`/basic/popup/${id}`)
  207. console.log('[Popup Delete] API 응답:', { data, error })
  208. if (error || !data?.success) {
  209. alert(error?.message || '삭제에 실패했습니다.')
  210. } else {
  211. alert(data.message || '삭제되었습니다.')
  212. loadPopups()
  213. }
  214. }
  215. // 날짜 포맷
  216. const formatDate = (dateString) => {
  217. if (!dateString) return '-'
  218. const date = new Date(dateString)
  219. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}`
  220. }
  221. // 상태 클래스
  222. const getStatusClass = (popup) => {
  223. const now = new Date()
  224. const start = new Date(popup.start_date)
  225. const end = new Date(popup.end_date)
  226. if (now < start) return 'admin--badge-scheduled'
  227. if (now > end) return 'admin--badge-ended'
  228. return 'admin--badge-active'
  229. }
  230. // 상태 텍스트
  231. const getStatusText = (popup) => {
  232. const now = new Date()
  233. const start = new Date(popup.start_date)
  234. const end = new Date(popup.end_date)
  235. if (now < start) return '예정'
  236. if (now > end) return '종료'
  237. return '진행중'
  238. }
  239. onMounted(() => {
  240. loadPopups()
  241. })
  242. </script>
  243. <style scoped>
  244. .admin--search-actions .admin--btn-primary {
  245. background: var(--admin-accent-primary);
  246. color: white;
  247. border-color: var(--admin-accent-primary);
  248. font-weight: 500;
  249. padding: 8px 18px;
  250. font-size: 13px;
  251. border-radius: 8px;
  252. transition: all 0.3s ease;
  253. }
  254. .admin--search-actions .admin--btn-primary:hover {
  255. background: var(--admin-accent-hover);
  256. border-color: var(--admin-accent-hover);
  257. transform: translateY(-1px);
  258. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  259. }
  260. </style>