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

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.locker_rental.create')): ?>
<a href="/sa/locker-rentals/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إيجار جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<!-- Stats Cards -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#059669;"><?= (int) ($stats['active'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">فعال</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#D97706;"><?= (int) ($stats['grace_period'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">فترة سماح</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#DC2626;"><?= (int) ($stats['pending_eviction'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">بانتظار الإخلاء</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#6B7280;"><?= (int) ($stats['available'] ?? 0) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">متاح</div>
    </div>
</div>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/locker-rentals" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($search ?? '') ?>" placeholder="رقم الإيجار، اسم اللاعب، كود اللوكر..." class="form-input">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($statusOptions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($status ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <?php if (($search ?? '') !== '' || ($status ?? '') !== ''): ?>
        <a href="/sa/locker-rentals" class="btn btn-outline" style="color:#6B7280;">مسح</a>
        <?php endif; ?>
    </form>
</div>

<!-- Results -->
<?php if (!empty($rentals)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم الإيجار</th>
                    <th>اللوكر</th>
                    <th>اللاعب</th>
                    <th>النوع</th>
                    <th>من</th>
                    <th>إلى</th>
                    <th>المبلغ</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rentals as $rental): ?>
                <?php
                $rst = $rental['status'] ?? '';
                $rstColors = ['active' => '#059669', 'grace_period' => '#D97706', 'pending_eviction' => '#DC2626', 'evicted' => '#6B7280', 'completed' => '#2563EB', 'cancelled' => '#6B7280'];
                $rstBg = ['active' => '#D1FAE5', 'grace_period' => '#FEF3C7', 'pending_eviction' => '#FEE2E2', 'evicted' => '#F3F4F6', 'completed' => '#DBEAFE', 'cancelled' => '#F3F4F6'];
                ?>
                <tr>
                    <td>
                        <a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>" style="color:#0D7377;text-decoration:none;font-weight:600;">
                            <?= e($rental['rental_number'] ?? '-') ?>
                        </a>
                    </td>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($rental['locker_code'] ?? '-') ?></code></td>
                    <td><?= e($rental['player_name'] ?? '-') ?></td>
                    <td><?= e($rentalTypeOptions[$rental['rental_type'] ?? ''] ?? '-') ?></td>
                    <td style="font-size:12px;"><?= e($rental['start_date'] ?? '-') ?></td>
                    <td style="font-size:12px;"><?= e($rental['end_date'] ?? '-') ?></td>
                    <td><?= number_format((float) ($rental['amount'] ?? 0), 2) ?> ج.م</td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $rstBg[$rst] ?? '#F3F4F6' ?>;color:<?= $rstColors[$rst] ?? '#6B7280' ?>;"><?= e($statusOptions[$rst] ?? '-') ?></span>
                    </td>
                    <td>
                        <div style="display:flex;gap:4px;">
                            <a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>" class="btn btn-sm btn-outline" title="عرض">
                                <i data-lucide="eye" style="width:14px;height:14px;"></i>
                            </a>
                            <?php if (in_array($rst, ['active', 'grace_period']) && can('sa.locker_rental.create')): ?>
                            <a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>/renew" class="btn btn-sm btn-outline" title="تجديد" style="color:#059669;border-color:#059669;">
                                <i data-lucide="refresh-cw" style="width:14px;height:14px;"></i>
                            </a>
                            <?php endif; ?>
                            <?php if (in_array($rst, ['grace_period', 'pending_eviction']) && can('sa.locker_rental.evict')): ?>
                            <a href="/sa/locker-rentals/<?= (int) $rental['id'] ?>/evict" class="btn btn-sm btn-outline" title="محضر فض" style="color:#DC2626;border-color:#DC2626;">
                                <i data-lucide="user-x" style="width:14px;height:14px;"></i>
                            </a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <?php if (($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php
        $queryParams = '';
        if (($search ?? '') !== '') $queryParams .= 'q=' . urlencode($search) . '&';
        if (($status ?? '') !== '') $queryParams .= 'status=' . urlencode($status) . '&';
        ?>
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination, 'baseUrl' => '/sa/locker-rentals?' . $queryParams]); ?>
    </div>
    <?php endif; ?>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="key" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد إيجارات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (($search ?? '') !== '' || ($status ?? '') !== ''): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإنشاء إيجار جديد.
        <?php endif; ?>
    </p>
    <?php if (can('sa.locker_rental.create')): ?>
    <a href="/sa/locker-rentals/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إيجار جديد</a>
    <?php endif; ?>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }
});
</script>
<?php $__template->endSection(); ?>
