<?php
$pageTitle = "Login - ShadiHall";
require_once 'includes/header.php';

if (isLoggedIn()) {
    $role = getUserRole();
    if ($role === 'admin') redirect(BASE_URL . '/admin/dashboard.php');
    elseif ($role === 'vendor') redirect(BASE_URL . '/vendor/dashboard.php');
    else redirect(BASE_URL . '/user/dashboard.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = clean($conn, $_POST['email']);
    $pass = $_POST['password'];
    
    $result = mysqli_query($conn, "SELECT * FROM users WHERE email='$email' LIMIT 1");
    if ($result && mysqli_num_rows($result) === 1) {
        $user = mysqli_fetch_assoc($result);
        if (password_verify($pass, $user['password'])) {
            if ($user['status'] !== 'active') {
                $error = "Your account is " . $user['status'] . ". Contact admin.";
            } else {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['name'] = $user['name'];
                $_SESSION['role'] = $user['role'];
                
                if ($user['role'] === 'admin') redirect(BASE_URL . '/admin/dashboard.php', 'Welcome Admin!');
                elseif ($user['role'] === 'vendor') redirect(BASE_URL . '/vendor/dashboard.php', 'Welcome Vendor!');
                else redirect(BASE_URL . '/user/dashboard.php', 'Welcome ' . $user['name'] . '!');
            }
        } else {
            $error = "Invalid email or password.";
        }
    } else {
        $error = "Invalid email or password.";
    }
}
?>

<div class="form-page">
    <div class="form-container">
        <h2>Welcome Back</h2>
        <p class="form-ornament">Sign in to your account</p>
        
        <?php showFlash(); ?>
        <?php if (!empty($error)): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="form-group">
                <label>Email Address</label>
                <input type="email" name="email" required placeholder="you@example.com">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" required placeholder="Enter password">
            </div>
            <button type="submit" class="btn" style="width:100%; margin-top:10px;">Sign In</button>
        </form>
        
        <p style="text-align:center; margin-top:25px; color:#666; font-size:14px;">
            New to ShadiHall? 
            <a href="<?php echo BASE_URL; ?>/register.php" style="color:var(--maroon); font-weight:600; text-decoration:none;">Create Account</a>
        </p>
        
        <div style="margin-top:30px; padding:18px; background:var(--cream); border-radius:4px; font-size:12px; border-left:3px solid var(--bronze);">
            <strong style="color:var(--maroon); letter-spacing:1px; text-transform:uppercase; font-size:11px;">Demo Accounts</strong><br><br>
            <span style="color:#666;">
                <strong>Admin:</strong> admin@shadihall.com / admin123<br>
                <strong>Vendor:</strong> vendor@shadihall.com / vendor123<br>
                <strong>User:</strong> user@shadihall.com / user123
            </span>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
