index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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-small admin--btn-small-primary" @click="handleSearch">
  18. 검색
  19. </button>
  20. <button class="admin--btn-small admin--btn-small-secondary" @click="handleReset">
  21. 초기화
  22. </button>
  23. </div>
  24. <div class="admin--search-actions">
  25. <button class="admin--btn-small admin--btn-small-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="!popups || popups.length === 0">
  46. <td colspan="7" class="admin--table-empty">
  47. 등록된 팝업이 없습니다.
  48. </td>
  49. </tr>
  50. <tr v-else v-for="(popup, index) in popups" :key="popup.id">
  51. <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  52. <td>
  53. <span class="admin--badge" :class="`admin--badge-${popup.type}`">
  54. {{ popup.type === 'html' ? 'HTML' : '단일페이지' }}
  55. </span>
  56. </td>
  57. <td class="admin--table-title">{{ popup.title }}</td>
  58. <td>{{ formatDate(popup.start_date) }}</td>
  59. <td>{{ formatDate(popup.end_date) }}</td>
  60. <td>
  61. <span class="admin--badge" :class="getStatusClass(popup)">
  62. {{ getStatusText(popup) }}
  63. </span>
  64. </td>
  65. <td>
  66. <div class="admin--table-actions">
  67. <button
  68. class="admin--btn-small admin--btn-small-primary"
  69. @click="goToEdit(popup.id)"
  70. >
  71. 수정
  72. </button>
  73. <button
  74. class="admin--btn-small admin--btn-small-danger"
  75. @click="handleDelete(popup.id)"
  76. >
  77. 삭제
  78. </button>
  79. </div>
  80. </td>
  81. </tr>
  82. </tbody>
  83. </table>
  84. </div>
  85. <!-- 페이지네이션 -->
  86. <div v-if="totalPages > 1" class="admin--pagination">
  87. <button
  88. class="admin--pagination-btn"
  89. :disabled="currentPage === 1"
  90. @click="changePage(currentPage - 1)"
  91. >
  92. 이전
  93. </button>
  94. <button
  95. v-for="page in visiblePages"
  96. :key="page"
  97. class="admin--pagination-btn"
  98. :class="{ 'is-active': page === currentPage }"
  99. @click="changePage(page)"
  100. >
  101. {{ page }}
  102. </button>
  103. <button
  104. class="admin--pagination-btn"
  105. :disabled="currentPage === totalPages"
  106. @click="changePage(currentPage + 1)"
  107. >
  108. 다음
  109. </button>
  110. </div>
  111. </div>
  112. </template>
  113. <script setup>
  114. import { ref, computed, onMounted } from 'vue'
  115. import { useRouter } from 'vue-router'
  116. definePageMeta({
  117. layout: 'admin',
  118. middleware: ['auth']
  119. })
  120. const router = useRouter()
  121. const { get, del } = useApi()
  122. const popups = ref([])
  123. const searchType = ref('title')
  124. const searchKeyword = ref('')
  125. const currentPage = ref(1)
  126. const perPage = ref(10)
  127. const totalCount = ref(0)
  128. const totalPages = ref(0)
  129. // 보이는 페이지 번호 계산
  130. const visiblePages = computed(() => {
  131. const pages = []
  132. const maxVisible = 5
  133. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
  134. let end = Math.min(totalPages.value, start + maxVisible - 1)
  135. if (end - start < maxVisible - 1) {
  136. start = Math.max(1, end - maxVisible + 1)
  137. }
  138. for (let i = start; i <= end; i++) {
  139. pages.push(i)
  140. }
  141. return pages
  142. })
  143. // 데이터 로드
  144. const loadPopups = async () => {
  145. const params = {
  146. page: currentPage.value,
  147. per_page: perPage.value
  148. }
  149. if (searchKeyword.value) {
  150. params.search_type = searchType.value
  151. params.search_keyword = searchKeyword.value
  152. }
  153. const { data, error } = await get('/basic/popup', params)
  154. console.log('[Popup List] API 응답:', { data, error })
  155. // API 응답: { success: true, data: { items, total, page }, message }
  156. if (data?.success && data?.data) {
  157. popups.value = data.data.items || []
  158. totalCount.value = data.data.total || 0
  159. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  160. console.log('[Popup List] 로드 성공:', {
  161. items: popups.value.length,
  162. total: totalCount.value,
  163. pages: totalPages.value
  164. })
  165. } else {
  166. console.log('[Popup List] 데이터 없음 또는 에러')
  167. }
  168. }
  169. // 검색
  170. const handleSearch = () => {
  171. currentPage.value = 1
  172. loadPopups()
  173. }
  174. // 초기화
  175. const handleReset = () => {
  176. searchType.value = 'title'
  177. searchKeyword.value = ''
  178. currentPage.value = 1
  179. loadPopups()
  180. }
  181. // 페이지 변경
  182. const changePage = (page) => {
  183. if (page < 1 || page > totalPages.value) return
  184. currentPage.value = page
  185. loadPopups()
  186. }
  187. // 등록 페이지로 이동
  188. const goToCreate = () => {
  189. router.push('/admin/basic/popup/create')
  190. }
  191. // 수정 페이지로 이동
  192. const goToEdit = (id) => {
  193. router.push(`/admin/basic/popup/edit/${id}`)
  194. }
  195. // 삭제
  196. const handleDelete = async (id) => {
  197. if (!confirm('정말 삭제하시겠습니까?')) return
  198. const { data, error } = await del(`/basic/popup/${id}`)
  199. console.log('[Popup Delete] API 응답:', { data, error })
  200. if (error || !data?.success) {
  201. alert(error?.message || '삭제에 실패했습니다.')
  202. } else {
  203. alert(data.message || '삭제되었습니다.')
  204. loadPopups()
  205. }
  206. }
  207. // 날짜 포맷
  208. const formatDate = (dateString) => {
  209. if (!dateString) return '-'
  210. const date = new Date(dateString)
  211. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}`
  212. }
  213. // 상태 클래스
  214. const getStatusClass = (popup) => {
  215. const now = new Date()
  216. const start = new Date(popup.start_date)
  217. const end = new Date(popup.end_date)
  218. if (now < start) return 'admin--badge-scheduled'
  219. if (now > end) return 'admin--badge-ended'
  220. return 'admin--badge-active'
  221. }
  222. // 상태 텍스트
  223. const getStatusText = (popup) => {
  224. const now = new Date()
  225. const start = new Date(popup.start_date)
  226. const end = new Date(popup.end_date)
  227. if (now < start) return '예정'
  228. if (now > end) return '종료'
  229. return '진행중'
  230. }
  231. onMounted(() => {
  232. loadPopups()
  233. })
  234. </script>
  235. <style scoped>
  236. .admin--search-actions .admin--btn-primary {
  237. background: var(--admin-accent-primary);
  238. color: white;
  239. border-color: var(--admin-accent-primary);
  240. font-weight: 500;
  241. padding: 8px 18px;
  242. font-size: 13px;
  243. border-radius: 8px;
  244. transition: all 0.3s ease;
  245. }
  246. .admin--search-actions .admin--btn-primary:hover {
  247. background: var(--admin-accent-hover);
  248. border-color: var(--admin-accent-hover);
  249. transform: translateY(-1px);
  250. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  251. }
  252. </style>