class PublicFileViewer { constructor() { this.files = []; this.currentPage = 1; this.itemsPerPage = 10; this.filteredFiles = []; this.init(); } async init() { console.log('π PublicFileViewer μ΄κΈ°ν μμ'); try { this.showLoading(true); console.log('π‘ νμΌ λͺ©λ‘ λ‘λ μ€...'); await this.loadFiles(); this.filteredFiles = [...this.files]; console.log(`β ${this.files.length}κ° νμΌ λ‘λ μλ£`); 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 = `
λ±λ‘λ μλ£μ μμΈ μ 보λ₯Ό νμΈνμΈμ