<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once __DIR__ . '/config.php';

$msg = '';

// Agar pehle se login hai toh dashboard par bhej dein
if (isset($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit;
}

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

    if (empty($login_input) || empty($password)) {
        $msg = "<div class='alert error'>Please fill in all fields.</div>";
    } else {
        // Check by username OR phone
        $stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ? OR phone = ?");
        $stmt->bind_param("ss", $login_input, $login_input);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $user = $result->fetch_assoc();
            // Verify password
            if (password_verify($password, $user['password'])) {
                // Set session
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                
                header('Location: dashboard.php');
                exit;
            } else {
                $msg = "<div class='alert error'>Incorrect password!</div>";
            }
        } else {
            $msg = "<div class='alert error'>No account found with this Username/Phone.</div>";
        }
    }
}

$bgURL = 'bg.png';
?>
<!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 - HEN POULTRY</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin:0; padding:0; box-sizing:border-box; font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
        .fixed-bg {
            position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
            background-color: #0f172a;
            background-image: linear-gradient(rgba(15, 23, 42, 0.7), rgba(15, 23, 42, 0.8)), url('<?php echo $bgURL; ?>');
            background-size: cover; background-position: center; z-index: -999;
        }
        body { color: #fff; display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 20px; }
        .login-box { 
            background: rgba(0, 0, 0, 0.6); border: 1px solid rgba(255, 255, 255, 0.15); 
            border-radius: 20px; padding: 30px 20px; width: 100%; max-width: 400px; 
            backdrop-filter: blur(10px); box-shadow: 0 8px 32px rgba(0,0,0,0.5); text-align: center;
        }
        .logo-icon { font-size: 3rem; color: #facc15; margin-bottom: 10px; }
        h2 { font-size: 1.5rem; color: #fff; margin-bottom: 20px; font-weight: 800; }
        
        .input-group { position: relative; margin-bottom: 15px; text-align: left; }
        .input-group i { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: #94a3b8; }
        .input-group input {
            width: 100%; padding: 15px 15px 15px 45px;
            background: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 12px; color: #fff; font-size: 1rem; outline: none; transition: 0.3s;
        }
        .input-group input:focus { border-color: #facc15; background: rgba(255, 255, 255, 0.1); }
        
        .btn-submit {
            width: 100%; padding: 15px; background: linear-gradient(135deg, #facc15, #eab308);
            color: #000; border: none; border-radius: 12px; font-size: 1rem; font-weight: 800;
            cursor: pointer; box-shadow: 0 4px 15px rgba(234, 179, 8, 0.4); transition: 0.2s; margin-top: 10px;
        }
        .btn-submit:active { transform: scale(0.98); }
        
        .alert { padding: 12px; border-radius: 8px; margin-bottom: 15px; font-size: 0.9rem; font-weight: 600; }
        .alert.error { background: rgba(239, 68, 68, 0.2); border: 1px solid #ef4444; color: #fca5a5; }
        
        .links { margin-top: 20px; font-size: 0.9rem; color: #cbd5e1; }
        .links a { color: #facc15; text-decoration: none; font-weight: 700; }
    </style>
</head>
<body>
    <div class="fixed-bg"></div>
    
    <div class="login-box">
        <i class="fa-solid fa-kiwi-bird logo-icon"></i>
        <h2>Welcome Back</h2>
        
        <?= $msg ?>
        
        <form method="POST">
            <div class="input-group">
                <i class="fa-solid fa-user"></i>
                <input type="text" name="login_input" placeholder="Username or Phone Number" required>
            </div>
            
            <div class="input-group">
                <i class="fa-solid fa-lock"></i>
                <input type="password" name="password" placeholder="Password" required>
            </div>
            
            <button type="submit" class="btn-submit">Login</button>
        </form>
        
        <div class="links">
            Don't have an account? <a href="register.php">Register Here</a>
        </div>
    </div>
</body>
</html>