// Open the modal for adding a new file
function openModal() {
    document.getElementById("modalTitle").innerText = "Add New File";
    document.getElementById("documentForm").reset(); // Reset the form
    document.getElementById("documentId").value = ""; // Ensure no documentId is set (for new files)
    document.getElementById("existingFileSection").style.display = "none"; // Hide the existing file section
    document.getElementById("documentModal").style.display = "block"; // Show the modal
    document.getElementById("divisionId").value = document.getElementById("divisionDropdown").value; // Set the division ID
}

// Open the modal for editing an existing file
function openEditModal(documentId, fileName, author, description, fileSize, resolution, filePath) {
    document.getElementById("modalTitle").innerText = "Edit File";
    document.getElementById("documentId").value = documentId; // Set document ID
    document.getElementById("fileName").value = fileName; // Pre-fill file name
    document.getElementById("fileAuthor").value = author; // Pre-fill author
    document.getElementById("fileDescription").value = description; // Pre-fill description

    // Show existing file information
    document.getElementById("existingFileSection").style.display = "block";
    document.getElementById("existingFileLink").href = filePath; // Link to the current file
    document.getElementById("existingFileLink").innerText = fileName + ' (' + fileSize + ' bytes, ' + resolution + ')';

    document.getElementById("documentModal").style.display = "block"; // Show the modal
}

// Wrapper closeModal that delegates to a global implementation if available.
function closeModal(modalId) {
    if (typeof window.closeModal === 'function' && window.closeModal !== closeModal) {
        window.closeModal(modalId);
        return;
    }

    const doc = document.getElementById('documentModal');
    if (doc) doc.style.display = 'none';
}

// Function to remove existing file
function removeExistingFile() {
    document.getElementById("existingFileSection").style.display = "none"; // Hide the existing file section
    // Optionally, you could set a flag in your form to mark the file for deletion server-side
}

// Function to close the file preview modal
function closeFileModal() {
    document.getElementById('fileModal').style.display = "none";
}

// Function to confirm file deletion
function confirmDelete(documentId, divisionId) {
    if (confirm('Are you sure you want to delete this file?')) {
        window.location.href = 'admin_dashboard.php?delete_id=' + documentId + '&division_id=' + divisionId; // Redirect to delete action
    }
}

// Function to handle division change
function handleDivisionChange() {
    const divisionId = document.getElementById("divisionDropdown").value;
    // Reload the page or make an AJAX call to update the table based on the selected division
    window.location.href = `admin_dashboard.php?division_id=${divisionId}`;
}

// Function to toggle the file statistics section
function toggleFileStatistics() {
    const sidebar = document.getElementById('sidebar');
    const sectionContent = document.querySelector('.section-content');
    sidebar.classList.toggle('hidden');
    sectionContent.classList.toggle('expanded');
}

// Add event listener to the "Add File" button
document.addEventListener('DOMContentLoaded', function() {
    const addButton = document.querySelector('.add-button');
    if (addButton) {
        addButton.addEventListener('click', openModal);
    }

    // Add event listeners for edit buttons
    const editButtons = document.querySelectorAll('.icon-button[onclick^="openEditModal"]');
    editButtons.forEach(button => {
        button.addEventListener('click', function() {
            const args = this.getAttribute('onclick').match(/openEditModal\((.*)\)/)[1].split(',');
            openEditModal(...args.map(arg => arg.trim().replace(/^'|'$/g, '')));
        });
    });

    // Add event listener to the division dropdown
    const divisionDropdown = document.getElementById("divisionDropdown");
    if (divisionDropdown) {
        divisionDropdown.addEventListener('change', handleDivisionChange);
    }

    // Add event listener to the sidebar toggle button
    document.getElementById('sidebarToggle').addEventListener('click', toggleFileStatistics);

    brython();
    create_pie_chart();
});
