@extends('layouts.layout1')
@section('title', 'Desain Prototipe || 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: var(--judges-base);
            --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: var(--judges-base);
            --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(--border-hover);
        }

        [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(--border-hover);
        }

        .prototype-image {
            width: 100%;
            height: 250px;
            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 rgba(102, 126, 234, 0.4);
        }

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

    <!-- Hero Section -->
    <section class="hero-section">
        <div class="hero-content">
            <h1 class="hero-title">Desain Prototipe</h1>
            <div class="hero-divider"></div>
            <p class="hero-subtitle">
                Desain Prototipe merupakan rancangan desain rumah yang dapat digunakan sebagai
                referensi dalam membangun atau merenovasi rumah impian Anda. Temukan berbagai
                pilihan desain yang sesuai dengan kebutuhan dan preferensi Anda.
            </p>
        </div>
    </section>

    <!-- Prototype Cards Section -->
    <section class="prototype-section">
        <div class="container">
            <h2 class="section-title">Koleksi Desain Prototipe</h2>
            <p class="section-subtitle">
                Jelajahi berbagai desain prototipe terbaik kami untuk inspirasi rumah impian Anda
            </p>

            <!-- Loading State -->
            <div id="loading-container" class="loading-container">
                <div class="spinner"></div>
            </div>

            <!-- Cards Grid -->
            <div id="prototype-grid" class="prototype-grid" style="display: none;">
                <!-- Cards will be inserted here by JavaScript -->
            </div>

            <!-- Empty State -->
            <div id="empty-state" class="empty-state" style="display: none;">
                <div class="empty-state-icon">📋</div>
                <p class="empty-state-text">Belum ada desain prototipe yang tersedia saat ini.</p>
            </div>

            <!-- Jawa-III Prototype Designs Section -->
            <div class="container" style="margin-top: 3rem;">
                <h2 class="section-title">Koleksi Desain Prototipe dari Klinik PKP Jawa-III</h2>
                <p class="section-subtitle">Desain prototipe yang diwariskan dari Klinik PKP Jawa-III</p>

                <!-- Loading State Jawa-III -->
                <div id="loading-container-jawa3" class="loading-container">
                    <div class="spinner"></div>
                </div>

                <!-- Cards Grid Jawa-III -->
                <div id="prototype-grid-jawa3" class="prototype-grid" style="display: none;"></div>

                <!-- Empty State Jawa-III -->
                <div id="empty-state-jawa3" class="empty-state" style="display: none;">
                    <div class="empty-state-icon">📋</div>
                    <p class="empty-state-text">Belum ada desain prototipe dari KRS Jawa-III.</p>
                </div>
            </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() {
            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 on page load
        syncDarkMode();

        // Watch for theme changes
        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() {
            const BASE_URL = "{{ env('BASE_URL') }}";
            const DAERAH = "{{ env('DAERAH') }}";

            console.log('Fetching data from:', `${BASE_URL}/api/public/sites/${DAERAH}`);

            // Fetch prototype designs data
            $.ajax({
                url: `${BASE_URL}/api/public/sites/${DAERAH}`,
                method: 'GET',
                success: function(response) {
                    console.log('API Response:', response);

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

                    // Get prototype designs from response
                    const prototypeDesigns = response.local?.prototypeDesigns || [];

                    console.log('Prototype Designs:', prototypeDesigns);

                    if (prototypeDesigns.length > 0) {
                        // Show grid
                        $('#prototype-grid').show();

                        // Render cards
                        renderPrototypeCards(prototypeDesigns);
                    } else {
                        // Show empty state
                        $('#empty-state').show();
                    }
                },
                error: function(xhr, status, error) {
                    console.error('Error fetching data:', error);
                    console.error('Status:', status);
                    console.error('Response:', xhr.responseText);

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

                    // Show empty state
                    $('#empty-state').show();
                    $('#empty-state .empty-state-text').text(
                        'Terjadi kesalahan saat memuat data. Silakan coba lagi nanti.');
                }
            });

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

                designs.forEach(design => {
                    const imageUrl = design.path_image ?
                        `${BASE_URL}/${design.path_image}` :
                        'https://placehold.co/400x300?text=No+Image';

                    const fileUrl = design.path_file ?
                        `${BASE_URL}/${design.path_file}` :
                        '#';

                    const rabUrl = design.path_rab ?
                        `${BASE_URL}/${design.path_rab}` :
                        null;

                    const categoryName = design.category?.name || 'Design Prototype';
                    const title = design.title || 'Untitled';
                    const description = design.description || 'Tidak ada deskripsi tersedia.';

                    const card = `
                        <div class="prototype-card">
                            <img src="${imageUrl}" 
                                 alt="${title}" 
                                 class="prototype-image"
                                 onerror="this.src='https://placehold.co/400x300?text=No+Image'">
                            <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
                                                            </a>
                                                        ` : ''}
                                    ${rabUrl ? `
                                                            <a href="${rabUrl}" 
                                                               target="_blank" 
                                                               class="btn-view">
                                                                <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                                                                    <path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0z"/>
                                                                    <path d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8zm8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z"/>
                                                                </svg>
                                                                Lihat RAB
                                                            </a>
                                                        ` : `
                                                            <a href="${imageUrl}" 
                                                               target="_blank" 
                                                               class="btn-view">
                                                                <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                                                                    <path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0z"/>
                                                                    <path d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8zm8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z"/>
                                                                </svg>
                                                                Lihat Detail
                                                            </a>
                                                        `}
                                </div>
                            </div>
                        </div>
                    `;

                    grid.append(card);
                });

                // Ensure dark mode is applied to new content
                syncDarkMode();
            }

            // Fetch Jawa-III prototype designs
            $.ajax({
                url: `${BASE_URL}/api/public/sites/${DAERAH}`,
                method: 'GET',
                success: function(response) {
                    console.log('API Response (Jawa-III):', response);
                    $('#loading-container-jawa3').hide();
                    // Get inherited prototype designs from response
                    const jawaPrototypes = response.inherited?.prototypeDesigns || [];
                    console.log('Inherited Prototypes from Jawa-III:', jawaPrototypes);
                    if (jawaPrototypes.length > 0) {
                        $('#prototype-grid-jawa3').show();
                        renderPrototypeCardsJawa(jawaPrototypes);
                    } else {
                        $('#empty-state-jawa3').show();
                    }
                },
                error: function(xhr, status, error) {
                    console.error('Error fetching Jawa-III prototypes:', error);
                    $('#loading-container-jawa3').hide();
                    $('#empty-state-jawa3').show();
                    $('#empty-state-jawa3 .empty-state-text').text(
                        'Terjadi kesalahan saat memuat data KRS Jawa-III. Silakan coba lagi nanti.');
                }
            });

            function renderPrototypeCardsJawa(designs) {
                const grid = $('#prototype-grid-jawa3');
                grid.empty();

                designs.forEach(design => {
                    const imageUrl = design.path_image ?
                        `${BASE_URL}/${design.path_image}` :
                        'https://placehold.co/400x300?text=No+Image';

                    const fileUrl = design.path_file ?
                        `${BASE_URL}/${design.path_file}` :
                        '#';

                    const rabUrl = design.path_rab ?
                        `${BASE_URL}/${design.path_rab}` :
                        null;

                    const categoryName = design.category?.name || 'Design Prototype';
                    const title = design.title || 'Untitled';
                    const description = design.description || 'Tidak ada deskripsi tersedia.';

                    const card = `
                        <div class="prototype-card">
                            <img src="${imageUrl}" 
                                 alt="${title}" 
                                 class="prototype-image"
                                 onerror="this.src='https://placehold.co/400x300?text=No+Image'">
                            <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
                                                            </a>
                                                        ` : ''}
                                    ${rabUrl ? `
                                                            <a href="${rabUrl}" 
                                                               target="_blank" 
                                                               class="btn-view">
                                                                <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                                                                    <path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0z"/>
                                                                    <path d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8zm8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z"/>
                                                                </svg>
                                                                Lihat RAB
                                                            </a>
                                                        ` : `
                                                            <a href="${imageUrl}" 
                                                               target="_blank" 
                                                               class="btn-view">
                                                                <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                                                                    <path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0z"/>
                                                                    <path d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8zm8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z"/>
                                                                </svg>
                                                                Lihat Detail
                                                            </a>
                                                        `}
                                </div>
                            </div>
                        </div>
                    `;

                    grid.append(card);
                });

                // Ensure dark mode is applied to new content
                syncDarkMode();
            }
        });
    </script>
    <x-footer_one />
@endsection
