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

<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">Salary Structures</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('salary-structures.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('salary-structures.import.csv') }}');">
                    </label>

                    @can("salary-structures-create") 
                    <a href="{{ route('salary-structures.create') }}" class='btn btn-primary'>
                        <i class="ri-add-line me-1"></i> Add Salary Structures
                    </a>
                    @endcan
                </div>
            </div>
            <div class="card-body">
                <!-- Search Form -->
                <form method="GET" action="{{ route('salary-structures.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('salary-structures.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('salary-structures.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', 'client_id') !!}
                                    </a>
                                </th>
                                <th>
    <a href="{{ route('salary-structures.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', 'client_id') !!}
    </a>
</th>
<th>
    <a href="{{ route('salary-structures.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'employee_id', 'sort_order' => request('sort_by') == 'employee_id' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Employee
        {!! renderSortIcon('employee_id', 'client_id') !!}
    </a>
</th>
<th>
    <a href="{{ route('salary-structures.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', 'client_id') !!}
    </a>
</th>
<th>
    <a href="{{ route('salary-structures.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'effective_from', 'sort_order' => request('sort_by') == 'effective_from' && request('sort_order') == 'asc' ? 'desc' : 'asc'])) }}" 
       class="text-decoration-none text-dark">
        Effective From
        {!! renderSortIcon('effective_from', 'client_id') !!}
    </a>
</th>
<th>
    <a href="{{ route('salary-structures.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', 'client_id') !!}
    </a>
</th>

                                <th class='text-end' style='width:10%'>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($salary_structures as $salary_structure)
                                <tr id="row_{{$salary_structure->id}}">
                                    <td>{{ $salary_structures->firstItem() + $loop->index }}</td>
                                    <td>{{ $salary_structure->client ? $salary_structure->client->name : '' }}</td>
<td>{{ $salary_structure->employee ? $salary_structure->employee->first_name : '' }}</td>
<td>{{ number_format((float) ($salary_structure->basic_salary ?? 0), 2) }}</td>
<td>{{ $salary_structure->effective_from ? date('M d, Y', strtotime($salary_structure->effective_from)) : '' }}</td>
<td>
    @if(!is_null($salary_structure->status))
        <span class="badge bg-soft-primary text-primary">{{ $salary_structure->status }}</span>
    @endif
</td>

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

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

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

                <!-- Pagination -->
                @if($salary_structures->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 {{ $salary_structures->firstItem() ?? 0 }} to {{ $salary_structures->lastItem() ?? 0 }} of {{ $salary_structures->total() }} entries
                        </p>
                    </div>
                    <nav aria-label="Page navigation">
                        <ul class="pagination pagination-sm mb-0">
                            {{-- Previous Page Link --}}
                            @if ($salary_structures->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="{{ $salary_structures->appends(request()->except('page'))->previousPageUrl() }}" rel="prev">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </a>
                                </li>
                            @endif

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

                            @if($startPage > 1)
                                <li class="page-item">
                                    <a class="page-link" href="{{ $salary_structures->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="{{ $salary_structures->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="{{ $salary_structures->appends(request()->except('page'))->url($lastPage) }}">{{ $lastPage }}</a>
                                </li>
                            @endif

                            {{-- Next Page Link --}}
                            @if ($salary_structures->hasMorePages())
                                <li class="page-item">
                                    <a class="page-link" href="{{ $salary_structures->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
