@extends('layouts.app')

@section('title', '입퇴사자 현황')

@section('content')
<div class="px-4 py-6">
    {{-- 페이지 헤더 --}}
    <div class="flex flex-col lg:flex-row lg:justify-between lg:items-center gap-4 mb-6">
        <div>
            <h1 class="text-2xl font-bold text-gray-800">입퇴사자 현황</h1>
            <p class="text-sm text-gray-500 mt-1">{{ now()->format('Y년 n월 j일') }} 현재</p>
        </div>
        <div class="flex flex-wrap items-center gap-2 sm:gap-3">
            <a href="{{ route('api.admin.hr.employees.tenure-export') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white text-sm font-medium rounded-lg transition-colors">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                </svg>
                엑셀 다운로드
            </a>
        </div>
    </div>

    {{-- 통계 카드 --}}
    <div class="grid gap-4 mb-6" style="grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));">
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">전체 사원</div>
            <div class="text-2xl font-bold text-gray-800">{{ $stats['total'] }}명</div>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">재직</div>
            <div class="text-2xl font-bold text-emerald-600">{{ $stats['active'] }}명</div>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">퇴직</div>
            <div class="text-2xl font-bold text-red-600">{{ $stats['resigned'] }}명</div>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">평균 근속기간</div>
            <div class="text-2xl font-bold text-blue-600">{{ $stats['avg_tenure_label'] }}</div>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">올해 입사</div>
            <div class="text-2xl font-bold text-indigo-600">{{ $stats['hired_this_year'] }}명</div>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">올해 퇴사</div>
            <div class="text-2xl font-bold text-orange-600">{{ $stats['resigned_this_year'] }}명</div>
        </div>
    </div>

    {{-- 테이블 컨테이너 --}}
    <div class="bg-white rounded-lg shadow-sm overflow-hidden">
        {{-- 필터 --}}
        <div class="px-6 py-4 border-b border-gray-200">
            <x-filter-collapsible id="tenureFilter">
                <form id="tenureFilterForm" class="flex flex-wrap gap-3 items-end">
                    <div style="flex: 1 1 200px; max-width: 280px;">
                        <label class="block text-xs text-gray-500 mb-1">이름 검색</label>
                        <input type="text" name="q" placeholder="사원명..."
                               value="{{ request('q') }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    <div style="flex: 0 1 160px;">
                        <label class="block text-xs text-gray-500 mb-1">부서</label>
                        <select name="department_id"
                                class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <option value="">전체 부서</option>
                            @foreach($departments as $dept)
                                <option value="{{ $dept->id }}" {{ request('department_id') == $dept->id ? 'selected' : '' }}>
                                    {{ $dept->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div style="flex: 0 1 130px;">
                        <label class="block text-xs text-gray-500 mb-1">상태</label>
                        <select name="status"
                                class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <option value="">전체</option>
                            <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>재직</option>
                            <option value="resigned" {{ request('status') === 'resigned' ? 'selected' : '' }}>퇴직</option>
                        </select>
                    </div>
                    <div style="flex: 0 1 150px;">
                        <label class="block text-xs text-gray-500 mb-1">입사일(시작)</label>
                        <input type="date" name="hire_from"
                               value="{{ request('hire_from') }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    <div style="flex: 0 1 150px;">
                        <label class="block text-xs text-gray-500 mb-1">입사일(종료)</label>
                        <input type="date" name="hire_to"
                               value="{{ request('hire_to') }}"
                               class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    <div style="flex: 0 1 160px;">
                        <label class="block text-xs text-gray-500 mb-1">정렬</label>
                        <select name="sort_by"
                                class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            <option value="hire_date_desc" {{ request('sort_by', 'hire_date_desc') === 'hire_date_desc' ? 'selected' : '' }}>입사일 최신순</option>
                            <option value="hire_date_asc" {{ request('sort_by') === 'hire_date_asc' ? 'selected' : '' }}>입사일 빠른순</option>
                            <option value="tenure_desc" {{ request('sort_by') === 'tenure_desc' ? 'selected' : '' }}>근속기간 긴순</option>
                            <option value="tenure_asc" {{ request('sort_by') === 'tenure_asc' ? 'selected' : '' }}>근속기간 짧은순</option>
                        </select>
                    </div>
                    <div class="shrink-0">
                        <button type="submit"
                                hx-get="{{ route('api.admin.hr.employees.tenure') }}"
                                hx-target="#tenure-table"
                                hx-include="#tenureFilterForm"
                                class="px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white text-sm rounded-lg transition-colors">
                            검색
                        </button>
                    </div>
                </form>
            </x-filter-collapsible>
        </div>

        {{-- HTMX 테이블 영역 --}}
        <div id="tenure-table"
             hx-get="{{ route('api.admin.hr.employees.tenure') }}"
             hx-trigger="load"
             hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
             class="min-h-[200px]">
            <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>
    </div>
</div>
@endsection

@push('scripts')
<script>
    document.getElementById('tenureFilterForm')?.addEventListener('submit', function(e) {
        e.preventDefault();
        htmx.trigger('#tenure-table', 'htmx:trigger');
    });
</script>
@endpush
