@if ($errors->any())
    <div class="alert alert-danger py-2 small"><ul class="mb-0">
        @foreach ($errors->all() as $e)<li>{{ $e }}</li>@endforeach
    </ul></div>
@endif

@php($r = $reading ?? null)
<div class="card border-0 shadow-sm">
    <div class="card-body">
        <form method="POST" action="{{ $action }}" style="max-width: 720px;">
            @csrf
            @if ($method === 'PUT') @method('PUT') @endif

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label small fw-medium" for="reading_date">Датум</label>
                    <input id="reading_date" name="reading_date" type="date" class="form-control @error('reading_date') is-invalid @enderror"
                           value="{{ old('reading_date', optional($r?->reading_date)->format('Y-m-d')) }}" required autofocus>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label small fw-medium" for="technician_id">Техничар</label>
                    <select id="technician_id" name="technician_id" class="form-select">
                        <option value="">—</option>
                        @foreach ($technicians as $t)
                            <option value="{{ $t->id }}" @selected((int) old('technician_id', $r?->technician_id) === $t->id)>{{ $t->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label small fw-medium" for="bw_counter">Црно-бел бројач</label>
                    <input id="bw_counter" name="bw_counter" type="number" class="form-control"
                           value="{{ old('bw_counter', $r?->bw_counter) }}">
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label small fw-medium" for="color_counter">Бројач во боја</label>
                    <input id="color_counter" name="color_counter" type="number" class="form-control"
                           value="{{ old('color_counter', $r?->color_counter) }}">
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label small fw-medium" for="scan_counter">Бројач скенирања</label>
                    <input id="scan_counter" name="scan_counter" type="number" class="form-control"
                           value="{{ old('scan_counter', $r?->scan_counter) }}">
                </div>
            </div>

            <label class="form-label small fw-medium">Тонер (1–5)</label>
            <div class="row">
                @foreach (['toner_black' => 'Црна', 'toner_cyan' => 'Cyan', 'toner_magenta' => 'Magenta', 'toner_yellow' => 'Yellow'] as $field => $lbl)
                    <div class="col-3 col-md-2 mb-3">
                        <label class="form-label small text-muted" for="{{ $field }}">{{ $lbl }}</label>
                        <input id="{{ $field }}" name="{{ $field }}" type="number" min="1" max="5"
                               class="form-control @error($field) is-invalid @enderror"
                               value="{{ old($field, $r?->{$field}) }}">
                    </div>
                @endforeach
            </div>

            <div class="mb-4">
                <label class="form-label small fw-medium" for="description">Опис</label>
                <textarea id="description" name="description" rows="2" class="form-control">{{ old('description', $r?->description) }}</textarea>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">Зачувај</button>
                <a href="{{ route('printers.show', $printer) }}" class="btn btn-light border">Откажи</a>
            </div>
        </form>
    </div>
</div>
