<x-app-layout>
    <x-page-header title="Сервисна книга" :subtitle="$printer->manufacturer.' '.$printer->model">
        <x-slot:actions>
            <a href="{{ route('readings.create', $printer) }}" class="btn btn-primary">
                <i class="bi bi-plus-lg me-1"></i>Ново читање
            </a>
            <a href="{{ route('printers.edit', $printer) }}" class="btn btn-light border">
                <i class="bi bi-pencil me-1"></i>Измени
            </a>
            <a href="{{ route('printers.qr', $printer) }}" class="btn btn-light border" target="_blank">
                <i class="bi bi-qr-code me-1"></i>Печати QR
            </a>
        </x-slot:actions>
    </x-page-header>

    <x-flash />

    <div class="row g-3 mb-3">
        <div class="col-12">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-white fw-medium">Печатач</div>
                <div class="card-body">
                    <dl class="row mb-0 small">
                        <dt class="col-5 text-muted">Клиент</dt>
                        <dd class="col-7"><a href="{{ route('clients.show', $printer->client) }}">{{ $printer->client->name }}</a></dd>
                        <dt class="col-5 text-muted">Сериски број</dt><dd class="col-7">{{ $printer->serial_number }}</dd>
                        <dt class="col-5 text-muted">Инсталација</dt><dd class="col-7">{{ optional($printer->installation_date)->format('d.m.Y') ?: '—' }}</dd>
                        <dt class="col-5 text-muted">Статус</dt><dd class="col-7">{{ $printer->status->label() }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-medium">Читања</div>
        <div class="table-responsive">
            <table class="table table-stack align-middle mb-0 small">
                <thead>
                    <tr>
                        <th>Датум</th>
                        <th class="text-end">Црно-бело</th>
                        <th class="text-end">Боја</th>
                        <th class="text-end">Скен</th>
                        <th>Тонер</th>
                        <th>Техничар</th>
                        <th>Опис</th>
                        <th class="text-end">Акции</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($log as $row)
                        @php($r = $row['reading'])
                        <tr>
                            <td data-label="Датум">{{ $r->reading_date->format('d.m.Y') }}</td>
                            <td data-label="Црно-бело" class="text-end">
                                {{ $r->bw_counter !== null ? number_format($r->bw_counter) : '—' }}
                                @if ($row['bw_used'] !== null)<div class="text-muted">+{{ number_format($row['bw_used']) }}</div>@endif
                            </td>
                            <td data-label="Боја" class="text-end">
                                {{ $r->color_counter !== null ? number_format($r->color_counter) : '—' }}
                                @if ($row['color_used'] !== null)<div class="text-muted">+{{ number_format($row['color_used']) }}</div>@endif
                            </td>
                            <td data-label="Скен" class="text-end">
                                {{ $r->scan_counter !== null ? number_format($r->scan_counter) : '—' }}
                                @if ($row['scan_used'] !== null)<div class="text-muted">+{{ number_format($row['scan_used']) }}</div>@endif
                            </td>
                            <td data-label="Тонер" class="text-nowrap">
                                @if ($r->toner_black || $r->toner_cyan || $r->toner_magenta || $r->toner_yellow)
                                    B{{ $r->toner_black ?? '–' }} C{{ $r->toner_cyan ?? '–' }} M{{ $r->toner_magenta ?? '–' }} Y{{ $r->toner_yellow ?? '–' }}
                                @else — @endif
                            </td>
                            <td data-label="Техничар">{{ $r->technician?->name ?? '—' }}</td>
                            <td data-label="Опис">{{ $r->description ?? '—' }}</td>
                            <td data-label="" class="text-end text-nowrap">
                                <a href="{{ route('readings.edit', $r) }}" class="btn btn-sm btn-light border">Измени</a>
                                <form method="POST" action="{{ route('readings.destroy', $r) }}" class="d-inline"
                                      onsubmit="return confirm('Избриши го читањето?');">
                                    @csrf @method('DELETE')
                                    <button class="btn btn-sm btn-outline-danger">Избриши</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="text-center text-muted py-4">Сè уште нема читања</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
