<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();

?>

<h1 class="title" style="margin-bottom:-10vw">SELECT CLIENT</h1>

<div class="search_form">


    <?php echo $html->select_html_from_db("clients", "record_id", "name", "", "client_id", "client_id"); ?>
    <?php echo $html->submit_btn('submit', 'SEARCH PASSWORDS', 'goto()'); ?>
    <?php echo $html->submit_btn('submit', 'SEND LINK', 'sendlink()'); ?>

</div>


<script>
    function goto() {
        var client_id = document.getElementById('client_id').value;
        window.location.href = 'passwords.report.php?client_id=' + client_id;
    }

    function sendlink() {
        var client_id = document.getElementById('client_id').value;
        var email = prompt("Please enter the email to send the link to:");

        if (email) {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "../../root.class.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        if (xhr.responseText == '1') {
                            alert("Sent");
                        } else {
                            alert(xhr.responseText);
                        }
                    } else {
                        alert("An error occurred while sending the request.");
                    }
                }
            };

            xhr.send("client_id=" + encodeURIComponent(client_id) + "&email=" + encodeURIComponent(email) + "&ajax_type=email_passwords");
        }
    }
</script>