<html>

<head>
    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />

    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
</head>

<body>

    <style>
        html {
            font-family: 'Poppins', sans-serif;
            font-size: 1.5vmax;
        }
        
        .center {
            margin: auto;
            padding: 10px;
            width: 40%;
            position: relative;
            top: 15vh;
            border-radius: 1vmax;
        }
        
        button {
            padding: 1vh 0.5vw;
            border: 0.1vmax solid #3f3e3e;
            border-radius: 0.2vmax;
            box-shadow: 0.1vh 0.1vw #888888;
            font-family: 'Poppins', sans-serif;
            margin: 2vh 0;
        }
        
        .login {
            background-color: #1c37ad;
            color: white;
        }
        
        .tabs .tab a {
            color: #1c37ad;
            font-size: 1.5vmax;
        }
        
        input[type=text] {
            border: 0.15vmax solid #888888 !important;
            border-radius: 1vmax !important;
            font-family: 'Poppins', sans-serif;
            font-size: 1.5vmax !important;
        }
        
        label {
            text-align: left !important;
        }
        
        h1 {
            overflow: hidden;
            text-align: center;
            font-size: 1.5vmax;
            margin: 0.8rem 0 0.68rem;
        }
        
        h1:before,
        h1:after {
            background-color: #000;
            content: "";
            display: inline-block;
            height: 1px;
            position: relative;
            vertical-align: middle;
            width: 50%;
        }
        
        h1:before {
            right: 0.5em;
            margin-left: -50%;
        }
        
        h1:after {
            left: 0.5em;
            margin-right: -50%;
        }
        
        @media only screen and (max-width: 720px) {
            .center {
                width: 80%;
            }
        }
    </style>

    <div class="card center">

        <div class="card-tabs">
            <ul class="tabs tabs-fixed-width">
                <li class="tab"><a href="#user">USER</a></li>
                <li class="tab"><a href="#company">COMPANY</a></li>
            </ul>
        </div>
        <div class="card-content grey lighten-4">
            <section id="user">
                <button class="google"> <i class="fab fa-google"></i> LOGIN WITH GOOGLE</button>
                <h1>OR</h1>
                <form id="login">
                    <label for="text">Username</label>
                    <input type="text" autocomplete="off" id="username" placeholder="Username" />
                    <label for="password">Password</label>
                    <input type="password" autocomplete="off" id="password" placeholder="Password" />

                    <a href="#resetpw">Forgot password?</a><br/>

                    <button class="login">LOGIN</button>
                </form>

                <p>New Here? <a href="#signup">Register here</a></p>
            </section>

            <!-- <section id="company">
                <button>GOOGLE</button>
                <hr/>
                <form>
                    <label for="email">Email</label>
                    <input type="text" id="email" name="email">
                    <label for="password">Password</label>
                    <input type="text" id="password" name="password">
                </form>

                <a href="#resetpw">Forgot password?</a><br/>

                <button>LOGIN</button>

                <p>New Here? <a href="#signup">Register here</a></p>

            </section>
            </div>
        </div> -->

            <!-- Compiled and minified JavaScript -->

            <script>
                const form = document.getElementById('login')
                form.addEventListener('submit', login)

                async function login(event) {
                    event.preventDefault()
                    const username = document.getElementById('username').value
                    const password = document.getElementById('password').value

                    const result = await fetch('/api/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            username,
                            password
                        })
                    }).then((res) => res.json())

                    if (result.status === 'ok') {
                        // everythign went fine
                        console.log('Got the token: ', result.data)
                        localStorage.setItem('token', result.data)
                        alert('Success')
                    } else {
                        alert(result.error)
                    }
                }
            </script>
</body>

</html>