<?php
$logs = isset($logs) ? $logs : array();
$stores = isset($stores) ? $stores : array();
$staff = isset($staff) ? $staff : array();
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1"><i class="fa-solid fa-clock-rotate-left text-primary me-2"></i>CheckIn Task History</h4>
            <p class="text-muted mb-0">Audit logs of completed routines, evidence submissions, and verification records from `CheckIn_Logs`.</p>
        </div>
        <div>
            <a href="<?php echo base_url('admin/photos'); ?>" class="btn btn-outline-primary me-2">
                <i class="fa-solid fa-camera me-1"></i> Photos
            </a>
            <a href="<?php echo base_url('admin/videos'); ?>" class="btn btn-outline-primary">
                <i class="fa-solid fa-video me-1"></i> Videos
            </a>
        </div>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4">Store Branch</th>
                            <th>Staff Member</th>
                            <th>Schedule ID</th>
                            <th>Submission Time</th>
                            <th>Checklist Tasks</th>
                            <th>Status</th>
                            <th class="text-end pe-4">Evidence</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($logs)): ?>
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">
                                    <i class="fa-solid fa-folder-open fs-1 opacity-25 mb-3 d-block"></i>
                                    No check-in logs submitted yet.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($logs as $logId => $log): ?>
                                <?php
                                $sName = isset($stores[$log['storeId']]['name']) ? $stores[$log['storeId']]['name'] : (isset($log['storeId']) ? $log['storeId'] : 'N/A');
                                $staffName = isset($log['staffId']) ? $log['staffId'] : 'Staff';
                                ?>
                                <tr>
                                    <td class="ps-4">
                                        <div class="fw-bold text-dark"><?php echo htmlspecialchars($sName); ?></div>
                                        <div class="small text-muted font-monospace">Store ID: <?php echo htmlspecialchars(isset($log['storeId']) ? $log['storeId'] : ''); ?></div>
                                    </td>
                                    <td>
                                        <div class="fw-semibold text-dark"><i class="fa-solid fa-user me-1 text-muted"></i><?php echo htmlspecialchars($staffName); ?></div>
                                    </td>
                                    <td class="font-monospace small text-muted">
                                        <?php echo htmlspecialchars(isset($log['scheduleId']) ? $log['scheduleId'] : '-'); ?>
                                    </td>
                                    <td class="small text-muted">
                                        <?php echo isset($log['submittedAt']) ? date('M d, Y h:i:s A', $log['submittedAt'] / 1000) : '-'; ?>
                                    </td>
                                    <td>
                                        <?php if (!empty($log['tasks']) && is_array($log['tasks'])): ?>
                                            <div class="small">
                                                <?php foreach ($log['tasks'] as $t): ?>
                                                    <div class="mb-1 text-truncate" style="max-width: 250px;">
                                                        <i class="fa-solid fa-check text-success me-1"></i>
                                                        <?php echo htmlspecialchars(isset($t['taskName']) ? $t['taskName'] : 'Task'); ?>
                                                    </div>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-1">
                                            <?php echo ucfirst(isset($log['status']) ? $log['status'] : 'completed'); ?>
                                        </span>
                                    </td>
                                    <td class="text-end pe-4">
                                        <?php if (!empty($log['tasks']) && is_array($log['tasks'])): ?>
                                            <div class="d-flex justify-content-end gap-1">
                                                <?php foreach ($log['tasks'] as $t): ?>
                                                    <?php if (!empty($t['mediaUrl'])): ?>
                                                        <a href="<?php echo htmlspecialchars($t['mediaUrl']); ?>" target="_blank" class="btn btn-sm btn-outline-primary p-2" title="<?php echo htmlspecialchars(isset($t['taskName']) ? $t['taskName'] : 'Media'); ?>">
                                                            <i class="fa-solid <?php echo (isset($t['mediaType']) && $t['mediaType'] === 'video') ? 'fa-video' : 'fa-image'; ?>"></i>
                                                        </a>
                                                    <?php endif; ?>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
