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

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

<?php
$statusPill = static function (?string $status, bool $onLeave = false, bool $isHoliday = false): string {
    if ($onLeave) {
        return '<span class="enterprise-status-pill enterprise-status-pill--inactive">On Leave</span>';
    }
    if ($isHoliday) {
        return '<span class="enterprise-status-pill" style="background:#e8f4fd;color:#0d6efd;">Holiday</span>';
    }
    $map = [
        'present'      => 'active',
        'auto_present' => 'active',
        'late'         => 'inactive',
        'absent'       => 'inactive',
        'half_day'     => 'inactive',
        'on_leave'     => 'inactive',
        'holiday'      => 'inactive',
    ];
    $class = $map[$status ?? ''] ?? 'inactive';
    $label = $status ? ucwords(str_replace('_', ' ', $status)) : 'Not Marked';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};

$formatTime = static function (?string $dt): string {
    return $dt ? date('h:i A', strtotime($dt)) : '—';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Attendance',
    'workspaceTitle'        => 'Daily Attendance Board',
    'workspaceDesc'         => 'Clock in/out directory employees, track presence, and run daily auto-punch.',
]) ?>

<div class="enterprise-kpi-grid mb-4">
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Employees</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-users"></i></span></div>
        <div class="enterprise-kpi-value"><?= $stats['total'] ?></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Present</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-circle-check"></i></span></div>
        <div class="enterprise-kpi-value text-success"><?= $stats['present'] + $stats['late'] ?></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Late</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-clock"></i></span></div>
        <div class="enterprise-kpi-value text-warning"><?= $stats['late'] ?></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Absent</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-user-xmark"></i></span></div>
        <div class="enterprise-kpi-value text-danger"><?= $stats['absent'] ?></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">On Leave</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-umbrella-beach"></i></span></div>
        <div class="enterprise-kpi-value"><?= $stats['on_leave'] ?></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Clocked In</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-fingerprint"></i></span></div>
        <div class="enterprise-kpi-value"><?= $stats['clocked'] ?></div>
    </article>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Employee Attendance — <?= esc(date('l, F j, Y', strtotime($date))) ?></h2>
        <p class="enterprise-data-panel__subtitle">Punch clock for employees in the directory. Auto-punch fills shift schedules overnight.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <div class="enterprise-filter-grid">
            <form action="<?= base_url('attendance') ?>" method="get" class="d-contents">
                <div>
                    <label class="enterprise-field-label">Date</label>
                    <input type="date" name="date" class="form-control form-control-sm" value="<?= esc($date) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Search Employee</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Name or number" value="<?= esc($search) ?>">
                </div>
                <div class="enterprise-filter-actions d-flex gap-2 flex-wrap align-items-end">
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                </div>
            </form>
            <div class="enterprise-filter-actions d-flex gap-2 flex-wrap align-items-end">
                <a href="<?= base_url('attendance/manual') ?>" class="btn btn-sm btn-outline-secondary">Manual Entry</a>
            </div>
        </div>
        <form action="<?= base_url('attendance/auto-punch') ?>" method="post" class="mt-2" onsubmit="return confirm('Run auto-punch for yesterday? This fills attendance from shift schedules.');">
            <?= csrf_field() ?>
            <input type="hidden" name="date" value="<?= esc(date('Y-m-d', strtotime($date . ' -1 day'))) ?>">
            <button type="submit" class="btn btn-sm btn-outline-primary"><i class="fa-solid fa-wand-magic-sparkles me-1"></i>Auto Punch Yesterday</button>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Shift</th>
                        <th>Clock In</th>
                        <th>Clock Out</th>
                        <th>Status</th>
                        <th>Late</th>
                        <th>OT</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($board as $row):
                        $emp    = $row['employee'];
                        $rec    = $row['record'];
                        $shift  = $row['shift'];
                        $hasIn  = $rec && $rec['clock_in'];
                        $hasOut = $rec && $rec['clock_out'];
                    ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($emp['full_name']) ?></span>
                                <br><small class="text-muted"><?= esc($emp['employee_number']) ?></small>
                            </td>
                            <td>
                                <?php if ($shift): ?>
                                    <?= esc($shift['name'] ?? $shift['shift_name'] ?? '—') ?>
                                    <br><small class="text-muted"><?= esc(substr($shift['start_time'] ?? '', 0, 5)) ?> – <?= esc(substr($shift['end_time'] ?? '', 0, 5)) ?></small>
                                <?php else: ?>
                                    <span class="text-muted">No shift</span>
                                <?php endif; ?>
                            </td>
                            <td><?= $formatTime($rec['clock_in'] ?? null) ?></td>
                            <td><?= $formatTime($rec['clock_out'] ?? null) ?></td>
                            <td><?= $statusPill($rec['status'] ?? null, $row['on_leave'], $row['is_holiday']) ?></td>
                            <td><?= ($rec['late_minutes'] ?? 0) > 0 ? esc($rec['late_minutes']) . ' min' : '—' ?></td>
                            <td><?= ($rec['overtime_minutes'] ?? 0) > 0 ? esc($rec['overtime_minutes']) . ' min' : '—' ?></td>
                            <td class="text-end enterprise-row-actions">
                                <div class="d-flex gap-1 justify-content-end flex-wrap">
                                    <?php if (! $row['on_leave'] && ! $row['is_holiday']): ?>
                                        <?php if (! $hasIn): ?>
                                            <form action="<?= base_url('attendance/clock-in') ?>" method="post">
                                                <?= csrf_field() ?>
                                                <input type="hidden" name="employee_id" value="<?= $emp['id'] ?>">
                                                <button type="submit" class="btn btn-sm btn-success" title="Clock In"><i class="fa-solid fa-right-to-bracket"></i></button>
                                            </form>
                                        <?php elseif (! $hasOut): ?>
                                            <form action="<?= base_url('attendance/clock-out') ?>" method="post">
                                                <?= csrf_field() ?>
                                                <input type="hidden" name="employee_id" value="<?= $emp['id'] ?>">
                                                <button type="submit" class="btn btn-sm btn-warning" title="Clock Out"><i class="fa-solid fa-right-from-bracket"></i></button>
                                            </form>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                    <?php if ($rec): ?>
                                        <a href="<?= base_url('attendance/corrections/create?record_id=' . $rec['id']) ?>" class="btn btn-sm btn-outline-secondary" title="Request Correction"><i class="fa-solid fa-pen-to-square"></i></a>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($board) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No active employees in the directory.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if (! empty($pager)): ?>
            <div class="enterprise-data-panel__footer">
                <?= $pager->links() ?>
            </div>
        <?php endif; ?>
    </div>
</div>

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