<?php $__env->startSection('title', Auth::user()->isCustomer() ? 'Cari Lapangan' : 'Kelola Lapangan'); ?>

<?php $__env->startSection('content'); ?>
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5>
            <i class="fas fa-map me-2"></i>
            <?php if(Auth::user()->isCustomer()): ?>
                Cari Lapangan
            <?php else: ?>
                Kelola Lapangan
            <?php endif; ?>
        </h5>
        <?php if(Auth::user()->isAdmin() || Auth::user()->isSuperAdmin()): ?>
            <div>
                <a href="<?php echo e(route('lapangans.create')); ?>" class="btn btn-success">
                    <i class="fas fa-plus me-2"></i>Tambah Lapangan
                </a>
            </div>
        <?php endif; ?>
    </div>
    <div class="card-body">
        <!-- Filter -->
        <form method="GET" action="<?php echo e(route('lapangans.index')); ?>" class="mb-3">
            <div class="row">
                <?php if(Auth::user()->isAdmin() || Auth::user()->isSuperAdmin()): ?>
                    <div class="col-md-3">
                        <select class="form-select" name="daerah">
                            <option value="">Semua Daerah</option>
                            <?php $__currentLoopData = $daerahList ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($d); ?>" <?php echo e(request('daerah') == $d ? 'selected' : ''); ?>><?php echo e($d); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" name="status">
                            <option value="">Semua Status</option>
                            <option value="aktif" <?php echo e(request('status') === 'aktif' ? 'selected' : ''); ?>>Aktif</option>
                            <option value="tidak_aktif" <?php echo e(request('status') === 'tidak_aktif' ? 'selected' : ''); ?>>Tidak Aktif</option>
                        </select>
                    </div>
                <?php endif; ?>
                <div class="col-md-4">
                    <input type="text" class="form-control" name="search" placeholder="Cari lapangan..." value="<?php echo e(request('search')); ?>">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-filter me-2"></i>Filter
                    </button>
                </div>
            </div>
        </form>

        <?php if(Auth::user()->isAdmin() || Auth::user()->isSuperAdmin()): ?>
            <!-- Stats -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card bg-primary text-white">
                        <div class="card-body">
                            <h5><i class="fas fa-map me-2"></i>Total</h5>
                            <h3><?php echo e($totalLapangan ?? 0); ?></h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-success text-white">
                        <div class="card-body">
                            <h5><i class="fas fa-check-circle me-2"></i>Aktif</h5>
                            <h3><?php echo e($activeLapangan ?? 0); ?></h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-danger text-white">
                        <div class="card-body">
                            <h5><i class="fas fa-times-circle me-2"></i>Tidak Aktif</h5>
                            <h3><?php echo e($inactiveLapangan ?? 0); ?></h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-info text-white">
                        <div class="card-body">
                            <h5><i class="fas fa-chart-line me-2"></i>Rata-rata</h5>
                            <h3><?php echo e($avgPrice ? formatRupiah($avgPrice) : '-'); ?></h3>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <!-- Lapangan Grid -->
        <div class="row">
            <?php $__empty_1 = true; $__currentLoopData = $lapangans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lapangan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <div class="col-md-4 mb-3">
                    <div class="card">
                        <?php if($lapangan->foto): ?>
                            <img src="<?php echo e(asset('storage/' . $lapangan->foto)); ?>" class="card-img-top" alt="<?php echo e($lapangan->nama); ?>" style="height: 200px; object-fit: cover;">
                        <?php endif; ?>
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <h6 class="mb-0"><?php echo e($lapangan->nama); ?></h6>
                                <span class="badge bg-<?php echo e($lapangan->status === 'aktif' ? 'success' : 'danger'); ?>">
                                    <?php echo e($lapangan->status === 'aktif' ? 'Aktif' : 'Tidak Aktif'); ?>

                                </span>
                            </div>
                            <p class="text-muted mb-2">
                                <i class="fas fa-map-marker-alt me-1"></i> <?php echo e($lapangan->lokasi); ?>

                            </p>
                            <div class="mb-2">
                                <small class="text-muted">Fasilitas:</small><br>
                                <?php if($lapangan->fasilitas): ?>
                                    <?php
                                        $fasilitasArray = explode(',', $lapangan->fasilitas);
                                    ?>
                                    <?php $__currentLoopData = array_slice($fasilitasArray, 0, 3); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $fasilitas): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <span class="badge bg-info me-1"><?php echo e(trim($fasilitas)); ?></span>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php if(count($fasilitasArray) > 3): ?>
                                        <span class="badge bg-secondary">+<?php echo e(count($fasilitasArray) - 3); ?></span>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <small class="text-muted">
                                        <i class="fas fa-users me-1"></i><?php echo e($lapangan->kapasitas); ?> orang<br>
                                        <i class="fas fa-money-bill-wave me-1"></i><?php echo e(formatRupiah($lapangan->harga_per_jam)); ?>/jam
                                    </small>
                                </div>
                                <div class="btn-group btn-group-sm">
                                    <button class="btn btn-info" onclick="showDetail(<?php echo e($lapangan->id); ?>)">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    <?php if(Auth::user()->isAdmin() || Auth::user()->isSuperAdmin()): ?>
                                        <a href="<?php echo e(route('lapangans.edit', $lapangan->id)); ?>" class="btn btn-warning">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <form action="<?php echo e(route('lapangans.toggleStatus', $lapangan->id)); ?>" method="POST" style="display:inline-block">
                                            <?php echo csrf_field(); ?>
                                            <button type="submit" class="btn btn-<?php echo e($lapangan->status === 'aktif' ? 'secondary' : 'success'); ?>" onclick="return confirm('Ubah status lapangan ini?')">
                                                <i class="fas fa-<?php echo e($lapangan->status === 'aktif' ? 'ban' : 'check'); ?>"></i>
                                            </button>
                                        </form>
                                        <form action="<?php echo e(route('lapangans.destroy', $lapangan->id)); ?>" method="POST" style="display:inline-block" onsubmit="return confirm('Hapus lapangan ini?')">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('DELETE'); ?>
                                            <button type="submit" class="btn btn-danger">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    <?php elseif(Auth::user()->isCustomer() && $lapangan->status === 'aktif'): ?>
                                        <a href="<?php echo e(route('bookings.create')); ?>?lapangan_id=<?php echo e($lapangan->id); ?>" class="btn btn-primary">
                                            <i class="fas fa-calendar-plus"></i>
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <div class="col-12">
                    <div class="text-center py-5">
                        <i class="fas fa-map fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">Tidak ada lapangan</h5>
                    </div>
                </div>
            <?php endif; ?>
        </div>

        <?php if($lapangans->hasPages()): ?>
            <div class="d-flex justify-content-center mt-3">
                <?php echo e($lapangans->links()); ?>

            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Detail Modal -->
<div class="modal fade" id="detailModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Detail Lapangan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" id="detail-content">
                <!-- Content will be loaded dynamically -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
function showDetail(id) {
    fetch(`/lapangans/${id}`)
        .then(response => response.json())
        .then(data => {
            const lapangan = data.lapangan;
            const fotoUrl = lapangan.foto ? `/storage/${lapangan.foto}` : '/storage/lapangan/default.jpg';
            const fasilitas = lapangan.fasilitas ? lapangan.fasilitas.split(',').map(f => '<span class="badge bg-info me-1">' + f.trim() + '</span>').join('') : '-';
            
            const content = `
                <div class="text-center mb-3">
                    <img src="${fotoUrl}" class="img-fluid rounded" alt="${lapangan.nama}" style="max-height: 200px;" onerror="this.src='/storage/lapangan/default.jpg'">
                </div>
                <table class="table table-sm">
                    <tr><td>Nama</td><td>${lapangan.nama}</td></tr>
                    <tr><td>Lokasi</td><td>${lapangan.lokasi}</td></tr>
                    <tr><td>Daerah</td><td><span class="badge bg-primary">${lapangan.daerah}</span></td></tr>
                    <tr><td>Kapasitas</td><td>${lapangan.kapasitas} orang</td></tr>
                    <tr><td>Harga per Jam</td><td>${formatRupiah(lapangan.harga_per_jam)}</td></tr>
                    <tr><td>Fasilitas</td><td>${fasilitas}</td></tr>
                    <tr><td>Status</td><td>${lapangan.status === 'aktif' ? '<span class="badge bg-success">Aktif</span>' : '<span class="badge bg-danger">Tidak Aktif</span>'}</td></tr>
                </table>
            `;
            
            document.getElementById('detail-content').innerHTML = content;
            new bootstrap.Modal(document.getElementById('detailModal')).show();
        })
        .catch(error => {
            console.error('Error:', error);
            Swal.fire('Error', 'Gagal memuat data lapangan', 'error');
        });
}
</script>
<?php $__env->stopSection(); ?>


<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\User\Documents\Projek Kelas 12\Manfutsal\resources\views/lapangans/index.blade.php ENDPATH**/ ?>