<?= $this->include('partials/html') ?>

<head>
    <?php echo view('partials/title-meta', ['title' => $title]) ?>
    <?= $this->include('partials/head-css') ?>
    <style>
        .va-container { background: #fff; padding: 20px; }
        .va-table-wrap { overflow: auto; }
        .va-table { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
        .va-table th, .va-table td { padding: 9px 10px; border-bottom: 1px solid #dee2e6; }
        .va-table th { background: #f8f9fa; font-size: 12px; text-transform: uppercase; letter-spacing: .3px; }
        .va-num { text-align: right; }
        .va-muted { color: #6c757d; }
        .va-month-down { background: #dc3545; color: #fff; }
        .va-month-up { background: #198754; color: #fff; }
    </style>
</head>

<body>
<div class="wrapper">
    <?= $this->include('partials/menu') ?>

    <div class="page-content">
        <div class="page-container">
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <h4 class="page-title"><?= esc($title) ?></h4>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body va-container">
                            <div class="va-table-wrap">
                                <table class="va-table">
                                    <thead>
                                        <tr>
                                            <th>Année</th>
                                            <th class="va-num">Janvier</th>
                                            <th class="va-num">Février</th>
                                            <th class="va-num">Mars</th>
                                            <th class="va-num">Avril</th>
                                            <th class="va-num">Mai</th>
                                            <th class="va-num">Juin</th>
                                            <th class="va-num">Juillet</th>
                                            <th class="va-num">Août</th>
                                            <th class="va-num">Septembre</th>
                                            <th class="va-num">Octobre</th>
                                            <th class="va-num">Novembre</th>
                                            <th class="va-num">Décembre</th>
                                            <th class="va-num">Total année</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($rows)): ?>
                                            <?php $currentYear = (int)date('Y'); ?>
                                            <?php $currentMonth = (int)date('n'); ?>
                                            <?php $rowsByYear = []; ?>
                                            <?php foreach ($rows as $yearRow) { $rowsByYear[(int)($yearRow['annee'] ?? 0)] = $yearRow; } ?>
                                            <?php $monthKeys = ['Janvier', 'Fevrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Decembre']; ?>
                                            <?php foreach ($rows as $row): ?>
                                                <?php
                                                    $year = (int)($row['annee'] ?? 0);
                                                    $previousYearRow = $rowsByYear[$year - 1] ?? null;
                                                ?>
                                                <tr>
                                                    <td><?= esc((string)($row['annee'] ?? '')) ?></td>
                                                    <?php foreach ($monthKeys as $monthKey): ?>
                                                        <?php
                                                            $monthValue = (float)($row[$monthKey] ?? 0);
                                                            $monthClass = '';
                                                            $monthSubLabel = '';
                                                            if ($previousYearRow !== null) {
                                                                $previousMonthValue = (float)($previousYearRow[$monthKey] ?? 0);
                                                                $monthIndex = array_search($monthKey, $monthKeys, true);
                                                                $isCurrentMonthCell = ($year === $currentYear) && ($monthIndex !== false) && ((int)$monthIndex + 1 === $currentMonth);

                                                                $comparisonCurrent = $monthValue;
                                                                $comparisonPrevious = $previousMonthValue;

                                                                if ($isCurrentMonthCell) {
                                                                    $comparisonCurrent = (float)($row['current_month_same_day_amount'] ?? $monthValue);
                                                                    $comparisonPrevious = (float)($row['previous_year_current_month_same_day_amount'] ?? $previousMonthValue);
                                                                    $monthSubLabel = '(' . number_format($comparisonPrevious, 2, ',', ' ') . ')';
                                                                }

                                                                if ($comparisonCurrent < $comparisonPrevious) {
                                                                    $monthClass = ' va-month-down';
                                                                } elseif ($comparisonCurrent > $comparisonPrevious) {
                                                                    $monthClass = ' va-month-up';
                                                                }
                                                            }
                                                        ?>
                                                        <td class="va-num<?= $monthClass ?>">
                                                            <?= number_format($monthValue, 2, ',', ' ') ?>
                                                            <?php if ($monthSubLabel !== ''): ?>
                                                                <div style="font-size: 11px; line-height: 1.2; margin-top: 2px; opacity: .9;">
                                                                    <?= esc($monthSubLabel) ?>
                                                                </div>
                                                            <?php endif; ?>
                                                        </td>
                                                    <?php endforeach; ?>
                                                    <td class="va-num">
                                                        <strong><?= number_format((float)($row['Total_annee'] ?? 0), 2, ',', ' ') ?></strong>
                                                        <?php if ($year === $currentYear): ?>
                                                            <div class="va-muted" style="font-size: 11px; line-height: 1.2; margin-top: 2px;">
                                                                estimation : <?= number_format((float)($row['estimation_fin_annee'] ?? 0), 2, ',', ' ') ?>
                                                            </div>
                                                        <?php endif; ?>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="14" class="va-muted">Aucune donnée disponible.</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->include('partials/footer-scripts') ?>
</body>
</html>
