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

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

<?php
$monthNames = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$formatMins = static function (int $mins): string {
    if ($mins <= 0) return '—';
    $h = intdiv($mins, 60);
    $m = $mins % 60;
    return $h > 0 ? "{$h}h {$m}m" : "{$m}m";
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Monthly Attendance',
    'workspaceTitle'        => 'Monthly Attendance Report',
    'workspaceDesc'         => $monthNames[$month] . ' ' . $year . ' — presence summary for all directory employees.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('attendance/monthly') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Month</label>
                    <select name="month" class="form-select form-select-sm">
                        <?php for ($m = 1; $m <= 12; $m++): ?>
                            <option value="<?= $m ?>" <?= $month === $m ? 'selected' : '' ?>><?= $monthNames[$m] ?></option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Year</label>
                    <input type="number" name="year" class="form-control form-control-sm" value="<?= $year ?>" min="2020" max="2099">
                </div>
                <div>
                    <label class="enterprise-field-label">Employee</label>
                    <select name="employee_id" class="form-select form-select-sm">
                        <option value="">All</option>
                        <?php foreach ($employees as $e): ?>
                            <option value="<?= $e['id'] ?>" <?= ($filters['employeeId'] ?? null) == $e['id'] ? 'selected' : '' ?>><?= esc($e['full_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Department</label>
                    <select name="department_id" class="form-select form-select-sm">
                        <option value="">All</option>
                        <?php foreach ($departments as $d): ?>
                            <option value="<?= $d['id'] ?>" <?= ($filters['departmentId'] ?? null) == $d['id'] ? 'selected' : '' ?>><?= esc($d['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                    <a href="<?= esc($prevUrl) ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-chevron-left"></i></a>
                    <a href="<?= esc($nextUrl) ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-chevron-right"></i></a>
                    <?= view('Modules\Reports\Views\partials\export_buttons', [
                        'actionPath' => 'attendance/monthly',
                        'query'      => [
                            'month'         => $month,
                            'year'          => $year,
                            'employee_id'   => $filters['employeeId'] ?? '',
                            'department_id' => $filters['departmentId'] ?? '',
                        ],
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Employee Summary</h2>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Days Recorded</th>
                        <th>Present</th>
                        <th>Late</th>
                        <th>Absent</th>
                        <th>On Leave</th>
                        <th>Total Overtime</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($summaryByEmployee as $sum): ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($sum['name']) ?></span>
                                <br><small class="text-muted"><?= esc($sum['number']) ?></small>
                            </td>
                            <td><?= $sum['days'] ?></td>
                            <td class="text-success"><?= $sum['present'] ?></td>
                            <td class="text-warning"><?= $sum['late'] ?></td>
                            <td class="text-danger"><?= $sum['absent'] ?></td>
                            <td><?= $sum['leave'] ?></td>
                            <td><?= $formatMins($sum['overtime']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($summaryByEmployee) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No attendance records for this period.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Daily Records</h2>
    </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>Shift</th>
                        <th>In</th>
                        <th>Out</th>
                        <th>Status</th>
                        <th>Work</th>
                        <th>Source</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($records as $r): ?>
                        <tr>
                            <td><?= esc(date('M j', strtotime($r['attendance_date']))) ?></td>
                            <td><?= esc($r['full_name']) ?></td>
                            <td><?= esc($r['shift_name'] ?? '—') ?></td>
                            <td><?= $r['clock_in'] ? esc(date('h:i A', strtotime($r['clock_in']))) : '—' ?></td>
                            <td><?= $r['clock_out'] ? esc(date('h:i A', strtotime($r['clock_out']))) : '—' ?></td>
                            <td><?= esc(ucwords(str_replace('_', ' ', $r['status']))) ?></td>
                            <td><?= $formatMins((int) $r['work_minutes']) ?></td>
                            <td><code><?= esc($r['source']) ?></code></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($records) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No daily records found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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