<?php 
session_start();
include('config.php');
include('header.php');

if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo "<div class='alert alert-danger'>Invalid broker ID.</div>";
    include('footer.php');
    exit;
}

$id = (int)$_GET['id'];

$stmt = $dbh->prepare("SELECT * FROM blackbrokers WHERE id = :id");
$stmt->execute([':id' => $id]);
$broker = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$broker) {
    echo "<div class='alert alert-warning'>Broker not found.</div>";
    include('footer.php');
    exit;
}
?>

<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        Broker Details: <?= htmlentities($broker['name']) ?>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-3">
                <?php if (!empty($broker['image_url'])): ?>
                    <img src="<?= htmlentities($broker['image_url']) ?>" class="img-fluid rounded" alt="Logo">
                <?php else: ?>
                    <p>No logo available</p>
                <?php endif; ?>
            </div>
            <div class="col-md-9">
                <p><strong>Name:</strong> <?= htmlentities($broker['name']) ?></p>
                <p><strong>Score:</strong> <?= htmlentities($broker['score']) ?></p>
                <p><strong>Regulation Status:</strong> <?= htmlentities($broker['regulation_status']) ?></p>
                <p><strong>Risk Labels:</strong> <?= nl2br(htmlentities($broker['risk_labels'])) ?></p>
                <p><strong>Years in Business:</strong> <?= htmlentities($broker['years_in_business']) ?></p>
                <p><strong>Flag URL:</strong> 
                    <?php if (!empty($broker['flag_url'])): ?>
                        <a href="<?= htmlentities($broker['flag_url']) ?>" target="_blank"><?= htmlentities($broker['flag_url']) ?></a>
                    <?php else: ?>
                        N/A
                    <?php endif; ?>
                </p>
                <p><strong>Detail URL:</strong> 
                    <?php if (!empty($broker['detail_url'])): ?>
                        <a href="<?= htmlentities($broker['detail_url']) ?>" target="_blank"><?= htmlentities($broker['detail_url']) ?></a>
                    <?php else: ?>
                        N/A
                    <?php endif; ?>
                </p>
                <p><strong>Created At:</strong> <?= htmlentities($broker['created_at']) ?></p>
            </div>
        </div>
    </div>
</div>

<a href="admin-broker-edit.php?id=<?= $broker['id'] ?>" class="btn btn-primary">Edit Broker</a>
<a href="blackbrokers.php" class="btn btn-secondary">Back to Brokers</a>

<?php include('footer.php'); ?>
