@extends('layouts.app')
@section('title', '수리이력')
@section('content')

<!-- 헤더 -->
<div class="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4 mb-6">
    <h1 class="text-2xl font-bold text-gray-800">수리이력</h1>
    <a href="{{ route('equipment.repairs.create') }}"
       class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition text-center">
        + 수리이력 등록
    </a>
</div>

<!-- 필터 -->
<x-filter-collapsible id="repairFilter">
    <form id="repairFilter" class="flex flex-wrap gap-2 sm:gap-4">
        <input type="hidden" name="per_page" id="perPageInput" value="20">
        <input type="hidden" name="page" id="pageInput" value="1">

        <div style="flex: 1 1 200px; max-width: 300px;">
            <input type="text" name="search" placeholder="설비명/수리내용 검색..."
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>

        <div class="shrink-0" style="width: 200px;">
            <select name="equipment_id"
                    class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">설비 전체</option>
                @foreach($equipmentList as $eq)
                    <option value="{{ $eq->id }}">{{ $eq->equipment_code }} - {{ $eq->name }}</option>
                @endforeach
            </select>
        </div>

        <div class="shrink-0" style="width: 140px;">
            <select name="repair_type"
                    class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">구분 전체</option>
                <option value="internal">사내</option>
                <option value="external">외주</option>
            </select>
        </div>

        <div class="shrink-0" style="width: 150px;">
            <input type="date" name="date_from" placeholder="시작일"
                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div class="shrink-0" style="width: 150px;">
            <input type="date" name="date_to" placeholder="종료일"
                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>

        <button type="submit" class="bg-gray-600 hover:bg-gray-700 text-white px-6 py-2 rounded-lg transition shrink-0">
            검색
        </button>
    </form>
</x-filter-collapsible>

<!-- 테이블 -->
<div id="repair-table"
     hx-get="/api/admin/equipment/repairs"
     hx-trigger="load, filterSubmit from:body"
     hx-include="#repairFilter"
     hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
     class="bg-white rounded-lg shadow-sm">
    <div class="flex justify-center items-center p-12">
        <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
    </div>
</div>

@endsection

@push('scripts')
<script>
    document.getElementById('repairFilter').addEventListener('submit', function(e) {
        e.preventDefault();
        document.getElementById('pageInput').value = 1;
        htmx.trigger('#repair-table', 'filterSubmit');
    });

    function confirmDeleteRepair(id) {
        showDeleteConfirm('수리이력', () => {
            fetch(`/api/admin/equipment/repairs/${id}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Accept': 'application/json',
                }
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    htmx.trigger('#repair-table', 'filterSubmit');
                    showToast(data.message, 'success');
                }
            });
        });
    }
</script>
@endpush
