<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Identifikasi Jenis Penyakit Kakao</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/x-icon">
</head>
<body class="bg-[#f0f9ff] font-poppins">
            <a href="/" class="flex items-center text-black font-semibold">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                </svg>
            </a>
    <!-- Konten Utama -->
    <div class="flex flex-col items-center justify-center min-h-screen bg-gradient-to-b from-[#e0f7ff] to-[#f0f9ff] relative">

        <!-- Teks Identifikasi Kakao dengan ikon Back to Dashboard -->
        <div class="flex items-center mb-12">
            <h1 class="text-3xl font-bold text-[#1cadc0]">Identifikasi Penyakit Buah Kakao</h1>
        </div>

        <!-- Form untuk unggah gambar -->
        <div class="bg-white p-10 rounded-2xl shadow-xl w-[600px] h-[400px] flex flex-col justify-center items-center relative">
            <input type="file" id="file-input" class="hidden" accept="image/*">
            <button id="upload-btn" class="bg-[#1cadc0] hover:bg-[#16a2b1] transition-colors duration-200 text-white py-3 px-6 rounded-lg text-lg font-semibold w-full">Unggah Gambar</button>
        </div>
        
        <!-- Modal Box -->
        <div id="result-modal" class="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 hidden">
            <div class="bg-white p-8 rounded-lg max-w-md w-full">
                <h2 class="text-xl font-bold mb-4" id="predicted-label"></h2>
                <p id="description"></p>
                <img id="uploaded-image" class="mt-4 w-full max-h-80 object-contain" src="" alt="Uploaded Image">
                <p class="mt-4 font-semibold">Akurasi: <span id="accuracy"></span></p>
                
                 <!-- Tombol Close di kiri -->
                <button class="bg-[#1cadc0] hover:bg-[#16a2b1] transition-colors duration-200 text-white mt-6 py-2 px-4 rounded-lg" onclick="closeModal()">Close</button>
    
                 <!-- Tombol Panduan di kanan -->
                <a href="/panduan" class="bg-[#1cadc0] hover:bg-[#16a2b1] transition-colors duration-200 text-white mt-6 py-2 px-4 rounded-lg">Panduan</a>
            </div>
        </div>
    </div>

    <script>
        const uploadBtn = document.getElementById('upload-btn');
        const fileInput = document.getElementById('file-input');
        const resultModal = document.getElementById('result-modal');
        const predictedLabel = document.getElementById('predicted-label');
        const description = document.getElementById('description');
        const uploadedImage = document.getElementById('uploaded-image');
        const accuracyText = document.getElementById('accuracy');

        // Handle click on upload button to trigger file input
        uploadBtn.addEventListener('click', () => {
            fileInput.click();
        });

        // Handle file input change and upload the file
        fileInput.addEventListener('change', (event) => {
            const file = event.target.files[0];
            if (file) {
                // Check file size (optional)
                const maxSizeInMB = 2; // Limit to 2MB
                const fileSizeInMB = file.size / 1024 / 1024;
                if (fileSizeInMB > maxSizeInMB) {
                    alert('Ukuran file terlalu besar. Maksimal 2MB.');
                    return;
                }

                const formData = new FormData();
                formData.append('file', file);

                fetch('/identifikasi', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    // Show modal with prediction result
                    if (data.error) {
                        alert(data.error);
                    } else {
                        predictedLabel.textContent = data.predicted_label;
                        description.textContent = data.description;
                        accuracyText.textContent = data.accuracy;
                        uploadedImage.src = `/uploads/${data.filename}`;
                        resultModal.classList.remove('hidden');
                    }
                })
                .catch(error => console.error('Error:', error));
            }
        });

        function closeModal() {
            resultModal.classList.add('hidden');
        }
    </script>
</body>
</html>
