| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <template>
- <div class="admin--popup-list">
- <!-- 검색 영역 -->
- <div class="admin--search-box">
- <div class="admin--search-form">
- <select v-model="searchType" class="admin--form-select admin--search-select">
- <option value="title">제목</option>
- <option value="status">상태</option>
- </select>
- <input
- v-model="searchKeyword"
- type="text"
- class="admin--form-input admin--search-input"
- placeholder="검색어를 입력하세요"
- @keyup.enter="handleSearch"
- >
- <button class="admin--btn admin--btn-primary" @click="handleSearch">
- 검색
- </button>
- <button class="admin--btn admin--btn-secondary" @click="handleReset">
- 초기화
- </button>
- </div>
- <div class="admin--search-actions">
- <button class="admin--btn admin--btn-primary" @click="goToCreate">
- + 팝업 등록
- </button>
- </div>
- </div>
- <!-- 테이블 -->
- <div class="admin--table-wrapper">
- <table class="admin--table">
- <thead>
- <tr>
- <th>NO</th>
- <th>출력형태</th>
- <th>제목</th>
- <th>시작일</th>
- <th>종료일</th>
- <th>상태</th>
- <th>관리</th>
- </tr>
- </thead>
- <tbody>
- <tr v-if="isLoading">
- <td colspan="7" class="admin--table-loading">
- 데이터를 불러오는 중...
- </td>
- </tr>
- <tr v-else-if="!popups || popups.length === 0">
- <td colspan="7" class="admin--table-empty">
- 등록된 팝업이 없습니다.
- </td>
- </tr>
- <tr v-else v-for="(popup, index) in popups" :key="popup.id">
- <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
- <td>
- <span class="admin--badge" :class="`admin--badge-${popup.type}`">
- {{ popup.type === 'html' ? 'HTML' : '단일페이지' }}
- </span>
- </td>
- <td class="admin--table-title">{{ popup.title }}</td>
- <td>{{ formatDate(popup.start_date) }}</td>
- <td>{{ formatDate(popup.end_date) }}</td>
- <td>
- <span class="admin--badge" :class="getStatusClass(popup)">
- {{ getStatusText(popup) }}
- </span>
- </td>
- <td>
- <div class="admin--table-actions">
- <button
- class="admin--btn-small admin--btn-small-primary"
- @click="goToEdit(popup.id)"
- >
- 수정
- </button>
- <button
- class="admin--btn-small admin--btn-small-danger"
- @click="handleDelete(popup.id)"
- >
- 삭제
- </button>
- </div>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- <!-- 페이지네이션 -->
- <div v-if="totalPages > 1" class="admin--pagination">
- <button
- class="admin--pagination-btn"
- :disabled="currentPage === 1"
- @click="changePage(currentPage - 1)"
- >
- 이전
- </button>
- <button
- v-for="page in visiblePages"
- :key="page"
- class="admin--pagination-btn"
- :class="{ 'is-active': page === currentPage }"
- @click="changePage(page)"
- >
- {{ page }}
- </button>
- <button
- class="admin--pagination-btn"
- :disabled="currentPage === totalPages"
- @click="changePage(currentPage + 1)"
- >
- 다음
- </button>
- </div>
- </div>
- </template>
- <script setup>
- import { ref, computed, onMounted } from 'vue'
- import { useRouter } from 'vue-router'
- definePageMeta({
- layout: 'admin',
- middleware: ['auth']
- })
- const router = useRouter()
- const { get, del } = useApi()
- const isLoading = ref(false)
- const popups = ref([])
- const searchType = ref('title')
- const searchKeyword = ref('')
- const currentPage = ref(1)
- const perPage = ref(10)
- const totalCount = ref(0)
- const totalPages = ref(0)
- // 보이는 페이지 번호 계산
- const visiblePages = computed(() => {
- const pages = []
- const maxVisible = 5
- let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
- let end = Math.min(totalPages.value, start + maxVisible - 1)
- if (end - start < maxVisible - 1) {
- start = Math.max(1, end - maxVisible + 1)
- }
- for (let i = start; i <= end; i++) {
- pages.push(i)
- }
- return pages
- })
- // 데이터 로드
- const loadPopups = async () => {
- isLoading.value = true
- const params = {
- page: currentPage.value,
- per_page: perPage.value
- }
- if (searchKeyword.value) {
- params.search_type = searchType.value
- params.search_keyword = searchKeyword.value
- }
- const { data, error } = await get('/basic/popup', params)
- if (data) {
- popups.value = data.items || []
- totalCount.value = data.total || 0
- totalPages.value = Math.ceil(totalCount.value / perPage.value)
- }
- isLoading.value = false
- }
- // 검색
- const handleSearch = () => {
- currentPage.value = 1
- loadPopups()
- }
- // 초기화
- const handleReset = () => {
- searchType.value = 'title'
- searchKeyword.value = ''
- currentPage.value = 1
- loadPopups()
- }
- // 페이지 변경
- const changePage = (page) => {
- if (page < 1 || page > totalPages.value) return
- currentPage.value = page
- loadPopups()
- }
- // 등록 페이지로 이동
- const goToCreate = () => {
- router.push('/admin/basic/popup/create')
- }
- // 수정 페이지로 이동
- const goToEdit = (id) => {
- router.push(`/admin/basic/popup/edit/${id}`)
- }
- // 삭제
- const handleDelete = async (id) => {
- if (!confirm('정말 삭제하시겠습니까?')) return
- const { error } = await del(`/basic/popup/${id}`)
- if (error) {
- alert('삭제에 실패했습니다.')
- } else {
- alert('삭제되었습니다.')
- loadPopups()
- }
- }
- // 날짜 포맷
- const formatDate = (dateString) => {
- if (!dateString) return '-'
- const date = new Date(dateString)
- return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}`
- }
- // 상태 클래스
- const getStatusClass = (popup) => {
- const now = new Date()
- const start = new Date(popup.start_date)
- const end = new Date(popup.end_date)
- if (now < start) return 'admin--badge-scheduled'
- if (now > end) return 'admin--badge-ended'
- return 'admin--badge-active'
- }
- // 상태 텍스트
- const getStatusText = (popup) => {
- const now = new Date()
- const start = new Date(popup.start_date)
- const end = new Date(popup.end_date)
- if (now < start) return '예정'
- if (now > end) return '종료'
- return '진행중'
- }
- onMounted(() => {
- loadPopups()
- })
- </script>
|