<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
  $map = [
    'active'     => 'active',
    'inactive'   => 'inactive',
    'on_leave'   => 'on_leave',
    'terminated' => 'terminated',
  ];
  $class = $map[$status] ?? 'inactive';
  $label = ucwords(str_replace('_', ' ', $status));
  return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent'   => 'Employees',
    'workspaceTitle'        => 'Employee Directory Workspace',
    'workspaceDesc'         => 'Centralized human capital registry for employee records, departmental assignments, identity documentation, and employment status governance.',
    'workspaceAction'       => [
        'href'  => base_url('employees/create'),
        'label' => 'Add Employee',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Employee Registry</h2>
        <p class="enterprise-data-panel__subtitle">Master list of personnel records with departmental alignment, CPR identity, and employment status.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('employees') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Name, ID, CPR..."
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['active', 'inactive', 'on_leave', 'terminated'] as $s): ?>
                            <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= esc(ucwords(str_replace('_', ' ', $s))) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Department</label>
                    <select name="department_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($departments as $d): ?>
                            <option value="<?= $d['id'] ?>" <?= (string) $filters['departmentId'] === (string) $d['id'] ? 'selected' : '' ?>><?= esc($d['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Branch</label>
                    <select name="branch_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($branches as $b): ?>
                            <option value="<?= $b['id'] ?>" <?= (string) $filters['branchId'] === (string) $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                    <div class="form-check mb-0">
                        <input class="form-check-input" type="checkbox" id="trashed" name="trashed" value="1" <?= $filters['showTrashed'] ? 'checked' : '' ?> onchange="this.form.submit()">
                        <label class="form-check-label small" for="trashed">Show deleted</label>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Employee ID</th>
                        <th>Department</th>
                        <th>CPR Number</th>
                        <th>Employment Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($employees as $e): ?>
                        <?php $deptName = array_column($departments, 'name', 'id')[$e['department_id']] ?? '—'; ?>
                        <tr class="<?= $e['deleted_at'] ? 'opacity-50' : '' ?>">
                            <td>
                                <div class="enterprise-employee-cell">
                                    <?php if (! empty($e['photo_path'])): ?>
                                        <img src="<?= base_url($e['photo_path']) ?>" class="enterprise-employee-avatar" alt="">
                                    <?php else: ?>
                                        <span class="enterprise-employee-avatar enterprise-employee-avatar--placeholder"><?= esc(strtoupper(substr($e['full_name'], 0, 1))) ?></span>
                                    <?php endif; ?>
                                    <div>
                                        <a href="<?= base_url('employees/' . $e['id']) ?>" class="enterprise-data-table__primary"><?= esc($e['full_name']) ?></a>
                                        <?php if ($e['job_title']): ?>
                                            <div class="enterprise-data-table__meta"><?= esc($e['job_title']) ?></div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </td>
                            <td><span class="enterprise-data-table__mono"><?= esc($e['employee_number']) ?></span></td>
                            <td><?= esc($deptName) ?></td>
                            <td><span class="enterprise-data-table__mono"><?= esc($e['cpr_number'] ?: '—') ?></span></td>
                            <td>
                                <?= $statusPill($e['status']) ?>
                                <?php if ($e['deleted_at']): ?>
                                    <span class="enterprise-status-pill enterprise-status-pill--deleted">Deleted</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end enterprise-row-actions">
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown" aria-label="Actions"><i class="fa-solid fa-ellipsis"></i></button>
                                    <ul class="dropdown-menu dropdown-menu-end enterprise-nav-menu">
                                        <li><a class="dropdown-item" href="<?= base_url('employees/' . $e['id']) ?>"><i class="fa-solid fa-eye me-2"></i>View Profile</a></li>
                                        <?php if (! $e['deleted_at']): ?>
                                            <li><a class="dropdown-item" href="<?= base_url('employees/' . $e['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit Record</a></li>
                                            <li>
                                                <?php if ($e['status'] === 'active'): ?>
                                                    <form action="<?= base_url('employees/' . $e['id'] . '/deactivate') ?>" method="post">
                                                        <?= csrf_field() ?>
                                                        <button type="submit" class="dropdown-item"><i class="fa-solid fa-ban me-2"></i>Deactivate</button>
                                                    </form>
                                                <?php else: ?>
                                                    <form action="<?= base_url('employees/' . $e['id'] . '/activate') ?>" method="post">
                                                        <?= csrf_field() ?>
                                                        <button type="submit" class="dropdown-item"><i class="fa-solid fa-check me-2"></i>Activate</button>
                                                    </form>
                                                <?php endif; ?>
                                            </li>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <form action="<?= base_url('employees/' . $e['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Move this employee to trash?');">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i>Delete</button>
                                                </form>
                                            </li>
                                        <?php else: ?>
                                            <li>
                                                <form action="<?= base_url('employees/' . $e['id'] . '/restore') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="dropdown-item"><i class="fa-solid fa-rotate-left me-2"></i>Restore</button>
                                                </form>
                                            </li>
                                        <?php endif; ?>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($employees) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No employees found in the registry.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php if ($pager->getPageCount() > 1): ?>
        <div class="enterprise-data-panel__footer"><?= $pager->links() ?></div>
    <?php endif; ?>
</div>

<?= $this->endSection() ?>
