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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Leave Balances',
    'workspaceTitle'      => 'Leave Balance Workspace',
    'workspaceDesc'       => 'Track employee leave entitlements, usage, and remaining days by type and year.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Leave Balance Registry</h2>
        <p class="enterprise-data-panel__subtitle">View and adjust entitlements per employee and leave type.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('leave/balances') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Year</label>
                    <input type="number" name="year" class="form-control form-control-sm" value="<?= esc($filters['year']) ?>" min="2020" max="2099">
                </div>
                <div>
                    <label class="enterprise-field-label">Employee</label>
                    <select name="employee_id" class="form-select form-select-sm" required onchange="this.form.submit()">
                        <option value="">— Select employee —</option>
                        <?php foreach ($employees as $e): ?>
                            <option value="<?= $e['id'] ?>" <?= (string) $filters['employeeId'] === (string) $e['id'] ? 'selected' : '' ?>>
                                <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">View Balances</button>
                </div>
            </div>
        </form>
        <form action="<?= base_url('leave/balances/initialize') ?>" method="post" class="mt-2">
            <?= csrf_field() ?>
            <input type="hidden" name="year" value="<?= esc($filters['year']) ?>">
            <button type="submit" class="btn btn-sm btn-outline-secondary">Initialize All Employees</button>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <?php if (! $filters['employeeId']): ?>
            <p class="text-muted text-center py-5">Select an employee to view their leave balances.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Leave Type</th>
                            <th>Entitled</th>
                            <th>Used</th>
                            <th>Remaining</th>
                            <th class="text-end">Adjust</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($types as $t): ?>
                            <?php
                            $bal = $balances[$t['id']] ?? null;
                            $entitled  = $bal ? (float) $bal['entitled'] : (float) $t['days_per_year'];
                            $used      = $bal ? (float) $bal['used'] : 0;
                            $remaining = max(0, $entitled - $used);
                            ?>
                            <tr>
                                <td>
                                    <span class="enterprise-data-table__primary"><?= esc($t['name']) ?></span>
                                    <span class="text-muted small d-block"><code><?= esc($t['code']) ?></code></span>
                                </td>
                                <td><?= number_format($entitled, 1) ?></td>
                                <td><?= number_format($used, 1) ?></td>
                                <td><span class="enterprise-data-table__amount"><?= number_format($remaining, 1) ?></span></td>
                                <td class="text-end">
                                    <?php if ($bal): ?>
                                        <form action="<?= base_url('leave/balances/' . $bal['id']) ?>" method="post" class="d-inline-flex gap-1 align-items-center">
                                            <?= csrf_field() ?>
                                            <input type="number" name="entitled" class="form-control form-control-sm" style="width:80px" step="0.5" min="0" value="<?= $entitled ?>">
                                            <button type="submit" class="btn btn-sm btn-outline-primary">Save</button>
                                        </form>
                                    <?php else: ?>
                                        <span class="text-muted small">Not initialized</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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