<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>حجز جديد<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/reservations" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<form method="POST" action="/reservations">
    <?= csrf_field() ?>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="calendar-plus" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات الحجز</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">

            <!-- Sport Discipline Filter -->
            <div>
                <label class="form-label">اللعبة / النشاط</label>
                <select id="disciplineFilter" class="form-input">
                    <option value="">-- جميع الألعاب --</option>
                    <?php foreach ($disciplines as $d): ?>
                    <option value="<?= (int) $d['id'] ?>"><?= e($d['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
                <small style="color:#9CA3AF;font-size:11px;">فلتر اختياري لتضييق المرافق المعروضة</small>
            </div>

            <!-- Facility -->
            <div>
                <label class="form-label">المرفق <span style="color:#DC2626;">*</span></label>
                <select name="facility_id" id="facilitySelect" class="form-input" required>
                    <option value="">-- اختر المرفق --</option>
                    <?php foreach ($facilities as $f): ?>
                    <option value="<?= (int) $f['id'] ?>" data-discipline="<?= (int) ($f['linked_discipline_id'] ?? 0) ?>" <?= old('facility_id') == $f['id'] ? 'selected' : '' ?>><?= e($f['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Reservation Date -->
            <div>
                <label class="form-label">تاريخ الحجز <span style="color:#DC2626;">*</span></label>
                <input type="date" name="reservation_date" value="<?= e(old('reservation_date')) ?>" class="form-input" required style="direction:ltr;text-align:left;">
            </div>

            <!-- Start Time -->
            <div>
                <label class="form-label">وقت البداية <span style="color:#DC2626;">*</span></label>
                <input type="time" name="start_time" value="<?= e(old('start_time')) ?>" class="form-input" required style="direction:ltr;text-align:left;">
            </div>

            <!-- End Time -->
            <div>
                <label class="form-label">وقت النهاية <span style="color:#DC2626;">*</span></label>
                <input type="time" name="end_time" value="<?= e(old('end_time')) ?>" class="form-input" required style="direction:ltr;text-align:left;">
            </div>

            <!-- Booker Type -->
            <div>
                <label class="form-label">نوع الحاجز <span style="color:#DC2626;">*</span></label>
                <select name="booker_type" class="form-input" required>
                    <option value="">-- اختر النوع --</option>
                    <?php foreach ($bookerTypes as $code => $label): ?>
                    <option value="<?= e($code) ?>" <?= old('booker_type') === $code ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Booker Name -->
            <div>
                <label class="form-label">اسم الحاجز <span style="color:#DC2626;">*</span></label>
                <input type="text" name="booker_name" value="<?= e(old('booker_name')) ?>" class="form-input" required placeholder="الاسم بالكامل">
            </div>

            <!-- Booker Phone -->
            <div>
                <label class="form-label">هاتف الحاجز</label>
                <input type="text" name="booker_phone" value="<?= e(old('booker_phone')) ?>" class="form-input" placeholder="رقم الهاتف" style="direction:ltr;text-align:left;">
            </div>

            <!-- Player ID -->
            <div>
                <label class="form-label">رقم اللاعب</label>
                <input type="number" name="player_id" value="<?= e(old('player_id')) ?>" class="form-input" placeholder="اختياري" style="direction:ltr;text-align:left;">
            </div>

            <!-- Member ID -->
            <div>
                <label class="form-label">رقم العضوية</label>
                <input type="number" name="member_id" value="<?= e(old('member_id')) ?>" class="form-input" placeholder="اختياري" style="direction:ltr;text-align:left;">
            </div>

            <!-- Notes -->
            <div style="grid-column:1/-1;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-input" rows="3" placeholder="ملاحظات إضافية..."><?= e(old('notes')) ?></textarea>
            </div>
        </div>
    </div>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary">
            <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إنشاء الحجز
        </button>
        <a href="/reservations" class="btn btn-outline">إلغاء</a>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();

    var disciplineFilter = document.getElementById('disciplineFilter');
    var facilitySelect = document.getElementById('facilitySelect');
    var allOptions = Array.from(facilitySelect.querySelectorAll('option[value]'));

    disciplineFilter.addEventListener('change', function() {
        var discId = this.value;
        facilitySelect.innerHTML = '<option value="">-- اختر المرفق --</option>';

        allOptions.forEach(function(opt) {
            if (!discId || opt.getAttribute('data-discipline') === discId || opt.getAttribute('data-discipline') === '0') {
                facilitySelect.appendChild(opt.cloneNode(true));
            }
        });
    });
});
</script>
<?php $__template->endSection(); ?>
