<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إيجار لوكر جديد<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/locker-rentals" 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="/sa/locker-rentals">
    <?= 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="key" 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="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">اللوكر <span style="color:#DC2626;">*</span></label>
                    <select name="locker_id" class="form-select" required data-searchable="true" data-placeholder="ابحث بالكود أو الاسم...">
                        <option value="">-- اختر لوكر متاح --</option>
                        <?php foreach ($lockers as $locker): ?>
                        <option value="<?= (int) $locker['id'] ?>" <?= old('locker_id') == $locker['id'] ? 'selected' : '' ?>>
                            <?= e($locker['code']) ?> - <?= e($locker['name_ar']) ?> (<?= e(\App\Modules\SportsActivity\Models\Locker::getTypeOptions()[$locker['locker_type'] ?? ''] ?? '') ?>)
                        </option>
                        <?php endforeach; ?>
                    </select>
                    <?php if (empty($lockers)): ?>
                    <small style="color:#D97706;font-size:11px;margin-top:4px;display:block;">لا توجد لوكرات متاحة حاليا.</small>
                    <?php endif; ?>
                </div>
                <div class="form-group">
                    <label class="form-label">اللاعب <span style="color:#DC2626;">*</span></label>
                    <select name="player_id" class="form-select" required id="playerSelect"
                            data-searchable="true"
                            data-search-url="/api/sa/players/search"
                            data-search-min="2"
                            data-placeholder="ابحث بالاسم أو الرقم القومي...">
                        <option value="">ابحث بالاسم أو الرقم القومي...</option>
                        <?php if (old('player_id')): ?>
                        <?php foreach ($players as $player): ?>
                        <?php if ((string) $player['id'] === old('player_id')): ?>
                        <option value="<?= (int) $player['id'] ?>" selected>
                            <?= e($player['full_name_ar']) ?> (<?= e($player['registration_serial'] ?? '') ?>)
                        </option>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">نوع الإيجار <span style="color:#DC2626;">*</span></label>
                    <select name="rental_type" class="form-select" required id="rentalTypeSelect">
                        <option value="">-- اختر --</option>
                        <?php foreach ($rentalTypeOptions as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= old('rental_type') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">تاريخ البداية <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="start_date" id="startDate" value="<?= e(old('start_date') ?: date('Y-m-d')) ?>" class="form-input" required style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">تاريخ النهاية (محسوب)</label>
                    <input type="text" id="endDateDisplay" class="form-input" readonly style="direction:ltr;text-align:left;background:#F9FAFB;" placeholder="يتم حسابه تلقائيا">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 2fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">المبلغ (ج.م) <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="amount" value="<?= e(old('amount')) ?>" class="form-input" required min="0.01" step="0.01" placeholder="0.00" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">ملاحظات</label>
                    <input type="text" name="notes" value="<?= e(old('notes')) ?>" class="form-input" placeholder="ملاحظات إضافية...">
                </div>
            </div>
        </div>
    </div>

    <!-- Submit -->
    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <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="/sa/locker-rentals" class="btn btn-outline">إلغاء</a>
    </div>
</form>

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

    var rentalType = document.getElementById('rentalTypeSelect');
    var startDate = document.getElementById('startDate');
    var endDisplay = document.getElementById('endDateDisplay');

    function calculateEndDate() {
        var type = rentalType.value;
        var start = startDate.value;
        if (!type || !start) { endDisplay.value = ''; return; }

        var d = new Date(start);
        switch (type) {
            case 'monthly': d.setMonth(d.getMonth() + 1); break;
            case '6_months': d.setMonth(d.getMonth() + 6); break;
            case 'yearly': d.setFullYear(d.getFullYear() + 1); break;
        }
        endDisplay.value = d.toISOString().split('T')[0];
    }

    rentalType.addEventListener('change', calculateEndDate);
    startDate.addEventListener('change', calculateEndDate);
    calculateEndDate();
});
</script>
<?php $__template->endSection(); ?>
