<?php
/**
 * @var array $report
 * @var string $dateFrom
 * @var string $dateTo
 * @var string $employeeId
 * @var array $employees
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-chart-bar me-2 text-gold"></i>Attendance Report</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-file-alt card-icon"></i>
            <h6 class="card-title">Summary Report</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('attendance') ?>" class="btn btn-sm btn-outline-secondary">
                <i class="fas fa-arrow-left me-1"></i>Back to Attendance
            </a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-8">
                <form method="GET" action="<?= url('attendance/report') ?>" class="d-flex gap-2 flex-wrap">
                    <div class="input-group" style="width:auto;">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-calendar-start text-gold"></i>
                        </span>
                        <input type="date" name="date_from" class="form-control form-control-sm bg-transparent"
                               value="<?= e($dateFrom) ?>">
                    </div>
                    <div class="input-group" style="width:auto;">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-calendar-end text-gold"></i>
                        </span>
                        <input type="date" name="date_to" class="form-control form-control-sm bg-transparent"
                               value="<?= e($dateTo) ?>">
                    </div>
                    <select name="employee_id" class="form-select form-select-sm bg-transparent" style="width:auto;">
                        <option value="">All Employees</option>
                        <?php foreach ($employees as $emp): ?>
                            <option value="<?= $emp->id ?>" <?= $employeeId == $emp->id ? 'selected' : '' ?>>
                                <?= e($emp->first_name . ' ' . $emp->last_name) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <button type="submit" class="btn btn-sm btn-gold">
                        <i class="fas fa-filter me-1"></i>Filter
                    </button>
                </form>
            </div>
        </div>

        <?php if (!empty($report)): ?>
            <div class="row g-3 mb-4">
                <div class="col-md-3 col-6">
                    <div class="glass-card text-center py-3">
                        <div class="fs-3 text-gold fw-bold"><?= array_sum(array_column($report, 'present_days')) ?></div>
                        <div class="small text-muted">Total Present</div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="glass-card text-center py-3">
                        <div class="fs-3 text-warning fw-bold"><?= array_sum(array_column($report, 'late_days')) ?></div>
                        <div class="small text-muted">Total Late</div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="glass-card text-center py-3">
                        <div class="fs-3 text-danger fw-bold"><?= array_sum(array_column($report, 'absent_days')) ?></div>
                        <div class="small text-muted">Total Absent</div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="glass-card text-center py-3">
                        <div class="fs-3 text-info fw-bold"><?= number_format(array_sum(array_column($report, 'total_hours')), 1) ?></div>
                        <div class="small text-muted">Total Hours</div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Job Title</th>
                        <th class="text-center">Total Days</th>
                        <th class="text-center">Present</th>
                        <th class="text-center">Late</th>
                        <th class="text-center">Absent</th>
                        <th class="text-end">Total Hours</th>
                        <th class="text-center">Attendance %</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($report)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-chart-simple fa-2x mb-2"></i>
                                <p class="mb-0">No data for the selected period</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($report as $row): ?>
                            <?php $attPct = $row->total_days > 0 ? round(($row->present_days / $row->total_days) * 100, 1) : 0; ?>
                            <tr>
                                <td>
                                    <a href="<?= url('employees/show/' . $row->employee_id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($row->first_name . ' ' . $row->last_name) ?></strong>
                                    </a>
                                </td>
                                <td><?= e($row->job_title ?? '-') ?></td>
                                <td class="text-center"><?= (int)$row->total_days ?></td>
                                <td class="text-center">
                                    <span class="text-success fw-bold"><?= (int)$row->present_days ?></span>
                                </td>
                                <td class="text-center">
                                    <span class="text-warning fw-bold"><?= (int)$row->late_days ?></span>
                                </td>
                                <td class="text-center">
                                    <span class="text-danger fw-bold"><?= (int)$row->absent_days ?></span>
                                </td>
                                <td class="text-end"><?= number_format($row->total_hours, 1) ?></td>
                                <td class="text-center">
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="progress flex-grow-1" style="height:6px;">
                                            <div class="progress-bar bg-gold" role="progressbar"
                                                 style="width:<?= $attPct ?>%;" aria-valuenow="<?= $attPct ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                        <small class="text-muted"><?= $attPct ?>%</small>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
