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

<?php $__template->section('page_actions'); ?>
<a href="/pool/<?= (int) $pool->id ?>/grid?date=<?= e($date) ?>" class="btn btn-outline"><i data-lucide="grid-3x3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> الشبكة</a>
<a href="/pool" 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'); ?>

<?php
$preselectedLane = (int) ($_GET['lane'] ?? 0);
$preselectedStart = $_GET['start'] ?? '';
$preselectedEnd = $_GET['end'] ?? '';
?>

<form method="POST" action="/pool/<?= (int) $pool->id ?>/book" id="bookingForm">
    <?= csrf_field() ?>

    <div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">
        <!-- Left: Lane Selection -->
        <div>
            <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="grid-3x3" style="width:18px;height:18px;color:#0D7377;"></i>
                    <h3 style="margin:0;color:#0D7377;font-size:15px;">اختر الحارات</h3>
                </div>
                <div style="padding:20px;">
                    <div style="margin-bottom:15px;">
                        <label style="display:flex;align-items:center;gap:8px;cursor:pointer;margin-bottom:10px;">
                            <input type="checkbox" id="selectAll" style="width:18px;height:18px;accent-color:#0D7377;">
                            <span style="font-size:13px;font-weight:600;">تحديد الحمام بالكامل</span>
                        </label>
                    </div>
                    <div style="display:flex;gap:8px;flex-wrap:wrap;">
                        <?php foreach ($lanes as $lane): ?>
                        <label style="display:flex;flex-direction:column;align-items:center;gap:4px;padding:12px 16px;border:2px solid #E5E7EB;border-radius:10px;cursor:pointer;transition:all 0.2s;min-width:70px;" class="lane-option">
                            <input type="checkbox" name="lanes[]" value="<?= (int) $lane['lane_number'] ?>"
                                   <?= $preselectedLane === (int) $lane['lane_number'] ? 'checked' : '' ?>
                                   style="width:18px;height:18px;accent-color:#0D7377;" class="lane-checkbox">
                            <span style="font-size:13px;font-weight:600;color:#1A1A2E;"><?= e($lane['label_ar'] ?? ('حارة ' . $lane['lane_number'])) ?></span>
                            <span style="font-size:10px;color:#9CA3AF;"><?= e($lane['width_meters']) ?>م</span>
                        </label>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right: Booking Details -->
        <div>
            <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" style="width:18px;height:18px;color:#2563EB;"></i>
                    <h3 style="margin:0;color:#2563EB;font-size:15px;">تفاصيل الحجز</h3>
                </div>
                <div style="padding:20px;">
                    <div class="form-group" style="margin-bottom:12px;">
                        <label class="form-label">التاريخ <span style="color:#DC2626;">*</span></label>
                        <input type="date" name="booking_date" value="<?= e($date) ?>" class="form-input" required style="direction:ltr;text-align:left;">
                    </div>
                    <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-bottom:12px;">
                        <div class="form-group">
                            <label class="form-label">من <span style="color:#DC2626;">*</span></label>
                            <input type="time" name="start_time" value="<?= e($preselectedStart ?: old('start_time') ?: '') ?>" class="form-input" required style="direction:ltr;text-align:left;">
                        </div>
                        <div class="form-group">
                            <label class="form-label">إلى <span style="color:#DC2626;">*</span></label>
                            <input type="time" name="end_time" value="<?= e($preselectedEnd ?: old('end_time') ?: '') ?>" class="form-input" required style="direction:ltr;text-align:left;">
                        </div>
                    </div>
                    <div class="form-group" style="margin-bottom:12px;">
                        <label class="form-label">نوع الحجز <span style="color:#DC2626;">*</span></label>
                        <select name="booking_type" class="form-select" required>
                            <option value="">-- اختر --</option>
                            <?php foreach ($bookingTypes as $key => $label): ?>
                            <option value="<?= e($key) ?>" <?= old('booking_type') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group" style="margin-bottom:12px;">
                        <label class="form-label">نوع الحاجز</label>
                        <select name="booker_type" class="form-select">
                            <?php foreach ($bookerTypes as $key => $label): ?>
                            <option value="<?= e($key) ?>" <?= old('booker_type') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group" style="margin-bottom:12px;">
                        <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>
                    </div>
                    <div class="form-group" style="margin-bottom:12px;">
                        <label class="form-label">عدد السبّاحين المتوقع</label>
                        <input type="number" name="expected_swimmers" value="<?= e(old('expected_swimmers') ?? '1') ?>" class="form-input" min="1" style="direction:ltr;text-align:left;">
                    </div>
                    <div class="form-group">
                        <label class="form-label">ملاحظات</label>
                        <textarea name="notes" class="form-input" rows="2"><?= e(old('notes') ?? '') ?></textarea>
                    </div>
                </div>
            </div>

            <button type="submit" class="btn btn-primary" style="width:100%;padding:14px;font-size:15px;">
                <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تأكيد الحجز
            </button>
        </div>
    </div>
</form>

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

    var selectAll = document.getElementById('selectAll');
    var checkboxes = document.querySelectorAll('.lane-checkbox');

    selectAll.addEventListener('change', function() {
        checkboxes.forEach(function(cb) { cb.checked = selectAll.checked; updateLaneStyle(cb); });
    });

    checkboxes.forEach(function(cb) {
        cb.addEventListener('change', function() { updateLaneStyle(cb); });
        updateLaneStyle(cb);
    });
});

function updateLaneStyle(cb) {
    var label = cb.closest('.lane-option');
    if (cb.checked) {
        label.style.borderColor = '#0D7377';
        label.style.background = '#F0FDFA';
    } else {
        label.style.borderColor = '#E5E7EB';
        label.style.background = '';
    }
}
</script>

<?php $__template->endSection(); ?>
