<?php
require __DIR__ . '/config.php';

// If already logged in, go to dashboard
if (is_admin_logged_in()) {
    header("Location: " . ADMIN_URL . "index.php");
        exit;
        }

        $error = "";

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            verify_csrf();

                $email = trim($_POST['email'] ?? '');
                    $password = $_POST['password'] ?? '';

                        if ($email && $password) {
                                $stmt = $pdo->prepare("SELECT * FROM admins WHERE email = ?");
                                        $stmt->execute([$email]);
                                                $admin = $stmt->fetch();

                                                        if ($admin && password_verify($password, $admin['password'])) {
                                                                    $_SESSION['is_admin'] = 1;
                                                                                $_SESSION['admin_id'] = $admin['id'];
                                                                                            $_SESSION['admin_email'] = $admin['email'];

                                                                                                        header("Location: " . ADMIN_URL . "index.php");
                                                                                                                    exit;
                                                                                                                            } else {
                                                                                                                                        $error = "ভুল ইমেইল বা পাসওয়ার্ড।";
                                                                                                                                                }
                                                                                                                                                    } else {
                                                                                                                                                            $error = "অনুগ্রহ করে ইমেইল এবং পাসওয়ার্ড লিখুন।";
                                                                                                                                                                }
                                                                                                                                                                }
                                                                                                                                                                ?>
                                                                                                                                                                <!DOCTYPE html>
                                                                                                                                                                <html lang="bn">
                                                                                                                                                                <head>
                                                                                                                                                                    <meta charset="UTF-8">
                                                                                                                                                                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                                                                                                                                                            <title>অ্যাডমিন লগইন</title>

                                                                                                                                                                                <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
                                                                                                                                                                                    
                                                                                                                                                                                        <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
                                                                                                                                                                                            <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">

                                                                                                                                                                                                <style>
                                                                                                                                                                                                        :root {
                                                                                                                                                                                                                    --bs-primary-rgb: 78, 115, 223;
                                                                                                                                                                                                                                --bs-body-bg: #f8f9fc;
                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                body {
                                                                                                                                                                                                                                                            font-family: 'Poppins', sans-serif;
                                                                                                                                                                                                                                                                        background-color: var(--bs-body-bg);
                                                                                                                                                                                                                                                                                    display: flex;
                                                                                                                                                                                                                                                                                                align-items: center;
                                                                                                                                                                                                                                                                                                            justify-content: center;
                                                                                                                                                                                                                                                                                                                        min-height: 100vh;
                                                                                                                                                                                                                                                                                                                                    padding: 2rem;
                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                    .login-card {
                                                                                                                                                                                                                                                                                                                                                                width: 100%;
                                                                                                                                                                                                                                                                                                                                                                            max-width: 450px;
                                                                                                                                                                                                                                                                                                                                                                                        border: none;
                                                                                                                                                                                                                                                                                                                                                                                                    border-radius: 0.75rem;
                                                                                                                                                                                                                                                                                                                                                                                                                box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.1);
                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                .login-card .card-header {
                                                                                                                                                                                                                                                                                                                                                                                                                                            background-color: rgb(var(--bs-primary-rgb));
                                                                                                                                                                                                                                                                                                                                                                                                                                                        color: white;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    border-top-left-radius: 0.75rem;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                border-top-right-radius: 0.75rem;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            padding: 1.5rem;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .form-floating label {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                color: #6c757d;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </style>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </head>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <body>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <main class="login-card">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <div class="card">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <div class="card-header text-center">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <h1 class="h3 mb-0">অ্যাডমিন লগইন</h1>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="card-body p-4 p-md-5">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <?php if($error): ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <div class="alert alert-danger d-flex align-items-center" role="alert">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <i class="bi bi-exclamation-triangle-fill me-2"></i>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <div><?php echo h($error); ?></div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <?php endif; ?>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <form method="post">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <?php csrf_field(); ?>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="form-floating mb-3">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <input type="email" name="email" class="form-control" id="floatingEmail" placeholder="name@example.com" required>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <label for="floatingEmail">ইমেইল</label>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <div class="form-floating mb-4">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <input type="password" name="password" class="form-control" id="floatingPassword" placeholder="Password" required>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <label for="floatingPassword">পাসওয়ার্ড</label>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="d-grid">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <button class="btn btn-primary btn-lg" type="submit">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <i class="bi bi-box-arrow-in-right me-2"></i> লগইন
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </button>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </form>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <p class="text-center text-muted mt-3 small">© <?php echo date("Y"); ?> All Rights Reserved</p>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </main>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </body>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </html>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            