<?php 
session_start();
include('config.php');
include('includes/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('includes/footer.php');
    exit;
}

$id = (int)$_GET['id'];
$stmt = $dbh->prepare("SELECT * FROM brokers 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('includes/footer.php');
    exit;
}

$brokerLogoUrl = !empty($broker['logo']) ? '../uploads/' . ltrim($broker['logo'], '/') : '';
$brokerType = trim((string)($broker['broker_type'] ?? 'Broker'));
$brokerRegulator = trim((string)($broker['regulator'] ?? 'N/A'));
$brokerCountry = trim((string)($broker['country'] ?? 'Global'));
$brokerRank = trim((string)($broker['rank'] ?? '-'));
$brokerScore = isset($broker['score']) && $broker['score'] !== '' ? number_format((float)$broker['score'], 2) : '0.00';

include "includes/sidebar.php";
?>

<style>
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap');

body {
    background: var(--dark-900, #0b0c0e);
    font-family: 'Plus Jakarta Sans', sans-serif;
    color: var(--text-1, #edf0f7);
    margin: 0;
    padding: 0;
}

.admin-main-content {
    margin-left: var(--sw, 260px);
    padding: 100px 40px 40px;
    transition: all 0.3s ease;
}

.broker-container {
    max-width: 1120px;
    margin: 0 auto;
    background: var(--dark-700, #121212);
    border: 1px solid rgba(255,255,255,0.04);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0,0,0,0.25);
}

.broker-hero {
    position: relative;
    display: grid;
    grid-template-columns: 160px minmax(0, 1fr) 260px;
    gap: 24px;
    align-items: center;
    padding: 32px;
    background: linear-gradient(135deg, var(--dark-800, #101214), var(--dark-700, #121212));
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

.broker-hero::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at top right, rgba(245,197,24,0.08), transparent 28%);
    pointer-events: none;
}

.broker-hero-media {
    width: 180px;
    height: 140px;
    border-radius: 26px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.06);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
    z-index: 1;
}

.broker-hero-media img {
    width: auto;
    height: 35%;
    object-fit: cover;
}

.broker-hero-fallback {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Playfair Display', serif;
    font-size: 3rem;
    color: var(--yellow);
}

.broker-hero-copy,
.broker-hero-side {
    position: relative;
    z-index: 1;
}

.broker-kicker {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.62rem;
    letter-spacing: 3px;
    text-transform: uppercase;
    color: var(--yellow-dim, #c9a010);
    margin-bottom: 8px;
}

.broker-title {
    font-family: 'Playfair Display', serif;
    font-size: 34px;
    line-height: 1.15;
    margin: 0 0 10px;
    color: var(--text-1);
}

.broker-subtitle {
    margin: 0 0 18px;
    color: var(--text-2);
    font-size: 14px;
}

.broker-meta-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.meta-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 12px;
    border-radius: 999px;
    background: var(--yellow-glass, rgba(245,197,24,0.09));
    border: 1px solid var(--yellow-border, rgba(245,197,24,0.18));
    color: var(--yellow);
    font-size: 12px;
    font-weight: 700;
}

.meta-pill-soft {
    color: var(--text-2);
    background: rgba(255,255,255,0.03);
    border-color: rgba(255,255,255,0.05);
}

.broker-hero-side {
    display: grid;
    gap: 12px;
}

.hero-side-box {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.05);
    border-radius: 14px;
    padding: 14px 16px;
}

.hero-side-box span {
    display: block;
    font-size: 12px;
    color: var(--text-2);
    margin-bottom: 4px;
}

.hero-side-box strong {
    font-size: 15px;
    color: var(--text-1);
}

.broker-body {
    padding: 28px 32px 34px;
}

.section-head {
    margin: 0 0 18px;
    padding-top: 8px;
}

.section-head h2 {
    font-family: 'Playfair Display', serif;
    font-size: 22px;
    margin: 0 0 6px;
    color: var(--text-1);
}

.section-head p {
    margin: 0;
    color: var(--text-2);
    font-size: 13px;
}

.broker-form {
    position: relative;
}

.form-grid {
    display: grid;
    gap: 16px;
}

.two-col { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.three-col { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
}

.field label {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-2);
}

.input,
.form-select {
    width: 100%;
    min-height: 46px;
    background: var(--dark-600, #1c2028);
    color: var(--text-1);
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 12px;
    padding: 11px 14px;
    font-size: 14px;
}

.input:focus,
.form-select:focus {
    outline: none;
    border-color: var(--yellow-border);
    box-shadow: 0 0 0 4px var(--yellow-glass);
}

.chip-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 4px 0 18px;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    border-radius: 999px;
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.06);
    color: var(--text-2);
    cursor: pointer;
}

.chip input { accent-color: var(--yellow); }

.broker-separator {
    height: 1px;
    background: rgba(255,255,255,0.06);
    margin: 24px 0;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.info-card {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.05);
    border-radius: 14px;
    padding: 16px;
}

.info-card span {
    display: block;
    font-size: 12px;
    color: var(--text-2);
    margin-bottom: 6px;
}

.info-card strong,
.info-card div {
    font-size: 15px;
    color: var(--text-1);
    line-height: 1.6;
}

.logo-inline {
    display: inline-flex;
    align-items: center;
    gap: 10px;
}

.logo-inline img {
    max-height: 44px;
    max-width: 120px;
    object-fit: contain;
}

.actions {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-top: 8px;
    flex-wrap: wrap;
    border-top: 1px solid rgba(255,255,255,0.05);
    padding-top: 20px;
}

.btn-save,
.btn-cancel {
    min-height: 44px;
    padding: 11px 18px;
    border-radius: 12px;
    border: 1px solid transparent;
    font-weight: 700;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.btn-save {
    background: linear-gradient(135deg, var(--yellow), var(--yellow-bright));
    color: var(--dark-900);
    box-shadow: 0 12px 24px rgba(245,197,24,0.18);
}

.btn-save:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 28px rgba(245,197,24,0.24);
}

.btn-cancel {
    background: var(--dark-600);
    color: var(--text-2);
    border-color: rgba(255,255,255,0.07);
}

.btn-cancel:hover {
    transform: translateY(-1px);
    color: var(--text-1);
    border-color: rgba(245,197,24,0.18);
}

@media (max-width: 1080px) {
    .broker-hero {
        grid-template-columns: 120px 1fr;
    }

    .broker-hero-side {
        grid-column: 1 / -1;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 900px) {
    .two-col,
    .three-col,
    .info-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .admin-main-content {
        margin-left: 0;
        padding: 20px 14px;
    }

    .broker-hero,
    .broker-body {
        padding: 20px;
    }

    .broker-hero {
        grid-template-columns: 1fr;
    }

    .broker-hero-media {
        width: 100px;
        height: 100px;
    }

    .broker-title {
        font-size: 28px;
    }

    .broker-hero-side {
        grid-template-columns: 1fr;
    }

    .actions {
        flex-direction: column;
        align-items: stretch;
    }

    .btn-save,
    .btn-cancel {
        width: 100%;
    }
}
</style>

<div class="admin-main-content">
    <div class="broker-container">
        <div class="broker-hero">
            <div class="broker-hero-media">
                <?php if (!empty($brokerLogoUrl)): ?>
                    <img src="<?= htmlspecialchars($brokerLogoUrl) ?>" alt="<?= htmlspecialchars($broker['name']) ?> logo">
                <?php else: ?>
                    <div class="broker-hero-fallback"><?= strtoupper(substr($broker['name'] ?? 'B', 0, 1)) ?></div>
                <?php endif; ?>
            </div>

            <div class="broker-hero-copy">
                <div class="broker-kicker">Broker Profile</div>
                <h1 class="broker-title"><?= htmlspecialchars($broker['name']) ?></h1>
                <p class="broker-subtitle">View core broker intelligence and open the edit screen when you need to update data.</p>
                <div class="broker-meta-row">
                    <span class="meta-pill"><?= htmlspecialchars($brokerType) ?></span>
                    <span class="meta-pill meta-pill-soft">Score: <?= htmlspecialchars($brokerScore) ?></span>
                    <span class="meta-pill meta-pill-soft">Rank: <?= htmlspecialchars($brokerRank) ?></span>
                    <span class="meta-pill meta-pill-soft"><?= htmlspecialchars($brokerCountry) ?></span>
                </div>
            </div>

            <div class="broker-hero-side">
                <div class="hero-side-box">
                    <span>Regulator</span>
                    <strong><?= htmlspecialchars($brokerRegulator) ?></strong>
                </div>
                <div class="hero-side-box">
                    <span>Overall Rating</span>
                    <strong><?= htmlspecialchars(number_format((float)($broker['overall_rating'] ?? 0), 2)) ?></strong>
                </div>
            </div>
        </div>

        <div class="broker-body">
            <div class="section-head">
                <h2>Broker Snapshot</h2>
                <p>Readonly profile data presented in a structured, article-like format.</p>
            </div>

            <div class="info-grid">
                <div class="info-card">
                    <span>Name</span>
                    <strong><?= htmlspecialchars($broker['name']) ?></strong>
                </div>
                <div class="info-card">
                    <span>Broker Type</span>
                    <strong><?= htmlspecialchars($brokerType) ?></strong>
                </div>
                <div class="info-card">
                    <span>Score / Spread</span>
                    <strong><?= htmlspecialchars($brokerScore) ?> / <?= htmlspecialchars(number_format((float)($broker['spread'] ?? 0), 2)) ?></strong>
                </div>
                <div class="info-card">
                    <span>Created At</span>
                    <strong><?= htmlspecialchars($broker['created_at'] ?? 'N/A') ?></strong>
                </div>
            </div>

            <div class="broker-separator"></div>

            <div class="section-head">
                <h2>Trading Access</h2>
                <p>Market exposure and operational details.</p>
            </div>

            <div class="info-grid">
                <div class="info-card">
                    <span>Markets Enabled</span>
                    <div>
                        Equity: <?= $broker['equity'] ? 'Yes' : 'No' ?><br>
                        Commodity: <?= $broker['commodity'] ? 'Yes' : 'No' ?><br>
                        Currency: <?= $broker['currency'] ? 'Yes' : 'No' ?><br>
                        Futures: <?= $broker['futures'] ? 'Yes' : 'No' ?><br>
                        Options: <?= $broker['options'] ? 'Yes' : 'No' ?>
                    </div>
                </div>
                <div class="info-card">
                    <span>Rank / Experience</span>
                    <strong><?= htmlspecialchars($brokerRank) ?> | <?= htmlspecialchars($broker['experience_years'] ?? 'N/A') ?> years</strong>
                </div>
                <div class="info-card">
                    <span>Assets</span>
                    <div><?= nl2br(htmlspecialchars($broker['assets'] ?? 'N/A')) ?></div>
                </div>
                <div class="info-card">
                    <span>Risk Level</span>
                    <strong><?= htmlspecialchars($broker['risk_level'] ?? 'N/A') ?></strong>
                </div>
            </div>

            <div class="broker-separator"></div>

            <div class="section-head">
                <h2>Pricing & Regulation</h2>
                <p>Charges, platform data, and compliance signals.</p>
            </div>

            <div class="info-grid">
                <div class="info-card">
                    <span>Account Charges</span>
                    <div>
                        Opening: <?= htmlspecialchars($broker['account_opening_charge'] ?? 'N/A') ?><br>
                        Maintenance: <?= htmlspecialchars($broker['account_maintenance_charge'] ?? 'N/A') ?>
                    </div>
                </div>
                <div class="info-card">
                    <span>Brokerage</span>
                    <div>
                        Delivery: <?= htmlspecialchars($broker['equity_delivery_brokerage'] ?? 'N/A') ?><br>
                        Intraday: <?= htmlspecialchars($broker['equity_intraday_brokerage'] ?? 'N/A') ?>
                    </div>
                </div>
                <div class="info-card">
                    <span>Software / License / Avg Transaction</span>
                    <div>
                        <?= htmlspecialchars(number_format((float)($broker['software'] ?? 0), 2)) ?> / <?= htmlspecialchars(number_format((float)($broker['license'] ?? 0), 2)) ?> / <?= htmlspecialchars(number_format((float)($broker['average_transaction'] ?? 0), 2)) ?>
                    </div>
                </div>
                <div class="info-card">
                    <span>Contact / Period / Country</span>
                    <div>
                        <?= htmlspecialchars($broker['contact_no'] ?? 'N/A') ?><br>
                        <?= htmlspecialchars($broker['operating_period'] ?? 'N/A') ?><br>
                        <?= htmlspecialchars($brokerCountry) ?>
                    </div>
                </div>
                <div class="info-card">
                    <span>Regulation Level</span>
                    <strong><?= htmlspecialchars($broker['regulation_level'] ?? 'N/A') ?></strong>
                </div>
                <div class="info-card">
                    <span>Regulator Logo</span>
                    <div class="logo-inline">
                        <?php if (!empty($broker['regulator_logo'])): ?>
                            <img src="<?= htmlspecialchars($brokerLogoUrl) ?>" alt="Regulator Logo">
                        <?php else: ?>
                            N/A
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="actions">
                <a href="admin-broker-edit.php?id=<?= (int)$broker['id'] ?>" class="btn-save">Edit Broker</a>
                <a href="brokers.php" class="btn-cancel">Back to Brokers</a>
            </div>
        </div>
    </div>
</div>

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