<?php
ob_start();
session_start();

error_reporting(E_ALL);
ini_set('display_errors', 1);


include("check_installation.php");


// Check if an admin is already logged in
if (isset($_SESSION['adminid'])) {
    header("Location: dashboard.php");
    exit();
}

$logininfo = "";


// Fetch system settings using mysqli
$sbank  = "Default Bank";     
$semail = "support@defaultbank.com"; 

$stmt = $con->prepare("SELECT bank_name, email FROM system_settings LIMIT 1");
if ($stmt) {
    $stmt->execute();
    $stmt->bind_result($dbBank, $dbEmail);
    if ($stmt->fetch()) {
        // If record exists, override defaults
        $sbank  = $dbBank ?: $sbank;
        $semail = $dbEmail ?: $semail;
    }
    $stmt->close();
}


// Handle form submission
if (isset($_POST['go'])) {
    $logid = $_POST['login'];
    $password = $_POST['password'];

    // Prepare and execute the query using a prepared statement.
    $stmt = $con->prepare("SELECT username, password FROM administrator WHERE username = ?");
    $stmt->bind_param("s", $logid);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 1) {
        $row = $result->fetch_assoc();

        
        if ($password === $row['password']) {
            $_SESSION["adminid"] = $logid;
            header("Location: dashboard.php");
            exit();
        } else {
            $logininfo = "Invalid username or password entered.";
        }
    } else {
        $logininfo = "Invalid username or password entered.";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Secure Admin Access | <?php echo $sbank; ?></title>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <link rel="shortcut icon" href="assets/images/favicon.ico">

    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap">

    <link rel="stylesheet" type="text/css" href="assets/vendor/font-awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="assets/vendor/bootstrap-icons/bootstrap-icons.css">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    
    <style>
        :root {
            --bank-primary: #0f172a; /* Deep Navy */
            --bank-accent: #2563eb;  /* Banking Blue */
        }

        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8fafc;
        }

        .auth-card {
            border: none;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            border-radius: 1rem;
        }

        .login-side-panel {
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
            color: white;
            padding: 3rem;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .form-control {
            padding: 0.75rem 1rem;
            border-color: #e2e8f0;
        }

        .form-control:focus {
            border-color: var(--bank-accent);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .btn-primary {
            background-color: var(--bank-accent);
            border-color: var(--bank-accent);
            padding: 0.75rem;
            font-weight: 600;
        }

        .btn-primary:hover {
            background-color: #1d4ed8;
            border-color: #1d4ed8;
        }

        .security-badge {
            display: inline-flex;
            align-items: center;
            padding: 0.5rem 1rem;
            background: #ecfdf5;
            color: #065f46;
            border-radius: 2rem;
            font-size: 0.75rem;
            font-weight: 600;
            margin-bottom: 1.5rem;
        }

        .bank-logo {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--bank-primary);
            letter-spacing: -0.5px;
            margin-bottom: 2rem;
        }

        .footer-text {
            font-size: 0.8rem;
            color: #64748b;
        }
    </style>

</head>

<body>

<main>
    <section class="p-0 d-flex align-items-center vh-100">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-12 col-md-10 col-lg-9 col-xl-8">
                    <div class="card auth-card overflow-hidden">
                        <div class="row g-0">
                            <div class="col-lg-5 login-side-panel d-none d-lg-flex">
                                <div class="mb-4">
                                    <i class="bi bi-shield-lock-fill display-4 text-info"></i>
                                </div>
                                <h2 class="text-white fw-bold">Internal Banking Portal</h2>
                                <p class="text-white-50 mt-3">Access restricted to authorized administrative personnel only. Your IP and login attempts are being logged for security purposes.</p>
                                
                                <div class="mt-5 border-top border-secondary pt-4">
                                    <small class="d-block text-white-50">System Status</small>
                                    <div class="d-flex align-items-center mt-2">
                                        <span class="badge bg-success rounded-circle p-1 me-2"><span class="visually-hidden">.</span></span>
                                        <span class="small">All Systems Operational</span>
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-7 bg-white">
                                <div class="p-4 p-sm-5">
                                    <div class="bank-logo">
                                        <i class="bi bi-bank2 me-2 text-primary"></i><?php echo $sbank; ?>
                                    </div>

                                    <div class="security-badge">
                                        <i class="bi bi-patch-check-fill me-2"></i> 256-bit SSL Encrypted Connection
                                    </div>

                                    <h1 class="h4 fw-bold mb-2">Admin Login</h1>
                                    <p class="mb-4 text-muted">Enter your credentials to manage the core banking system.</p>

                                    <?php if ($logininfo != ""): ?>
                                        <div class="alert alert-danger d-flex align-items-center border-0 shadow-sm" role="alert">
                                            <i class="bi bi-exclamation-triangle-fill me-2"></i>
                                            <div><?= htmlspecialchars($logininfo) ?></div>
                                        </div>
                                    <?php endif; ?>

                                    <form class="mt-2" method="POST" action="login.php">
                                        <div class="mb-3">
                                            <label class="form-label fw-medium">Username</label>
                                            <div class="input-group">
                                                <span class="input-group-text bg-light border-end-0"><i class="bi bi-person text-muted"></i></span>
                                                <input type="text" class="form-control border-start-0" name="login" placeholder="e.g. ADMIN-001" required>
                                            </div>
                                        </div>

                                        <div class="mb-3">
                                            <label class="form-label fw-medium">Secure Password</label>
                                            <div class="input-group">
                                                <span class="input-group-text bg-light border-end-0"><i class="bi bi-lock text-muted"></i></span>
                                                <input class="form-control border-start-0 fakepassword" type="password" name="password" id="psw-input" placeholder="••••••••" required>
                                                <span class="input-group-text bg-light cursor-pointer">
                                                    <i class="fakepasswordicon fas fa-eye-slash text-muted"></i>
                                                </span>
                                            </div>
                                        </div>

                                        <div class="mb-4 d-flex justify-content-between align-items-center">
                                            <div class="form-check">
                                                <input type="checkbox" class="form-check-input" id="rememberCheck">
                                                <label class="form-check-label small" for="rememberCheck">Authorize this device</label>
                                            </div>
                                            <a href="#" class="small text-decoration-none">Forgot access?</a>
                                        </div>

                                        <button type="submit" name="go" class="btn btn-primary w-100 shadow-sm">
                                            Sign In to Console <i class="bi bi-arrow-right ms-2"></i>
                                        </button>

                                        <div class="mt-5 text-center footer-text">
                                            Copyright &copy; <?php echo date('Y'); ?> <strong><?php echo $sbank ?></strong>. 
                                            <br>Global Administrative Portal.
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div> </div>
                </div>
            </div>
        </div>
    </section>
</main>

<script src="assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/functions.js"></script>

</body>
</html>