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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Holiday Calendar',
    'workspaceTitle'      => 'Holiday Calendar Workspace',
    'workspaceDesc'       => 'Manage public holidays and company-wide non-working days.',
    'workspaceAction'   => [
        'href'  => base_url('holidays/create'),
        'label' => 'Add Holiday',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Holiday Registry</h2>
        <p class="enterprise-data-panel__subtitle">Public holidays shown on the leave calendar.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('holidays') ?>" 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="Holiday name"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <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">Branch</label>
                    <select name="branch_id" class="form-select form-select-sm">
                        <option value="">All branches</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>
                    <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>Holiday</th>
                        <th>Date</th>
                        <th>Branch</th>
                        <th>Recurring</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($holidays as $h): ?>
                        <?php $branchName = array_column($branches, 'name', 'id')[$h['branch_id']] ?? 'All branches'; ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($h['name']) ?></span></td>
                            <td><?= esc(date('D, M j, Y', strtotime($h['date']))) ?></td>
                            <td><?= esc($branchName) ?></td>
                            <td><?= $h['is_recurring'] ? 'Yes' : 'No' ?></td>
                            <td class="text-end enterprise-row-actions">
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown"><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('holidays/' . $h['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit</a></li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li>
                                            <form action="<?= base_url('holidays/' . $h['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this holiday?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="dropdown-item text-danger"><i class="fa-solid fa-trash me-2"></i>Delete</button>
                                            </form>
                                        </li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($holidays) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No holidays configured for this period.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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