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

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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Leave Requests',
    'workspaceTitle'      => 'Leave Request Workspace',
    'workspaceDesc'       => 'Submit, review, and approve employee leave requests through the approval workflow.',
    'workspaceAction'   => [
        'href'  => base_url('leave/requests/create'),
        'label' => 'New Request',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Leave Request Registry</h2>
        <p class="enterprise-data-panel__subtitle">Pending approvals, approved leave, and request history.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('leave/requests') ?>" 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="Reason..."
                           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 (['pending', 'approved', 'rejected', 'cancelled'] as $s): ?>
                            <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Employee</label>
                    <select name="employee_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($employees as $e): ?>
                            <option value="<?= $e['id'] ?>" <?= (string) $filters['employeeId'] === (string) $e['id'] ? 'selected' : '' ?>><?= esc($e['full_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Leave Type</label>
                    <select name="leave_type_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($types as $t): ?>
                            <option value="<?= $t['id'] ?>" <?= (string) $filters['leaveTypeId'] === (string) $t['id'] ? 'selected' : '' ?>><?= esc($t['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                    <a href="<?= base_url('leave/calendar') ?>" class="btn btn-sm btn-outline-secondary">Leave Calendar</a>
                </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>Leave Type</th>
                        <th>Dates</th>
                        <th>Days</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($requests as $r): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($employeeMap[$r['employee_id']] ?? '—') ?></span></td>
                            <td><?= esc($typeMap[$r['leave_type_id']] ?? '—') ?></td>
                            <td><?= esc($r['start_date']) ?> → <?= esc($r['end_date']) ?></td>
                            <td><?= number_format((float) $r['days'], 1) ?></td>
                            <td><?= $statusPill($r['status']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('leave/requests/' . $r['id']) ?>" class="btn btn-sm btn-outline-secondary">View</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($requests) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No leave requests found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="mt-3"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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