@if(empty($inspections))
    <div class="p-12 text-center text-gray-500">
        <p>점검 가능한 설비가 없습니다.</p>
        <p class="text-sm mt-2">설비 등록대장에서 점검항목을 추가해주세요.</p>
    </div>
@else
    @php
        $date = \Carbon\Carbon::createFromFormat('Y-m', $yearMonth);
        $daysInMonth = $date->daysInMonth;
    @endphp

    <div class="overflow-x-auto">
        <table class="min-w-full border-collapse text-xs">
            <thead>
                <tr class="bg-gray-100">
                    <th class="border border-gray-300 px-2 py-1 text-center whitespace-nowrap sticky left-0 bg-gray-100 z-10" style="min-width: 80px;">설비</th>
                    <th class="border border-gray-300 px-2 py-1 text-center whitespace-nowrap sticky bg-gray-100 z-10" style="left: 80px; min-width: 80px;">점검항목</th>
                    @for($d = 1; $d <= $daysInMonth; $d++)
                        @php
                            $dayDate = $date->copy()->day($d);
                            $dayOfWeek = $dayDate->dayOfWeek;
                            $isWeekend = in_array($dayOfWeek, [0, 6]);
                        @endphp
                        <th class="border border-gray-300 px-1 py-1 text-center {{ $isWeekend ? 'bg-red-50 text-red-600' : '' }}" style="min-width: 32px;">
                            {{ $d }}
                        </th>
                    @endfor
                    <th class="border border-gray-300 px-2 py-1 text-center whitespace-nowrap" style="min-width: 60px;">판정</th>
                </tr>
            </thead>
            <tbody>
                @foreach($inspections as $item)
                    @php
                        $equipment = $item['equipment'];
                        $templates = $item['templates'];
                        $inspection = $item['inspection'];
                        $details = $item['details'];
                        $rowCount = $templates->count();
                    @endphp

                    @foreach($templates as $idx => $tmpl)
                        <tr class="{{ $idx === 0 ? 'border-t-2 border-gray-400' : '' }}">
                            @if($idx === 0)
                                <td class="border border-gray-300 px-2 py-1 text-center font-medium whitespace-nowrap sticky left-0 bg-white z-10"
                                    rowspan="{{ $rowCount }}" style="min-width: 80px;">
                                    <div class="text-xs text-blue-600">{{ $equipment->equipment_code }}</div>
                                    <div class="text-xs">{{ Str::limit($equipment->name, 8) }}</div>
                                </td>
                            @endif
                            <td class="border border-gray-300 px-2 py-1 whitespace-nowrap sticky bg-white z-10" style="left: 80px; min-width: 80px;">
                                <span class="text-gray-600">{{ $tmpl->check_point }}</span>
                            </td>

                            @for($d = 1; $d <= $daysInMonth; $d++)
                                @php
                                    $checkDate = $date->copy()->day($d)->format('Y-m-d');
                                    $key = $tmpl->id . '_' . $checkDate;
                                    $detail = isset($details[$key]) ? $details[$key]->first() : null;
                                    $symbol = $detail ? $detail->result_symbol : '';
                                    $color = $detail ? $detail->result_color : 'text-gray-400';
                                    $dayDate = $date->copy()->day($d);
                                    $isWeekend = in_array($dayDate->dayOfWeek, [0, 6]);
                                @endphp
                                <td class="border border-gray-300 text-center cursor-pointer select-none {{ $isWeekend ? 'bg-red-50' : '' }}"
                                    style="min-width: 32px; padding: 2px;"
                                    onclick="toggleCell({{ $equipment->id }}, {{ $tmpl->id }}, '{{ $checkDate }}', this)">
                                    <span class="inspection-cell text-lg font-bold {{ $color }}">{{ $symbol }}</span>
                                </td>
                            @endfor

                            @if($idx === 0)
                                <td class="border border-gray-300 px-2 py-1 text-center whitespace-nowrap"
                                    rowspan="{{ $rowCount }}">
                                    @if($inspection && $inspection->overall_judgment)
                                        <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium {{ $inspection->judgment_color }}">
                                            {{ $inspection->judgment_label }}
                                        </span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>
                            @endif
                        </tr>
                    @endforeach
                @endforeach
            </tbody>
        </table>
    </div>

    <!-- 범례 -->
    <div class="p-4 border-t flex flex-wrap gap-4 text-sm text-gray-600">
        <span><span class="text-green-600 font-bold text-lg">○</span> 양호</span>
        <span><span class="text-red-600 font-bold text-lg">X</span> 이상</span>
        <span><span class="text-yellow-600 font-bold text-lg">△</span> 수리완료</span>
        <span class="text-gray-400">셀 클릭: 빈칸 → ○ → X → △ → 빈칸</span>
    </div>
@endif
