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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Request Correction',
    'workspaceTitle'        => 'Attendance Correction Request',
    'workspaceDesc'         => 'Submit corrected clock times with a reason for manager approval.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-pen-to-square"></i>
        <h2 class="enterprise-settings-card__title">Correction Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <?php if (! $record): ?>
        <div class="alert alert-warning">No attendance record selected. Go to the <a href="<?= base_url('attendance') ?>">attendance board</a> and click the correction icon on a row.</div>
    <?php else: ?>
        <div class="mb-3 p-3 bg-light rounded small">
            <strong>Record #<?= $record['id'] ?></strong> — <?= esc($record['attendance_date']) ?><br>
            Current: In <?= $record['clock_in'] ? esc(date('h:i A', strtotime($record['clock_in']))) : '—' ?>,
            Out <?= $record['clock_out'] ? esc(date('h:i A', strtotime($record['clock_out']))) : '—' ?>
            (<?= esc(ucwords(str_replace('_', ' ', $record['status']))) ?>)
        </div>

        <form action="<?= base_url('attendance/corrections') ?>" method="post" novalidate>
            <?= csrf_field() ?>
            <input type="hidden" name="attendance_record_id" value="<?= $record['id'] ?>">
            <div class="enterprise-settings-fields enterprise-settings-fields--single">
                <div class="row g-3">
                    <div class="col-md-6">
                        <div class="enterprise-field">
                            <label class="enterprise-field-label">Corrected Clock In</label>
                            <input type="time" name="corrected_clock_in" class="form-control"
                                value="<?= esc(old('corrected_clock_in', $record['clock_in'] ? date('H:i', strtotime($record['clock_in'])) : '')) ?>">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="enterprise-field">
                            <label class="enterprise-field-label">Corrected Clock Out</label>
                            <input type="time" name="corrected_clock_out" class="form-control"
                                value="<?= esc(old('corrected_clock_out', $record['clock_out'] ? date('H:i', strtotime($record['clock_out'])) : '')) ?>">
                        </div>
                    </div>
                </div>
                <div class="enterprise-field">
                    <label class="enterprise-field-label">Corrected Status</label>
                    <select name="corrected_status" class="form-select">
                        <?php foreach (['present', 'late', 'half_day', 'absent'] as $st): ?>
                            <option value="<?= $st ?>" <?= old('corrected_status', $record['status']) === $st ? 'selected' : '' ?>><?= ucwords(str_replace('_', ' ', $st)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-field">
                    <label class="enterprise-field-label">Reason <span class="text-danger">*</span></label>
                    <textarea name="reason" class="form-control" rows="3" required><?= esc(old('reason')) ?></textarea>
                </div>
            </div>
            <div class="enterprise-settings-card__footer">
                <a href="<?= base_url('attendance/corrections') ?>" class="btn btn-outline-secondary">Cancel</a>
                <button type="submit" class="btn btn-primary">Submit Request</button>
            </div>
        </form>
    <?php endif; ?>
</article>

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