<?php
/**
 * @var object $employees
 * @var string $search
 * @var string $status
 * @var string $branchId
 * @var array $branches
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-users me-2 text-gold"></i>Employees</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Employees</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('employees_create')): ?>
                <a href="<?= url('employees/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Employee
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-8">
                <form method="GET" action="<?= url('employees') ?>" class="d-flex gap-2 flex-wrap">
                    <div class="input-group flex-grow-1">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                               placeholder="Search by name, email or phone..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search)): ?>
                            <a href="<?= url('employees') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                    <select name="status" class="form-select form-select-sm bg-transparent" style="width:auto;" onchange="this.form.submit()">
                        <option value="">All Status</option>
                        <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                        <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                    </select>
                    <select name="branch_id" class="form-select form-select-sm bg-transparent" style="width:auto;" onchange="this.form.submit()">
                        <option value="">All Branches</option>
                        <?php foreach ($branches as $branch): ?>
                            <option value="<?= $branch->id ?>" <?= $branchId == $branch->id ? 'selected' : '' ?>>
                                <?= e($branch->name) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Job Title</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th class="text-end">Salary</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($employees->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-user-slash fa-2x mb-2"></i>
                                <p class="mb-0">No employees found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($employees->currentPage - 1) * $employees->perPage + 1; ?>
                        <?php foreach ($employees->data as $employee): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('employees/show/' . $employee->id) ?>" class="text-gold text-decoration-none">
                                        <div class="d-flex align-items-center gap-2">
                                            <?php if (!empty($employee->photo)): ?>
                                                <img src="<?= uploadUrl($employee->photo) ?>" alt="" class="rounded-circle" width="32" height="32" style="object-fit:cover;">
                                            <?php else: ?>
                                                <div class="avatar-placeholder rounded-circle d-flex align-items-center justify-content-center"
                                                     style="width:32px;height:32px;background:var(--gold);color:#000;font-size:14px;font-weight:600;">
                                                    <?= strtoupper(substr($employee->first_name ?? '?', 0, 1) . substr($employee->last_name ?? '', 0, 1)) ?>
                                                </div>
                                            <?php endif; ?>
                                            <strong><?= e($employee->first_name . ' ' . $employee->last_name) ?></strong>
                                        </div>
                                    </a>
                                </td>
                                <td><?= e($employee->job_title ?? '-') ?></td>
                                <td><?= e($employee->email ?? '-') ?></td>
                                <td><?= e($employee->phone ?? '-') ?></td>
                                <td class="text-end">
                                    <?php if ($employee->salary_type === 'fixed'): ?>
                                        <?= currency($employee->salary_amount) ?>/mo
                                    <?php elseif ($employee->salary_type === 'hourly'): ?>
                                        <?= currency($employee->hourly_rate) ?>/hr
                                    <?php elseif ($employee->salary_type === 'commission_only'): ?>
                                        Commission Only
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $employee->is_active ? 'success' : 'danger' ?>">
                                        <?= $employee->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('employees/show/' . $employee->id) ?>" class="btn btn-outline-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <?php if (can('employees_edit')): ?>
                                            <a href="<?= url('employees/edit/' . $employee->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('employees_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $employee->id ?>"
                                                    data-name="<?= e($employee->first_name . ' ' . $employee->last_name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($employees->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($employees->currentPage - 1) * $employees->perPage) + 1 ?>
                    to <?= min($employees->currentPage * $employees->perPage, $employees->total) ?>
                    of <?= $employees->total ?> entries
                </div>
                <?= paginateLinks($employees, url('employees')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>


