@extends('layouts.app')
@section('title', '설비 현황')
@section('content')

<!-- 헤더 -->
<div class="mb-6">
    <h1 class="text-2xl font-bold text-gray-800">설비 현황</h1>
    <p class="text-sm text-gray-500 mt-1">{{ now()->format('Y년 m월 d일') }} 기준</p>
</div>

<!-- 상단 카드 -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
    <div class="bg-white rounded-lg shadow-sm p-5">
        <div class="text-sm text-gray-500 mb-1">총 설비</div>
        <div class="text-3xl font-bold text-gray-800">{{ $stats['total'] }}</div>
        <div class="text-xs text-gray-400 mt-1">대</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-5">
        <div class="text-sm text-gray-500 mb-1">가동 중</div>
        <div class="text-3xl font-bold text-green-600">{{ $stats['active'] }}</div>
        <div class="text-xs text-gray-400 mt-1">대</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-5">
        <div class="text-sm text-gray-500 mb-1">유휴</div>
        <div class="text-3xl font-bold text-yellow-600">{{ $stats['idle'] }}</div>
        <div class="text-xs text-gray-400 mt-1">대</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-5">
        <div class="text-sm text-gray-500 mb-1">폐기</div>
        <div class="text-3xl font-bold text-gray-400">{{ $stats['disposed'] }}</div>
        <div class="text-xs text-gray-400 mt-1">대</div>
    </div>
</div>

<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
    <!-- 이번달 점검 현황 -->
    <div class="bg-white rounded-lg shadow-sm p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">
            이번달 점검 현황
            <span class="text-sm font-normal text-gray-500 ml-2">{{ now()->format('Y년 m월') }}</span>
        </h2>
        <div class="space-y-3">
            <div class="flex justify-between items-center">
                <span class="text-gray-600">점검 대상</span>
                <span class="font-semibold">{{ $inspectionStats['total'] }}대</span>
            </div>
            <div class="flex justify-between items-center">
                <span class="text-gray-600">점검 완료</span>
                <span class="font-semibold text-green-600">{{ $inspectionStats['inspected'] }}대</span>
            </div>
            @if($inspectionStats['total'] > 0)
                <div class="w-full bg-gray-200 rounded-full h-2">
                    <div class="bg-green-500 h-2 rounded-full"
                         style="width: {{ min(100, round($inspectionStats['inspected'] / $inspectionStats['total'] * 100)) }}%"></div>
                </div>
                <div class="text-sm text-gray-500 text-right">
                    {{ round($inspectionStats['inspected'] / $inspectionStats['total'] * 100) }}% 완료
                </div>
            @endif
            <div class="flex justify-between items-center">
                <span class="text-gray-600">이상 발견</span>
                <span class="font-semibold {{ $inspectionStats['issue_count'] > 0 ? 'text-red-600' : 'text-gray-400' }}">
                    {{ $inspectionStats['issue_count'] }}건
                </span>
            </div>
        </div>
    </div>

    <!-- 설비 유형별 현황 -->
    <div class="bg-white rounded-lg shadow-sm p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">설비 유형별 현황</h2>
        @if(!empty($typeStats))
            <div class="space-y-2">
                @foreach($typeStats as $type => $count)
                    <div class="flex justify-between items-center">
                        <span class="text-gray-600">{{ $type ?? '미분류' }}</span>
                        <div class="flex items-center gap-2">
                            <div class="bg-gray-200 rounded-full h-2" style="width: 100px;">
                                @php $maxCount = max($typeStats); @endphp
                                <div class="bg-blue-500 h-2 rounded-full"
                                     style="width: {{ $maxCount > 0 ? round($count / $maxCount * 100) : 0 }}%"></div>
                            </div>
                            <span class="font-semibold text-sm" style="min-width: 30px; text-align: right;">{{ $count }}</span>
                        </div>
                    </div>
                @endforeach
            </div>
        @else
            <p class="text-gray-500 text-center py-4">데이터가 없습니다.</p>
        @endif
    </div>
</div>

<!-- 최근 수리이력 -->
<div class="bg-white rounded-lg shadow-sm p-6">
    <div class="flex justify-between items-center mb-4 pb-2 border-b">
        <h2 class="text-lg font-semibold text-gray-800">최근 수리이력</h2>
        <a href="{{ route('equipment.repairs') }}" class="text-blue-600 hover:text-blue-800 text-sm">
            전체보기 &rarr;
        </a>
    </div>

    @if($recentRepairs->isNotEmpty())
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700">수리일</th>
                        <th class="px-3 py-2 text-left text-sm font-semibold text-gray-700">설비</th>
                        <th class="px-3 py-2 text-center text-sm font-semibold text-gray-700">보전구분</th>
                        <th class="px-3 py-2 text-left text-sm font-semibold text-gray-700">수리내용</th>
                        <th class="px-3 py-2 text-right text-sm font-semibold text-gray-700">비용</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200">
                    @foreach($recentRepairs as $repair)
                        <tr class="hover:bg-gray-50">
                            <td class="px-3 py-2 text-sm text-center">{{ $repair->repair_date->format('m-d') }}</td>
                            <td class="px-3 py-2 text-sm">{{ $repair->equipment?->name ?? '-' }}</td>
                            <td class="px-3 py-2 text-sm text-center">
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium
                                    {{ $repair->repair_type === 'internal' ? 'bg-blue-100 text-blue-800' : 'bg-orange-100 text-orange-800' }}">
                                    {{ $repair->repair_type_label }}
                                </span>
                            </td>
                            <td class="px-3 py-2 text-sm">{{ Str::limit($repair->description, 40) ?? '-' }}</td>
                            <td class="px-3 py-2 text-sm text-right font-mono">{{ $repair->formatted_cost }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @else
        <p class="text-gray-500 text-center py-8">최근 수리이력이 없습니다.</p>
    @endif
</div>

@endsection
