<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الحضور - <?= e($playground['playground_name_ar']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<form method="post" action="/playgrounds/<?= (int) $playground['id'] ?>/attendance/remind" class="d-inline">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-outline-warning" onclick="return confirm('إرسال تذكيرات؟')">إرسال تذكيرات</button>
</form>
<a href="/playgrounds/<?= (int) $playground['id'] ?>" class="btn btn-outline-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card mb-3">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-end">
            <div class="col-auto">
                <label class="form-label">التاريخ</label>
                <input type="date" name="date" class="form-control" value="<?= e($date) ?>">
            </div>
            <div class="col-auto d-flex align-items-end">
                <button type="submit" class="btn btn-primary">عرض</button>
            </div>
        </form>
    </div>
</div>

<?php if (empty($slots) && empty($attendance)): ?>
<div class="card" style="padding:40px;text-align:center;">
    <p style="color:#6B7280;">لا توجد خانات تدريب لهذا اليوم أو لا يوجد شبكة مرتبطة بالملعب</p>
</div>
<?php else: ?>
<form method="post" action="/playgrounds/<?= (int) $playground['id'] ?>/attendance">
    <?= csrf_field() ?>
    <input type="hidden" name="attendance_date" value="<?= e($date) ?>">

    <?php if (!empty($slots)): ?>
    <div class="card mb-3">
        <div class="card-header">المدربين المجدولين</div>
        <div class="card-body">
            <table class="table table-bordered">
                <thead><tr><th>المدرب</th><th>الساعة</th><th>الحالة</th><th>ملاحظات</th></tr></thead>
                <tbody>
                <?php
                $coachesSeen = [];
                $idx = 0;
                foreach ($slots as $slot):
                    if (empty($slot['coach_id']) || isset($coachesSeen[$slot['coach_id']])) continue;
                    $coachesSeen[$slot['coach_id']] = true;
                ?>
                <tr>
                    <td><?= e($slot['coach_name'] ?? '-') ?></td>
                    <td><?= e($slot['slot_hour']) ?></td>
                    <td>
                        <input type="hidden" name="attendance[<?= $idx ?>][entity_type]" value="coach">
                        <input type="hidden" name="attendance[<?= $idx ?>][entity_id]" value="<?= (int) $slot['coach_id'] ?>">
                        <input type="hidden" name="attendance[<?= $idx ?>][entity_name]" value="<?= e($slot['coach_name'] ?? '') ?>">
                        <select name="attendance[<?= $idx ?>][status]" class="form-select form-select-sm">
                            <option value="present">حاضر</option>
                            <option value="absent">غائب</option>
                            <option value="late">متأخر</option>
                            <option value="excused">بعذر</option>
                        </select>
                    </td>
                    <td><input type="text" name="attendance[<?= $idx ?>][notes]" class="form-control form-control-sm"></td>
                </tr>
                <?php $idx++; endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <?php if (!empty($attendance)): ?>
    <div class="card mb-3">
        <div class="card-header">سجل الحضور المسجل</div>
        <div class="card-body">
            <table class="table table-bordered table-sm">
                <thead><tr><th>الاسم</th><th>النوع</th><th>الحالة</th><th>الوقت</th></tr></thead>
                <tbody>
                <?php foreach ($attendance as $rec): ?>
                <tr>
                    <td><?= e($rec['entity_name_cache'] ?? '-') ?></td>
                    <td><?= $rec['entity_type'] === 'coach' ? 'مدرب' : 'متدرب' ?></td>
                    <td>
                        <?php if ($rec['status'] === 'present'): ?><span class="badge bg-success">حاضر</span>
                        <?php elseif ($rec['status'] === 'absent'): ?><span class="badge bg-danger">غائب</span>
                        <?php elseif ($rec['status'] === 'late'): ?><span class="badge bg-warning text-dark">متأخر</span>
                        <?php else: ?><span class="badge bg-secondary">بعذر</span><?php endif; ?>
                    </td>
                    <td><?= e($rec['check_in_time'] ?? '-') ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <button type="submit" class="btn btn-success btn-lg">حفظ الحضور</button>
</form>
<?php endif; ?>
<?php $__template->endSection(); ?>
