class PublicFileViewer { constructor() { this.files = []; this.currentPage = 1; this.itemsPerPage = 10; this.filteredFiles = []; this.init(); } async init() { try { this.showLoading(true); await this.loadFiles(); this.filteredFiles = [...this.files]; this.bindEvents(); this.renderFiles(); this.updatePagination(); } catch (error) { console.error('초기화 오류:', error); this.showNotification('데이터를 불러오는 중 오류가 발생했습니다.', 'error'); } finally { this.showLoading(false); } } bindEvents() { // 검색 및 정렬 이벤트 document.getElementById('searchBtn').addEventListener('click', () => this.handleSearch()); document.getElementById('searchInput').addEventListener('keyup', (e) => { if (e.key === 'Enter') this.handleSearch(); }); document.getElementById('categoryFilter').addEventListener('change', () => this.handleSearch()); document.getElementById('sortBy').addEventListener('change', () => this.handleSearch()); // 페이지네이션 이벤트 document.getElementById('prevPage').addEventListener('click', () => this.goToPrevPage()); document.getElementById('nextPage').addEventListener('click', () => this.goToNextPage()); } async loadFiles() { try { const response = await fetch('/api/files/public'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); this.files = data.data || []; console.log('파일 로드 완료:', this.files.length, '개'); } catch (error) { console.error('파일 로드 오류:', error); this.files = []; throw error; } } handleSearch() { const searchTerm = document.getElementById('searchInput').value.toLowerCase(); const categoryFilter = document.getElementById('categoryFilter').value; let filteredFiles = this.files; if (searchTerm) { filteredFiles = filteredFiles.filter(file => file.title.toLowerCase().includes(searchTerm) || file.description.toLowerCase().includes(searchTerm) || (file.tags && file.tags.some(tag => tag.toLowerCase().includes(searchTerm))) ); } if (categoryFilter) { filteredFiles = filteredFiles.filter(file => file.category === categoryFilter); } this.filteredFiles = filteredFiles; this.currentPage = 1; // 검색 시 첫 페이지로 리셋 this.renderFiles(); this.updatePagination(); } renderFiles() { const fileList = document.getElementById('fileList'); const sortBy = document.getElementById('sortBy').value; // 정렬 const sortedFiles = [...this.filteredFiles].sort((a, b) => { switch (sortBy) { case 'title': return a.title.localeCompare(b.title); case 'category': return a.category.localeCompare(b.category); case 'date': default: return new Date(b.created_at) - new Date(a.created_at); } }); // 페이지네이션 적용 const startIndex = (this.currentPage - 1) * this.itemsPerPage; const endIndex = startIndex + this.itemsPerPage; const paginatedFiles = sortedFiles.slice(startIndex, endIndex); if (sortedFiles.length === 0) { fileList.innerHTML = `
등록된 자료의 상세 정보를 확인하세요