<?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 - Zlecenia serwisowe (admin - pełna lista)
 */

$zlecenia = vbk_read_zlecenia();

usort($zlecenia, function($a, $b) {
    return strcmp($b['data'], $a['data']);
});

// Podział na otwarte i zamknięte
$otwarte = [];
$zamkniete = [];
foreach ($zlecenia as $z) {
    $dz = $z['data_zakonczenia'] ?? '';
    if (empty($dz) || $dz === '0000-00-00') {
        $otwarte[] = $z;
    } else {
        $zamkniete[] = $z;
    }
}
?>

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

    <?= ibok_help_section('admin_serwis', __('help.show_help'), __('help_admin.serwis_section_content')) ?>

    <!-- Tabs: otwarte / zamknięte -->
    <ul class="nav nav-tabs mb-3" role="tablist">
        <li class="nav-item">
            <a class="nav-link active" data-bs-toggle="tab" href="#tabOtwarte" role="tab">
                <i class="bi bi-exclamation-circle me-1"></i> <?= __('service.open_orders') ?>
                <span class="badge bg-warning text-dark ms-1"><?= count($otwarte) ?></span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-bs-toggle="tab" href="#tabZamkniete" role="tab">
                <i class="bi bi-check-circle me-1"></i> <?= __('service.closed_orders') ?>
                <span class="badge bg-secondary ms-1"><?= count($zamkniete) ?></span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-bs-toggle="tab" href="#tabWszystkie" role="tab">
                <?= __('common.all') ?>
                <span class="badge bg-secondary ms-1"><?= count($zlecenia) ?></span>
            </a>
        </li>
    </ul>

    <div class="tab-content">
        <!-- Otwarte zlecenia -->
        <div class="tab-pane fade show active" id="tabOtwarte" role="tabpanel">
            <div class="card">
                <div class="card-body">
                    <?php if (empty($otwarte)): ?>
                        <p class="text-muted text-center py-4"><?= __('service.no_orders') ?></p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover ibok-table" id="tblSerwisOtwarte">
                                <thead>
                                    <tr>
                                        <th><?= __('service.order_number') ?></th>
                                        <th><?= __('service.client') ?> ID</th>
                                        <th><?= __('service.client') ?></th>
                                        <th><?= __('service.location') ?></th>
                                        <th><?= __('service.contact_data') ?></th>
                                        <th><?= __('service.date') ?></th>
                                        <th><?= __('service.description') ?></th>
                                        <th><?= __('service.notes') ?></th>
                                        <th><?= __('service.days') ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($otwarte as $z): ?>
                                    <tr>
                                        <td><?= e($z['nr']) ?></td>
                                        <td>
                                            <a href="admin.php?page=klient_info&id=<?= urlencode($z['id_klienta']) ?>"
                                               class="text-decoration-none"><?= e($z['id_klienta']) ?></a>
                                        </td>
                                        <td><?= e($z['nazwisko']) ?></td>
                                        <td>
                                            <?= e($z['miejscowosc']) ?>
                                            <?php if (!empty($z['adres'])): ?>
                                                <br><small class="text-muted"><?= e($z['adres']) ?></small>
                                            <?php endif; ?>
                                        </td>
                                        <td class="small">
                                            <?php if (!empty($z['tel1'])): ?>
                                                <i class="bi bi-telephone me-1"></i><?= e($z['tel1']) ?><br>
                                            <?php endif; ?>
                                            <?php if (!empty($z['tel2'])): ?>
                                                <i class="bi bi-telephone me-1"></i><?= e($z['tel2']) ?><br>
                                            <?php endif; ?>
                                            <?php if (!empty($z['email'])): ?>
                                                <i class="bi bi-envelope me-1"></i><?= e($z['email']) ?>
                                            <?php endif; ?>
                                        </td>
                                        <td data-order="<?= e($z['data']) ?>"><?= format_date($z['data']) ?></td>
                                        <td><?= e($z['tresc']) ?></td>
                                        <td><?= e($z['uwagi']) ?></td>
                                        <td><?= e($z['dni']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Zamknięte zlecenia -->
        <div class="tab-pane fade" id="tabZamkniete" role="tabpanel">
            <div class="card">
                <div class="card-body">
                    <?php if (empty($zamkniete)): ?>
                        <p class="text-muted text-center py-4"><?= __('service.no_orders') ?></p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover ibok-table" id="tblSerwisZamkniete">
                                <thead>
                                    <tr>
                                        <th><?= __('service.order_number') ?></th>
                                        <th><?= __('service.client') ?> ID</th>
                                        <th><?= __('service.client') ?></th>
                                        <th><?= __('service.location') ?></th>
                                        <th><?= __('service.date') ?></th>
                                        <th><?= __('service.closed_date') ?></th>
                                        <th><?= __('service.description') ?></th>
                                        <th><?= __('service.days') ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($zamkniete as $z): ?>
                                    <tr class="text-muted">
                                        <td><?= e($z['nr']) ?></td>
                                        <td>
                                            <a href="admin.php?page=klient_info&id=<?= urlencode($z['id_klienta']) ?>"
                                               class="text-decoration-none"><?= e($z['id_klienta']) ?></a>
                                        </td>
                                        <td><?= e($z['nazwisko']) ?></td>
                                        <td><?= e($z['miejscowosc']) ?></td>
                                        <td data-order="<?= e($z['data']) ?>"><?= format_date($z['data']) ?></td>
                                        <td data-order="<?= e($z['data_zakonczenia']) ?>"><?= format_date($z['data_zakonczenia']) ?></td>
                                        <td><?= e($z['tresc']) ?></td>
                                        <td><?= e($z['dni']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Wszystkie zlecenia -->
        <div class="tab-pane fade" id="tabWszystkie" role="tabpanel">
            <div class="card">
                <div class="card-body">
                    <?php if (empty($zlecenia)): ?>
                        <p class="text-muted text-center py-4"><?= __('service.no_orders') ?></p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover ibok-table" id="tblSerwisAdmin">
                                <thead>
                                    <tr>
                                        <th><?= __('service.order_number') ?></th>
                                        <th><?= __('service.client') ?> ID</th>
                                        <th><?= __('service.client') ?></th>
                                        <th><?= __('service.location') ?></th>
                                        <th><?= __('service.date') ?></th>
                                        <th><?= __('service.description') ?></th>
                                        <th><?= __('service.notes') ?></th>
                                        <th><?= __('service.days') ?></th>
                                        <th><?= __('common.status') ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($zlecenia as $z):
                                        $dz = $z['data_zakonczenia'] ?? '';
                                        $is_closed = !empty($dz) && $dz !== '0000-00-00';
                                    ?>
                                    <tr class="<?= $is_closed ? 'text-muted' : '' ?>">
                                        <td><?= e($z['nr']) ?></td>
                                        <td>
                                            <a href="admin.php?page=klient_info&id=<?= urlencode($z['id_klienta']) ?>"
                                               class="text-decoration-none"><?= e($z['id_klienta']) ?></a>
                                        </td>
                                        <td><?= e($z['nazwisko']) ?></td>
                                        <td><?= e($z['miejscowosc']) ?></td>
                                        <td data-order="<?= e($z['data']) ?>"><?= format_date($z['data']) ?></td>
                                        <td><?= e($z['tresc']) ?></td>
                                        <td><?= e($z['uwagi']) ?></td>
                                        <td><?= e($z['dni']) ?></td>
                                        <td>
                                            <?php if ($is_closed): ?>
                                                <span class="badge bg-secondary"><?= __('service.closed') ?></span>
                                            <?php else: ?>
                                                <span class="badge bg-success"><?= __('service.open') ?></span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
