index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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>{{ formatDateTime(popup.start_date, popup.start_time) }}</td>
  59. <td>{{ formatDateTime(popup.end_date, popup.end_time) }}</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(1)"
  91. title="처음"
  92. >
  93. </button>
  94. <button
  95. class="admin--pagination-btn"
  96. :disabled="currentPage === 1"
  97. @click="changePage(currentPage - 1)"
  98. title="이전"
  99. >
  100. </button>
  101. <button
  102. v-for="page in visiblePages"
  103. :key="page"
  104. class="admin--pagination-btn"
  105. :class="{ 'is-active': page === currentPage }"
  106. @click="changePage(page)"
  107. >
  108. {{ page }}
  109. </button>
  110. <button
  111. class="admin--pagination-btn"
  112. :disabled="currentPage === totalPages"
  113. @click="changePage(currentPage + 1)"
  114. title="다음"
  115. >
  116. </button>
  117. <button
  118. class="admin--pagination-btn"
  119. :disabled="currentPage === totalPages"
  120. @click="changePage(totalPages)"
  121. title="끝"
  122. >
  123. </button>
  124. </div>
  125. </div>
  126. </template>
  127. <script setup>
  128. import { ref, computed, onMounted } from 'vue'
  129. import { useRouter } from 'vue-router'
  130. definePageMeta({
  131. layout: 'admin',
  132. middleware: ['auth']
  133. })
  134. const router = useRouter()
  135. const { get, del } = useApi()
  136. const popups = ref([])
  137. const searchType = ref('title')
  138. const searchKeyword = ref('')
  139. const currentPage = ref(1)
  140. const perPage = ref(10)
  141. const totalCount = ref(0)
  142. const totalPages = ref(0)
  143. // 보이는 페이지 번호 계산
  144. const visiblePages = computed(() => {
  145. const pages = []
  146. const maxVisible = 5
  147. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
  148. let end = Math.min(totalPages.value, start + maxVisible - 1)
  149. if (end - start < maxVisible - 1) {
  150. start = Math.max(1, end - maxVisible + 1)
  151. }
  152. for (let i = start; i <= end; i++) {
  153. pages.push(i)
  154. }
  155. return pages
  156. })
  157. // 데이터 로드
  158. const loadPopups = async () => {
  159. const params = {
  160. page: currentPage.value,
  161. per_page: perPage.value
  162. }
  163. if (searchKeyword.value) {
  164. params.search_type = searchType.value
  165. params.search_keyword = searchKeyword.value
  166. }
  167. const { data, error } = await get('/basic/popup', { params })
  168. console.log('[Popup List] API 응답:', { data, error })
  169. // API 응답: { success: true, data: { items, total, page }, message }
  170. if (data?.success && data?.data) {
  171. popups.value = data.data.items || []
  172. totalCount.value = data.data.total || 0
  173. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  174. console.log('[Popup List] 로드 성공:', {
  175. items: popups.value.length,
  176. total: totalCount.value,
  177. pages: totalPages.value
  178. })
  179. } else {
  180. console.log('[Popup List] 데이터 없음 또는 에러')
  181. }
  182. }
  183. // 검색
  184. const handleSearch = () => {
  185. currentPage.value = 1
  186. loadPopups()
  187. }
  188. // 초기화
  189. const handleReset = () => {
  190. searchType.value = 'title'
  191. searchKeyword.value = ''
  192. currentPage.value = 1
  193. loadPopups()
  194. }
  195. // 페이지 변경
  196. const changePage = (page) => {
  197. if (page < 1 || page > totalPages.value) return
  198. currentPage.value = page
  199. loadPopups()
  200. window.scrollTo({ top: 0, behavior: 'smooth' })
  201. }
  202. // 등록 페이지로 이동
  203. const goToCreate = () => {
  204. router.push('/site-manager/basic/popup/create')
  205. }
  206. // 수정 페이지로 이동
  207. const goToEdit = (id) => {
  208. router.push(`/site-manager/basic/popup/edit/${id}`)
  209. }
  210. // 삭제
  211. const handleDelete = async (id) => {
  212. if (!confirm('정말 삭제하시겠습니까?')) return
  213. const { data, error } = await del(`/basic/popup/${id}`)
  214. console.log('[Popup Delete] API 응답:', { data, error })
  215. if (error || !data?.success) {
  216. alert(error?.message || '삭제에 실패했습니다.')
  217. } else {
  218. alert(data.message || '삭제되었습니다.')
  219. loadPopups()
  220. }
  221. }
  222. // 날짜 포맷
  223. const formatDate = (dateString) => {
  224. if (!dateString) return '-'
  225. const date = new Date(dateString)
  226. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}`
  227. }
  228. // 날짜+시간 포맷 ("HH:MM:SS" → "오전/오후 H:MM")
  229. const formatDateTime = (dateString, timeString) => {
  230. const d = formatDate(dateString)
  231. if (!timeString) return d
  232. const [hStr, mStr] = String(timeString).split(':')
  233. let h = Number(hStr)
  234. const m = Number(mStr || 0)
  235. if (isNaN(h)) return d
  236. const period = h >= 12 ? '오후' : '오전'
  237. const hour12 = h % 12 === 0 ? 12 : h % 12
  238. return `${d} ${period} ${hour12}:${String(m).padStart(2, '0')}`
  239. }
  240. // 팝업 시작/종료 Date 계산 (시간 null 처리 포함)
  241. const buildStart = (popup) => new Date(`${popup.start_date}T${popup.start_time || '00:00:00'}`)
  242. const buildEnd = (popup) => new Date(`${popup.end_date}T${popup.end_time || '23:59:59'}`)
  243. // 상태 클래스
  244. const getStatusClass = (popup) => {
  245. const now = new Date()
  246. const start = buildStart(popup)
  247. const end = buildEnd(popup)
  248. if (now < start) return 'admin--badge-scheduled'
  249. if (now > end) return 'admin--badge-ended'
  250. return 'admin--badge-active'
  251. }
  252. // 상태 텍스트
  253. const getStatusText = (popup) => {
  254. const now = new Date()
  255. const start = buildStart(popup)
  256. const end = buildEnd(popup)
  257. if (now < start) return '예정'
  258. if (now > end) return '종료'
  259. return '진행중'
  260. }
  261. onMounted(() => {
  262. loadPopups()
  263. })
  264. </script>
  265. <style scoped>
  266. .admin--search-actions .admin--btn-primary {
  267. background: var(--admin-accent-primary);
  268. color: white;
  269. border-color: var(--admin-accent-primary);
  270. font-weight: 500;
  271. padding: 8px 18px;
  272. font-size: 13px;
  273. border-radius: 8px;
  274. transition: all 0.3s ease;
  275. }
  276. .admin--search-actions .admin--btn-primary:hover {
  277. background: var(--admin-accent-hover);
  278. border-color: var(--admin-accent-hover);
  279. transform: translateY(-1px);
  280. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  281. }
  282. </style>