

document.addEventListener('DOMContentLoaded', function() {
    const table = document.getElementById('user_table');
    const form = document.getElementById('pid_form');
    const pidInput = document.getElementById('pid_input');
    const popup = document.getElementById("popup");
    const popupMessage = document.getElementById("popup-message");
    const confirmButton = document.getElementById("confirm-button");
    const cancelButton = document.getElementById("cancel-button");
    const searchBtn = document.getElementById("btn_search");
    const sectionOne = document.getElementById("search_sec");
    const sectionTwo = document.getElementById("userlist_sec");
    let currentPid = null;

    table.addEventListener('click', function(event) {
        const target = event.target;
        if (target.classList.contains('btn_delete')) {
            // Handle delete button click
            currentPid = target.getAttribute('data-pid');
            if (currentPid) {
                popupMessage.textContent = "Do you really want to delete the user: " + currentPid + "?";
                popup.style.display = "block";
            }
            event.stopPropagation();
        } 
        else if (target.classList.contains('btn_modify')) {
        // Handle modify button click
        const pid = target.getAttribute('data-pid');
        if (pid) {
            pidInput.value = pid;
            form.action = '../user/user_edit.php';
            form.submit();
        }
        } 
        else {
            // Handle row click   //cancel this//
            const row = target.closest('tr');
            if (row) {
                const pid = row.getAttribute('data-pid');
                if (pid) {
                    pidInput.value = pid;
                    form.submit();
                }
            }
        }
    });

    confirmButton.addEventListener('click', function() {
        const deleteForm = document.createElement('form');
        deleteForm.action = '../user/user_delete.php';
        deleteForm.method = 'POST';

        const pidInputField = document.createElement('input');
        pidInputField.type = 'hidden';
        pidInputField.name = 'pid';
        pidInputField.value = currentPid;
        deleteForm.appendChild(pidInputField);

        document.body.appendChild(deleteForm);
        deleteForm.submit();
        popup.style.display = "none";
    });

   
    cancelButton.addEventListener('click', function() {
        popup.style.display = "none";
    });

    // Close popup when clicking outside of it
    window.addEventListener('click', function(event) {
        if (event.target == popup) {
            popup.style.display = "none";
        }
    });
});



document.addEventListener("DOMContentLoaded", function() {
    const clearButton = document.getElementById("clear_btn");
    const searchInput = document.getElementById("search");

    clearButton.addEventListener("click", function(event) {
        event.preventDefault(); // Prevent the default button behavior
        searchInput.value = ''; // Clear the search input field
    });
});