@php
    use App\Domain\Training\Support\GroupColor;
@endphp

<x-print.sheet
    :title="$facility->name_ar"
    :subtitle="__('جدول اليوم') . ' — ' . $dayLabel"
    :size="count($segments) > 6 ? 'A3 landscape' : 'A4 landscape'"
    :meta="[
        __('الفرع') => $facility->branch?->name_ar ?? '—',
        __('التاريخ') => $date->format('Y-m-d'),
        __('الحجوزات') => count($reservations),
    ]">

    <x-slot:legend>
        @foreach($legendGroups as $g)
            <span class="item">
                <span class="dot" style="background: {{ GroupColor::for($g['id']) }}"></span>
                {{ $g['name'] }}
            </span>
        @endforeach
        @if(empty($legendGroups))
            <span style="color:#6B7280">{{ __('لا توجد حجوزات في هذا اليوم') }}</span>
        @endif
    </x-slot:legend>

    @push('styles')
    <style>
        .board td { height: 30px; padding: 0; }
        .cell {
            height: 100%;
            padding: 4px 5px;
            border-radius: 3px;
            font-size: 10.5px;
            line-height: 1.3;
            overflow: hidden;
        }
        .cell .g { font-weight: 700; display: block; }
        /* Tint the secondary line from the group hue rather than graying it,
           so it holds contrast on the filled cell. */
        .cell .t { font-size: 9.5px; opacity: .8; display: block; }
        .seg-head { font-size: 10px; font-weight: 700; }
        .seg-head span { display: block; font-weight: 400; opacity: .7; font-size: 9px; }
        .free { background: repeating-linear-gradient(45deg,#fff,#fff 5px,#FAFAFA 5px,#FAFAFA 10px); }
    </style>
    @endpush

    <table class="board">
        <thead>
            <tr>
                <th class="time-col">{{ __('الوقت') }}</th>
                @foreach($segments as $segment)
                    <th class="seg-head">
                        {{ $segment->name_ar }}
                        <span class="num">{{ $segment->code }}</span>
                    </th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @foreach($slots as $slot)
                <tr>
                    <td class="time-col">{{ $slot['label'] }}</td>
                    @foreach($segments as $segment)
                        @php $booking = $cells[$slot['key']][$segment->id] ?? null; @endphp
                        <td>
                            @if($booking)
                                <div class="cell" style="background: {{ GroupColor::for($booking['group_id']) }}26;
                                          border: 1px solid {{ GroupColor::for($booking['group_id']) }}66;
                                          color: {{ GroupColor::for($booking['group_id']) }};">
                                    <span class="g">{{ $booking['title'] }}</span>
                                    <span class="t num">{{ $booking['start'] }}–{{ $booking['end'] }}</span>
                                    @if($booking['trainer'])
                                        <span class="t">{{ $booking['trainer'] }}</span>
                                    @endif
                                </div>
                            @else
                                <div class="cell free"></div>
                            @endif
                        </td>
                    @endforeach
                </tr>
            @endforeach
        </tbody>
    </table>
</x-print.sheet>
