@extends('layouts.layout1')
@section('title', 'Rumah Liliput UGM || KLINIK PKP JAWA 3')

@section('content')
    <style>
        /* CSS Custom Properties for better dark mode support */
        :root {
            --bg-primary: #f9fafb;
            --bg-secondary: #ffffff;
            --bg-card: #ffffff;
            --bg-card-hover: #f8fafc;
            --text-primary: #1f2937;
            --text-secondary: #6b7280;
            --text-muted: #d1d5db;
            --border-color: #e5e7eb;
            --border-hover: #667eea;
            --shadow-soft: 0 4px 6px rgba(0, 0, 0, 0.1);
            --shadow-medium: 0 12px 24px rgba(0, 0, 0, 0.15);
        }

        [data-theme="dark"], 
        body.dark-mode {
            --bg-primary: #1f2937;
            --bg-secondary: #111827;
            --bg-card: #374151;
            --bg-card-hover: #4b5563;
            --text-primary: #f9fafb;
            --text-secondary: #d1d5db;
            --text-muted: #6b7280;
            --border-color: #4b5563;
            --border-hover: #667eea;
            --shadow-soft: 0 4px 6px rgba(102, 126, 234, 0.1);
            --shadow-medium: 0 12px 24px rgba(102, 126, 234, 0.2);
        }

        /* Hero Section */
        .hero-section {
            background: linear-gradient(135deg, var(--judges-base) 0%,  var(--judges-base) 100%);
            min-height: 500px;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            overflow: hidden;
            padding: 4rem 2rem;
        }

        .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;
        }

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

        /* Container Section */
        .prototype-section {
            padding: 4rem 2rem;
            background-color: var(--bg-primary);
            transition: background-color 0.3s ease;
        }

        [data-theme="dark"] .prototype-section,
        body.dark-mode .prototype-section {
            background-color: var(--bg-primary);
        }

        .section-title {
            text-align: center;
            font-size: 2.5rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 1rem;
            transition: color 0.3s ease;
        }

        .section-subtitle {
            text-align: center;
            font-size: 1.125rem;
            color: var(--text-secondary);
            margin-bottom: 3rem;
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
            transition: color 0.3s ease;
        }

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

        /* Prototype Card */
        .prototype-card {
            background: var(--bg-card);
            border-radius: 16px;
            overflow: hidden;
            box-shadow: var(--shadow-soft);
            transition: all 0.3s ease;
            cursor: pointer;
            border: 1px solid var(--border-color);
        }

        .prototype-card:hover {
            transform: translateY(-8px);
            box-shadow: var(--shadow-medium);
            border-color: var(--judges-base);
        }

        [data-theme="dark"] .prototype-card,
        body.dark-mode .prototype-card {
            background: var(--bg-card);
            border-color: var(--border-color);
        }

        [data-theme="dark"] .prototype-card:hover,
        body.dark-mode .prototype-card:hover {
            box-shadow: var(--shadow-medium);
            border-color: var(--judges-base);
        }

        .prototype-image {
            width: 100%;
            height: 500px;
            object-fit: cover;
            background-color: var(--border-color);
            transition: background-color 0.3s ease;
        }

        .prototype-content {
            padding: 1.5rem;
        }

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

        .prototype-title {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 0.75rem;
            line-height: 1.4;
            transition: color 0.3s ease;
        }

        .prototype-description {
            font-size: 0.9375rem;
            color: var(--text-secondary);
            line-height: 1.6;
            margin-bottom: 1.5rem;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
            transition: color 0.3s ease;
        }

        .prototype-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, var(--judges-base) 0%,  var(--judges-base) 100%);
            color: white;
            border: none;
        }

        .btn-download:hover {
            background: linear-gradient(135deg, var(--judges-base) 0%,  var(--judges-base) 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px var(--judges-base);
            color: white;
        }

        .btn-view {
            background: var(--bg-card);
            color: var(--judges-base);
            border: 2px solid var(--judges-base);
            transition: all 0.3s ease;
        }

        .btn-view:hover {
            background: var(--judges-base);
            color: white;
        }

        [data-theme="dark"] .btn-view,
        body.dark-mode .btn-view {
            background: var(--bg-card);
            color: var(--judges-base);
            border-color: var(--judges-base);
        }

        [data-theme="dark"] .btn-view:hover,
        body.dark-mode .btn-view:hover {
            background: var(--judges-base);
            color: white;
        }

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

        .empty-state-icon {
            font-size: 4rem;
            color: var(--text-muted);
            margin-bottom: 1rem;
            transition: color 0.3s ease;
        }

        .empty-state-text {
            font-size: 1.125rem;
            color: var(--text-secondary);
            transition: color 0.3s ease;
        }

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

        .spinner {
            width: 50px;
            height: 50px;
            border: 4px solid var(--border-color);
            border-top-color: var(--judges-base);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            transition: border-color 0.3s ease;
        }

        [data-theme="dark"] .spinner,
        body.dark-mode .spinner {
            border-color: var(--border-color);
            border-top-color: var(--judges-base);
        }

        @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;
            }

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

    <section class="hero-section">
        <div class="hero-content">
            <h1 class="hero-title">Rumah Liliput UGM</h1>
            <div class="hero-divider"></div>
            <p class="hero-subtitle">
                <ul justify="left" style="list-style-type: disc; padding-left: 20px; text-align: left;">
                    <strong>Perancang :</strong>
                    <li>Prof. Dr. Ing. Ir. Eugenius Pradipto</li>
                    <li>Wisnu Agung Hardiansyah, S.Ars. M.Arch.</li>
                    <li>Mahasiswa Kelas Pengembangan Desain dan Konstruksi Arsitektur Daerah (PDKAD) genap semester 4 2025 (nama tercantum pada poster)</li>
                </ul>
                <br>
            </p>
            <!-- tombol whatsapp -->
            <a href="https://wa.me/628816132890" class="btn btn-view" target="_blank" rel="noopener noreferrer">Hubungi Kami via WhatsApp</a>
        </div>
    </section>

    <section class="prototype-section">
        <div class="container">
            <div style="text-align: center; margin-bottom: 40px;">
                <h2 class="section-title">Koleksi Rumah Liliput UGM</h2>
                <p class="section-subtitle">
                    Jelajahi berbagai Rumah Liliput UGM terbaik kami untuk inspirasi rumah impian Anda
                </p>
            </div>

            <div id="loading-container" class="loading-container">
                <div class="spinner"></div>
                <p style="margin-top: 10px; color: #666;">Memuat desain...</p>
            </div>

            <div id="prototype-grid" class="prototype-grid" style="display: none;">
                </div>

            <div id="empty-state" class="empty-state" style="display: none;">
                <div class="empty-state-icon" style="font-size: 48px; margin-bottom: 10px;">📋</div>
                <p class="empty-state-text">Belum ada Rumah Liliput UGM yang tersedia saat ini.</p>
            </div>
        </div>
    </section>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        // Dark mode synchronization function
        function syncDarkMode() {
            // Cek attribute pada html tag (biasanya data-theme)
            const isDarkMode = document.documentElement.getAttribute('data-theme') === 'dark';
            if (isDarkMode) {
                document.body.classList.add('dark-mode');
            } else {
                document.body.classList.remove('dark-mode');
            }
        }

        // Initialize dark mode
        syncDarkMode();

        // Observer untuk perubahan theme
        const observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
                    syncDarkMode();
                }
            });
        });

        observer.observe(document.documentElement, {
            attributes: true,
            attributeFilter: ['data-theme']
        });

        $(document).ready(function() {
            // Hapus trailing slash jika ada pada BASE_URL untuk konsistensi
            let rawBaseUrl = "{{ env('BASE_URL') }}";
            const BASE_URL = rawBaseUrl.replace(/\/$/, ""); 
            
            // Fetch prototype designs data
            $.ajax({
                url: `${BASE_URL}/api/public/liliput-designs`,
                method: 'GET',
                success: function(response) {
                    console.log('API Response:', response);

                    // Hide loading
                    $('#loading-container').hide();

                    let liliputDesigns = [];

                    // PERBAIKAN: Handle struktur response (Array langsung vs Object Wrapper)
                    if (Array.isArray(response)) {
                        liliputDesigns = response;
                    } else if (response.local && Array.isArray(response.local.liliputDesigns)) {
                        liliputDesigns = response.local.liliputDesigns;
                    } else if (response.data && Array.isArray(response.data)) {
                        liliputDesigns = response.data;
                    }

                    console.log('Processed Designs:', liliputDesigns);

                    if (liliputDesigns.length > 0) {
                        $('#prototype-grid').show();
                        renderPrototypeCards(liliputDesigns, BASE_URL);
                    } else {
                        $('#empty-state').show();
                    }
                },
                error: function(xhr, status, error) {
                    console.error('Error fetching data:', error);
                    $('#loading-container').hide();
                    $('#empty-state').show();
                    $('#empty-state .empty-state-text').text('Gagal memuat data dari server.');
                }
            });

            function renderPrototypeCards(designs, baseUrl) {
                const grid = $('#prototype-grid');
                grid.empty();

                designs.forEach(design => {
                    // PERBAIKAN: Ganti backslash (\) dengan forward slash (/) agar URL valid di browser
                    // Backend mengirim: "uploads\images\file.png" -> Diubah jadi: "uploads/images/file.png"
                    
                    let imagePath = design.path_image ? design.path_image.replace(/\\/g, '/') : null;
                    const imageUrl = imagePath ? `${baseUrl}/${imagePath}` : 'https://placehold.co/400x600?text=No+Image';

                    let filePath = design.path_file ? design.path_file.replace(/\\/g, '/') : null;
                    const fileUrl = filePath ? `${baseUrl}/${filePath}` : '#';

                    // Safety check untuk null values
                    const categoryName = (design.category && design.category.name) ? design.category.name : 'Liliput';
                    const title = design.title || 'Tanpa Judul';
                    const description = design.description || 'Tidak ada deskripsi.';

                    const card = `
                        <div class="prototype-card">
                            <img src="${imageUrl}" 
                                 alt="${title}" 
                                 class="prototype-image"
                                 onerror="this.src='https://placehold.co/400x600?text=Image+Error'">
                            <div class="prototype-content">
                                <span class="prototype-category">${categoryName}</span>
                                <h3 class="prototype-title">${title}</h3>
                                <p class="prototype-description">${description}</p>
                                <div class="prototype-actions">
                                    ${fileUrl !== '#' ? `
                                        <a href="${fileUrl}" target="_blank" class="btn-download" download>
                                            <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                                                <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
                                                <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
                                            </svg>
                                            Download PDF
                                        </a>
                                    ` : '<button class="btn-download" style="background:#ccc; cursor:not-allowed;">File Tidak Tersedia</button>'}
                                </div>
                            </div>
                        </div>
                    `;

                    grid.append(card);
                });
            }
        });
    </script>
    <x-footer_one />
@endsection
