<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HBF Admin - Email Validation</title>
    <link rel="stylesheet" href="/css/adminValidate.css">
    <link rel="icon" type="image/x-icon" href="/images/logo.png">
</head>
<body>
    <div id="wrapper">
        <header>
            <div class="feedHeader">
                <div class="logo">
                    <div>
                        <img src="/images/logo.png" alt="HBF_LOGO">
                    </div>
                    <h1 class="logoName">
                        <a class="whiteText" href="https://hospitalbedfinder.com">H<span class="nameLower">ospital</span> B<span class="nameLower">ed</span> F<span class="nameLower">inder</span></a>
                    </h1>    
                </div>
            </div>
        </header>
        <main>
            <div>
                <h1 class="centerHeader">Unvalidated</h1>
                <div class="container">
                    <div class="innerWrapper">
                        <% unvalidatedHospitals.forEach(hospital => { %>
                            <div class="unvalidatedCard card">
                                <div>
                                    <p><span class="normal">Email: </span><%= hospital.email %> </p>
                                    <p><span class="normal">Name: </span><%= hospital.name %> </p>
                                </div>
                            </div>
                        <% }) %>
                    </div>
                </div>
            </div>
            <div class="container">
                <div class="inputWrapper">
                    <label for="email">Enter validated email: </label>
                    <input type="text" name="email" id="email">
                    <div class="buttonWrapper">
                        <button id="validateBtn" onclick="validateEmail()">Validate</button>
                    </div>
                    <p id="error"></p>
                </div>
            </div>
            <div>
                <h1 class="centerHeader">Validated</h1>
                <div class="container">
                    <div class="innerWrapper">
                        <% approvedEmails.forEach(hospital => { %>
                            <div class="validatedCard card">
                                <div>
                                    <p><span class="normal">Email: </span><%= hospital.email %> </p>
                                    <p><span class="normal">Validated on: </span><%= hospital.validatedOn %> </p>
                                </div>
                            </div>
                        <% }) %>
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
<script>
    let baseUrl = window.location.origin;

    async function validateEmail(){
        const email = document.getElementById("email").value;
        const emailObj = {email: email};
        const res = await fetch(baseUrl + "/api/registerEmail", {
            method: "post",
            body: JSON.stringify(emailObj),
            mode: "cors",
            headers: new Headers({
                "Content-Type": "application/json",
            }),
        });
        const resBody = await res.json();
        if (resBody.success){
            window.location.reload();
            return;
        }
        document.getElementById("error").innerText = resBody.error;
    }
</script>
</html>