 
@extends('layouts.metor.admin')
@section('content')
@section('title','Employees')

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex flex-column flex-md-row align-items-start align-items-md-center justify-content-between gap-2">
                <div class="flex-grow-1 min-w-0">
                    <h4 class="card-title mb-0 text-truncate">Employees</h4>
                </div>
                <div class="d-flex flex-wrap gap-2 justify-content-start justify-content-md-end w-100 w-md-auto">
                    <a href="{{ route('employees.export.excel') }}" class="btn btn-success">
                        <i class="ri-file-text-line me-1"></i> Export CSV
                    </a>

                    <label class="btn btn-outline-success mb-0" style="cursor:pointer;">
                        <i class="ri-upload-cloud-2-line me-1"></i> Import CSV
                        <input type="file" name="file" accept=".csv,text/csv" style="display:none;" onchange="importCsvAjax(this, '{{ route('employees.import.csv') }}');">
                    </label>

                    @can("employees-create") 
                    <a href="{{ route('employees.create') }}" class='btn btn-primary'>
                        <i class="ri-add-line me-1"></i> Add Employees
                    </a>
                    @endcan
                </div>
            </div>
            <div class="card-body">
                <!-- Search Form -->
                <form method="GET" action="{{ route('employees.index') }}" class="mb-3">
                    <div class="row g-2">
                        <div class="col-md-4">
                            <input type="text" 
                                   name="search" 
                                   class="form-control" 
                                   placeholder="Search..." 
                                   value="{{ request('search') }}">
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="ri-search-line"></i> Search
                            </button>
                        </div>
                        @if(request('search'))
                            <div class="col-md-2">
                                <a href="{{ route('employees.index') }}" class="btn btn-secondary w-100">
                                    <i class="ri-close-line"></i> Clear
                                </a>
                            </div>
                        @endif
                    </div>
                    <!-- Preserve sort parameters -->
                    @if(request('sort_by'))
                        <input type="hidden" name="sort_by" value="{{ request('sort_by') }}">
                    @endif
                    @if(request('sort_order'))
                        <input type="hidden" name="sort_order" value="{{ request('sort_order') }}">
                    @endif
                </form>

                <div class="js-index-cards d-none"></div>

                <div class="table-responsive js-index-table d-none d-md-block">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th style='width:5%'>
                                    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'id', 'sort_order' => request('sort_by') == 'id' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
                                       class="text-decoration-none text-dark">
                                        Sr.
                                        {!! renderSortIcon('id', 'cnic') !!}
                                    </a>
                                </th>
                                <th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'client_id', 'sort_order' => request('sort_by') == 'client_id' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Client
        {!! renderSortIcon('client_id', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'employee_code', 'sort_order' => request('sort_by') == 'employee_code' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Employee Code
        {!! renderSortIcon('employee_code', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'first_name', 'sort_order' => request('sort_by') == 'first_name' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        First Name
        {!! renderSortIcon('first_name', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'email', 'sort_order' => request('sort_by') == 'email' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Email
        {!! renderSortIcon('email', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'shift_id', 'sort_order' => request('sort_by') == 'shift_id' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Shift
        {!! renderSortIcon('shift_id', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'basic_salary', 'sort_order' => request('sort_by') == 'basic_salary' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Basic Salary
        {!! renderSortIcon('basic_salary', 'cnic') !!}
    </a>
</th>
<th>Picture</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'skills', 'sort_order' => request('sort_by') == 'skills' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Skills
        {!! renderSortIcon('skills', 'cnic') !!}
    </a>
</th>
<th>
    <a href="{{ route('employees.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'status', 'sort_order' => request('sort_by') == 'status' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Status
        {!! renderSortIcon('status', 'cnic') !!}
    </a>
</th>

                                <th class='text-end' style='width:10%'>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($employees as $employee)
                                <tr id="row_{{$employee->id}}">
                                    <td>{{ $employees->firstItem() + $loop->index }}</td>
                                    <td>{{ $employee->client ? $employee->client->name : '' }}</td>
<td>{{ $employee->employee_code ?? '' }}</td>
<td>{{ $employee->first_name ?? '' }}</td>
<td>{{ $employee->email ?? '' }}</td>
<td>{{ $employee->shift ? $employee->shift->name : '' }}</td>
<td>{{ number_format((float) ($employee->basic_salary ?? 0), 2) }}</td>
<td>
    @if($employee->picture)
        <img src="{{ asset('images/' . $employee->picture) }}" 
             class="imgThumb" 
             onclick="suBModal('{{ asset('images/' . $employee->picture) }}')" 
             style="cursor: pointer; max-width: 50px; height: auto;" 
             alt="Image">
    @endif
</td>
<td>
    @php
        $__ids = array_filter(array_map('trim', explode(',', (string) ($employee->skills ?? ''))));
        $__names = [];
        if (!empty($__ids)) {
            $__names = \App\Models\Skill::whereIn('id', $__ids)->pluck('name')->toArray();
        }
    @endphp
    {{ implode(', ', $__names) }}
</td>
<td>
    @if(!is_null($employee->status))
        <span class="badge bg-soft-primary text-primary">{{ $employee->status }}</span>
    @endif
</td>

                                    <td class="text-end">
                                        <div id="action_{{$employee->id}}" class="d-flex gap-1 justify-content-end">
                                            @can('employees-edit')
                                                <a href="{{ route('employees.edit', $employee->id) }}" 
                                                   class="btn btn-sm btn-primary" 
                                                   title="Edit">
                                                    <i class="ri-pencil-line"></i>
                                                </a>
                                            @endcan

                                            @can('employees-delete')
                                                <a id="trash_{{$employee->id}}"
                                                   class="btn btn-sm btn-danger"
                                                   title="Move to Trash"
                                                   onclick="suSoftDelete('{{ $employee->id }}', 'employees')">
                                                    <i class="ri-delete-bin-line"></i>
                                                </a>

                                                <a id="restore_{{$employee->id}}"
                                                   class="btn btn-sm btn-success"
                                                   title="Restore"
                                                   onclick="suRestore('{{ $employee->id }}', 'employees')"
                                                   style="display:none;">
                                                    <i class="ri-restart-line"></i>
                                                </a>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="11" class="text-center">No records found.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                @if($employees->hasPages())
                <div class="mt-4 d-flex justify-content-between align-items-center flex-wrap">
                    <div class="mb-2">
                        <p class="text-muted mb-0">
                            Showing {{ $employees->firstItem() ?? 0 }} to {{ $employees->lastItem() ?? 0 }} of {{ $employees->total() }} entries
                        </p>
                    </div>
                    <nav aria-label="Page navigation">
                        <ul class="pagination pagination-sm mb-0">
                            {{-- Previous Page Link --}}
                            @if ($employees->onFirstPage())
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </span>
                                </li>
                            @else
                                <li class="page-item">
                                    <a class="page-link" href="{{ $employees->appends(request()->except('page'))->previousPageUrl() }}" rel="prev">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </a>
                                </li>
                            @endif

                            {{-- Pagination Elements --}}
                            @php
                                $currentPage = $employees->currentPage();
                                $lastPage = $employees->lastPage();
                                $startPage = max(1, $currentPage - 2);
                                $endPage = min($lastPage, $currentPage + 2);
                            @endphp

                            @if($startPage > 1)
                                <li class="page-item">
                                    <a class="page-link" href="{{ $employees->appends(request()->except('page'))->url(1) }}">1</a>
                                </li>
                                @if($startPage > 2)
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                @endif
                            @endif

                            @for($page = $startPage; $page <= $endPage; $page++)
                                @if ($page == $currentPage)
                                    <li class="page-item active">
                                        <span class="page-link">{{ $page }}</span>
                                    </li>
                                @else
                                    <li class="page-item">
                                        <a class="page-link" href="{{ $employees->appends(request()->except('page'))->url($page) }}">{{ $page }}</a>
                                    </li>
                                @endif
                            @endfor

                            @if($endPage < $lastPage)
                                @if($endPage < $lastPage - 1)
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                @endif
                                <li class="page-item">
                                    <a class="page-link" href="{{ $employees->appends(request()->except('page'))->url($lastPage) }}">{{ $lastPage }}</a>
                                </li>
                            @endif

                            {{-- Next Page Link --}}
                            @if ($employees->hasMorePages())
                                <li class="page-item">
                                    <a class="page-link" href="{{ $employees->appends(request()->except('page'))->nextPageUrl() }}" rel="next">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </a>
                                </li>
                            @else
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </span>
                                </li>
                            @endif
                        </ul>
                    </nav>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>

@endsection
