<?php
session_start();
require_once "config.php";

if (isset($_SESSION['user'])) {
    switch ($_SESSION['level']) {
        case 'A':
            header("Location: admin/index.php");
            break;
        case 'S':
            header("Location: satpam/index.php");
            break;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cemail']) && isset($_POST['cpassword'])) {
    $cemail = $_POST["cemail"];
    $cpassword = $_POST["cpassword"];

    $stmt = $conn->prepare("SELECT cemail, cjenis, dtglExpired FROM tbuserwebsite WHERE cemail = ? AND cpassword = ?");
    $stmt->bind_param("ss", $cemail, $cpassword);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        
        if (strtotime($user['dtglExpired']) >= strtotime(date('Y-m-d'))) {
            $_SESSION['user'] = $user['cemail']; 
            $_SESSION['level'] = $user['cjenis'];

            switch ($_SESSION['level']) {
                case 'A':
                    header("Location: admin/index.php");
                    break;
                case 'S':
                    header("Location: satpam/index.php");
                    break;
            }
            exit;
        } else {
            $error_message = 'Akun Anda sudah kadaluarsa.';
        }
    } else {
        $error_message = 'Email atau password salah.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
    <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
    <link rel="stylesheet" type="text/css" href="css/util.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
    <div class="limiter">
        <div class="container-login100">
            <div class="wrap-login100">
                <div class="login100-pic js-tilt" data-tilt>
                    <img src="images/img-01.png" alt="IMG">
                </div>

                <form class="login100-form validate-form" action="" method="POST">
                    <span class="login100-form-title">Login</span>

                    <?php if (isset($error_message)): ?>
                        <div class="alert alert-danger" role="alert">
                            <?php echo htmlspecialchars($error_message); ?>
                        </div>
                    <?php endif; ?>

                    <div class="wrap-input100 validate-input">
                        <input class="input100" type="text" name="cemail" placeholder="Email" required>
                        <span class="focus-input100"></span>
                        <span class="symbol-input100">
                            <i class="fa fa-user" aria-hidden="true"></i>
                        </span>
                    </div>

                    <div class="wrap-input100 validate-input">
                        <input class="input100" type="password" name="cpassword" placeholder="Password" required>
                        <span class="focus-input100"></span>
                        <span class="symbol-input100">
                            <i class="fa fa-lock" aria-hidden="true"></i>
                        </span>
                    </div>

                    <div class="container-login100-form-btn">
                        <button class="login100-form-btn">Login</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="vendor/jquery/jquery-3.2.1.min.js"></script>
    <script src="vendor/bootstrap/js/popper.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="vendor/select2/select2.min.js"></script>
    <script src="vendor/tilt/tilt.jquery.min.js"></script>

    
    <script>
        $('.js-tilt').tilt({
            scale: 1.1
        })
    </script>
    <script src="js/main.js"></script>
</body>
</html>
