<?php
session_start();

// 1. Database Connection
$db_path = file_exists('../includes/db.php') ? '../includes/db.php' : 'includes/db.php';
if (file_exists($db_path)) {
    require $db_path;
} else {
    // Fallback connection
    $conn = new mysqli('localhost', 'root', '', 'bajipari');
}

// 2. Redirect if already logged in
if (isset($_SESSION['user_id'])) {
    header("Location: dashboard.php");
    exit();
}

$error_msg = "";

// 3. Handle Login Submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize input (this will be either username OR mobile number)
    $login_input = $conn->real_escape_string($_POST['username']); 
    $password = $_POST['password'];

    // Query User: Check BOTH username AND phone columns
    $sql = "SELECT id, username, password FROM users WHERE username = '$login_input' OR phone = '$login_input'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        // Verify Password
        if (password_verify($password, $row['password'])) {
            // Set Session
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];
            
            // Redirect
            header("Location: dashboard.php");
            exit();
        } else {
            $error_msg = "Invalid Password!";
        }
    } else {
        $error_msg = "User not found!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Login - Bet365 Style</title>
    
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">

    <style>
        body {
            font-family: 'Roboto', sans-serif;
            /* Bet365 Background Color (Deep Green/Grey mix) */
            background-color: #273832; 
            color: #d1d5db;
        }

        /* Bet365 Header Green */
        .header-bg {
            background-color: #137a5a; 
            border-bottom: 1px solid #0e5e45;
        }

        .custom-input {
            background-color: #3b4c46; /* Input field background */
            border: 1px solid #137a5a; /* Subtle green border */
            color: #ffffff;
            transition: all 0.3s;
        }
        .custom-input::placeholder {
            color: #8fa199;
        }
        .custom-input:focus {
            border-color: #ffdf1b; /* Focus yellow */
            outline: none;
            background-color: #3b4c46;
        }
        
        /* Bet365 Yellow Button */
        .btn-bet365 {
            background-color: #ffdf1b !important;
            color: #123628 !important; /* Dark Green Text */
            font-weight: bold;
            border: none;
            transition: all 0.2s ease-in-out;
        }
        .btn-bet365:hover {
            background-color: #eacb15 !important;
            transform: translateY(-1px);
        }

        /* Logo Styling */
        .logo-text {
            font-family: 'Arial', sans-serif;
            font-weight: 900;
            font-style: italic;
            letter-spacing: -1px;
        }
        
        .brand-yellow {
            color: #ffdf1b;
        }
        
        .brand-link {
            color: #ffdf1b;
            transition: color 0.2s;
        }
        .brand-link:hover {
            color: #fff;
        }
    </style>
</head>
<body>

    <div class="header-bg h-[60px] flex items-center justify-between px-4 shadow-md">
        <div class="text-white text-lg font-bold italic">Log In</div>
        <a href="../index.php" class="text-green-200 hover:text-white text-xl">
            <i class="fas fa-times"></i>
        </a>
    </div>

    <div class="px-5 pt-12">

        <div class="flex flex-col items-center mb-10">
            <div class="mb-2">
                <span class="text-white text-5xl logo-text">BET</span><span class="brand-yellow text-5xl logo-text">365</span>
            </div>
            <div class="text-gray-400 text-xs font-medium tracking-wider uppercase">Sports & Casino</div>
        </div>

        <?php if($error_msg): ?>
            <div class="bg-red-900/80 border border-red-600 text-white px-4 py-3 rounded mb-6 text-sm text-center shadow">
                <i class="fas fa-exclamation-circle mr-1"></i> <?php echo $error_msg; ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="">
            
            <div class="mb-5">
                <label class="block text-gray-300 text-xs font-bold mb-2 uppercase tracking-wide">Username / Mobile</label>
                <input type="text" name="username" placeholder="Username" required
                    class="custom-input w-full px-4 py-3 rounded text-sm">
            </div>

            <div class="mb-4">
                <label class="block text-gray-300 text-xs font-bold mb-2 uppercase tracking-wide">Password</label>
                <div class="relative">
                    <input type="password" name="password" id="password" placeholder="Password" required
                        class="custom-input w-full px-4 py-3 rounded text-sm pr-10">
                    
                    <i class="fas fa-eye-slash absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 cursor-pointer hover:text-white transition" 
                       id="togglePass" onclick="togglePassword()"></i>
                </div>
            </div>

            <div class="text-right mb-8">
                <a href="forgot_password.php" class="text-gray-400 text-xs hover:text-white transition">Cannot access account?</a>
            </div>

            <button type="submit" class="btn-bet365 w-full py-3 rounded text-base shadow-lg">
                Log In
            </button>

        </form>

        <div class="mt-8 text-center text-sm border-t border-gray-600 pt-6">
            <span class="text-gray-400">New to BET365?</span>
            <a href="signup.php" class="brand-link ml-1 font-bold">Join Now</a>
        </div>

    </div>

    <script>
        function togglePassword() {
            const passwordInput = document.getElementById('password');
            const icon = document.getElementById('togglePass');

            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
                icon.classList.add('brand-yellow');
            } else {
                passwordInput.type = 'password';
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
                icon.classList.remove('brand-yellow');
            }
        }
    </script>

</body>
</html>