<?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-clipboard-list text-primary me-2"></i>Checklist Templates</h4>
            <p class="text-muted mb-0">Predefined routine templates for opening, hygiene, inventory, and shift handovers.</p>
        </div>
        <a href="<?php echo base_url('admin/reminder_schedules'); ?>" class="btn btn-primary">
            <i class="fa-solid fa-plus me-1"></i> Create New Template
        </a>
    </div>

    <div class="row g-4">
        <?php if (empty($schedules)): ?>
            <div class="col-12 text-center py-5 text-muted card border-0 shadow-sm rounded-4">
                No templates configured yet.
            </div>
        <?php else: ?>
            <?php foreach ($schedules as $id => $sc): ?>
                <div class="col-md-6 col-xl-4">
                    <div class="card border-0 shadow-sm rounded-4 h-100">
                        <div class="card-body p-4">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <h5 class="fw-bold text-dark mb-0"><?php echo htmlspecialchars(isset($sc['title']) ? $sc['title'] : 'Routine Template'); ?></h5>
                                <span class="badge bg-primary bg-opacity-10 text-primary rounded-pill"><?php echo isset($sc['tasks']) ? count($sc['tasks']) : 0; ?> Tasks</span>
                            </div>
                            <div class="small text-muted mb-3"><i class="fa-regular fa-clock me-1"></i>Default Schedule: <?php echo isset($sc['scheduledAt']) ? date('h:i A', $sc['scheduledAt'] / 1000) : 'Daily'; ?></div>
                            
                            <h6 class="small fw-bold text-muted text-uppercase mb-2">Checklist Tasks</h6>
                            <ul class="list-unstyled mb-3 small">
                                <?php if (!empty($sc['tasks'])): ?>
                                    <?php foreach (array_slice($sc['tasks'], 0, 4) as $t): ?>
                                        <li class="py-1 text-truncate"><i class="fa-solid fa-check text-success me-2"></i><?php echo htmlspecialchars(isset($t['taskName']) ? $t['taskName'] : 'Task'); ?></li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>
