<?php
session_start();
include $_SERVER['DOCUMENT_ROOT'] . '/assets/db/db.php';

if (isset($_SESSION['login'])) {
    header("Location: ../entry/admin.php");
    exit;
}

if (isset($_POST['login'])) {
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = $_POST['password'];

    $result = $conn->query("SELECT * FROM users WHERE username = '$username'");

    if ($result->num_rows === 1) {
        $row = $result->fetch_assoc();
        if (password_verify($password, $row['password'])) {
            $_SESSION['login'] = true;
            $_SESSION['admin_id'] = $row['id'];
            $_SESSION['nama_admin'] = $row['nama_admin'];
            header("Location: ../entry/admin.php");
            exit;
        }
    }
    $error = true;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Login - SINKARKES</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body { background: #f4f7f6; height: 100vh; display: flex; align-items: center; justify-content: center; }
        .login-card { border: none; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); width: 100%; max-width: 400px; background: #fff; overflow: hidden; }
        .login-header { background: #40ada6; color: white; padding: 30px; text-align: center; }
        .btn-teal { background: #40ada6; color: white; border: none; font-weight: bold; }
        .btn-teal:hover { background: #358e88; color: white; }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="login-header">
            <img src="../assets/img/SINKARKES.png" height="50" class="mb-3" style="filter: brightness(0) invert(1);">
            <h5 class="mb-0">Portal Administrasi</h5>
        </div>
        <div class="card-body p-4">
            <?php if (isset($error)) : ?>
                <div class="alert alert-danger small">Username atau Password salah!</div>
            <?php endif; ?>
            <form action="" method="POST">
                <div class="form-group">
                    <label>Username</label>
                    <div class="input-group">
                        <div class="input-group-prepend"><span class="input-group-text"><i class="fas fa-user"></i></span></div>
                        <input type="text" name="username" class="form-control" required autofocus>
                    </div>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <div class="input-group">
                        <div class="input-group-prepend"><span class="input-group-text"><i class="fas fa-lock"></i></span></div>
                        <input type="password" name="password" class="form-control" required>
                    </div>
                </div>
                <button type="submit" name="login" class="btn btn-teal btn-block btn-lg mt-4">MASUK SYSTEM</button>
            </form>
        </div>
    </div>
</body>
</html>