<?php
require_once 'config/db.php';
session_start();

$settings_stmt = $pdo->query("SELECT registration_enabled FROM settings WHERE id = 1");
$settings_res = $settings_stmt->fetch();
$can_register = isset($settings_res['registration_enabled']) ? $settings_res['registration_enabled'] : true;

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

$error = '';

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

    // Search by email, username, or phone
    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? OR username = ? OR phone = ?");
    $stmt->execute([$login_input, $login_input, $login_input]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password'])) {
        if ($user['status'] === 'blocked') {
            $error = "Your account is blocked. Please contact admin.";
        } else {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['name'] = $user['name'];
            $_SESSION['balance'] = $user['balance'];

            // Unified Dashboard
            header("Location: admin/dashboard.php");
            exit();
        }
    } else {
        $error = "Invalid credentials";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?php echo $site_name; ?></title>
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <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">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        body { 
            background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-card { 
            width: 100%;
            max-width: 450px;
            padding: 3rem;
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.1);
        }
        .form-control {
            border-radius: 12px;
            padding: 0.75rem 1rem;
            border: 1px solid #e2e8f0;
        }
        .form-control:focus {
            box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
            border-color: #4f46e5;
        }
    </style>
</head>
<body>

<div class="login-card animate-fade-in">
    <div class="text-center mb-5">
        <div class="bg-primary bg-opacity-10 d-inline-block p-3 rounded-circle mb-3">
            <i class="fas fa-lock text-primary fa-2x"></i>
        </div>
        <h2 class="fw-bold m-0">Welcome Back</h2>
        <p class="text-muted small">Sign in to your account</p>
    </div>

    <?php if($error): ?>
        <div class="alert alert-danger border-0 rounded-3 small animate-fade-in"><?php echo $error; ?></div>
    <?php endif; ?>

    <form method="POST">
        <div class="mb-4">
            <label class="form-label fw-semibold small">User ID / Email / Phone</label>
            <div class="input-group">
                <span class="input-group-text bg-light border-end-0"><i class="fas fa-user text-muted"></i></span>
                <input type="text" name="login_input" class="form-control border-start-0" placeholder="Enter credentials" required>
            </div>
        </div>
        <div class="mb-4">
            <label class="form-label fw-semibold small">Password</label>
            <div class="input-group">
                <span class="input-group-text bg-light border-end-0"><i class="fas fa-key text-muted"></i></span>
                <input type="password" name="password" class="form-control border-start-0" placeholder="••••••••" required>
            </div>
        </div>
        
        <button type="submit" class="btn btn-premium w-100 py-3 mb-4 shadow">
            Sign In <i class="fas fa-arrow-right ms-2"></i>
        </button>
    </form>

    <?php if ($can_register): ?>
    <div class="text-center">
        <p class="text-muted small mb-0">Don't have an account?</p>
        <a href="register.php" class="text-primary fw-bold text-decoration-none">Create New Account</a>
    </div>
    <?php endif; ?>
</div>

</body>
</html>
