File size: 4,591 Bytes
06b20b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

// Shared utility functions
function formatNumber(num) {
    return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

document.addEventListener('DOMContentLoaded', function() {
const fileUpload = document.getElementById('file-upload');
    const fileList = document.getElementById('file-list');
    const submitBtn = document.getElementById('submit-btn');
    const dropZone = document.querySelector('.border-dashed');
    
    let files = [];
    
    // Handle file selection
    fileUpload.addEventListener('change', function(e) {
        handleFiles(e.target.files);
    });
    
    // Drag and drop functionality
    dropZone.addEventListener('dragover', function(e) {
        e.preventDefault();
        this.classList.add('border-blue-400', 'bg-blue-50');
    });
    
    dropZone.addEventListener('dragleave', function() {
        this.classList.remove('border-blue-400', 'bg-blue-50');
    });
    
    dropZone.addEventListener('drop', function(e) {
        e.preventDefault();
        this.classList.remove('border-blue-400', 'bg-blue-50');
        handleFiles(e.dataTransfer.files);
    });
    
    // Submit button
    submitBtn.addEventListener('click', function() {
        if (files.length === 0) {
            alert('Please select at least one file to upload.');
            return;
        }
        
        // Here you would typically send files to server
        alert(`Successfully uploaded ${files.length} file(s)!`);
        files = [];
        fileList.innerHTML = '';
    });
    
    function handleFiles(newFiles) {
        for (let i = 0; i < newFiles.length; i++) {
            files.push(newFiles[i]);
            createFileItem(newFiles[i]);
        }
    }
    
    function createFileItem(file) {
        const fileItem = document.createElement('div');
        fileItem.className = 'file-item bg-gray-50 rounded-lg p-4 flex justify-between items-center';
        
        const fileInfo = document.createElement('div');
        fileInfo.className = 'flex items-center';
        
        const fileIcon = document.createElement('i');
        fileIcon.setAttribute('data-feather', getFileIcon(file.type));
        fileIcon.className = 'w-5 h-5 text-gray-500 mr-3';
        
        const fileName = document.createElement('span');
        fileName.className = 'text-gray-700';
        fileName.textContent = file.name;
        
        const fileSize = document.createElement('span');
        fileSize.className = 'text-gray-500 text-sm ml-2';
        fileSize.textContent = formatFileSize(file.size);
        
        const fileActions = document.createElement('div');
        fileActions.className = 'file-actions flex items-center';
        
        const removeBtn = document.createElement('button');
        removeBtn.className = 'text-red-500 hover:text-red-700 transition-colors duration-300';
        removeBtn.innerHTML = '<i data-feather="trash-2" class="w-4 h-4"></i>';
        removeBtn.addEventListener('click', function() {
            files = files.filter(f => f !== file);
            fileItem.remove();
        });
        
        fileInfo.appendChild(fileIcon);
        fileInfo.appendChild(fileName);
        fileInfo.appendChild(fileSize);
        fileActions.appendChild(removeBtn);
        fileItem.appendChild(fileInfo);
        fileItem.appendChild(fileActions);
        fileList.appendChild(fileItem);
        
        feather.replace();
    }
    
    function getFileIcon(type) {
        if (type.match('image.*')) return 'image';
        if (type.match('video.*')) return 'film';
        if (type.match('audio.*')) return 'music';
        if (type.match('application/pdf')) return 'file-text';
        if (type.match('application/msword') || type.match('application/vnd.openxmlformats-officedocument.wordprocessingml.document')) return 'file-text';
        if (type.match('application/vnd.ms-excel') || type.match('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) return 'file-text';
        if (type.match('application/vnd.ms-powerpoint') || type.match('application/vnd.openxmlformats-officedocument.presentationml.presentation')) return 'file-text';
        if (type.match('application/zip') || type.match('application/x-rar-compressed')) return 'archive';
        return 'file';
    }
    
    function formatFileSize(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }
});