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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Corrections',
    'workspaceTitle'        => 'Attendance Corrections',
    'workspaceDesc'         => 'Review and approve punch time corrections for directory employees.',
    'workspaceAction'       => [
        'href'  => base_url('attendance/corrections/create'),
        'label' => 'Request Correction',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('attendance/corrections') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">All</option>
                        <?php foreach (['pending', 'approved', 'rejected'] as $s): ?>
                            <option value="<?= $s ?>" <?= $status === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Filter</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Employee</th>
                        <th>Original</th>
                        <th>Corrected</th>
                        <th>Reason</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $fmt = static fn (?string $dt) => $dt ? date('h:i A', strtotime($dt)) : '—';
                    foreach ($corrections as $c):
                    ?>
                        <tr>
                            <td><?= esc(date('M j, Y', strtotime($c['attendance_date']))) ?></td>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($c['full_name']) ?></span>
                                <br><small class="text-muted"><?= esc($c['employee_number']) ?></small>
                            </td>
                            <td>
                                <small>In: <?= $fmt($c['original_clock_in']) ?></small><br>
                                <small>Out: <?= $fmt($c['original_clock_out']) ?></small>
                            </td>
                            <td>
                                <small>In: <?= $fmt($c['corrected_clock_in']) ?></small><br>
                                <small>Out: <?= $fmt($c['corrected_clock_out']) ?></small>
                            </td>
                            <td><small><?= esc($c['reason']) ?></small></td>
                            <td><span class="enterprise-status-pill enterprise-status-pill--<?= $c['status'] === 'approved' ? 'active' : 'inactive' ?>"><?= ucfirst($c['status']) ?></span></td>
                            <td class="text-end">
                                <?php if ($c['status'] === 'pending'): ?>
                                    <form action="<?= base_url('attendance/corrections/' . $c['id'] . '/approve') ?>" method="post" class="d-inline">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-success" title="Approve"><i class="fa-solid fa-check"></i></button>
                                    </form>
                                    <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#rejectModal<?= $c['id'] ?>" title="Reject"><i class="fa-solid fa-xmark"></i></button>
                                    <div class="modal fade" id="rejectModal<?= $c['id'] ?>" tabindex="-1">
                                        <div class="modal-dialog modal-sm">
                                            <div class="modal-content">
                                                <form action="<?= base_url('attendance/corrections/' . $c['id'] . '/reject') ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <div class="modal-header">
                                                        <h6 class="modal-title">Reject Correction</h6>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <textarea name="rejection_reason" class="form-control form-control-sm" rows="2" placeholder="Reason (optional)"></textarea>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-sm btn-danger">Reject</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <span class="text-muted small">—</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($corrections) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No correction requests.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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