 

<?php $__env->startSection('content'); ?>
<?php $__env->startSection('title','Departments'); ?>

<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">Departments</h4>
                </div>
                <div class="d-flex flex-wrap gap-2 justify-content-start justify-content-md-end w-100 w-md-auto">
                    <a href="<?php echo e(route('departments.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, '<?php echo e(route('departments.import.csv')); ?>');">
                    </label>

                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check("departments-create")): ?> 
                    <a href="<?php echo e(route('departments.create')); ?>" class='btn btn-primary'>
                        <i class="ri-add-line me-1"></i> Add Departments
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="card-body">
                <!-- Search Form -->
                <form method="GET" action="<?php echo e(route('departments.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="<?php echo e(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>
                        <?php if(request('search')): ?>
                            <div class="col-md-2">
                                <a href="<?php echo e(route('departments.index')); ?>" class="btn btn-secondary w-100">
                                    <i class="ri-close-line"></i> Clear
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                    <!-- Preserve sort parameters -->
                    <?php if(request('sort_by')): ?>
                        <input type="hidden" name="sort_by" value="<?php echo e(request('sort_by')); ?>">
                    <?php endif; ?>
                    <?php if(request('sort_order')): ?>
                        <input type="hidden" name="sort_order" value="<?php echo e(request('sort_order')); ?>">
                    <?php 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="<?php echo e(route('departments.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.
                                        <?php echo renderSortIcon('id', 'name'); ?>

                                    </a>
                                </th>
                                <th>
    <a href="<?php echo e(route('departments.index', array_merge(request()->except(['sort_by', 'sort_order', 'page']), ['sort_by' => 'name', 'sort_order' => request('sort_by') == 'name' && request('sort_order') == 'asc' ? 'desc' : 'asc']))); ?>" 
       class="text-decoration-none text-dark">
        Name
        <?php echo renderSortIcon('name', 'name'); ?>

    </a>
</th>
<th>
    <a href="<?php echo e(route('departments.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
        <?php echo renderSortIcon('status', 'name'); ?>

    </a>
</th>

                                <th class='text-end' style='width:10%'>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $departments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $department): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr id="row_<?php echo e($department->id); ?>">
                                    <td><?php echo e($departments->firstItem() + $loop->index); ?></td>
                                    <td><?php echo e($department->name ?? ''); ?></td>
<td>
    <?php if(!is_null($department->status)): ?>
        <span class="badge bg-soft-primary text-primary"><?php echo e($department->status); ?></span>
    <?php endif; ?>
</td>

                                    <td class="text-end">
                                        <div id="action_<?php echo e($department->id); ?>" class="d-flex gap-1 justify-content-end">
                                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('departments-edit')): ?>
                                                <a href="<?php echo e(route('departments.edit', $department->id)); ?>" 
                                                   class="btn btn-sm btn-primary" 
                                                   title="Edit">
                                                    <i class="ri-pencil-line"></i>
                                                </a>
                                            <?php endif; ?>

                                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('departments-delete')): ?>
                                                <a id="trash_<?php echo e($department->id); ?>"
                                                   class="btn btn-sm btn-danger"
                                                   title="Move to Trash"
                                                   onclick="suSoftDelete('<?php echo e($department->id); ?>', 'departments')">
                                                    <i class="ri-delete-bin-line"></i>
                                                </a>

                                                <a id="restore_<?php echo e($department->id); ?>"
                                                   class="btn btn-sm btn-success"
                                                   title="Restore"
                                                   onclick="suRestore('<?php echo e($department->id); ?>', 'departments')"
                                                   style="display:none;">
                                                    <i class="ri-restart-line"></i>
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="4" class="text-center">No records found.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <?php if($departments->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 <?php echo e($departments->firstItem() ?? 0); ?> to <?php echo e($departments->lastItem() ?? 0); ?> of <?php echo e($departments->total()); ?> entries
                        </p>
                    </div>
                    <nav aria-label="Page navigation">
                        <ul class="pagination pagination-sm mb-0">
                            
                            <?php if($departments->onFirstPage()): ?>
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </span>
                                </li>
                            <?php else: ?>
                                <li class="page-item">
                                    <a class="page-link" href="<?php echo e($departments->appends(request()->except('page'))->previousPageUrl()); ?>" rel="prev">
                                        <i class="ri-arrow-left-s-line"></i>
                                    </a>
                                </li>
                            <?php endif; ?>

                            
                            <?php
                                $currentPage = $departments->currentPage();
                                $lastPage = $departments->lastPage();
                                $startPage = max(1, $currentPage - 2);
                                $endPage = min($lastPage, $currentPage + 2);
                            ?>

                            <?php if($startPage > 1): ?>
                                <li class="page-item">
                                    <a class="page-link" href="<?php echo e($departments->appends(request()->except('page'))->url(1)); ?>">1</a>
                                </li>
                                <?php if($startPage > 2): ?>
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                <?php endif; ?>
                            <?php endif; ?>

                            <?php for($page = $startPage; $page <= $endPage; $page++): ?>
                                <?php if($page == $currentPage): ?>
                                    <li class="page-item active">
                                        <span class="page-link"><?php echo e($page); ?></span>
                                    </li>
                                <?php else: ?>
                                    <li class="page-item">
                                        <a class="page-link" href="<?php echo e($departments->appends(request()->except('page'))->url($page)); ?>"><?php echo e($page); ?></a>
                                    </li>
                                <?php endif; ?>
                            <?php endfor; ?>

                            <?php if($endPage < $lastPage): ?>
                                <?php if($endPage < $lastPage - 1): ?>
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                <?php endif; ?>
                                <li class="page-item">
                                    <a class="page-link" href="<?php echo e($departments->appends(request()->except('page'))->url($lastPage)); ?>"><?php echo e($lastPage); ?></a>
                                </li>
                            <?php endif; ?>

                            
                            <?php if($departments->hasMorePages()): ?>
                                <li class="page-item">
                                    <a class="page-link" href="<?php echo e($departments->appends(request()->except('page'))->nextPageUrl()); ?>" rel="next">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </a>
                                </li>
                            <?php else: ?>
                                <li class="page-item disabled">
                                    <span class="page-link">
                                        <i class="ri-arrow-right-s-line"></i>
                                    </span>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.metor.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /Applications/MAMP/htdocs/payroll/resources/views/departments/index.blade.php ENDPATH**/ ?>