<?php
require_once '../../includes/header.php';
require_once '../../classes/MraEisOnboarding.php';
require_once '../../classes/MraEisCompliance.php';

if (!$auth->isSuperAdmin($_SESSION['user_id'])) {
    header('Location: ../../unauthorized.php');
    exit;
}

$eisSetup = new MraEisOnboarding($conn);
$compliance = new MraEisCompliance($conn);
 
$message = null;
$messageType = 'success';

$terminals = $eisSetup->all();
$selectedId = isset($_GET['terminal_id']) ? (int)$_GET['terminal_id'] : 0;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'refresh_configuration') {
    $selectedId = (int)($_POST['terminal_id'] ?? 0);
    try {
        $compliance->refreshLatestConfiguration($selectedId);
        $message = 'Latest configuration downloaded from MRA EIS.';
        $messageType = 'success';
    } catch (Throwable $e) {
        $message = $e->getMessage();
        $messageType = 'danger';
    }
}

if (!$selectedId && !empty($terminals)) {
    $selectedId = (int)$terminals[0]['id'];
}

$summary = null;
if ($selectedId) {
    try {
        $summary = $compliance->configurationSummary($selectedId);
    } catch (Throwable $e) {
        $message = $message ?? $e->getMessage();
        $messageType = $message ? $messageType : 'danger';
    }
}

$selectedTerminal = $summary['terminal'] ?? null;
$taxRateCount = $summary ? count($summary['global']['tax_rates']) : 0;
$levyRateCount = $summary ? count($summary['global']['levy_rates']) : 0;
$standardVatRate = null;
if ($summary) {
    foreach ($summary['global']['tax_rates'] as $rate) {
        if (strtoupper(trim((string)($rate['id'] ?? ''))) === 'A' && is_numeric($rate['rate'] ?? null)) {
            $standardVatRate = (float)$rate['rate'];
            break;
        }
    }
}

function eis_config_yesno($value) {
    if ($value === null) {
        return '<span class="eis-pill eis-pill-muted">Unknown</span>';
    }
    return $value
        ? '<span class="eis-pill eis-pill-good">Yes</span>'
        : '<span class="eis-pill eis-pill-bad">No</span>';
}
?>

<style>
    body { background: #f1f3f6; }
    .eis-shell { max-width: 860px; margin: 30px auto 60px; padding: 0 15px; }
    .eis-topbar { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 24px; }
    .eis-title { font-weight: 700; color: #172033; letter-spacing: -0.01em; }
    .eis-subtitle { color: #667085; font-size: 0.92rem; max-width: 560px; }
    .eis-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
    .eis-actions .btn { white-space: nowrap; }

    .eis-doc { background: #fff; border-radius: 10px; box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04); border: 1px solid #e7e9ee; overflow: hidden; }

    .eis-terminal-bar { padding: 18px 28px; border-bottom: 1px solid #eef0f4; background: #fafbfc; }
    .eis-terminal-bar label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: #8a94a6; font-weight: 600; margin-bottom: 6px; }

    .eis-refresh-bar { padding: 14px 28px; border-bottom: 1px solid #eef0f4; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
    .eis-refresh-bar .text-muted { font-size: 0.85rem; }

    .eis-section { padding: 26px 28px; border-bottom: 1px solid #eef0f4; }
    .eis-section:last-child { border-bottom: none; }
    .eis-section-title { font-size: 0.98rem; font-weight: 700; color: #172033; margin: 0 0 4px; display: flex; align-items: center; gap: 8px; }
    .eis-section-title i { color: #6b7280; font-size: 0.85rem; }
    .eis-section-desc { color: #8a94a6; font-size: 0.82rem; margin-bottom: 18px; }

    .eis-fields { display: grid; grid-template-columns: 220px 1fr; row-gap: 12px; column-gap: 16px; }
    .eis-fields dt { color: #667085; font-weight: 600; font-size: 0.86rem; align-self: center; }
    .eis-fields dd { margin: 0; color: #172033; font-size: 0.92rem; align-self: center; word-break: break-word; }

    .eis-pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; }
    .eis-pill-good { background: #e6f6ed; color: #147a3d; }
    .eis-pill-bad { background: #fdecec; color: #b3261e; }
    .eis-pill-muted { background: #eef0f4; color: #667085; }
    .eis-pill-env-prod { background: #fdecec; color: #b3261e; }
    .eis-pill-env-sandbox { background: #eef0f4; color: #475467; }

    .eis-count { display: inline-block; margin-left: 8px; background: #eef0f4; color: #475467; border-radius: 999px; padding: 1px 9px; font-size: 0.76rem; font-weight: 600; vertical-align: middle; }

    .eis-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
    .eis-table th { text-align: left; color: #8a94a6; font-weight: 600; font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.03em; padding: 8px 10px; border-bottom: 1px solid #eef0f4; }
    .eis-table td { padding: 9px 10px; border-bottom: 1px solid #f4f5f7; color: #172033; }
    .eis-table tr:last-child td { border-bottom: none; }
    .eis-table-wrap { overflow-x: auto; }

    .eis-empty { color: #8a94a6; font-size: 0.86rem; padding: 6px 0; }

    @media (max-width: 640px) {
        .eis-topbar { flex-direction: column; }
        .eis-actions { justify-content: flex-start; width: 100%; }
        .eis-actions .btn { flex: 1 1 auto; }
        .eis-fields { grid-template-columns: 1fr; row-gap: 4px; }
        .eis-fields dt { margin-top: 8px; }
    }
</style>

<style>
    :root {
        --eis-ink: #0f172a;
        --eis-muted: #64748b;
        --eis-line: #e2e8f0;
        --eis-primary: #2563eb;
        --eis-primary-dark: #1d4ed8;
        --eis-success: #059669;
        --eis-warning: #d97706;
        --eis-surface: #ffffff;
    }
    body {
        background:
            radial-gradient(circle at 8% 0%, rgba(37, 99, 235, .09), transparent 26rem),
            linear-gradient(180deg, #f8fafc 0, #f1f5f9 100%);
        color: var(--eis-ink);
    }
    .eis-shell { max-width: 1240px; margin: 28px auto 72px; padding: 0 22px; }
    .eis-topbar {
        position: relative; overflow: hidden; align-items: center; margin-bottom: 22px;
        padding: 30px 32px; border-radius: 20px;
        background: linear-gradient(125deg, #0f172a 0%, #172554 55%, #1d4ed8 145%);
        box-shadow: 0 20px 45px rgba(15, 23, 42, .16);
    }
    .eis-topbar::after {
        content: ''; position: absolute; width: 260px; height: 260px; right: -85px; top: -130px;
        border-radius: 50%; background: rgba(96, 165, 250, .14); border: 1px solid rgba(255,255,255,.12);
    }
    .eis-topbar > * { position: relative; z-index: 1; }
    .eis-title { color: #fff; font-size: clamp(1.55rem, 2.4vw, 2.15rem); font-weight: 800; letter-spacing: -.035em; }
    .eis-eyebrow { display: block; margin-bottom: 7px; color: #93c5fd; font-size: .69rem; font-weight: 800; text-transform: uppercase; letter-spacing: .14em; }
    .eis-title i { color: #93c5fd; margin-right: 8px; }
    .eis-subtitle { color: #cbd5e1; max-width: 650px; line-height: 1.6; }
    .eis-actions { align-items: center; }
    .eis-actions .btn {
        border-color: rgba(255,255,255,.23) !important; color: #f8fafc !important;
        background: rgba(255,255,255,.08); border-radius: 10px; padding: 8px 12px;
        backdrop-filter: blur(8px); font-weight: 600;
    }
    .eis-actions .btn:hover { background: #fff; color: #172554 !important; border-color: #fff !important; transform: translateY(-1px); }
    .eis-overview { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
    .eis-stat {
        display: flex; align-items: center; gap: 13px; min-height: 96px; padding: 18px;
        background: rgba(255,255,255,.92); border: 1px solid rgba(226,232,240,.95); border-radius: 15px;
        box-shadow: 0 6px 20px rgba(15,23,42,.055);
    }
    .eis-stat-icon {
        width: 43px; height: 43px; flex: 0 0 43px; display: grid; place-items: center;
        border-radius: 12px; background: #eff6ff; color: #2563eb; font-size: 1rem;
    }
    .eis-stat:nth-child(2) .eis-stat-icon { background: #ecfdf5; color: #059669; }
    .eis-stat:nth-child(3) .eis-stat-icon { background: #fff7ed; color: #ea580c; }
    .eis-stat:nth-child(4) .eis-stat-icon { background: #f5f3ff; color: #7c3aed; }
    .eis-stat-label { display: block; color: #64748b; font-size: .73rem; font-weight: 700; text-transform: uppercase; letter-spacing: .055em; }
    .eis-stat-value { display: block; margin-top: 3px; color: #0f172a; font-size: 1.12rem; font-weight: 800; line-height: 1.2; overflow-wrap: anywhere; }
    .eis-doc { display: block; background: #fff; border: 1px solid var(--eis-line); border-radius: 4px; box-shadow: 0 8px 24px rgba(15,23,42,.08); overflow: hidden; }
    .eis-terminal-bar, .eis-refresh-bar, .eis-section {
        background: var(--eis-surface); border: 1px solid var(--eis-line) !important;
        border-radius: 16px; box-shadow: 0 7px 24px rgba(15,23,42,.05);
    }
    .eis-terminal-bar { grid-column: 1 / -1; padding: 20px 22px; }
    .eis-terminal-bar label { color: #475569; font-size: .72rem; letter-spacing: .065em; }
    .eis-terminal-bar .form-select { min-height: 44px; border-color: #cbd5e1; border-radius: 10px; font-weight: 600; color: #1e293b; }
    .eis-terminal-bar .btn { min-height: 44px; border-radius: 10px; font-weight: 700; }
    .eis-refresh-bar { grid-column: 1 / -1; justify-content: space-between; padding: 16px 20px; }
    .eis-refresh-bar::before { content: 'Configuration synchronization'; color: #334155; font-size: .88rem; font-weight: 700; margin-right: auto; }
    .eis-refresh-bar .btn { border: 0; border-radius: 10px; padding: 9px 15px; background: linear-gradient(135deg, var(--eis-primary), var(--eis-primary-dark)); font-weight: 700; box-shadow: 0 6px 14px rgba(37,99,235,.2); }
    .eis-section { padding: 24px; }
    .eis-section.pb-0 { grid-column: 1 / -1; padding: 0 !important; border: 0 !important; background: transparent; box-shadow: none; }
    .eis-section.pb-0 .alert { border-radius: 14px; border: 1px solid #fed7aa; }
    .eis-section-title { color: #0f172a; font-size: 1rem; letter-spacing: -.01em; }
    .eis-section-title i { width: 30px; height: 30px; display: inline-grid; place-items: center; border-radius: 9px; background: #eff6ff; color: #2563eb; }
    .eis-section-desc { margin: 7px 0 20px 38px; color: #64748b; line-height: 1.5; }
    .eis-fields { grid-template-columns: minmax(135px, .7fr) minmax(0, 1.3fr); row-gap: 0; column-gap: 16px; }
    .eis-fields dt, .eis-fields dd { min-height: 43px; display: flex; align-items: center; padding: 9px 0; border-bottom: 1px solid #f1f5f9; }
    .eis-fields dt { color: #64748b; font-size: .79rem; }
    .eis-fields dd { color: #1e293b; font-size: .87rem; font-weight: 600; }
    .eis-pill { padding: 4px 10px; font-size: .72rem; letter-spacing: .02em; }
    .eis-pill-good { background: #dcfce7; color: #166534; }
    .eis-pill-bad, .eis-pill-env-prod { background: #fee2e2; color: #991b1b; }
    .eis-pill-env-sandbox { background: #dbeafe; color: #1d4ed8; }
    .eis-count { margin-left: auto; background: #f1f5f9; color: #334155; padding: 3px 9px; }
    .eis-table-wrap { border: 1px solid #e2e8f0; border-radius: 12px; }
    .eis-table th { padding: 11px 12px; background: #f8fafc; color: #64748b; border-bottom-color: #e2e8f0; }
    .eis-table td { padding: 12px; color: #334155; }
    .eis-table tbody tr:hover { background: #f8fbff; }
    .eis-empty { padding: 24px; text-align: center; border: 1px dashed #cbd5e1; border-radius: 12px; background: #f8fafc; }
    .alert { border-radius: 13px; }
    @media (max-width: 960px) {
        .eis-overview { grid-template-columns: repeat(2, minmax(0, 1fr)); }
        .eis-doc { grid-template-columns: 1fr; }
        .eis-terminal-bar, .eis-refresh-bar, .eis-section.pb-0 { grid-column: 1; }
    }
    @media (max-width: 640px) {
        .eis-shell { padding: 0 13px; margin-top: 14px; }
        .eis-topbar { padding: 24px 20px; border-radius: 16px; }
        .eis-overview { grid-template-columns: 1fr; gap: 10px; }
        .eis-stat { min-height: 78px; padding: 14px; }
        .eis-section { padding: 20px 17px; }
        .eis-section-desc { margin-left: 0; }
        .eis-fields { grid-template-columns: 1fr; }
        .eis-fields dt { min-height: auto; padding-bottom: 2px; border-bottom: 0; }
        .eis-fields dd { min-height: auto; padding-top: 2px; padding-bottom: 10px; }
        .eis-refresh-bar::before { width: 100%; }
    }
</style>

<div class="eis-shell">
    <?php $eisNavCurrent = 'configuration'; require __DIR__ . '/../../includes/eis_admin_nav.php'; ?>

    <div class="eis-topbar">
        <div>
            <span class="eis-eyebrow">Administration report</span>
            <h2 class="eis-title mb-1"><i class="fas fa-file-alt"></i> MRA EIS Configuration Report</h2>
            <p class="eis-subtitle mb-0">Taxpayer, terminal, tax and levy configuration currently recorded for the selected EIS terminal.</p>
        </div>
        <div class="eis-actions">
            <button type="button" class="btn btn-outline-dark btn-sm" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
            <a href="../dashboard/admin.php" class="btn btn-outline-secondary btn-sm"><i class="fas fa-arrow-left"></i> Dashboard</a>
        </div>
    </div>

    <div class="eis-report-meta" aria-label="Report details">
        <div><small>Report number</small><strong>EIS-CFG-<?php echo str_pad((string)$selectedId, 4, '0', STR_PAD_LEFT); ?></strong></div>
        <div><small>Generated</small><strong><?php echo htmlspecialchars(date('d M Y, H:i')); ?></strong></div>
        <div><small>Environment</small><strong><?php echo htmlspecialchars($selectedTerminal ? ucfirst((string)$selectedTerminal['environment']) : 'Not selected'); ?></strong></div>
        <div><small>Configuration state</small><strong><?php echo !empty($summary['has_downloaded_config']) ? 'Synchronized' : 'Awaiting sync'; ?></strong></div>
    </div>

    <?php if ($message): ?>
        <div class="alert alert-<?php echo htmlspecialchars($messageType); ?> alert-dismissible fade show" role="alert">
            <?php echo htmlspecialchars($message); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <div class="eis-overview" aria-label="Configuration overview">
        <div class="eis-stat">
            <span class="eis-stat-icon"><i class="fas fa-plug"></i></span>
            <span>
                <span class="eis-stat-label">Terminal status</span>
                <span class="eis-stat-value"><?php echo htmlspecialchars($selectedTerminal ? ucfirst((string)$selectedTerminal['status']) : 'Not selected'); ?></span>
            </span>
        </div>
        <div class="eis-stat">
            <span class="eis-stat-icon"><i class="fas fa-percentage"></i></span>
            <span>
                <span class="eis-stat-label">Standard VAT</span>
                <span class="eis-stat-value"><?php echo $standardVatRate !== null ? htmlspecialchars(number_format($standardVatRate, 2)) . '%' : 'Not available'; ?></span>
            </span>
        </div>
        <div class="eis-stat">
            <span class="eis-stat-icon"><i class="fas fa-layer-group"></i></span>
            <span>
                <span class="eis-stat-label">Rates configured</span>
                <span class="eis-stat-value"><?php echo (int)$taxRateCount; ?> tax &middot; <?php echo (int)$levyRateCount; ?> levy</span>
            </span>
        </div>
        <div class="eis-stat">
            <span class="eis-stat-icon"><i class="fas fa-cloud-download-alt"></i></span>
            <span>
                <span class="eis-stat-label">Configuration state</span>
                <span class="eis-stat-value"><?php echo !empty($summary['has_downloaded_config']) ? 'Synchronized' : 'Awaiting sync'; ?></span>
            </span>
        </div>
    </div>

    <?php if (empty($terminals)): ?>
        <div class="alert alert-info">No MRA EIS terminals have been prepared yet. Set one up on the Onboarding page first.</div>
    <?php else: ?>
        <div class="eis-doc">
            <div class="eis-terminal-bar">
                <form method="GET" class="row g-2 align-items-end">
                    <div class="col-md-9">
                        <label class="form-label mb-1">Terminal</label>
                        <select name="terminal_id" class="form-select" onchange="this.form.submit()">
                            <?php foreach ($terminals as $terminal): ?>
                                <option value="<?php echo (int)$terminal['id']; ?>" <?php echo (int)$terminal['id'] === $selectedId ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars(($terminal['branch_name'] ?? 'Unknown branch') . ' - ' . $terminal['terminal_label'] . ' (' . ucfirst($terminal['environment']) . ')'); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <button type="submit" class="btn btn-outline-primary w-100"><i class="fas fa-eye"></i> View</button>
                    </div>
                </form>
            </div>

            <?php if ($summary): ?>
                <?php $terminal = $summary['terminal']; ?>

                <?php if (!$summary['has_downloaded_config']): ?>
                    <div class="eis-section pb-0" style="border-bottom:none; padding-bottom:0;">
                        <div class="alert alert-warning mb-0">
                            No configuration has been downloaded from MRA EIS for this terminal yet. Values below fall back to what was captured during activation/confirmation. Use "Refresh from MRA" to pull the latest.
                        </div>
                    </div>
                <?php endif; ?>

                <div class="eis-refresh-bar">
                    <form method="POST" class="d-flex align-items-center gap-2 mb-0" id="eisRefreshForm">
                        <input type="hidden" name="action" value="refresh_configuration">
                        <input type="hidden" name="terminal_id" value="<?php echo (int)$selectedId; ?>">
                        <button type="submit" class="btn btn-primary btn-sm" <?php echo (trim((string)$terminal['jwt_token']) === '') ? 'disabled' : ''; ?>>
                            <i class="fas fa-sync"></i> Refresh from MRA
                        </button>
                    </form>
                    <?php if (trim((string)$terminal['jwt_token']) === ''): ?>
                        <span class="text-muted">Activate and confirm this terminal before refreshing its configuration.</span>
                    <?php else: ?>
                        <span class="text-muted">Every activated terminal also syncs automatically (at least every <?php echo (int)MraEisCompliance::AUTO_CONFIG_SYNC_INTERVAL_HOURS; ?> hours) when an admin visits the dashboard - use this button only if you need the latest configuration immediately.</span>
                    <?php endif; ?>
                </div>

                <div class="eis-section">
                    <h3 class="eis-section-title"><i class="fas fa-id-card"></i> Taxpayer Information</h3>
                    <p class="eis-section-desc">Registration details reported by MRA for this taxpayer.</p>
                    <dl class="eis-fields">
                        <dt>Taxpayer TIN</dt><dd><?php echo htmlspecialchars($summary['taxpayer']['taxpayer_tin'] ?: 'Not set'); ?></dd>
                        <dt>Taxpayer ID</dt><dd><?php echo htmlspecialchars((string)($summary['taxpayer']['taxpayer_id'] ?? 'Not available')); ?></dd>
                        <dt>Taxpayer Name</dt><dd><?php echo htmlspecialchars((string)($summary['taxpayer']['taxpayer_name'] ?? 'Not available')); ?></dd>
                        <dt>Site ID</dt><dd><?php echo htmlspecialchars($summary['taxpayer']['site_id'] ?: 'Not set'); ?></dd>
                        <dt>VAT Registered</dt><dd><?php echo eis_config_yesno($summary['taxpayer']['is_vat_registered']); ?></dd>
                        <dt>Taxpayer Config Version</dt><dd><?php echo (int)$summary['taxpayer']['config_version']; ?></dd>
                        <?php if (!empty($summary['taxpayer']['activated_tax_rates'])): ?>
                        <dt>Activated Tax Rates</dt><dd><?php echo htmlspecialchars(implode(', ', array_map('strval', $summary['taxpayer']['activated_tax_rates']))); ?></dd>
                        <?php endif; ?>
                        <?php if (!empty($summary['taxpayer']['activated_levies'])): ?>
                        <dt>Activated Levies</dt><dd><?php echo htmlspecialchars(implode(', ', array_map('strval', $summary['taxpayer']['activated_levies']))); ?></dd>
                        <?php endif; ?>
                    </dl>
                </div>

                <div class="eis-section">
                    <h3 class="eis-section-title"><i class="fas fa-network-wired"></i> Terminal &amp; Global Config</h3>
                    <p class="eis-section-desc">Versioning and connection details for this specific terminal.</p>
                    <dl class="eis-fields">
                        <dt>MRA Terminal ID</dt><dd><?php echo htmlspecialchars((string)($summary['terminal_config']['terminal_id'] ?? 'Not issued')); ?></dd>
                        <dt>Environment</dt><dd><span class="eis-pill <?php echo $terminal['environment'] === 'production' ? 'eis-pill-env-prod' : 'eis-pill-env-sandbox'; ?>"><?php echo htmlspecialchars(ucfirst($terminal['environment'])); ?></span></dd>
                        <dt>API Base URL</dt><dd><?php echo htmlspecialchars($terminal['api_base_url']); ?></dd>
                        <dt>Global Config Version</dt><dd><?php echo (int)$summary['global']['config_version']; ?></dd>
                        <dt>Terminal Config Version</dt><dd><?php echo (int)$summary['terminal_config']['config_version']; ?></dd>
                        <?php if (!empty($summary['terminal_config']['offline_limit'])): ?>
                        <dt>Offline Limit</dt><dd><?php echo htmlspecialchars(json_encode($summary['terminal_config']['offline_limit'])); ?></dd>
                        <?php endif; ?>
                    </dl>
                </div>

                <div class="eis-section">
                    <h3 class="eis-section-title"><i class="fas fa-percentage"></i> Tax Rates <span class="eis-count"><?php echo count($summary['global']['tax_rates']); ?></span></h3>
                    <p class="eis-section-desc">Rate groups downloaded in the global configuration.</p>
                    <?php if (empty($summary['global']['tax_rates'])): ?>
                        <div class="eis-empty">No tax rates downloaded yet.</div>
                    <?php else: ?>
                        <div class="eis-table-wrap">
                            <table class="eis-table">
                                <thead><tr><th>ID</th><th>Name</th><th>Rate (%)</th></tr></thead>
                                <tbody>
                                    <?php foreach ($summary['global']['tax_rates'] as $rate): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars((string)($rate['id'] ?? '')); ?></td>
                                            <td><?php echo htmlspecialchars((string)($rate['name'] ?? '')); ?></td>
                                            <td><?php echo htmlspecialchars((string)($rate['rate'] ?? '')); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="eis-section">
                    <h3 class="eis-section-title"><i class="fas fa-hand-holding-usd"></i> Levy Rates <span class="eis-count"><?php echo count($summary['global']['levy_rates']); ?></span></h3>
                    <p class="eis-section-desc">Levies applied on top of tax, as configured by MRA.</p>
                    <?php if (empty($summary['global']['levy_rates'])): ?>
                        <div class="eis-empty">No levy rates downloaded yet.</div>
                    <?php else: ?>
                        <div class="eis-table-wrap">
                            <table class="eis-table">
                                <thead><tr><th>ID</th><th>Name</th><th>Rate</th><th>Charge Mode</th></tr></thead>
                                <tbody>
                                    <?php foreach ($summary['global']['levy_rates'] as $rate): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars((string)($rate['id'] ?? '')); ?></td>
                                            <td><?php echo htmlspecialchars((string)($rate['name'] ?? '')); ?></td>
                                            <td><?php echo htmlspecialchars((string)($rate['rate'] ?? '')); ?></td>
                                            <td><?php echo htmlspecialchars((string)($rate['chargeMode'] ?? '')); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
            <div class="eis-report-foot">
                <span>Generated by BR IMS &middot; MRA EIS Administration</span>
                <span>EIS-CFG-<?php echo str_pad((string)$selectedId, 4, '0', STR_PAD_LEFT); ?></span>
            </div>
        </div>
    <?php endif; ?>
</div>

<style>
/* Formal report presentation */
body { background: #edf0f3; }
.eis-shell { max-width: 1100px; margin: 26px auto 60px; }
.eis-topbar { display:grid; grid-template-columns:minmax(0,1fr) auto; align-items:start; margin-bottom:0; padding:32px 36px 25px; border:1px solid #d8dee6; border-top:6px solid #174ea6; border-radius:4px 4px 0 0; background:#fff; box-shadow:0 7px 22px rgba(15,23,42,.07); }
.eis-topbar::after { display:none; }
.eis-eyebrow,.eis-title i { color:#174ea6; }
.eis-title { color:#101828; font-size:clamp(1.45rem,2.3vw,1.95rem); }
.eis-subtitle { color:#667085; }
.eis-actions .btn { border-color:#cbd5e1 !important; color:#344054 !important; background:#fff; border-radius:6px; box-shadow:none; backdrop-filter:none; }
.eis-actions .btn:hover { background:#f8fafc; color:#174ea6 !important; border-color:#174ea6 !important; transform:none; }
.eis-report-meta { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); background:#f7f8fa; border:1px solid #d8dee6; border-top:0; box-shadow:0 7px 22px rgba(15,23,42,.07); }
.eis-report-meta > div { padding:13px 18px; border-right:1px solid #dfe3e8; }
.eis-report-meta > div:last-child { border-right:0; }
.eis-report-meta small { display:block; margin-bottom:3px; color:#667085; font-size:.65rem; font-weight:800; letter-spacing:.07em; text-transform:uppercase; }
.eis-report-meta strong { display:block; color:#172033; font-size:.82rem; overflow-wrap:anywhere; }
.eis-overview { grid-template-columns:repeat(4,minmax(0,1fr)); gap:0; margin:18px 0 0; border:1px solid #d8dee6; border-bottom:0; background:#fff; }
.eis-stat { min-height:82px; padding:15px 18px; border:0; border-right:1px solid #e2e8f0; border-radius:0; box-shadow:none; }
.eis-stat:last-child { border-right:0; }
.eis-stat-icon { width:35px; height:35px; flex-basis:35px; border-radius:50%; font-size:.82rem; }
.eis-stat-label { font-size:.65rem; }
.eis-stat-value { font-size:.92rem; }
.eis-doc { border-color:#d8dee6; border-radius:0 0 4px 4px; box-shadow:0 9px 25px rgba(15,23,42,.08); }
.eis-terminal-bar,.eis-refresh-bar,.eis-section { border:0 !important; border-bottom:1px solid #e2e8f0 !important; border-radius:0; box-shadow:none; background:#fff; }
.eis-terminal-bar { padding:18px 28px; background:#f8fafc; }
.eis-terminal-bar .form-select,.eis-terminal-bar .btn { min-height:40px; border-radius:5px; }
.eis-refresh-bar { padding:14px 28px; }
.eis-refresh-bar .btn { border-radius:5px; box-shadow:none; }
.eis-section { padding:25px 28px; }
.eis-section.pb-0 { padding:16px 28px 0 !important; border:0 !important; }
.eis-section-title { padding-bottom:9px; border-bottom:2px solid #174ea6; color:#101828; font-size:.88rem; text-transform:uppercase; letter-spacing:.025em; }
.eis-section-title i { width:auto; height:auto; background:transparent; color:#174ea6; }
.eis-section-desc { margin:8px 0 15px; }
.eis-fields { grid-template-columns:minmax(175px,.62fr) minmax(0,1.38fr); border:1px solid #e2e8f0; }
.eis-fields dt,.eis-fields dd { min-height:40px; padding:9px 12px; border-bottom:1px solid #e2e8f0; }
.eis-fields dt { background:#f8fafc; }
.eis-table-wrap { border-radius:0; }
.eis-table th { background:#f7f8fa; }
.eis-table tbody tr:hover { background:transparent; }
.eis-report-foot { display:flex; justify-content:space-between; gap:20px; padding:14px 28px; background:#f7f8fa; border-top:1px solid #dfe3e8; color:#667085; font-size:.69rem; }
@media (max-width:760px) {
 .eis-topbar { grid-template-columns:1fr; padding:25px 21px; }
 .eis-actions { margin-top:17px; }
 .eis-report-meta,.eis-overview { grid-template-columns:repeat(2,minmax(0,1fr)); }
 .eis-report-meta > div:nth-child(2),.eis-stat:nth-child(2) { border-right:0; }
 .eis-report-meta > div:nth-child(-n+2),.eis-stat:nth-child(-n+2) { border-bottom:1px solid #dfe3e8; }
 .eis-fields { grid-template-columns:1fr; }
}
@media (max-width:480px) {
 .eis-report-meta,.eis-overview { grid-template-columns:1fr; }
 .eis-report-meta > div,.eis-stat { border-right:0; border-bottom:1px solid #dfe3e8; }
 .eis-terminal-bar,.eis-refresh-bar,.eis-section { padding-left:18px; padding-right:18px; }
}
@media print {
 @page { size:A4 portrait; margin:7mm; }
 body { background:#fff !important; }
 body > nav,body > header,footer,.eisnav,.eis-actions,.eis-terminal-bar,.eis-refresh-bar,.eis-shell > .alert,.alert-dismissible .btn-close { display:none !important; }
 .eis-shell { max-width:none; margin:0; padding:0; }
 .eis-topbar,.eis-report-meta,.eis-overview,.eis-doc { box-shadow:none; }
 .eis-topbar { padding:10px 14px 8px; border-top-width:4px; }
 .eis-eyebrow { margin-bottom:2px; font-size:.52rem; }
 .eis-title { margin:0 !important; font-size:1.15rem; }
 .eis-subtitle { font-size:.62rem; line-height:1.25; }
 .eis-report-meta > div { padding:6px 9px; }
 .eis-report-meta small,.eis-stat-label { font-size:.48rem; }
 .eis-report-meta strong { font-size:.62rem; }
 .eis-overview { margin-top:7px; }
 .eis-stat { min-height:47px; padding:6px 9px; gap:6px; }
 .eis-stat-icon { width:24px; height:24px; flex-basis:24px; font-size:.58rem; }
 .eis-stat-value { margin-top:1px; font-size:.68rem; }
 .eis-doc { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); align-items:start; }
 .eis-section { padding:8px 10px; break-inside:avoid; }
 .eis-section.pb-0 { grid-column:1 / -1; padding:5px 10px 0 !important; }
 .eis-section.pb-0 .alert { margin:0; padding:5px 8px; font-size:.56rem; line-height:1.2; }
 .eis-section-title { margin-bottom:2px; padding-bottom:4px; font-size:.63rem; border-bottom-width:1px; }
 .eis-section-title i { font-size:.58rem; }
 .eis-section-desc { margin:2px 0 5px; font-size:.53rem; line-height:1.2; }
 .eis-count { padding:1px 5px; font-size:.5rem; }
 .eis-fields { grid-template-columns:minmax(105px,.72fr) minmax(0,1.28fr); }
 .eis-fields dt,.eis-fields dd { min-height:0; padding:3px 5px; font-size:.54rem; line-height:1.18; }
 .eis-pill { padding:1px 5px; font-size:.48rem; }
 .eis-table { font-size:.53rem; }
 .eis-table th { padding:4px 5px; font-size:.47rem; }
 .eis-table td { padding:3px 5px; line-height:1.15; }
 .eis-empty { padding:8px; font-size:.54rem; }
 .eis-report-foot { grid-column:1 / -1; padding:5px 10px; font-size:.48rem; }
 .eis-table thead { display:table-header-group; }
 .eis-table tr { break-inside:avoid; }
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function () {
    var form = document.getElementById('eisRefreshForm');
    if (!form) return;
    form.addEventListener('submit', function () {
        var button = form.querySelector('button[type="submit"]');
        if (!button || button.disabled) return;
        button.disabled = true;
        button.innerHTML = '<i class="fas fa-circle-notch fa-spin"></i> Syncing with MRA...';
    });
});
</script>

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