<?php
use App\Modules\Facilities\Models\Facility;
use App\Modules\Disciplines\Models\SportDiscipline;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>الملاعب والمرافق<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/facilities/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة مرفق</a>
<?php $__template->endSection(); ?>

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

<?php
$currentType = $filters['facility_type'] ?? '';
$currentLocation = $filters['location'] ?? '';
$allTypes = Facility::getTypes();
$allLocations = Facility::getLocations();

// Map facility types to Lucide icons
$typeIcons = [
    'pitch'  => 'layout-grid',
    'court'  => 'square',
    'hall'   => 'warehouse',
    'lane'   => 'move-horizontal',
    'table'  => 'table-2',
    'device' => 'dumbbell',
    'track'  => 'route',
    'pool'   => 'waves',
];
?>

<!-- Type Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/facilities<?= $currentLocation !== '' ? '?location=' . urlencode($currentLocation) : '' ?><?= ($filters['q'] ?? '') !== '' ? ($currentLocation !== '' ? '&' : '?') . 'q=' . urlencode($filters['q']) : '' ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentType === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($allTypes as $typeKey => $typeLabel): ?>
        <a href="/facilities?facility_type=<?= e($typeKey) ?><?= $currentLocation !== '' ? '&location=' . urlencode($currentLocation) : '' ?><?= ($filters['q'] ?? '') !== '' ? '&q=' . urlencode($filters['q']) : '' ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentType === $typeKey ? '#0D7377' : 'transparent' ?>;color:<?= $currentType === $typeKey ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            <?= e($typeLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search Bar with Location Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/facilities" 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($filters['q'] ?? '') ?>" placeholder="ابحث بالاسم أو الكود..." class="form-input" style="min-width:200px;">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الموقع</label>
            <select name="location" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($allLocations as $locKey => $locLabel): ?>
                <option value="<?= e($locKey) ?>" <?= $currentLocation === $locKey ? 'selected' : '' ?>><?= e($locLabel) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if ($currentType !== ''): ?>
        <input type="hidden" name="facility_type" value="<?= e($currentType) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/facilities" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Facilities Grid -->
<?php if (!empty($facilities)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:20px;margin-bottom:20px;">
    <?php foreach ($facilities as $f):
        $fType     = $f['facility_type'] ?? 'pitch';
        $fLocation = $f['location'] ?? 'indoor';
        $typeColor = Facility::getTypeColor($fType);
        $typeLabel = Facility::getTypeLabel($fType);
        $locLabel  = Facility::getLocationLabel($fLocation);
        $isActive  = (int) ($f['is_active'] ?? 0);
        $icon      = $typeIcons[$fType] ?? 'building';
        $capacity  = (int) ($f['capacity'] ?? 0);
        $rateAM    = number_format((float) ($f['hourly_rate_member'] ?? 0), 0);
        $ratePM    = number_format((float) ($f['hourly_rate_member_pm'] ?? 0), 0);

        // Get linked discipline name
        $disciplineName = '';
        if (!empty($f['linked_discipline_id']) && !empty($disciplines)) {
            foreach ($disciplines as $disc) {
                if ((int) $disc['id'] === (int) $f['linked_discipline_id']) {
                    $disciplineName = $disc['name_ar'] ?? '';
                    break;
                }
            }
        }
    ?>
    <div class="card" style="transition:box-shadow 0.2s;position:relative;overflow:hidden;<?= !$isActive ? 'opacity:0.65;' : '' ?>">
        <?php if (!$isActive): ?>
        <div style="position:absolute;top:12px;left:12px;z-index:2;">
            <span class="badge" style="background:#FEE2E2;color:#DC2626;font-size:11px;padding:3px 10px;border-radius:10px;">معطّل</span>
        </div>
        <?php endif; ?>

        <a href="/facilities/<?= (int) $f['id'] ?>" style="text-decoration:none;color:inherit;display:block;">
            <!-- Card Header with Icon -->
            <div style="padding:20px 20px 15px;display:flex;align-items:start;gap:15px;">
                <div style="width:52px;height:52px;border-radius:12px;background:linear-gradient(135deg, <?= $typeColor ?>15, <?= $typeColor ?>30);display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                    <i data-lucide="<?= e($icon) ?>" style="width:26px;height:26px;color:<?= $typeColor ?>;"></i>
                </div>
                <div style="flex:1;min-width:0;">
                    <h3 style="margin:0 0 4px;font-size:16px;font-weight:700;color:#1A1A2E;"><?= e($f['name_ar']) ?></h3>
                    <?php if (!empty($f['name_en'])): ?>
                    <div style="font-size:12px;color:#9CA3AF;margin-bottom:6px;"><?= e($f['name_en']) ?></div>
                    <?php endif; ?>
                    <div style="display:flex;gap:6px;flex-wrap:wrap;">
                        <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $typeColor ?>15;color:<?= $typeColor ?>;"><?= e($typeLabel) ?></span>
                        <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $fLocation === 'indoor' ? '#DBEAFE' : '#FEF3C7' ?>;color:<?= $fLocation === 'indoor' ? '#1D4ED8' : '#92400E' ?>;">
                            <i data-lucide="<?= $fLocation === 'indoor' ? 'home' : 'sun' ?>" style="width:11px;height:11px;vertical-align:middle;margin-left:2px;"></i>
                            <?= e($locLabel) ?>
                        </span>
                    </div>
                </div>
            </div>

            <!-- Card Body Stats -->
            <div style="padding:0 20px 15px;display:flex;gap:15px;font-size:12px;color:#6B7280;flex-wrap:wrap;">
                <?php if ($capacity > 0): ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="users" style="width:14px;height:14px;"></i>
                    <?= $capacity ?> سعة
                </span>
                <?php endif; ?>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="sun" style="width:14px;height:14px;"></i>
                    صباحي: <?= $rateAM ?> ج.م
                </span>
                <span style="display:flex;align-items:center;gap:4px;">
                    <i data-lucide="moon" style="width:14px;height:14px;"></i>
                    مسائي: <?= $ratePM ?> ج.م
                </span>
            </div>

            <?php if ($disciplineName !== ''): ?>
            <div style="padding:0 20px 12px;">
                <span style="display:inline-flex;align-items:center;gap:4px;font-size:11px;color:#0D7377;background:#F0FDFA;padding:3px 10px;border-radius:8px;">
                    <i data-lucide="activity" style="width:12px;height:12px;"></i>
                    <?= e($disciplineName) ?>
                </span>
            </div>
            <?php endif; ?>
        </a>

        <!-- Card Footer -->
        <div style="padding:10px 20px;border-top:1px solid #F3F4F6;display:flex;justify-content:space-between;align-items:center;">
            <code style="font-size:11px;color:#9CA3AF;background:#F9FAFB;padding:2px 6px;border-radius:4px;"><?= e($f['code'] ?? '') ?></code>
            <div style="display:flex;gap:6px;">
                <a href="/facilities/<?= (int) $f['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                    <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                </a>
                <a href="/facilities/<?= (int) $f['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                    <i data-lucide="edit-3" style="width:13px;height:13px;vertical-align:middle;"></i> تعديل
                </a>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="building" 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 (!empty($filters['q']) || !empty($filters['facility_type']) || !empty($filters['location'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة مرفق جديد للنادي.
        <?php endif; ?>
    </p>
    <a href="/facilities/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة مرفق</a>
</div>
<?php endif; ?>

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