@extends('layouts.app')
@section('title', 'Konsultasi Mandiri - Asisten Virtual')
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>

@section('content')
    <div class="container mx-auto px-2 sm:px-4 py-2 sm:py-4">

        <div class="flex justify-center mb-6 md:mb-10">
            <div id="chatbot-wrapper"
                class="w-full max-w-5xl bg-white rounded-xl md:rounded-2xl shadow-md border border-gray-200 overflow-hidden relative flex flex-col">

                <div
                    class="action-bar bg-slate-50 border-b border-gray-200 px-4 md:px-5 py-3 md:py-4 flex justify-between items-center z-10">
                    <div class="flex flex-col pr-2">
                        <h1 class="text-base md:text-xl font-bold text-gray-800 leading-tight">Ruang Edukasi & Informasi
                        </h1>
                        <p class="text-[10px] md:text-sm text-gray-500 mt-0.5 md:mt-1 leading-snug">Tanyakan seputar program
                            KB, kesehatan reproduksi, atau pencegahan stunting secara langsung.</p>
                    </div>

                    <div class="flex gap-1.5 md:gap-2 shrink-0">
                        <button id="btn-print"
                            class="flex items-center gap-1 md:gap-2 text-xs md:text-sm font-medium text-slate-600 hover:text-blue-600 bg-white px-2.5 md:px-3 py-1.5 md:py-2 border border-slate-300 rounded-lg shadow-sm transition">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round" class="w-4 h-4 md:w-5 md:h-5">
                                <polyline points="6 9 6 2 18 2 18 9"></polyline>
                                <path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"></path>
                                <rect x="6" y="14" width="12" height="8"></rect>
                            </svg>
                            <span class="hidden md:inline">Cetak</span>
                        </button>
                        <button id="btn-clear"
                            class="flex items-center gap-1 md:gap-2 text-xs md:text-sm font-medium text-slate-600 hover:text-red-600 bg-white px-2.5 md:px-3 py-1.5 md:py-2 border border-slate-300 rounded-lg shadow-sm transition">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round" class="w-4 h-4 md:w-5 md:h-5">
                                <polyline points="3 6 5 6 21 6"></polyline>
                                <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2">
                                </path>
                            </svg>
                            <span class="hidden md:inline">Bersihkan</span>
                        </button>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <style>
        #chatbot-wrapper {
            height: calc(100vh - 140px);
            min-height: 450px;
            max-height: 800px;
            display: flex;
            flex-direction: column;
            border: 1px solid #e2e8f0;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            background-color: #ffffff;
            z-index: 1;
            position: relative;
        }

        @media (max-width: 640px) {
            #chatbot-wrapper {
                height: calc(100vh - 110px);
                border-radius: 0.75rem;
            }
        }

        .chatbot-toggler-wrapper,
        .chatbot-header {
            display: none !important;
        }

        .chatbot-container {
            position: relative !important;
            top: auto !important;
            bottom: auto !important;
            left: auto !important;
            right: auto !important;
            transform: none !important;
            width: 100% !important;
            height: 100% !important;
            opacity: 1 !important;
            pointer-events: auto !important;
            box-shadow: none !important;
            border-radius: 0 !important;
            margin: 0 !important;
            display: flex !important;
            flex-direction: column !important;
            overflow: hidden !important;
            flex-grow: 1 !important;
            background: transparent !important;
            z-index: 1 !important;
        }

        .chatbox {
            flex-grow: 1 !important;
            height: 0 !important;
            overflow-y: auto !important;
            border-radius: 0 !important;
            padding: 15px 12px 15px !important;
            background-color: #f8fafc !important;
        }

        @media (min-width: 768px) {
            .chatbox {
                padding: 30px 20px 20px !important;
            }
        }

        .chat-input {
            position: relative !important;
            width: 100% !important;
            border-top: 1px solid #e2e8f0 !important;
            background: #ffffff !important;
            padding: 10px 12px !important;
            display: flex !important;
            flex-wrap: wrap !important;
            align-items: flex-end !important;
            gap: 8px !important;
        }

        @media (min-width: 768px) {
            .chat-input {
                padding: 14px 20px !important;
                gap: 12px !important;
            }
        }

        .chat-input textarea {
            background-color: #f1f5f9 !important;
            border-radius: 20px !important;
            padding: 12px 16px !important;
            border: 1px solid #cbd5e1 !important;
            transition: all 0.3s ease !important;
            flex-grow: 1 !important;
            width: auto !important;
            margin: 0 !important;
            max-height: 100px !important;
            min-height: 45px !important;
            font-size: 0.9rem !important;
        }

        @media (min-width: 768px) {
            .chat-input textarea {
                padding: 14px 18px !important;
                min-height: 50px !important;
                font-size: 1rem !important;
            }
        }

        .chat-input span {
            flex-shrink: 0 !important;
            margin: 0 !important;
            width: 45px !important;
            height: 45px !important;
            display: flex !important;
            align-items: center !important;
            justify-content: center !important;
            border-radius: 14px !important;
            background-color: #0ea5e9 !important;
            color: white !important;
            cursor: pointer !important;
            transition: background-color 0.2s ease !important;
        }

        @media (min-width: 768px) {
            .chat-input span {
                width: 50px !important;
                height: 50px !important;
                border-radius: 16px !important;
            }
        }

        .chat-input span:hover {
            background-color: #0284c7 !important;
        }

        .starter-prompts-container {
            display: grid;
            grid-template-columns: 1fr;
            gap: 8px !important;
            margin: 10px 0 !important;
            padding: 0 5px !important;
        }

        @media (min-width: 768px) {
            .starter-prompts-container {
                grid-template-columns: 1fr 1fr;
                gap: 12px !important;
            }
        }

        .starter-card {
            padding: 10px 14px !important;
            border-radius: 10px !important;
            border: 1px solid #e2e8f0;
            background-color: #ffffff;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        @media (min-width: 768px) {
            .starter-card {
                padding: 12px 16px !important;
            }
        }

        .starter-card:hover {
            background-color: #f8fafc;
            border-color: #cbd5e1;
            transform: translateY(-2px);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }

        .starter-card strong {
            display: block;
            font-size: 0.85rem !important;
            color: #0f172a;
            margin-bottom: 2px !important;
        }

        .starter-card span {
            font-size: 0.75rem !important;
            color: #64748b;
            line-height: 1.3 !important;
        }

        .disclaimer-text {
            font-size: 0.7rem;
            color: #94a3b8;
            text-align: center;
            width: 100%;
            margin-top: 6px;
            font-style: italic;
        }

        @media (min-width: 768px) {
            .disclaimer-text {
                font-size: 0.75rem;
                margin-top: 8px;
            }
        }

        @media print {
            body * {
                visibility: hidden;
            }

            #chatbot-wrapper,
            #chatbot-wrapper * {
                visibility: visible;
            }

            #chatbot-wrapper {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: auto !important;
                border: none;
                box-shadow: none;
                overflow: visible !important;
            }

            .action-bar,
            .chat-input,
            .starter-prompts-container,
            .disclaimer-text {
                display: none !important;
            }

            .chatbox {
                height: auto !important;
                overflow: visible !important;
                background-color: white !important;
            }
        }

        .chat.incoming {
            scroll-margin-top: 20px;
        }

        .message-content strong {
            font-weight: 700 !important;
            color: #0f172a !important;
        }

        .message-content p {
            margin-bottom: 0.75rem !important;
        }

        .message-content ul {
            list-style-type: disc !important;
            padding-left: 1.5rem !important;
            margin-bottom: 0.75rem !important;
        }

        .message-content ol {
            list-style-type: decimal !important;
            padding-left: 1.5rem !important;
            margin-bottom: 0.75rem !important;
        }

        .message-content li {
            margin-bottom: 0.25rem !important;
        }
    </style>

    <script>
        document.addEventListener("DOMContentLoaded", () => {
            const chatbotContainer = document.querySelector('.chatbot-container');
            const targetWrapper = document.getElementById('chatbot-wrapper');

            function showStarterPrompts() {
                const chatbox = document.querySelector('.chatbox');
                if (!chatbox) return;
                if (document.getElementById('starter-prompts')) return;

                const starterDiv = document.createElement('div');
                starterDiv.id = 'starter-prompts';
                starterDiv.className = 'starter-prompts-container';
                starterDiv.innerHTML = `
                <div class="starter-card" data-question="Apa saja syarat kesehatan yang harus diperiksa sebelum menikah?">
                    <strong>💍 Kesiapan Pranikah</strong>
                    <span>Ketahui syarat pemeriksaan kesehatan fisik sebelum menikah.</span>
                </div>
                <div class="starter-card" data-question="Bagaimana cara mencegah stunting sejak masa kehamilan?">
                    <strong>👶 Pencegahan Stunting</strong>
                    <span>Pelajari asupan dan kebiasaan sehat di 1000 Hari Pertama Kehidupan.</span>
                </div>
                <div class="starter-card" data-question="Apa saja efek samping menggunakan KB Implan?">
                    <strong>💊 Informasi Kontrasepsi</strong>
                    <span>Mitos, fakta, dan efek samping penggunaan KB jenis Implan.</span>
                </div>
                <div class="starter-card" data-question="Kapan waktu yang tepat untuk melakukan konseling program hamil?">
                    <strong>🩺 Konseling Reproduksi</strong>
                    <span>Informasi seputar pelayanan konseling kehamilan yang tepat.</span>
                </div>
            `;

                chatbox.appendChild(starterDiv);
                chatbox.scrollTo(0, chatbox.scrollHeight);

                const cards = starterDiv.querySelectorAll('.starter-card');
                const textarea = document.querySelector('.chat-input textarea');
                const sendBtn = document.querySelector('.chat-input span');

                cards.forEach(card => {
                    card.addEventListener('click', function() {
                        textarea.value = this.getAttribute('data-question');
                        sendBtn.click();
                        starterDiv.remove();
                    });
                });

                sendBtn.addEventListener('click', () => {
                    const prompts = document.getElementById('starter-prompts');
                    if (prompts) prompts.remove();
                });
                textarea.addEventListener("keydown", (e) => {
                    if (e.key === "Enter" && !e.shiftKey) {
                        const prompts = document.getElementById('starter-prompts');
                        if (prompts) prompts.remove();
                    }
                });
            }

            if (chatbotContainer && targetWrapper) {
                targetWrapper.appendChild(chatbotContainer);
                document.body.classList.add("show-chatbot");

                const chatInputArea = document.querySelector('.chat-input');
                if (chatInputArea && !document.querySelector('.disclaimer-text')) {
                    const disclaimer = document.createElement('div');
                    disclaimer.className = 'disclaimer-text';
                    disclaimer.innerHTML =
                        'Asisten Virtual BKKBN ini menggunakan AI. Percakapan direkam secara anonim untuk evaluasi sistem. Harap konsultasikan keluhan medis secara langsung dengan tenaga kesehatan.';
                    chatInputArea.appendChild(disclaimer);
                }

                document.getElementById('btn-print').addEventListener('click', () => {
                    window.print();
                });

                document.getElementById('btn-clear').addEventListener('click', () => {
                    const chatbox = document.querySelector('.chatbox');
                    if (chatbox) {
                        const messages = chatbox.querySelectorAll('.chat');
                        for (let i = 1; i < messages.length; i++) {
                            messages[i].remove();
                        }

                        const aiSuggestions = chatbox.querySelectorAll('.suggestions-container');
                        aiSuggestions.forEach(suggestion => suggestion.remove());

                        localStorage.removeItem("riwayat_chat_bkkbn");

                        showStarterPrompts();
                    }
                });

                setTimeout(() => {
                    const savedChat = localStorage.getItem("riwayat_chat_bkkbn");
                    if (!savedChat) {
                        showStarterPrompts();
                    }
                }, 600);
            }
        });
    </script>
@endsection
