<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($schedule['plan_name_ar']) ?> - <?= e($playground['playground_name_ar']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules/<?= (int) $schedule['id'] ?>/edit" class="btn btn-warning">تعديل</a>
<a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules" class="btn btn-outline-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$startTime = $schedule['day_start_time'] ?? '07:00:00';
$endTime = $schedule['day_end_time'] ?? '22:00:00';
$startHour = (int) substr($startTime, 0, 2);
$endHour = (int) substr($endTime, 0, 2);
?>

<div class="card mb-3">
    <div class="card-body">
        <div class="row align-items-center">
            <div class="col-auto">
                <span class="badge bg-<?= $schedule['status'] === 'active' ? 'success' : 'secondary' ?>"><?= $schedule['status'] === 'active' ? 'نشط' : 'مسودة' ?></span>
            </div>
            <div class="col-auto">الشهر: <?= e($schedule['plan_month']) ?></div>
            <div class="col">
                <form method="get" class="d-flex gap-2 align-items-center">
                    <label class="form-label mb-0">التاريخ:</label>
                    <input type="date" name="date" class="form-control form-control-sm" value="<?= e($date) ?>" style="width:auto;">
                    <button type="submit" class="btn btn-sm btn-primary">عرض</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">خانات يوم <?= e($date) ?></div>
    <div class="card-body p-0">
        <table class="table table-bordered table-hover mb-0">
            <thead>
                <tr>
                    <th style="width:80px;">الساعة</th>
                    <?php if ($playground['has_rows']): ?>
                    <th>الصف</th>
                    <?php endif; ?>
                    <th>المدرب</th>
                    <th>الأكاديمية</th>
                    <th>الفئة</th>
                    <th>إجراء</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $slotsByHour = [];
            foreach ($slots as $s) {
                $h = substr($s['slot_hour'], 0, 2);
                $slotsByHour[$h][] = $s;
            }
            ?>
            <?php for ($h = $startHour; $h < $endHour; $h++):
                $hourKey = str_pad((string) $h, 2, '0', STR_PAD_LEFT);
                $hourSlots = $slotsByHour[$hourKey] ?? [];
            ?>
                <?php if (empty($hourSlots)): ?>
                <tr>
                    <td style="text-align:center;font-weight:700;"><?= $hourKey ?>:00</td>
                    <?php if ($playground['has_rows']): ?><td>-</td><?php endif; ?>
                    <td colspan="3" style="color:#9CA3AF;">فارغ</td>
                    <td><a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules/<?= (int) $schedule['id'] ?>/slots/<?= e($date) ?>/<?= $hourKey ?>" class="btn btn-sm btn-outline-primary">تعيين</a></td>
                </tr>
                <?php else: ?>
                    <?php foreach ($hourSlots as $i => $slot): ?>
                    <tr>
                        <?php if ($i === 0): ?>
                        <td style="text-align:center;font-weight:700;" rowspan="<?= count($hourSlots) ?>"><?= $hourKey ?>:00</td>
                        <?php endif; ?>
                        <?php if ($playground['has_rows']): ?>
                        <td><?= $slot['row_number'] ? (e($playground['row_label_ar'] ?? 'حارة') . ' ' . $slot['row_number']) : '-' ?></td>
                        <?php endif; ?>
                        <td><?= e($slot['coach_name'] ?? '-') ?></td>
                        <td><?= e($slot['academy_name'] ?? '-') ?></td>
                        <td><?= e($slot['age_group_ar'] ?? '-') ?> <?= $slot['gender'] ? '(' . e($slot['gender']) . ')' : '' ?></td>
                        <?php if ($i === 0): ?>
                        <td rowspan="<?= count($hourSlots) ?>"><a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules/<?= (int) $schedule['id'] ?>/slots/<?= e($date) ?>/<?= $hourKey ?>" class="btn btn-sm btn-outline-warning">تعديل</a></td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            <?php endfor; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
