<?php
/**
 * @var object $attendance
 * @var string $date
 * @var string $employeeId
 * @var array $employees
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-clipboard-check me-2 text-gold"></i>Attendance</h5>
</div>

<?php if (can('attendance_create')): ?>
    <div class="mb-3">
        <form method="POST" action="<?= url('attendance/checkin') ?>" class="d-inline">
            <?= csrf_field() ?>
            <?php if (\core\Auth::employeeId()): ?>
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-sign-in-alt me-1"></i>Check In Now
                </button>
            <?php else: ?>
                <div class="input-group" style="width:auto;display:inline-flex">
                    <select name="employee_id" class="form-select form-select-sm bg-transparent" style="width:200px" required>
                        <option value="">Select Employee</option>
                        <?php foreach ($employees as $emp): ?>
                            <option value="<?= $emp->id ?>"><?= e($emp->first_name . ' ' . $emp->last_name) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-sign-in-alt me-1"></i>Check In
                    </button>
                </div>
            <?php endif; ?>
        </form>
    </div>
<?php endif; ?>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Attendance Records</h6>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-8">
                <form method="GET" action="<?= url('attendance') ?>" 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 text-gold"></i>
                        </span>
                        <input type="date" name="date" class="form-control form-control-sm bg-transparent"
                               value="<?= e($date) ?>" onchange="this.form.submit()">
                    </div>
                    <select name="employee_id" class="form-select form-select-sm bg-transparent" style="width:auto;" onchange="this.form.submit()">
                        <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>
                    <a href="<?= url('attendance/report') ?>" class="btn btn-sm btn-outline-info">
                        <i class="fas fa-chart-bar me-1"></i>Report
                    </a>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Employee</th>
                        <th>Job Title</th>
                        <th>Date</th>
                        <th>Check In</th>
                        <th>Check Out</th>
                        <th>Total Hours</th>
                        <th>Status</th>
                        <th width="100" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($attendance->data)): ?>
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">
                                <i class="fas fa-calendar-times fa-2x mb-2"></i>
                                <p class="mb-0">No attendance records found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($attendance->currentPage - 1) * $attendance->perPage + 1; ?>
                        <?php foreach ($attendance->data as $record): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('employees/show/' . $record->employee_id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($record->first_name . ' ' . $record->last_name) ?></strong>
                                    </a>
                                </td>
                                <td><?= e($record->job_title ?? '-') ?></td>
                                <td><?= formatDate($record->date) ?></td>
                                <td><?= !empty($record->check_in) ? date(TIME_FORMAT, strtotime($record->check_in)) : '-' ?></td>
                                <td><?= !empty($record->check_out) ? date(TIME_FORMAT, strtotime($record->check_out)) : '-' ?></td>
                                <td><?= $record->total_hours ? number_format($record->total_hours, 2) . ' hrs' : '-' ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($record->status) ?>">
                                        <?= e(ucfirst($record->status)) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <?php if (empty($record->check_out) && can('attendance_edit')): ?>
                                        <form method="POST" action="<?= url('attendance/checkout/' . $record->id) ?>" class="d-inline"
                                              onsubmit="return confirm('Check out this employee?')">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-warning">
                                                <i class="fas fa-sign-out-alt"></i> Check Out
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <span class="text-muted small">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($attendance->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($attendance->currentPage - 1) * $attendance->perPage) + 1 ?>
                    to <?= min($attendance->currentPage * $attendance->perPage, $attendance->total) ?>
                    of <?= $attendance->total ?> entries
                </div>
                <?= paginateLinks($attendance, url('attendance')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
