<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعيين خانة <?= e($hour) ?>:00 - <?= e($playground['playground_name_ar']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $hasRows = (bool) $playground['has_rows']; $rowCount = (int) ($playground['row_count'] ?? 1); ?>
<div class="container-fluid" style="max-width:900px;">
    <div class="card mb-3">
        <div class="card-body">
            <strong>التاريخ:</strong> <?= e($date) ?> | <strong>الساعة:</strong> <?= e($hour) ?>:00
        </div>
    </div>

    <form method="post" action="/playgrounds/<?= (int) $playground['id'] ?>/schedules/<?= $schedule_id ?>/slots/<?= e($date) ?>/<?= e($hour) ?>">
        <?= csrf_field() ?>

        <div id="slotContainer">
        <?php
        $existing = [];
        foreach ($slots as $s) {
            $existing[$s['row_number'] ?? 0] = $s;
        }
        $count = $hasRows ? $rowCount : max(1, count($slots));
        for ($r = 0; $r < $count; $r++):
            $rowNum = $hasRows ? ($r + 1) : null;
            $slot = $existing[$rowNum] ?? ($existing[0] ?? null);
            if (!$hasRows && $r > 0) $slot = $slots[$r] ?? null;
        ?>
        <div class="card mb-3 slot-card">
            <div class="card-header">
                <?php if ($hasRows): ?>
                    <?= e($playground['row_label_ar'] ?? 'حارة') ?> <?= $r + 1 ?>
                    <input type="hidden" name="slots[<?= $r ?>][row_number]" value="<?= $r + 1 ?>">
                <?php else: ?>
                    خانة <?= $r + 1 ?>
                <?php endif; ?>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">المدرب</label>
                        <select name="slots[<?= $r ?>][coach_id]" class="form-select">
                            <option value="">-- بدون --</option>
                            <?php foreach ($coaches as $c): ?>
                            <option value="<?= (int) $c['id'] ?>" <?= ($slot['coach_id'] ?? '') == $c['id'] ? 'selected' : '' ?>><?= e($c['name_ar']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">الأكاديمية</label>
                        <select name="slots[<?= $r ?>][academy_id]" class="form-select">
                            <option value="">-- بدون --</option>
                            <?php foreach ($academies as $a): ?>
                            <option value="<?= (int) $a['id'] ?>" <?= ($slot['academy_id'] ?? '') == $a['id'] ? 'selected' : '' ?>><?= e($a['name_ar']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">نوع المجموعة</label>
                        <input type="text" name="slots[<?= $r ?>][group_type]" class="form-control" value="<?= e($slot['group_type'] ?? '') ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">أقصى عدد</label>
                        <input type="number" name="slots[<?= $r ?>][max_trainees]" class="form-control" value="<?= (int) ($slot['max_trainees'] ?? 0) ?>" min="0">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">الفئة العمرية</label>
                        <input type="text" name="slots[<?= $r ?>][age_group_ar]" class="form-control" value="<?= e($slot['age_group_ar'] ?? '') ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">الجنس</label>
                        <select name="slots[<?= $r ?>][gender]" class="form-select">
                            <option value="">مختلط</option>
                            <option value="male" <?= ($slot['gender'] ?? '') === 'male' ? 'selected' : '' ?>>ذكور</option>
                            <option value="female" <?= ($slot['gender'] ?? '') === 'female' ? 'selected' : '' ?>>إناث</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">ملاحظات</label>
                        <input type="text" name="slots[<?= $r ?>][notes]" class="form-control" value="<?= e($slot['notes'] ?? '') ?>">
                    </div>
                </div>
            </div>
        </div>
        <?php endfor; ?>
        </div>

        <button type="submit" class="btn btn-success btn-lg">حفظ الخانة</button>
        <a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules/<?= $schedule_id ?>?date=<?= e($date) ?>" class="btn btn-secondary btn-lg">إلغاء</a>
    </form>
</div>
<?php $__template->endSection(); ?>
