<?php
/**
 * Super Admin — Branch Details (single branch overview)
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$branch = $branch ?? [];
$stats  = $stats  ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;">
        <a href="<?= $bp ?>/super-admin/branches" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;display:inline-flex;align-items:center;gap:4px;">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>
            Back to Branches
        </a>
        <a href="<?= $bp ?>/super-admin/branches/<?= (int) ($branch['id'] ?? 0) ?>/edit" style="background:#f39c12;color:#fff;text-decoration:none;padding:8px 16px;border-radius:6px;font-size:0.85rem;font-weight:600;">Edit Branch</a>
    </div>

    <!-- Header -->
    <div class="admin-card" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border-radius:12px;padding:24px;margin-bottom:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:16px;">
            <div>
                <h1 style="margin:0;font-size:1.4rem;font-family:'Poppins',sans-serif;"><?= htmlspecialchars($branch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h1>
                <p style="margin:6px 0 0;opacity:0.9;font-size:0.9rem;">
                    <code style="background:rgba(255,255,255,0.25);padding:2px 10px;border-radius:4px;"><?= htmlspecialchars($branch['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></code>
                    · <?= htmlspecialchars($branch['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($branch['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                </p>
            </div>
            <div style="text-align:right;">
                <span style="display:inline-block;padding:6px 14px;border-radius:20px;font-size:0.78rem;font-weight:600;text-transform:uppercase;background:rgba(255,255,255,0.2);"><?= htmlspecialchars($branch['status'] ?? 'pending', ENT_QUOTES, 'UTF-8') ?></span>
                <?php if (!empty($branch['is_verified'])): ?>
                    <div style="font-size:0.78rem;margin-top:6px;opacity:0.9;">&#10003; Verified</div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Stats Row -->
    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:14px;margin-bottom:20px;">
        <div style="background:#fff;border-radius:10px;padding:18px;text-align:center;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#27ae60;font-family:'Poppins',sans-serif;"><?= (int) ($stats['students'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Students</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;text-align:center;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#6c3fe8;font-family:'Poppins',sans-serif;"><?= (int) ($stats['certificates'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Certificates</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;text-align:center;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#f39c12;font-family:'Poppins',sans-serif;"><?= (int) ($stats['exams'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Exams</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;text-align:center;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#16a085;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['fee_collected'] ?? 0), 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Fee Collected</div>
        </div>
    </div>

    <!-- Details Grid -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
        <!-- Branch Info -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 14px;font-size:1rem;color:#2c1e4a;">Branch Information</h2>
            <table style="width:100%;font-size:0.86rem;">
                <tr><td style="padding:6px 0;color:#666;">Phone</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Address</td><td style="padding:6px 0;text-align:right;font-weight:600;max-width:240px;"><?= htmlspecialchars($branch['address'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Opening Hours</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['opening_time'] ?? '—', ENT_QUOTES, 'UTF-8') ?> - <?= htmlspecialchars($branch['closing_time'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Running Since</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['running_since'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Referral Code</td><td style="padding:6px 0;text-align:right;font-weight:600;"><code style="background:#f0f0f0;padding:2px 8px;border-radius:4px;"><?= htmlspecialchars($branch['referral_code'] ?? '—', ENT_QUOTES, 'UTF-8') ?></code></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Referred By</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['referred_by_name'] ?? 'Direct', ENT_QUOTES, 'UTF-8') ?> <?= !empty($branch['referred_by_code']) ? '(' . htmlspecialchars($branch['referred_by_code'], ENT_QUOTES, 'UTF-8') . ')' : '' ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Created At</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= date('M j, Y g:i A', strtotime($branch['created_at'] ?? 'now')) ?></td></tr>
            </table>
        </div>

        <!-- Owner & Admin Info -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 14px;font-size:1rem;color:#2c1e4a;">Owner & Admin</h2>
            <table style="width:100%;font-size:0.86rem;">
                <tr><td colspan="2" style="padding:6px 0;color:#6c3fe8;font-weight:600;text-transform:uppercase;font-size:0.74rem;">Owner</td></tr>
                <tr><td style="padding:6px 0;color:#666;">Name</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['owner_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Phone</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['owner_phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Email</td><td style="padding:6px 0;text-align:right;font-weight:600;font-size:0.8rem;"><?= htmlspecialchars($branch['owner_email'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td colspan="2" style="padding:14px 0 6px;color:#6c3fe8;font-weight:600;text-transform:uppercase;font-size:0.74rem;">Branch Admin User</td></tr>
                <tr><td style="padding:6px 0;color:#666;">Name</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['admin_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Email</td><td style="padding:6px 0;text-align:right;font-weight:600;font-size:0.8rem;"><?= htmlspecialchars($branch['admin_email'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Phone</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['admin_phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Account Status</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($branch['admin_status'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Joined</td><td style="padding:6px 0;text-align:right;font-weight:600;font-size:0.8rem;"><?= !empty($branch['admin_created_at']) ? date('M j, Y', strtotime($branch['admin_created_at'])) : '—' ?></td></tr>
            </table>
        </div>
    </div>
</div>
