import { initializeModalButtons } from '../pop-modals.js';
import { updateAllProjectProgress } from '../../js/projects/calculate-all-projects-time.js';

document.addEventListener('DOMContentLoaded', function () {
    const inProgressProjectsContainer = document.getElementById('inProgressProjectsContainer');
    const projectsByStatusContainer = document.getElementById('projectsByStatusContainer');
    const projectLoader = document.getElementById('projectLoader');
    const statusesSelectTag = document.getElementById('statusesSelectTag');

    statusesSelectTag.addEventListener('change', function () {
        var selectedStatus = statusesSelectTag.value;

        projectLoader.classList.remove('hidden');
        inProgressProjectsContainer.classList.add('hidden');
        projectsByStatusContainer.classList.add('hidden');

        if (selectedStatus === 'All') {
            fetch('/all-projects/')
                .then(response => response.text())
                .then(data => {
                    projectsByStatusContainer.classList.remove('hidden');
                    $(projectsByStatusContainer).html(data);
                    projectLoader.classList.add('hidden');
                    initializeModalButtons();
                    updateAllProjectProgress();
                })
                .catch(error => {
                    console.error('Error:', error);
                    projectLoader.classList.add('hidden');
                });
        } else {
            fetch('/projects/status/' + selectedStatus + '/')
                .then(response => response.text())
                .then(data => {
                    projectsByStatusContainer.classList.remove('hidden');
                    $(projectsByStatusContainer).html(data);
                    projectLoader.classList.add('hidden');
                    initializeModalButtons();
                    updateAllProjectProgress();
                })
                .catch(error => {
                    console.error('Error:', error);
                    projectLoader.classList.add('hidden');
                });
        }
    });
});
