<?php
// public/admin_login.php
session_start();
require_once __DIR__ . '/../config/admin.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    if (admin_login($email, $password)) {
        // Redirect to dashboard based on role
        $role = $_SESSION['admin_role'];
        switch ($role) {
            case 'SUPER_ADMIN':
                header('Location: /school-crm/public/admin/dashboard_super.php'); exit;
            case 'STATE_ADMIN':
                header('Location: /school-crm/public/admin/dashboard_state.php'); exit;
            case 'DISTRICT_ADMIN':
                header('Location: /school-crm/public/admin/dashboard_district.php'); exit;
            case 'BLOCK_ADMIN':
                header('Location: /school-crm/public/admin/dashboard_block.php'); exit;
            case 'PANCHAYAT_ADMIN':
                header('Location: /school-crm/public/admin/dashboard_panchayat.php'); exit;
            default:
                $error = 'Unknown admin role.';
        }
    } else {
        $error = 'Invalid email or password.';
    }
}
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Login</title>
    <link rel="stylesheet" href="/school-crm/public/assets/bootstrap.min.css">
</head>
<body class="bg-light">
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-4">
            <div class="card shadow">
                <div class="card-header text-center bg-primary text-white">
                    <h4>Admin Login</h4>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"> <?= htmlspecialchars($error) ?> </div>
                    <?php endif; ?>
                    <form method="post">
                        <div class="mb-3">
                            <label>Email</label>
                            <input type="email" name="email" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label>Password</label>
                            <input type="password" name="password" class="form-control" required>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">Login</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<?php
session_start();
require_once __DIR__ . "/../config/db.php"; // <- আপনার path অনুযায়ী ঠিক করুন

$error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $pass  = trim($_POST['password'] ?? '');

    if ($email === '' || $pass === '') {
        $error = "Email and password required.";
    } else {
        $stmt = $db->prepare("SELECT id, name, email, password, role, district_id, block_id, panchayat_id, status
                              FROM admins
                              WHERE email = ?
                              LIMIT 1");
        $stmt->execute([$email]);
        $admin = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$admin || $admin['status'] !== 'ACTIVE') {
            $error = "Invalid email or password.";
        } else {
            // ✅ If DB has HASHED password
            $isHashed = str_starts_with($admin['password'], '$2y$') || str_starts_with($admin['password'], '$argon2');
            $ok = $isHashed ? password_verify($pass, $admin['password']) : hash_equals($admin['password'], $pass);

            if (!$ok) {
                $error = "Invalid email or password.";
            } else {
                session_regenerate_id(true);

                $_SESSION['admin_id'] = (int)$admin['id'];
                $_SESSION['admin_name'] = $admin['name'];
                $_SESSION['admin_role'] = $admin['role'];
                $_SESSION['district_id'] = $admin['district_id'];
                $_SESSION['block_id'] = $admin['block_id'];
                $_SESSION['panchayat_id'] = $admin['panchayat_id'];

                // Role based redirect
                switch ($admin['role']) {
                    case 'SUPER_ADMIN':
                        header("Location: /admin/super/dashboard.php"); exit;
                    case 'STATE_ADMIN':
                        header("Location: /admin/state/dashboard.php"); exit;
                    case 'DISTRICT_ADMIN':
                        header("Location: /admin/district/dashboard.php"); exit;
                    case 'BLOCK_ADMIN':
                        header("Location: /admin/block/dashboard.php"); exit;
                    case 'PANCHAYAT_ADMIN':
                        header("Location: /admin/panchayat/dashboard.php"); exit;
                    default:
                        header("Location: /admin/dashboard.php"); exit;
                }
            }
        }
    }
}
?>      