<?php
require_once __DIR__ . '/../config.php';
requireRole(['procurement']);

$pageTitle = 'Procurement Reports';
require_once __DIR__ . '/../includes/admin-header.php';

$activeTab = $_GET['tab'] ?? 'overview';

$filterQuarter = $_GET['quarter'] ?? '';
$filterMonth = $_GET['month'] ?? '';
$filterYear = $_GET['year'] ?? '';

$quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
$months = [
    '01' => 'January', '02' => 'February', '03' => 'March',
    '04' => 'April', '05' => 'May', '06' => 'June',
    '07' => 'July', '08' => 'August', '09' => 'September',
    '10' => 'October', '11' => 'November', '12' => 'December'
];
$years = ['2026', '2025', '2024', '2023', '2022'];

$filterLabel = 'Full Dataset';
if (!empty($filterYear)) {
    $filterLabel = $filterYear;
    if (!empty($filterQuarter)) {
        $filterLabel = $filterQuarter . ' ' . $filterYear;
    } elseif (!empty($filterMonth)) {
        $filterLabel = $months[$filterMonth] . ' ' . $filterYear;
    }
} elseif (!empty($filterQuarter)) {
    $filterLabel = $filterQuarter . ' 2025 (Default)';
}

$importVsLocal = [
    ['name' => 'Foreign Imports', 'value' => 4087, 'percentage' => 56, 'orders' => 141],
    ['name' => 'Local Purchases', 'value' => 3248, 'percentage' => 44, 'orders' => 1315],
];

$transactionTrends = [
    ['category' => 'General Goods', 'q4_2025' => 1104, 'q4_2024' => 1537, 'change' => -28],
    ['category' => 'Critical Stocks', 'q4_2025' => 351, 'q4_2024' => 435, 'change' => -19],
];

$paymentTerms = [
    ['name' => 'Prepayment', 'value' => 1321, 'current' => 21, 'target' => 10],
    ['name' => '14-day Credit', 'value' => 128, 'current' => 2, 'target' => 5],
    ['name' => '30+ day Credit', 'value' => 4944, 'current' => 77, 'target' => 85],
];

$supplierData = [
    ['category' => 'Feed', 'supplier' => 'In2 Feed', 'q4_2025' => 3015.2, 'q4_2024' => 4478.0, 'change' => -33],
    ['category' => 'Feed', 'supplier' => 'Penguin Fishing', 'q4_2025' => 75.5, 'q4_2024' => 71.9, 'change' => 5],
    ['category' => 'Capex', 'supplier' => 'ACT (Thailand) Co. Ltd', 'q4_2025' => 381.8, 'q4_2024' => 403.0, 'change' => -5],
    ['category' => 'Chemicals', 'supplier' => 'DF trading (Dyna Fog)', 'q4_2025' => 93.4, 'q4_2024' => 117.6, 'change' => -21],
    ['category' => 'R&M', 'supplier' => 'Under Control', 'q4_2025' => 70.3, 'q4_2024' => 105.6, 'change' => -33],
    ['category' => 'Medicines', 'supplier' => 'V-Tech', 'q4_2025' => 29.4, 'q4_2024' => 70.5, 'change' => -58],
];

$report1Data = [
    ['desc' => 'Value of Foreign Imports – US$000s', 'q4_25' => '4,087', 'q4_25_pct' => '56%', 'q4_24' => '6,390', 'q4_24_pct' => '60%', 'fy_26' => '3,806', 'fy_26_pct' => '60%'],
    ['desc' => 'No of Foreign purchasing orders', 'q4_25' => '141', 'q4_25_pct' => '10%', 'q4_24' => '241', 'q4_24_pct' => '12%', 'fy_26' => '150', 'fy_26_pct' => '11%'],
    ['desc' => 'Value of Local Purchases – US$000s', 'q4_25' => '3,248', 'q4_25_pct' => '44%', 'q4_24' => '4,267', 'q4_24_pct' => '40%', 'fy_26' => '2,551', 'fy_26_pct' => '40%'],
    ['desc' => 'No of Local purchasing orders', 'q4_25' => '1,315', 'q4_25_pct' => '90%', 'q4_24' => '1,731', 'q4_24_pct' => '88%', 'fy_26' => '1,274', 'fy_26_pct' => '89%'],
    ['desc' => 'Total purchases in period – US$000s', 'q4_25' => '7,335', 'q4_25_pct' => '100%', 'q4_24' => '10,657', 'q4_24_pct' => '100%', 'fy_26' => '6,357', 'fy_26_pct' => '100%'],
];

$report4Data = [
    ['supplier' => 'In2 Feed', 'category' => 'Feed', 'q4_25' => '3,015.2', 'q4_24' => '4,478.0', 'change' => '-33%', 'fy_26' => '2,088.0'],
    ['supplier' => 'Penguin Fishing', 'category' => 'Feed', 'q4_25' => '75.5', 'q4_24' => '71.9', 'change' => '5%', 'fy_26' => '100.0'],
    ['supplier' => 'ACT Thailand', 'category' => 'Capex', 'q4_25' => '381.8', 'q4_24' => '403.0', 'change' => '-5%', 'fy_26' => '550.0'],
    ['supplier' => 'DF Trading', 'category' => 'Chemicals', 'q4_25' => '93.4', 'q4_24' => '117.6', 'change' => '-21%', 'fy_26' => '150.0'],
    ['supplier' => 'V-Tech', 'category' => 'Medicines', 'q4_25' => '29.4', 'q4_24' => '70.5', 'change' => '-58%', 'fy_26' => '100.0'],
];

$report5Data = [
    ['supplier' => 'Integrated Hygiene', 'goods' => 'Chemicals', 'current' => '14 days', 'target' => '30 days', 'date' => 'Jun-25'],
    ['supplier' => 'PPC Zimbabwe Limited', 'goods' => 'Cement', 'current' => 'Prepayment', 'target' => '30 days', 'date' => 'Mar-26'],
    ['supplier' => 'Chisseller Services', 'goods' => 'Feed', 'current' => 'Prepayment', 'target' => '30 days', 'date' => 'Jun-25'],
    ['supplier' => 'Intofeed Pvt Ltd', 'goods' => 'Feed', 'current' => '30 days', 'target' => '30 days', 'date' => 'Met'],
    ['supplier' => 'Strauss Logistics', 'goods' => 'Fuels', 'current' => '30 days', 'target' => '30 days', 'date' => 'Met'],
];

function renderStatBox($label, $value, $trend, $subtext = '') {
    $trendClass = $trend < 0 ? 'text-danger' : 'text-success';
    $trendIcon = $trend < 0 ? '↓' : '↑';
    $trendSign = $trend < 0 ? '' : '+';
    
    echo '<div class="stat-card">
        <div class="stat-icon primary">📊</div>
        <div class="stat-content">
            <div class="stat-value">' . $value . '</div>
            <div class="stat-label">' . $label . '</div>
            ' . ($subtext ? '<div class="text-muted small mt-1">' . $subtext . '</div>' : '') . '
            ' . ($trend !== null ? '<div class="mt-2 ' . $trendClass . ' fw-bold">' . $trendIcon . ' ' . $trendSign . abs($trend) . '%</div>' : '') . '
        </div>
    </div>';
}
?>

<style>
.report-page {
    background: var(--bg);
    min-height: calc(100vh - 120px);
}
.report-header {
    background: var(--surface);
    border-bottom: 2px solid var(--border-light);
    padding: 16px 28px;
    margin-bottom: 24px;
}
.report-header h1 {
    font-size: 1.5rem;
    margin: 0;
    color: var(--primary);
}
.report-header p {
    margin: 4px 0 0;
    color: var(--text-muted);
    font-size: 0.9rem;
}
.nav-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}
.nav-tab {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text-light);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.nav-tab:hover {
    background: var(--secondary-light);
    border-color: var(--primary);
    color: var(--text);
}
.nav-tab.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 28px;
}
.stats-grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 24px;
}
@media (max-width: 768px) {
    .stats-grid-2 {
        grid-template-columns: 1fr;
    }
}
.report-card {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border-light);
    box-shadow: var(--shadow);
    margin-bottom: 24px;
}
.report-card-header {
    padding: 16px 22px;
    border-bottom: 1px solid var(--border-light);
    background: linear-gradient(180deg, #faf9f7 0%, #f5f4f2 100%);
}
.report-card-header h3 {
    font-size: 0.9rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--primary);
    margin: 0;
}
.report-card-body {
    padding: 22px;
}
.pie-chart-container {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.pie-legend {
    display: flex;
    justify-content: center;
    gap: 24px;
    margin-top: 16px;
    flex-wrap: wrap;
}
.pie-legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
}
.pie-legend-color {
    width: 16px;
    height: 16px;
    border-radius: 4px;
}
.bar-chart-container {
    padding: 20px;
}
.bar-chart-row {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
}
.bar-chart-label {
    width: 140px;
    font-size: 0.85rem;
    font-weight: 500;
}
.bar-chart-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.bar-track {
    height: 28px;
    background: var(--secondary-light);
    border-radius: 4px;
    overflow: hidden;
    position: relative;
}
.bar-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s;
}
.bar-fill.current {
    background: var(--primary);
}
.bar-fill.target {
    background: #94a3b8;
}
.bar-chart-value {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.75rem;
    font-weight: 600;
    color: #fff;
}
.bar-values {
    display: flex;
    gap: 16px;
    font-size: 0.8rem;
    color: var(--text-muted);
}
.report-footer {
    text-align: center;
    padding: 24px;
    color: var(--text-muted);
    font-size: 0.85rem;
    border-top: 1px solid var(--border-light);
    background: var(--surface);
    margin-top: 24px;
}
.tab-content {
    display: none;
}
.tab-content.active {
    display: block;
}
.filter-card {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border-light);
    padding: 16px 20px;
    margin-bottom: 24px;
}
.filter-form {
    display: flex;
    align-items: flex-end;
    gap: 16px;
    flex-wrap: wrap;
}
.filter-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.filter-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
}
.filter-group .form-select {
    min-width: 140px;
    padding: 8px 12px;
    font-size: 0.85rem;
}
.filter-actions {
    flex-direction: row;
    align-items: center;
    gap: 8px;
}
</style>

<div class="report-page">
    <div class="report-header">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1>ProcureMetrics <span class="text-muted fw-normal">| Dashboard</span></h1>
                <p><?php echo $filterLabel; ?> YTD vs Prior Year</p>
            </div>
        </div>
    </div>

    <div class="nav-tabs">
        <a href="?tab=overview<?php echo !empty($filterYear) ? '&year=' . $filterYear : ''; ?><?php echo !empty($filterQuarter) ? '&quarter=' . $filterQuarter : ''; ?><?php echo !empty($filterMonth) ? '&month=' . $filterMonth : ''; ?>" class="nav-tab <?php echo $activeTab === 'overview' ? 'active' : ''; ?>">
            <span>📊</span> Overview
        </a>
        <a href="?tab=tables<?php echo !empty($filterYear) ? '&year=' . $filterYear : ''; ?><?php echo !empty($filterQuarter) ? '&quarter=' . $filterQuarter : ''; ?><?php echo !empty($filterMonth) ? '&month=' . $filterMonth : ''; ?>" class="nav-tab <?php echo $activeTab === 'tables' ? 'active' : ''; ?>">
            <span>📋</span> Raw Tables (5 Reports)
        </a>
        <a href="?tab=suppliers<?php echo !empty($filterYear) ? '&year=' . $filterYear : ''; ?><?php echo !empty($filterQuarter) ? '&quarter=' . $filterQuarter : ''; ?><?php echo !empty($filterMonth) ? '&month=' . $filterMonth : ''; ?>" class="nav-tab <?php echo $activeTab === 'suppliers' ? 'active' : ''; ?>">
            <span>🏢</span> Suppliers & Categories
        </a>
        <a href="?tab=payments<?php echo !empty($filterYear) ? '&year=' . $filterYear : ''; ?><?php echo !empty($filterQuarter) ? '&quarter=' . $filterQuarter : ''; ?><?php echo !empty($filterMonth) ? '&month=' . $filterMonth : ''; ?>" class="nav-tab <?php echo $activeTab === 'payments' ? 'active' : ''; ?>">
            <span>💳</span> Payment Terms
        </a>
        <a href="?tab=transactions<?php echo !empty($filterYear) ? '&year=' . $filterYear : ''; ?><?php echo !empty($filterQuarter) ? '&quarter=' . $filterQuarter : ''; ?><?php echo !empty($filterMonth) ? '&month=' . $filterMonth : ''; ?>" class="nav-tab <?php echo $activeTab === 'transactions' ? 'active' : ''; ?>">
            <span>📄</span> Transactions
        </a>
    </div>

    <div class="filter-card">
        <form method="GET" class="filter-form">
            <input type="hidden" name="tab" value="<?php echo htmlspecialchars($activeTab); ?>">
            <div class="filter-group">
                <label class="filter-label">Year</label>
                <select name="year" class="form-select">
                    <option value="">All Years</option>
                    <?php foreach ($years as $year): ?>
                        <option value="<?php echo $year; ?>" <?php echo $filterYear === $year ? 'selected' : ''; ?>><?php echo $year; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="filter-group">
                <label class="filter-label">Quarter</label>
                <select name="quarter" class="form-select">
                    <option value="">All Quarters</option>
                    <?php foreach ($quarters as $q): ?>
                        <option value="<?php echo $q; ?>" <?php echo $filterQuarter === $q ? 'selected' : ''; ?>><?php echo $q; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="filter-group">
                <label class="filter-label">Month</label>
                <select name="month" class="form-select">
                    <option value="">All Months</option>
                    <?php foreach ($months as $m => $mname): ?>
                        <option value="<?php echo $m; ?>" <?php echo $filterMonth === $m ? 'selected' : ''; ?>><?php echo $mname; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="filter-group filter-actions">
                <button type="submit" class="btn btn-primary"><span>🔍</span> Filter</button>
                <a href="?tab=<?php echo $activeTab; ?>" class="btn btn-outline-secondary"><span>✕</span> Clear</a>
            </div>
        </form>
    </div>

    <div class="tab-content <?php echo $activeTab === 'overview' ? 'active' : ''; ?>" id="overview">
        <div class="stats-grid">
            <?php renderStatBox('Total Purchase Value', '$7,335k', -31, 'US$000s'); ?>
            <?php renderStatBox('Total Orders', '1,460', -26, 'Volume vs LY'); ?>
            <?php renderStatBox('Foreign Imports', '$4,087k', -36, '56% of total'); ?>
            <?php renderStatBox('Local Purchases', '$3,248k', -24, '44% of total'); ?>
        </div>

        <div class="stats-grid-2">
            <div class="report-card">
                <div class="report-card-header">
                    <h3>Spend Mix: Foreign vs Local</h3>
                </div>
                <div class="report-card-body">
                    <div class="pie-chart-container">
                        <div style="position: relative; width: 200px; height: 200px; border-radius: 50%; background: conic-gradient(#3b82f6 0deg 201.6deg, #10b981 201.6deg 360deg);"></div>
                    </div>
                    <div class="pie-legend">
                        <div class="pie-legend-item">
                            <div class="pie-legend-color" style="background: #3b82f6;"></div>
                            <span>Foreign Imports (56%)</span>
                        </div>
                        <div class="pie-legend-item">
                            <div class="pie-legend-color" style="background: #10b981;"></div>
                            <span>Local Purchases (44%)</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="report-card">
                <div class="report-card-header">
                    <h3>Payment Terms Profile (Target vs Actual %)</h3>
                </div>
                <div class="report-card-body">
                    <?php foreach ($paymentTerms as $term): ?>
                        <div class="bar-chart-row">
                            <div class="bar-chart-label"><?php echo $term['name']; ?></div>
                            <div class="bar-chart-group">
                                <div class="bar-chart-label" style="width: auto; margin-bottom: 4px;">Current: <?php echo $term['current']; ?>%</div>
                                <div class="bar-track">
                                    <div class="bar-fill current" style="width: <?php echo $term['current']; ?>%;">
                                        <span class="bar-chart-value"><?php echo $term['current']; ?>%</span>
                                    </div>
                                </div>
                                <div class="bar-chart-label" style="width: auto; margin-bottom: 4px; margin-top: 8px;">Target: <?php echo $term['target']; ?>%</div>
                                <div class="bar-track">
                                    <div class="bar-fill target" style="width: <?php echo $term['target']; ?>%;">
                                        <span class="bar-chart-value"><?php echo $term['target']; ?>%</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-content <?php echo $activeTab === 'tables' ? 'active' : ''; ?>" id="tables">
        <div class="report-card">
            <div class="report-card-header">
                <h3>Report 1: Value of Imports vs Local purchases in the period</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 40%;">Description</th>
                                <th class="text-end">Q4 2025 YTD</th>
                                <th class="text-end">Q4 2025 %</th>
                                <th class="text-end">Q4 2024 YTD</th>
                                <th class="text-end">Q4 2024 %</th>
                                <th class="text-end">2026 Full Yr</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report1Data as $row): ?>
                            <tr>
                                <td class="fw-medium"><?php echo $row['desc']; ?></td>
                                <td class="text-end"><?php echo $row['q4_25']; ?></td>
                                <td class="text-end text-muted"><?php echo $row['q4_25_pct']; ?></td>
                                <td class="text-end"><?php echo $row['q4_24']; ?></td>
                                <td class="text-end text-muted"><?php echo $row['q4_24_pct']; ?></td>
                                <td class="text-end fw-medium text-primary"><?php echo $row['fy_26']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="report-card">
            <div class="report-card-header">
                <h3>Report 2: Number of Transactions (Goods Type Breakdown)</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 40%;">Goods Type</th>
                                <th class="text-end">Q4 2025 YTD</th>
                                <th class="text-end">Q4 2024 YTD</th>
                                <th class="text-end">Change %</th>
                                <th class="text-end">2026 Full Yr</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="fw-medium">No of Orders General Goods</td>
                                <td class="text-end">1,104</td>
                                <td class="text-end">1,537</td>
                                <td class="text-end text-danger">-28%</td>
                                <td class="text-end">1,076</td>
                            </tr>
                            <tr>
                                <td class="fw-medium">No of Orders Critical Stocks</td>
                                <td class="text-end">351</td>
                                <td class="text-end">435</td>
                                <td class="text-end text-danger">-19%</td>
                                <td class="text-end">348</td>
                            </tr>
                            <tr class="fw-bold" style="background: var(--secondary-light);">
                                <td>Total Number of Orders</td>
                                <td class="text-end">1,456</td>
                                <td class="text-end">1,972</td>
                                <td class="text-end text-danger">-26%</td>
                                <td class="text-end">1,424</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="report-card">
            <div class="report-card-header">
                <h3>Report 3: Buyer Efficiency & Workload</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 40%;">Metric</th>
                                <th class="text-end">Q4 2025 YTD</th>
                                <th class="text-end">Q4 2024 YTD</th>
                                <th class="text-end">Variance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="fw-medium">Number of Buyers</td>
                                <td class="text-end">2</td>
                                <td class="text-end">3</td>
                                <td class="text-end text-danger">-33%</td>
                            </tr>
                            <tr>
                                <td class="fw-medium">Average Orders per Buyer</td>
                                <td class="text-end">728</td>
                                <td class="text-end">657</td>
                                <td class="text-end text-success">+11%</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="report-card">
            <div class="report-card-header">
                <h3>Report 4: Foreign Imports by Major Supplier (US$000s)</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 30%;">Supplier</th>
                                <th style="width: 20%;">Category</th>
                                <th class="text-end">Q4 2025 YTD</th>
                                <th class="text-end">Q4 2024 YTD</th>
                                <th class="text-end">Change %</th>
                                <th class="text-end">2026 Forecast</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report4Data as $row): ?>
                            <tr>
                                <td class="fw-medium"><?php echo $row['supplier']; ?></td>
                                <td class="text-muted"><?php echo $row['category']; ?></td>
                                <td class="text-end fw-medium">$<?php echo $row['q4_25']; ?></td>
                                <td class="text-end">$<?php echo $row['q4_24']; ?></td>
                                <td class="text-end fw-semibold text-danger"><?php echo $row['change']; ?></td>
                                <td class="text-end text-primary">$<?php echo $row['fy_26']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="report-card">
            <div class="report-card-header">
                <h3>Report 5: Profile of payment terms by key supplier</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 25%;">Supplier</th>
                                <th style="width: 20%;">Goods Supplied</th>
                                <th style="width: 20%;">Current Credit Terms</th>
                                <th style="width: 20%;">Target Credit Terms</th>
                                <th style="width: 15%;">Target Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report5Data as $row): ?>
                            <tr>
                                <td class="fw-medium"><?php echo $row['supplier']; ?></td>
                                <td class="text-muted"><?php echo $row['goods']; ?></td>
                                <td>
                                    <span class="badge <?php echo $row['current'] === 'Prepayment' ? 'badge-warning' : 'badge-secondary'; ?>">
                                        <?php echo $row['current']; ?>
                                    </span>
                                </td>
                                <td class="fw-medium text-primary"><?php echo $row['target']; ?></td>
                                <td class="fw-medium"><?php echo $row['date']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-content <?php echo $activeTab === 'suppliers' ? 'active' : ''; ?>" id="suppliers">
        <div class="report-card">
            <div class="report-card-header">
                <h3>Key Suppliers & Variance Analysis</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 20%;">Category</th>
                                <th style="width: 30%;">Supplier</th>
                                <th class="text-end">Q4-2025 (k)</th>
                                <th class="text-end">Q4-2024 (k)</th>
                                <th class="text-end">% Change</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($supplierData as $row): ?>
                            <tr>
                                <td class="text-muted fw-medium"><?php echo $row['category']; ?></td>
                                <td class="fw-medium"><?php echo $row['supplier']; ?></td>
                                <td class="text-end">$<?php echo number_format($row['q4_2025'], 1); ?></td>
                                <td class="text-end text-muted">$<?php echo number_format($row['q4_2024'], 1); ?></td>
                                <td class="text-end fw-semibold <?php echo $row['change'] < 0 ? 'text-danger' : 'text-success'; ?>">
                                    <?php echo $row['change'] > 0 ? '+' : ''; ?><?php echo $row['change']; ?>%
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-content <?php echo $activeTab === 'payments' ? 'active' : ''; ?>" id="payments">
        <div class="report-card">
            <div class="report-card-header">
                <h3>Credit Terms Optimization Tracker</h3>
            </div>
            <div class="report-card-body" style="padding: 0;">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 25%;">Supplier</th>
                                <th style="width: 20%;">Current Terms</th>
                                <th style="width: 20%;">Target Terms</th>
                                <th style="width: 15%;">Target Date</th>
                                <th style="width: 20%;">Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report5Data as $row): ?>
                            <tr>
                                <td class="fw-medium"><?php echo $row['supplier']; ?></td>
                                <td>
                                    <span class="badge <?php echo $row['current'] === 'Prepayment' ? 'badge-warning' : 'badge-secondary'; ?>">
                                        <?php echo $row['current']; ?>
                                    </span>
                                </td>
                                <td class="fw-medium text-primary"><?php echo $row['target']; ?></td>
                                <td class="text-muted"><?php echo $row['date']; ?></td>
                                <td class="text-small">
                                    <?php if ($row['date'] === 'Met'): ?>
                                        <span class="badge badge-success">Compliant</span>
                                    <?php else: ?>
                                        <span class="badge badge-warning">Pending Action</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-content <?php echo $activeTab === 'transactions' ? 'active' : ''; ?>" id="transactions">
        <div class="report-card">
            <div class="report-card-header">
                <h3>Buyer Efficiency: Orders Per Buyer</h3>
            </div>
            <div class="report-card-body">
                <div style="display: flex; align-items: center; gap: 24px; margin-bottom: 24px; padding: 16px; background: var(--primary-light); border-radius: var(--radius); flex-wrap: wrap;">
                    <div>
                        <div class="text-small text-primary fw-bold text-uppercase mb-1">Current Buyers</div>
                        <div class="fw-bold" style="font-size: 1.5rem;">2</div>
                    </div>
                    <div style="width: 1px; height: 40px; background: var(--primary); opacity: 0.3;"></div>
                    <div>
                        <div class="text-small text-primary fw-bold text-uppercase mb-1">Orders/Buyer</div>
                        <div class="fw-bold" style="font-size: 1.5rem;">728</div>
                    </div>
                    <div style="width: 1px; height: 40px; background: var(--primary); opacity: 0.3;"></div>
                    <div>
                        <div class="text-small text-primary fw-bold text-uppercase mb-1">Efficiency Change</div>
                        <div class="fw-bold text-success" style="font-size: 1.5rem;">+11%</div>
                    </div>
                </div>

                <div style="padding: 16px;">
                    <?php foreach ($transactionTrends as $trend): ?>
                        <div class="bar-chart-row">
                            <div class="bar-chart-label"><?php echo $trend['category']; ?></div>
                            <div class="bar-chart-group" style="width: calc(100% - 160px);">
                                <div class="bar-track" style="margin-bottom: 8px;">
                                    <div class="bar-fill current" style="width: <?php echo min(100, ($trend['q4_2025'] / 1537) * 100); ?>%;">
                                        <span class="bar-chart-value"><?php echo $trend['q4_2025']; ?></span>
                                    </div>
                                </div>
                                <div class="bar-track">
                                    <div class="bar-fill target" style="width: <?php echo min(100, ($trend['q4_2024'] / 1537) * 100); ?>%;">
                                        <span class="bar-chart-value"><?php echo $trend['q4_2024']; ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                    <div class="pie-legend" style="margin-top: 16px;">
                        <div class="pie-legend-item">
                            <div class="pie-legend-color" style="background: var(--primary);"></div>
                            <span>Q4 2025</span>
                        </div>
                        <div class="pie-legend-item">
                            <div class="pie-legend-color" style="background: #94a3b8;"></div>
                            <span>Q4 2024</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="report-footer">
        <p>© 2026 Procurement Analytics System • Generated from Metrix Reporting Source</p>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/admin-footer.php';