<?php
require_once 'config/db.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';

// ── Mobile detection ─────────────────────────────────────────────
function isMobile() {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
    $keywords = ['mobile','android','iphone','ipad','ipod','blackberry',
                 'windows phone','opera mini','opera mobi','silk','kindle',
                 'webos','midp','symbian','tablet'];
    foreach ($keywords as $kw) {
        if (str_contains($ua, $kw)) return true;
    }
    return false;
}

// ── Resolve landing page based on role ───────────────────────────
function getLandingPage($role) {
    if ($role === 'admin') {
        return isMobile()
            ? '/cash_lending/mobile/dashboard.php'
            : '/cash_lending/dashboard.php';
    }
    // Staff → Borrowers only
    return isMobile()
        ? '/cash_lending/mobile/borrowers.php'
        : '/cash_lending/borrowers.php';
}

// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
    header('Location: ' . getLandingPage($_SESSION['role']));
    exit();
}

$error = '';

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

    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password';
    } else {
        if (loginUser($conn, $username, $password)) {
            header('Location: ' . getLandingPage($_SESSION['role']));
            exit();
        } else {
            $error = 'Invalid username or password';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login — Cash Lending System</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background: #f0f2f5; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
        .login-card { background: #fff; border-radius: 16px; padding: 40px 36px; width: 100%; max-width: 400px; box-shadow: 0 4px 24px rgba(0,0,0,.10); }
        .login-logo { text-align: center; margin-bottom: 28px; }
        .login-logo i { font-size: 40px; color: #1a56db; }
        .login-logo h4 { font-weight: 800; margin: 8px 0 2px; color: #111; }
        .login-logo small { color: #6b7280; font-size: 13px; }
        .form-control { border-radius: 10px; padding: 11px 14px; font-size: 14px; }
        .btn-login { border-radius: 10px; padding: 12px; font-weight: 700; font-size: 15px; background: #1a56db; border: none; }
        .btn-login:hover { background: #1447b2; }
    </style>
</head>
<body>
<div class="login-card">
    <div class="login-logo">
        <i class="fas fa-hand-holding-usd"></i>
        <h4>Cash Lending System</h4>
        <small>Sign in to your account</small>
    </div>

    <?php if ($error): ?>
        <div class="alert alert-danger py-2 px-3" style="font-size:13px;border-radius:10px;">
            <i class="fas fa-exclamation-circle"></i> <?php echo htmlspecialchars($error); ?>
        </div>
    <?php endif; ?>

    <form method="POST">
        <div class="mb-3">
            <label class="form-label fw-semibold" style="font-size:13px;">Username</label>
            <input type="text" class="form-control" name="username"
                   value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                   placeholder="Enter username" autofocus required>
        </div>
        <div class="mb-4">
            <label class="form-label fw-semibold" style="font-size:13px;">Password</label>
            <input type="password" class="form-control" name="password" placeholder="Enter password" required>
        </div>
        <button type="submit" class="btn btn-primary btn-login w-100">
            <i class="fas fa-sign-in-alt"></i> Sign In
        </button>
    </form>
</div>
</body>
</html>
