<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تسجيل الحضور<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4>تسجيل الحضور</h4>
        <div>
            <a href="/facility-grids/<?= $grid_id ?>/attendance/report" class="btn btn-outline-info">تقرير الحضور</a>
            <form method="post" action="/facility-grids/<?= $grid_id ?>/attendance/remind" class="d-inline">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-warning" onclick="return confirm('إرسال تذكيرات الدفع؟')">إرسال تذكيرات</button>
            </form>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-body">
            <form method="get" class="row g-3">
                <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">
                    <label class="form-label">الساعة (اختياري)</label>
                    <input type="time" name="hour" class="form-control" value="<?= e($hour_slot ?? '') ?>">
                </div>
                <div class="col-auto d-flex align-items-end">
                    <button type="submit" class="btn btn-primary">عرض</button>
                </div>
            </form>
        </div>
    </div>

    <form method="post" action="/facility-grids/<?= $grid_id ?>/attendance">
        <?= csrf_field() ?>
        <input type="hidden" name="attendance_date" value="<?= e($date) ?>">
        <input type="hidden" name="hour_slot" value="<?= e($hour_slot ?? '') ?>">

        <?php if (!empty($coaches)): ?>
        <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></tr></thead>
                    <tbody>
                    <?php foreach ($coaches as $i => $coach): ?>
                    <tr>
                        <td><?= e($coach['name_ar']) ?></td>
                        <td>
                            <input type="hidden" name="attendance[<?= $i ?>][entity_type]" value="coach">
                            <input type="hidden" name="attendance[<?= $i ?>][entity_id]" value="<?= $coach['coach_id'] ?>">
                            <input type="hidden" name="attendance[<?= $i ?>][entity_name]" value="<?= e($coach['name_ar']) ?>">
                            <select name="attendance[<?= $i ?>][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[<?= $i ?>][notes]" class="form-control form-control-sm"></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <?php if (!empty($trainees)): ?>
        <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></tr></thead>
                    <tbody>
                    <?php $offset = count($coaches); foreach ($trainees as $j => $trainee): ?>
                    <tr>
                        <td><?= e($trainee['name_ar']) ?></td>
                        <td>
                            <input type="hidden" name="attendance[<?= $offset + $j ?>][entity_type]" value="trainee">
                            <input type="hidden" name="attendance[<?= $offset + $j ?>][entity_id]" value="<?= $trainee['player_id'] ?>">
                            <input type="hidden" name="attendance[<?= $offset + $j ?>][entity_name]" value="<?= e($trainee['name_ar']) ?>">
                            <select name="attendance[<?= $offset + $j ?>][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[<?= $offset + $j ?>][notes]" class="form-control form-control-sm"></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

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