index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="admin--board-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="name">이름</option>
  9. <option value="content">내용</option>
  10. </select>
  11. <input
  12. v-model="searchKeyword"
  13. type="text"
  14. class="admin--form-input admin--search-input"
  15. placeholder="검색어를 입력하세요"
  16. @keyup.enter="handleSearch"
  17. >
  18. <button class="admin--btn admin--btn-primary" @click="handleSearch">검색</button>
  19. <button class="admin--btn admin--btn-secondary" @click="handleReset">초기화</button>
  20. </div>
  21. <div class="admin--search-actions">
  22. <button class="admin--btn admin--btn-primary" @click="goToCreate">+ 등록</button>
  23. </div>
  24. </div>
  25. <!-- 테이블 -->
  26. <div class="admin--table-wrapper">
  27. <table class="admin--table">
  28. <thead>
  29. <tr>
  30. <th>NO</th>
  31. <th>제목</th>
  32. <th>이름</th>
  33. <th>등록일</th>
  34. <th>조회</th>
  35. <th>관리</th>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. <tr v-if="isLoading">
  40. <td colspan="6" class="admin--table-loading">데이터를 불러오는 중...</td>
  41. </tr>
  42. <tr v-else-if="!posts || posts.length === 0">
  43. <td colspan="6" class="admin--table-empty">등록된 게시물이 없습니다.</td>
  44. </tr>
  45. <tr v-else v-for="(post, index) in posts" :key="post.id">
  46. <td>{{ post.is_notice ? '공지' : totalCount - ((currentPage - 1) * perPage + index) }}</td>
  47. <td class="admin--table-title">{{ post.title }}</td>
  48. <td>{{ post.name }}</td>
  49. <td>{{ formatDate(post.created_at) }}</td>
  50. <td>{{ post.views }}</td>
  51. <td>
  52. <div class="admin--table-actions">
  53. <button class="admin--btn-small admin--btn-small-primary" @click="goToEdit(post.id)">수정</button>
  54. <button class="admin--btn-small admin--btn-small-danger" @click="handleDelete(post.id)">삭제</button>
  55. </div>
  56. </td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. </div>
  61. <!-- 페이지네이션 -->
  62. <div v-if="totalPages > 1" class="admin--pagination">
  63. <button class="admin--pagination-btn" :disabled="currentPage === 1" @click="changePage(currentPage - 1)">이전</button>
  64. <button v-for="page in visiblePages" :key="page" class="admin--pagination-btn" :class="{ 'is-active': page === currentPage }" @click="changePage(page)">{{ page }}</button>
  65. <button class="admin--pagination-btn" :disabled="currentPage === totalPages" @click="changePage(currentPage + 1)">다음</button>
  66. </div>
  67. </div>
  68. </template>
  69. <script setup>
  70. import { ref, computed, onMounted } from 'vue'
  71. import { useRouter } from 'vue-router'
  72. definePageMeta({ layout: 'admin', middleware: ['auth'] })
  73. const router = useRouter()
  74. const { get, del } = useApi()
  75. const isLoading = ref(false)
  76. const posts = ref([])
  77. const searchType = ref('title')
  78. const searchKeyword = ref('')
  79. const currentPage = ref(1)
  80. const perPage = ref(10)
  81. const totalCount = ref(0)
  82. const totalPages = ref(0)
  83. const visiblePages = computed(() => {
  84. const pages = []
  85. const maxVisible = 5
  86. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
  87. let end = Math.min(totalPages.value, start + maxVisible - 1)
  88. if (end - start < maxVisible - 1) start = Math.max(1, end - maxVisible + 1)
  89. for (let i = start; i <= end; i++) pages.push(i)
  90. return pages
  91. })
  92. const loadPosts = async () => {
  93. isLoading.value = true
  94. const params = { page: currentPage.value, per_page: perPage.value }
  95. if (searchKeyword.value) {
  96. params.search_type = searchType.value
  97. params.search_keyword = searchKeyword.value
  98. }
  99. const { data } = await get('/board/ir', params)
  100. if (data) {
  101. posts.value = data.items || []
  102. totalCount.value = data.total || 0
  103. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  104. }
  105. isLoading.value = false
  106. }
  107. const handleSearch = () => { currentPage.value = 1; loadPosts() }
  108. const handleReset = () => { searchType.value = 'title'; searchKeyword.value = ''; currentPage.value = 1; loadPosts() }
  109. const changePage = (page) => { if (page < 1 || page > totalPages.value) return; currentPage.value = page; loadPosts() }
  110. const goToCreate = () => router.push('/admin/board/ir/create')
  111. const goToEdit = (id) => router.push(`/admin/board/ir/edit/${id}`)
  112. const handleDelete = async (id) => {
  113. if (!confirm('정말 삭제하시겠습니까?')) return
  114. const { error } = await del(`/board/ir/${id}`)
  115. if (error) alert('삭제에 실패했습니다.')
  116. else { alert('삭제되었습니다.'); loadPosts() }
  117. }
  118. const formatDate = (dateString) => {
  119. if (!dateString) return '-'
  120. const date = new Date(dateString)
  121. return `${date.getFullYear()}.${String(date.getMonth() + 1).padStart(2, '0')}.${String(date.getDate()).padStart(2, '0')}`
  122. }
  123. onMounted(() => loadPosts())
  124. </script>