<?php
$schedules = isset($schedules) ? $schedules : 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-list-check text-primary me-2"></i>Checklist Items Library</h4>
            <p class="text-muted mb-0">Master catalog of standardized checklist tasks and photo/video verification items.</p>
        </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">Task Item Name</th>
                            <th>Parent Schedule / Routine</th>
                            <th>Evidence Verification Type</th>
                            <th>Mandatory</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $hasItems = false;
                        foreach ($schedules as $sc) {
                            if (!empty($sc['tasks'])) {
                                foreach ($sc['tasks'] as $t) {
                                    $hasItems = true;
                                    ?>
                                    <tr>
                                        <td class="ps-4">
                                            <div class="fw-bold text-dark"><i class="fa-solid fa-check-circle text-success me-2"></i><?php echo htmlspecialchars(isset($t['taskName']) ? $t['taskName'] : 'Task'); ?></div>
                                        </td>
                                        <td class="text-muted small">
                                            <?php echo htmlspecialchars(isset($sc['title']) ? $sc['title'] : 'Routine'); ?>
                                        </td>
                                        <td>
                                            <span class="badge bg-secondary bg-opacity-10 text-secondary border">
                                                <i class="fa-solid <?php echo (isset($t['mediaType']) && $t['mediaType'] === 'video') ? 'fa-video' : 'fa-camera'; ?> me-1"></i>
                                                <?php echo ucfirst(isset($t['mediaType']) ? $t['mediaType'] : 'both'); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php if (!empty($t['isRequired'])): ?>
                                                <span class="badge bg-danger bg-opacity-10 text-danger">Required</span>
                                            <?php else: ?>
                                                <span class="badge bg-light text-muted border">Optional</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                        }
                        if (!$hasItems):
                        ?>
                            <tr>
                                <td colspan="4" class="text-center py-5 text-muted">No checklist task items found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
