<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 - Korekty faktur
 */

$client_id = $_SESSION['client_id'];
$korekty = vbk_read_korekty($client_id);

usort($korekty, function($a, $b) {
    return strcmp($b['data_wystawienia'], $a['data_wystawienia']);
});
?>

<div class="fade-in">
    <h4 class="mb-4">
        <i class="bi bi-pencil-square me-2"></i> <?= __('corrections.title') ?>
        <span class="badge bg-secondary ms-2"><?= count($korekty) ?></span>
    </h4>

    <?= ibok_help_section('korekty', __('help.show_help'), __('help.korekty_section_content')) ?>

    <div class="card">
        <div class="card-body">
            <?php if (empty($korekty)): ?>
                <p class="text-muted text-center py-4"><?= __('corrections.no_corrections') ?></p>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover ibok-table" id="tblKorekty">
                        <thead>
                            <tr>
                                <th><?= __('invoices.issue_date') ?></th>
                                <th><?= __('corrections.correction_number') ?><?= ibok_tooltip(__('help.tooltip_correction_doc')) ?></th>
                                <th><?= __('invoices.pdf') ?></th>
                                <th><?= __('invoices.amount') ?><?= ibok_tooltip(__('help.tooltip_correction_amount')) ?></th>
                                <th><?= __('invoices.status') ?><?= ibok_tooltip(__('help.tooltip_status')) ?></th>
                                <th><?= __('invoices.due_date') ?></th>
                                <th class="text-center"><?= __('common.actions') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($korekty as $k): ?>
                            <tr>
                                <td data-order="<?= e($k['data_wystawienia']) ?>"><?= format_date($k['data_wystawienia']) ?></td>
                                <td><?= e($k['nr_korekty']) ?></td>
                                <td>
                                    <?php if (!empty($k['pdf_filename'])): ?>
                                        <a href="pobierz.php?id=<?= urlencode($client_id) ?>&file=<?= urlencode($k['pdf_filename']) ?>"
                                           class="btn btn-sm btn-outline-danger" target="_blank" title="<?= __('invoices.download_pdf') ?>">
                                            <i class="bi bi-file-pdf"></i> PDF
                                        </a>
                                    <?php endif; ?>
                                </td>
                                <td class="fw-bold"><?= e($k['kwota_brutto']) ?></td>
                                <td><?= status_badge($k['status']) ?></td>
                                <td data-order="<?= e($k['termin_platnosci']) ?>"><?= format_date($k['termin_platnosci']) ?></td>
                                <td></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
