<?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 klienta
 */

$client_id = $_SESSION['client_id'];
$zlecenia = db_read_zlecenia_klienta($client_id);

$otwarte = [];
$zamkniete = [];
foreach ($zlecenia as $z) {
    if ($z['otwarte']) {
        $otwarte[] = $z;
    } else {
        $zamkniete[] = $z;
    }
}
?>

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

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

    <?php if (empty($zlecenia)): ?>
        <div class="card">
            <div class="card-body">
                <p class="text-muted text-center py-4"><?= __('service.no_orders') ?></p>
            </div>
        </div>
    <?php else: ?>

        <!-- Otwarte zlecenia -->
        <?php if (!empty($otwarte)): ?>
        <div class="card mb-4 border-warning">
            <div class="card-header bg-warning bg-opacity-10">
                <i class="bi bi-exclamation-circle text-warning me-1"></i>
                <?= __('service.open_orders') ?>
                <span class="badge bg-warning text-dark ms-1"><?= count($otwarte) ?></span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th><?= __('service.order_number') ?></th>
                                <th><?= __('service.date') ?></th>
                                <th><?= __('service.description') ?></th>
                                <th><?= __('service.status') ?><?= ibok_tooltip(__('help.tooltip_order_status')) ?></th>
                                <th><?= __('service.duration') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($otwarte as $z): ?>
                            <tr>
                                <td class="fw-bold"><?= e($z['nr_txt'] ?: $z['nr']) ?></td>
                                <td><?= format_date($z['data']) ?></td>
                                <td>
                                    <?= e($z['tresc']) ?>
                                    <?php if (!empty($z['uwagi'])): ?>
                                        <br><small class="text-muted"><?= e($z['uwagi']) ?></small>
                                    <?php endif; ?>
                                </td>
                                <td><span class="badge bg-warning text-dark"><i class="bi bi-clock me-1"></i><?= __('service.status_open') ?></span></td>
                                <td>
                                    <?php
                                    $days = (int)((time() - strtotime($z['data'])) / 86400);
                                    echo $days . ' ' . __('service.days');
                                    ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Zamknięte zlecenia -->
        <?php if (!empty($zamkniete)): ?>
        <div class="card">
            <div class="card-header">
                <i class="bi bi-check-circle text-success me-1"></i>
                <?= __('service.closed_orders') ?>
                <span class="badge bg-secondary ms-1"><?= count($zamkniete) ?></span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover ibok-table mb-0" id="tblSerwis">
                        <thead class="table-light">
                            <tr>
                                <th><?= __('service.order_number') ?></th>
                                <th><?= __('service.date') ?></th>
                                <th><?= __('service.description') ?></th>
                                <th><?= __('service.status') ?></th>
                                <th><?= __('service.closed_date') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($zamkniete as $z): ?>
                            <tr>
                                <td><?= e($z['nr_txt'] ?: $z['nr']) ?></td>
                                <td data-order="<?= e($z['data']) ?>"><?= format_date($z['data']) ?></td>
                                <td>
                                    <?= e($z['tresc']) ?>
                                    <?php if (!empty($z['uwagi'])): ?>
                                        <br><small class="text-muted"><?= e($z['uwagi']) ?></small>
                                    <?php endif; ?>
                                </td>
                                <td><span class="badge bg-success"><i class="bi bi-check-lg me-1"></i><?= __('service.status_closed') ?></span></td>
                                <td data-order="<?= e($z['data_zakonczenia']) ?>"><?= format_date($z['data_zakonczenia']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php endif; ?>

    <?php endif; ?>
</div>
