<?php
include '../../includes/header.php';
require_once __DIR__ . '/_module.php';

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$date = $_GET['date'] ?? date('Y-m-d');
$attendanceRecords = $payroll->getAttendanceRecords($date);
$employees = $payroll->getAllEmployees();

payrollModuleStyles();
?>

<div class="container-fluid px-4">
    <?php payrollModuleNav('attendance'); ?>

    <div class="payroll-page-head">
        <div>
            <h1>Attendance</h1>
            <p>Monitor attendance, absence, worked hours, and overtime for payroll processing.</p>
        </div>
        <div class="no-print">
            <a href="../dashboard/admin.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Admin Dashboard</a>
            <button type="button" class="btn btn-outline-secondary" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
        </div>
    </div>

    <?php payrollMigrationNotice($payroll, ['hr_attendance'], 'Attendance history and overtime need the professional HR schema. A printable daily register is shown until the table is installed.'); ?>

    <div class="payroll-panel">
        <div class="payroll-panel-title">
            <h2>Daily Register</h2>
            <form method="GET" class="d-flex gap-2 no-print">
                <input type="date" class="form-control form-control-sm" name="date" value="<?php echo htmlspecialchars($date); ?>">
                <button class="btn btn-sm btn-outline-primary" type="submit">Filter</button>
            </form>
        </div>

        <div class="table-responsive">
            <table class="table table-bordered payroll-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Branch</th>
                        <th>Position</th>
                        <th>Status</th>
                        <th>Clock In</th>
                        <th>Clock Out</th>
                        <th>Hours</th>
                        <th>Overtime</th>
                        <th>Remarks</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($attendanceRecords)): ?>
                        <?php foreach ($attendanceRecords as $record): ?>
                            <tr>
                                <td><?php echo payrollText($record['employee_fullname'] ?? ''); ?></td>
                                <td><?php echo payrollText($record['branch'] ?? ''); ?></td>
                                <td><?php echo payrollText($record['employee_position'] ?? ''); ?></td>
                                <td><?php echo payrollStatusBadge($record['status'] ?? 'present'); ?></td>
                                <td><?php echo payrollText($record['clock_in'] ?? ''); ?></td>
                                <td><?php echo payrollText($record['clock_out'] ?? ''); ?></td>
                                <td><?php echo number_format((float)($record['hours_worked'] ?? 0), 2); ?></td>
                                <td><?php echo number_format((float)($record['overtime_hours'] ?? 0), 2); ?></td>
                                <td><?php echo payrollText($record['remarks'] ?? ''); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <?php foreach ($employees as $employee): ?>
                            <tr>
                                <td><?php echo payrollText($employee['employee_fullname'] ?? ''); ?></td>
                                <td><?php echo payrollText($employee['branch_name'] ?? $employee['branch'] ?? ''); ?></td>
                                <td><?php echo payrollText($employee['employee_position'] ?? ''); ?></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($employees)): ?>
                            <tr><td colspan="9" class="text-center">No employees found.</td></tr>
                        <?php endif; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php payrollModuleEnd(); ?>
</div>

<?php include '../../includes/footer.php'; ?>
