
<?php $__env->startSection('title', 'Sebaran Rusun || KLINIK PKP JAWA 3'); ?>

<?php $__env->startSection('content'); ?>
    <!-- Leaflet CSS -->
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
        integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />

    <style>
        /* Hero Section */
        .hero-section {
            background: linear-gradient(135deg, #667eea 0%, #178ee3 100%);
            min-height: 500px;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            padding: 4rem 2rem;
        }

        [data-theme="dark"] .hero-section {
            background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
        }

        .hero-section::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="%23ffffff" fill-opacity="0.1" d="M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,112C672,96,768,96,864,112C960,128,1056,160,1152,160C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>') no-repeat bottom;
            background-size: cover;
        }

        .hero-content {
            position: relative;
            z-index: 1;
            max-width: 800px;
            text-align: center;
            color: white;
        }

        .hero-title {
            font-size: 3rem;
            font-weight: 700;
            margin-bottom: 1rem;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
            color: #f9fafb;
        }

        .hero-subtitle {
            font-size: 1.25rem;
            margin-bottom: 2rem;
            opacity: 0.95;
            line-height: 1.8;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }

        [data-theme="dark"] .hero-subtitle {
            color: #e2e8f0;
        }

        .hero-divider {
            width: 100px;
            height: 4px;
            background: white;
            margin: 2rem auto;
            border-radius: 2px;
        }

        /* Container Section */
        .sebaran-section {
            padding: 4rem 2rem;
            background-color: #f9fafb;
        }

        [data-theme="dark"] .sebaran-section {
            background-color: #1c1c1c;
        }

        /* Map Container */
        #map {
            width: 100%;
            height: 600px;
            border-radius: 16px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 2rem;
        }

        .map-wrapper {
            background: white;
            padding: 1.5rem;
            border-radius: 16px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 2rem;
        }

        [data-theme="dark"] .map-wrapper {
            background: #0f172a;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
        }

        .map-title {
            font-size: 1.5rem;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 1rem;
        }

        [data-theme="dark"] .map-title {
            color: #f1f5f9;
        }

        /* Custom Leaflet Popup */
        .custom-popup .leaflet-popup-content-wrapper {
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }

        .custom-popup .leaflet-popup-content {
            margin: 1rem;
            font-family: inherit;
        }

        .popup-title {
            font-size: 1.125rem;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 0.5rem;
        }

        .popup-info {
            font-size: 0.875rem;
            color: #6b7280;
            margin-bottom: 0.25rem;
        }

        .popup-info strong {
            color: #374151;
        }

        .section-title {
            text-align: center;
            font-size: 2.5rem;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 1rem;
        }

        [data-theme="dark"] .section-title {
            color: #f1f5f9;
        }

        .section-subtitle {
            text-align: center;
            font-size: 1.125rem;
            color: #6b7280;
            margin-bottom: 3rem;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }

        [data-theme="dark"] .section-subtitle {
            color: #cbd5e0;
        }

        /* Card Grid */
        .sebaran-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 2rem;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        /* sebaran Card */
        .sebaran-card {
            background: white;
            border-radius: 16px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        [data-theme="dark"] .sebaran-card {
            background: #0f172a;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
        }

        .sebaran-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
        }

        [data-theme="dark"] .sebaran-card:hover {
            box-shadow: 0 12px 24px rgba(0, 0, 0, 0.5);
        }

        .sebaran-image {
            width: 100%;
            height: 250px;
            object-fit: cover;
            background-color: #e5e7eb;
        }

        [data-theme="dark"] .sebaran-image {
            background-color: #374151;
        }

        .sebaran-content {
            padding: 1.5rem;
        }

        [data-theme="dark"] .sebaran-content {
            background-color: #0f172a;
        }

        .sebaran-category {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 0.75rem;
            font-weight: 600;
            border-radius: 12px;
            margin-bottom: 0.75rem;
            text-transform: uppercase;
        }

        .sebaran-title {
            font-size: 1.5rem;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 0.75rem;
            line-height: 1.4;
        }

        [data-theme="dark"] .sebaran-title {
            color: #f1f5f9;
        }

        .sebaran-description {
            font-size: 0.9375rem;
            color: #6b7280;
            line-height: 1.6;
            margin-bottom: 1.5rem;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        [data-theme="dark"] .sebaran-description {
            color: #94a3b8;
        }

        .sebaran-actions {
            display: flex;
            gap: 0.75rem;
        }

        .btn-download,
        .btn-view {
            flex: 1;
            padding: 0.75rem 1rem;
            border-radius: 8px;
            font-weight: 600;
            font-size: 0.875rem;
            text-align: center;
            text-decoration: none;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }

        .btn-download {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
        }

        .btn-download:hover {
            background: linear-gradient(135deg, #5568d3 0%, #6941a0 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }

        .btn-view {
            background: white;
            color: #667eea;
            border: 2px solid #667eea;
        }

        [data-theme="dark"] .btn-view {
            background: #1e293b;
            color: #818cf8;
            border-color: #818cf8;
        }

        .btn-view:hover {
            background: #667eea;
            color: white;
        }

        [data-theme="dark"] .btn-view:hover {
            background: #818cf8;
            color: white;
        }

        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 4rem 2rem;
        }

        .empty-state-icon {
            font-size: 4rem;
            color: #d1d5db;
            margin-bottom: 1rem;
        }

        [data-theme="dark"] .empty-state-icon {
            color: #475569;
        }

        .empty-state-text {
            font-size: 1.125rem;
            color: #6b7280;
        }

        [data-theme="dark"] .empty-state-text {
            color: #94a3b8;
        }

        /* Loading */
        .loading-container {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 4rem;
        }

        .spinner {
            width: 50px;
            height: 50px;
            border: 4px solid #e5e7eb;
            border-top-color: #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        [data-theme="dark"] .spinner {
            border-color: #374151;
            border-top-color: #818cf8;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        /* Responsive */
        @media (max-width: 768px) {
            .hero-title {
                font-size: 2rem;
            }

            .hero-subtitle {
                font-size: 1rem;
            }

            .section-title {
                font-size: 2rem;
            }

            .sebaran-grid {
                grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
                gap: 1.5rem;
            }
        }
    </style>

    <!-- Hero Section -->
    <section class="hero-section">
        <div class="hero-content">
            <h1 class="hero-title">Sebaran Rusun</h1>
            <div class="hero-divider"></div>
            <p class="hero-subtitle">
                Berisi Informasi tentang sebaran rusun di provinsi <?php echo e(env('KABKOTA')); ?>.
            </p>
        </div>
    </section>

    <!-- sebaran Cards Section -->
    <section class="sebaran-section">
        <div class="container">
            <h2 class="section-title">Peta Sebaran Rusun</h2>
            <p class="section-subtitle">
                Jelajahi lokasi rusun-rusun di <?php echo e(env('KABKOTA')); ?> melalui peta interaktif di bawah ini
            </p>

            <!-- Map Container -->
            <div class="map-wrapper">
                <div id="map"></div>
            </div>
        </div>
    </section>

    <!-- Leaflet JS -->
    <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
        integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

    <script>
        // === 1. Setup dasar peta ===
        const map = L.map('map').setView([-7.0051, 110.4381], 9); // pusat Jawa Tengah
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
            maxZoom: 19
        }).addTo(map);

        const rusunIcon = L.icon({
            iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-blue.png',
            shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
            iconSize: [25, 41],
            iconAnchor: [12, 41],
            popupAnchor: [1, -34],
            shadowSize: [41, 41]
        });

        // === 2. Variabel dari Laravel ===
        const BASE_URL = '<?php echo e(env('BASE_URL')); ?>';
        const DAERAH_RAW = '<?php echo e(env('DAERAH', '')); ?>'; // contoh: "kota-yogyakarta" / "sleman" / "semarang"
        const DAERAH = DAERAH_RAW.toLowerCase().trim(); // normalize ke lowercase

        // Deteksi apakah harus pakai endpoint DIY atau Jateng
        const diyRegions = ['kota-yogyakarta', 'sleman', 'bantul', 'gunungkidul', 'kulon-progo'];
        const endpoint = diyRegions.includes(DAERAH) ? 'diy' : 'jateng';
        const API_ENDPOINT = `${BASE_URL}/api/public/sites/flats/${endpoint}`;

        console.log('DAERAH (env):', DAERAH_RAW);
        console.log('DAERAH (normalized):', DAERAH);
        console.log('Endpoint dipakai:', API_ENDPOINT);

        // Penampung marker buat fitBounds
        const markers = [];

        // === 3. Fungsi filter berdasarkan kolom region dari API ===
        function matchesRegion(rusun, daerahEnvLower) {
            // Jika DAERAH kosong di env, tampilkan semua
            if (!daerahEnvLower || daerahEnvLower === '') {
                return true;
            }

            // Ambil region dari data API (sudah ada kolom region)
            const rusunRegion = (rusun.region || '').toLowerCase().trim();

            console.log('Checking rusun:', rusun.name, '| region:', rusunRegion, '| target:', daerahEnvLower);

            // Bandingkan region dari API dengan DAERAH dari env
            return rusunRegion === daerahEnvLower;
        }

        // === 4. Ambil data, filter by kabupaten/kota, lalu render marker ===
        $.ajax({
            url: API_ENDPOINT,
            method: 'GET',
            dataType: 'json',
            success: function(response) {
                console.log('Data rusun berhasil dimuat:', response);

                if (!Array.isArray(response) || response.length === 0) {
                    tampilkanEmptyState('Tidak ada data rusun yang ditemukan dari API.');
                    return;
                }

                // FILTER BERDASARKAN KOLOM REGION
                const filteredData = response.filter(item => matchesRegion(item, DAERAH));

                console.log('Setelah filter region "' + DAERAH_RAW + '":', filteredData);
                console.log('Jumlah rusun ditemukan:', filteredData.length);

                if (filteredData.length === 0) {
                    tampilkanEmptyState(
                        'Tidak ada rusun yang cocok untuk region "' + DAERAH_RAW + '".' +
                        '<br>Pastikan nilai DAERAH di .env sesuai dengan kolom region di database.' +
                        '<br>Contoh: kota-yogyakarta, sleman, bantul, semarang, dll.'
                    );
                    return;
                }

                // loop data terfilter, taruh marker satu-satu
                filteredData.forEach(rusun => {
                    // Validasi koordinat
                    if (!rusun.latitude || !rusun.longitude) return;

                    const lat = parseFloat(rusun.latitude);
                    const lng = parseFloat(rusun.longitude);
                    if (Number.isNaN(lat) || Number.isNaN(lng)) return;

                    // bikin marker
                    const marker = L.marker([lat, lng], {
                        icon: rusunIcon
                    }).addTo(map);

                    // isi popup
                    const popupContent = `
                    <div class="custom-popup">
                        <div class="popup-title">${rusun.name ?? '-'}</div>
                        <div class="popup-info"><strong>Alamat:</strong> ${rusun.address ?? '-'}</div>
                        <div class="popup-info"><strong>Jumlah Unit:</strong> ${rusun.unit ?? '-'}</div>
                        <div class="popup-info"><strong>Jumlah Tower:</strong> ${rusun.tower ?? '-'}</div>
                        <div class="popup-info"><strong>Jumlah Lantai:</strong> ${rusun.floor ?? '-'}</div>
                        <div class="popup-info"><strong>Tipe:</strong> ${rusun.type ?? '-'}</div>
                        <div class="popup-info"><strong>Tahun Pembangunan:</strong> ${rusun.development_year ?? '-'}</div>
                        <div class="popup-info"><strong>Tahun Serah Terima:</strong> ${rusun.handover_year ?? '-'}</div>
                        <div class="popup-info"><strong>Unit Terisi:</strong> ${rusun.occupied ?? '-'} / ${rusun.unit ?? '-'}</div>
                        <div class="popup-info"><strong>Kontraktor:</strong> ${rusun.contractor ?? '-'}</div>
                    </div>
                `;

                    marker.bindPopup(popupContent);

                    // tooltip hover
                    marker.bindTooltip(rusun.name || 'Rusun', {
                        permanent: false,
                        direction: 'top',
                        offset: [0, -40]
                    });

                    markers.push(marker);
                });

                // zoom ke semua marker yang lolos filter
                if (markers.length > 0) {
                    const group = L.featureGroup(markers);
                    map.fitBounds(group.getBounds().pad(0.1));
                } else {
                    tampilkanEmptyState(
                        'Data ada, tapi tidak ada koordinat valid untuk wilayah "' + DAERAH_RAW + '".'
                    );
                }
            },
            error: function(xhr, status, error) {
                console.error('Error fetching rusun data:', error);
                console.error('Status:', status);
                console.error('Response:', xhr.responseText);

                tampilkanEmptyState(
                    '<span style="color:red;">Gagal memuat data rusun. Coba lagi nanti.</span>'
                );
            }
        });

        // === 5. Helper buat nampilin fallback marker kalau kosong/gagal ===
        function tampilkanEmptyState(messageHtml) {
            const fallbackMarker = L.marker([-7.0051, 110.4381]).addTo(map);
            fallbackMarker.bindPopup(
                `<div style="text-align:center;font-size:0.9rem;line-height:1.4;">${messageHtml}</div>`
            ).openPopup();
        }
    </script>


    <?php if (isset($component)) { $__componentOriginal86ed18220e5516026aef66b347e6e7af = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal86ed18220e5516026aef66b347e6e7af = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.footer_one','data' => []] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('footer_one'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal86ed18220e5516026aef66b347e6e7af)): ?>
<?php $attributes = $__attributesOriginal86ed18220e5516026aef66b347e6e7af; ?>
<?php unset($__attributesOriginal86ed18220e5516026aef66b347e6e7af); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal86ed18220e5516026aef66b347e6e7af)): ?>
<?php $component = $__componentOriginal86ed18220e5516026aef66b347e6e7af; ?>
<?php unset($__componentOriginal86ed18220e5516026aef66b347e6e7af); ?>
<?php endif; ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.layout1', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\laravel\krs-jawa3-template\krs-jawa3-web-template\resources\views/home/sebaranrusun.blade.php ENDPATH**/ ?>