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

<?php $__template->section('page_actions'); ?>
<a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>" 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'); ?>

<!-- Current Rental Info -->
<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="info" style="width:18px;height:18px;color:#2563EB;"></i>
        <h3 style="margin:0;color:#2563EB;font-size:15px;">الإيجار الحالي</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:15px;">
            <div>
                <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">اللوكر</div>
                <div style="font-weight:600;"><?= e($rental['locker_code'] ?? '') ?> - <?= e($rental['locker_name'] ?? '') ?></div>
            </div>
            <div>
                <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">اللاعب</div>
                <div style="font-weight:600;"><?= e($rental['player_name'] ?? '') ?></div>
            </div>
            <div>
                <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">ينتهي في</div>
                <div style="font-weight:600;"><?= e($rental['end_date'] ?? '-') ?></div>
            </div>
            <div>
                <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">المبلغ السابق</div>
                <div style="font-weight:600;"><?= number_format((float) ($rental['amount'] ?? 0), 2) ?> ج.م</div>
            </div>
        </div>
    </div>
</div>

<!-- Renewal Form -->
<form method="POST" action="/sa/locker-rentals/<?= (int) $rental['id'] ?>/renew">
    <?= 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="refresh-cw" style="width:18px;height:18px;color:#059669;"></i>
            <h3 style="margin:0;color:#059669;font-size:15px;">بيانات التجديد</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">نوع الإيجار <span style="color:#DC2626;">*</span></label>
                    <select name="rental_type" class="form-select" required id="rentalTypeSelect">
                        <?php foreach ($rentalTypeOptions as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= (old('rental_type') ?: $rental['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') ?: $rental['end_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;">
                </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') ?: $rental['amount'] ?? '') ?>" class="form-input" required min="0.01" step="0.01" 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" style="background:#059669;border-color:#059669;"><i data-lucide="refresh-cw" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تأكيد التجديد</button>
        <a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>" 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(); ?>
