@props([
    'action', 
    'search' => '', 
    'selectedMonth' => '', 
    'status' => '1',
    'role' => '',
    'department' => '',
    'flagSend' => ''
])

<form id="filterForm" action="{{ $action }}" method="GET" class="flex flex-col md:flex-row md:flex-wrap items-stretch md:items-center gap-3.5 w-full mb-6 bg-white p-5 rounded-[24px] border border-white shadow-[0_10px_30px_rgba(15,23,42,0.05)]">
<div class="search-wrapper flex-1 relative flex items-center w-full">
    <span class="absolute left-4 text-gray-400 pointer-events-none w-5 h-5 flex items-center justify-center z-10">
        <svg class="w-4 h-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
            <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
        </svg>
    </span>
    <input type="text" id="searchInput" name="search" value="{{ $search }}" placeholder="Tìm MSNV hoặc Email..." 
           class="form-input form-input-with-icon !rounded-xl !focus:border-[#3462f7] !focus:ring-[#3462f7]/20 w-full" 
           style="padding-left: 44px; padding-right: 40px;" />
    <button type="button" id="btnClearSearch" class="{{ empty($search) ? 'hidden' : '' }} absolute right-3.5 text-gray-400 hover:text-gray-600 focus:outline-none">
        <svg class="w-4 h-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
            <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
        </svg>
    </button>
</div>
    
    <!-- Status Filter -->
    <select name="status" id="statusFilter" class="form-input !text-sm !w-full md:!w-[150px] !rounded-xl bg-white shrink-0">
        <option value="" {{ $status === '' ? 'selected' : '' }}>Tất cả trạng thái</option>
        <option value="1" {{ $status === '1' ? 'selected' : '' }}>Hoạt động</option>
        <option value="0" {{ $status === '0' ? 'selected' : '' }}>Nghỉ việc</option>
    </select>

    <!-- Role Filter -->
    <select name="role" id="roleFilter" class="form-input !text-sm !w-full md:!w-[130px] !rounded-xl bg-white shrink-0">
        <option value="" {{ $role === '' ? 'selected' : '' }}>Tất cả vai trò</option>
        <option value="1" {{ $role === '1' ? 'selected' : '' }}>Admin</option>
        <option value="0" {{ $role === '0' ? 'selected' : '' }}>Member</option>
    </select>

    <!-- Department Filter -->
    <select name="department" id="departmentFilter" class="form-input !text-sm !w-full md:!w-[170px] !rounded-xl bg-white shrink-0">
        <option value="" {{ $department === '' ? 'selected' : '' }}>Tất cả phòng ban</option>
        @foreach(config('constants.DEPARTMENTS') as $dept)
            <option value="{{ $dept }}" {{ $department === $dept ? 'selected' : '' }}>{{ $dept }}</option>
        @endforeach
    </select>

    <!-- Flag Send Filter -->
    <select name="flag_send" id="flagSendFilter" class="form-input !text-sm !w-full md:!w-[150px] !rounded-xl bg-white shrink-0">
        <option value="" {{ $flagSend === '' ? 'selected' : '' }}>Tất cả quyền gửi</option>
        <option value="1" {{ $flagSend === '1' ? 'selected' : '' }}>Được gửi</option>
        <option value="0" {{ $flagSend === '0' ? 'selected' : '' }}>Không được gửi</option>
    </select>

    <input type="month" name="month" id="monthFilter" value="{{ $selectedMonth }}" class="form-input !text-sm !w-full md:!w-[140px] !rounded-xl bg-white shrink-0">

    <div class="flex items-center gap-2 w-full md:w-auto md:ml-auto shrink-0">
        <button type="submit" id="btnFilter" class="btn-primary !h-[48px] !px-5 text-sm rounded-xl font-bold shadow-md shadow-blue-500/10 !bg-[#3462f7] hover:!bg-blue-700 flex items-center justify-center min-w-[80px] flex-1 md:flex-none">Tìm</button>
        <button type="button" id="btnClearFilters" class="btn-secondary !h-[48px] !px-5 text-sm rounded-xl font-bold border border-[#d9dfe7] hover:bg-gray-100 flex items-center justify-center min-w-[100px] flex-1 md:flex-none">Xóa bộ lọc</button>
    </div>
</form>

@push('styles')
<style>
    #departmentFilter-ts-wrapper {
        width: 100% !important;
    }
    @media (min-width: 768px) {
        #departmentFilter-ts-wrapper {
            width: 220px !important;
        }
    }
</style>
@endpush

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        new TomSelect("#departmentFilter", {
            create: false,
            sortField: {
                field: "text",
                direction: "asc"
            },
            placeholder: "Tìm phòng ban...",
            render: {
                item: function(data, escape) {
                    return '<div class="item" title="' + escape(data.text) + '">' + escape(data.text) + '</div>';
                },
                no_results: function(data, escape) {
                    return '<div class="no-results p-3 text-gray-500">Không tìm thấy phòng ban nào</div>';
                }
            }
        });
    });
</script>
@endpush
