<?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 - Lista klientów (admin)
 */

$clients = vbk_read_klienci();

// Sortuj po ID
uksort($clients, function($a, $b) {
    return (int)$a - (int)$b;
});

$count_active = 0;
$count_inactive = 0;
foreach ($clients as $c) {
    if ($c['active_flag'] === '1') $count_active++;
    else $count_inactive++;
}
?>

<div class="fade-in">
    <h4 class="mb-4">
        <i class="bi bi-people me-2"></i> <?= __('admin_clients.title') ?>
        <span class="badge bg-secondary ms-2" id="clientsCount"><?= $count_active ?></span>
    </h4>

    <?= ibok_help_section('admin_klienci', __('help.show_help'), __('help_admin.klienci_section_content')) ?>

    <div class="card">
        <div class="card-body">
            <div class="mb-3">
                <div class="btn-group btn-group-sm" role="group">
                    <button type="button" class="btn btn-success active" id="btnActiveOnly">
                        <i class="bi bi-check-circle me-1"></i> <?= __('admin_clients.active') ?> (<?= $count_active ?>)
                    </button>
                    <button type="button" class="btn btn-outline-secondary" id="btnAll">
                        <i class="bi bi-list me-1"></i> <?= __('admin_clients.show_all') ?> (<?= count($clients) ?>)
                    </button>
                </div>
            </div>

            <div class="table-responsive">
                <table class="table table-hover ibok-table" id="tblKlienci">
                    <thead>
                        <tr>
                            <th><?= __('admin_clients.id') ?><?= ibok_tooltip(__('help_admin.tooltip_client_id')) ?></th>
                            <th><?= __('admin_clients.name') ?></th>
                            <th><?= __('admin_clients.login') ?></th>
                            <th><?= __('admin_clients.tariff') ?></th>
                            <th><?= __('admin_clients.balance') ?><?= ibok_tooltip(__('help_admin.tooltip_client_balance')) ?></th>
                            <th><?= __('admin_clients.status') ?></th>
                            <th class="text-center"><?= __('common.actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($clients as $c): ?>
                        <tr>
                            <td><?= e($c['id']) ?></td>
                            <td>
                                <a href="admin.php?page=klient_info&id=<?= urlencode($c['id']) ?>" class="text-decoration-none fw-bold">
                                    <?= e($c['nazwisko']) ?>
                                </a>
                            </td>
                            <td class="font-monospace small"><?= e($c['login']) ?></td>
                            <td>
                                <small>
                                    <i class="bi bi-arrow-down text-success"></i> <?= e($c['dl_dzien']) ?>
                                    <i class="bi bi-arrow-up text-primary ms-1"></i> <?= e($c['ul_dzien']) ?>
                                </small>
                            </td>
                            <td>
                                <?php
                                $wg = (int)$c['wg_salda'];
                                $zalega = $c['zalega'];
                                if ($wg > 0 || $zalega === '1'):
                                ?>
                                    <span class="badge bg-danger"><?= $wg > 0 ? e($wg) : __('admin_clients.debt') ?></span>
                                <?php else: ?>
                                    <span class="badge bg-success">OK</span>
                                <?php endif; ?>
                            </td>
                            <td><span class="d-none"><?= $c['active_flag'] === '1' ? 'A' : 'N' ?></span><?php if ($c['active_flag'] === '1'): ?><span class="badge bg-success"><?= __('admin_clients.active') ?></span><?php else: ?><span class="badge bg-secondary"><?= __('admin_clients.inactive') ?></span><?php endif; ?></td>
                            <td class="text-center">
                                <a href="admin.php?page=klient_info&id=<?= urlencode($c['id']) ?>"
                                   class="btn btn-sm btn-outline-primary" title="<?= __('admin_clients.view') ?>">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script nonce="<?= csp_nonce() ?>">
document.addEventListener('DOMContentLoaded', function() {
    // Poczekaj na jQuery i DataTables
    var checkDT = setInterval(function() {
        if (typeof $ !== 'undefined' && $.fn.DataTable && $.fn.DataTable.isDataTable('#tblKlienci')) {
            clearInterval(checkDT);
            var dt = $('#tblKlienci').DataTable();
            var statusCol = 5; // kolumna Status

            // Domyślnie: tylko aktywni (regex ^A — komórka zaczyna się od ukrytego "A")
            dt.column(statusCol).search('^A', true, false).draw();

            document.getElementById('btnActiveOnly').addEventListener('click', function() {
                dt.column(statusCol).search('^A', true, false).draw();
                this.className = 'btn btn-success active';
                document.getElementById('btnAll').className = 'btn btn-outline-secondary';
                document.getElementById('clientsCount').textContent = '<?= $count_active ?>';
            });
            document.getElementById('btnAll').addEventListener('click', function() {
                dt.column(statusCol).search('').draw();
                this.className = 'btn btn-secondary active';
                document.getElementById('btnActiveOnly').className = 'btn btn-outline-success';
                document.getElementById('clientsCount').textContent = '<?= count($clients) ?>';
            });
        }
    }, 50);
});
</script>
