<?php
session_start();
require 'header.php';


//cek cookie
if (isset($_COOKIE['id']) && isset($_COOKIE['key'])) {
    $id = $_COOKIE['id'];
    $key = $_COOKIE['key'];

    // ambil username berdasarkan id
    $result = mysqli_query($conn, "SELECT username FROM user WHERE id = $id");
    $row = mysqli_fetch_assoc($result);

    //cek cookie dan username
    if ($key === hash('sha256', $row['username'])) {
        $_SESSION['login'] = true;
    }
}

if (isset($_SESSION["login"])) {
    header("Location: index.php");
    exit;
}

if (isset($_POST["login"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];

    $result = mysqli_query($conn, "SELECT * FROM user WHERE username = '$username'");

    // cek username
    if (mysqli_num_rows($result) === 1) {

        //cek password
        $row = mysqli_fetch_assoc($result);
        if (password_verify($password, $row["password"])) {


            if ($_SESSION["Captcha"] === $_POST["nilaiCaptcha"]) {


                // set session
                $_SESSION["login"] = true;
                $_SESSION["level"] = $row["level"];
                $_SESSION["akun"] = $row["id"];

                //cek remember me
                if (isset($_POST['remember'])) {
                    //buat cookie
                    setcookie('id', $row['id'], time() + 60);
                    setcookie('key', hash('sha256', $row['username']), time() + 60);
                }
                header("Location: index.php");
                exit;
            }
        }
    }

    $error = true;
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        table {
            margin-top: 200px;
        }

        .pincit {
            margin-top: 100px;
        }

        .a {
            margin-left: 20px;
            width: 350px;
        }

        .r {
            margin-left: 20px;
        }

        form {
            height: 300px;
        }
    </style>
</head>

<body>

    <div class="container">
        <form action="" method="post">
            <table align="center">
                <tr>
                    <td>
                        <div>
                            <label for="username">Username</label>
                    </td>
                    <td>
                        <input type="text" name="username" class="a" id="username">
    </div>
    </td>
    </tr>
    <tr>
        <td>
            <div>
                <label for="password">Password</label>
        </td>
        <td>
            <input type="password" name="password" class="a" id="password">
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div>

                <?php
                if (isset($error)) : ?>
                    <p style="color : red; fond-style:italic;"> username / password salah</p>
                <?php endif; ?>
            </div>
        </td>
    </tr>
    <tr>
        <td><img src="captcha.php" alt="gambar" /> </td>
        <td><input name="nilaiCaptcha" value="" class="a" placeholder="Masukkan Captcha" /></td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <input type="checkbox" name="remember" id="remember" class="r">
            <label for="remember">Remember Me</label>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <center>
                <div class="pincit">
                    <button type="submit" class="btn btn-primary" name="login">Login</button>
                </div>
            </center>
        </td>
    </tr>

    </table>
    </form>
    <br>
    <br>
    <p align="center">Belum Punya Akun? <a href="User_Registrasi.php">Daftar Akun</a></p>
    </div>

    <?php
    require_once(dirname(__FILE__) . '\footer.php');
    ?>

</body>

</html>