<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload & QR Code Generator</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 40px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .header p {
            opacity: 0.8;
            font-size: 1.1rem;
        }
        
        .content {
            padding: 40px;
        }
        
        .upload-section {
            background: #f8f9fa;
            padding: 30px;
            border-radius: 10px;
            margin-bottom: 30px;
            border: 2px dashed #dee2e6;
            text-align: center;
            transition: all 0.3s ease;
        }
        
        .upload-section:hover {
            border-color: #3498db;
            background: #f1f8ff;
        }
        
        .upload-section.dragover {
            border-color: #27ae60;
            background: #f0fff4;
        }
        
        .file-input-wrapper {
            position: relative;
            display: inline-block;
            margin: 20px 0;
        }
        
        .file-input {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }
        
        .file-input-button {
            background: #3498db;
            color: white;
            padding: 15px 30px;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-block;
        }
        
        .file-input-button:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }
        
        .upload-info {
            color: #7f8c8d;
            margin-top: 15px;
        }
        
        .selected-file {
            background: #e8f5e8;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            border-left: 4px solid #27ae60;
        }
        
        .upload-button {
            background: #27ae60;
            color: white;
            padding: 15px 40px;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
        }
        
        .upload-button:hover:not(:disabled) {
            background: #229954;
            transform: translateY(-2px);
        }
        
        .upload-button:disabled {
            background: #95a5a6;
            cursor: not-allowed;
            transform: none;
        }
        
        .result-section {
            margin-top: 30px;
            padding: 30px;
            background: #f8f9fa;
            border-radius: 10px;
            display: none;
        }
        
        .result-success {
            border-left: 4px solid #27ae60;
        }
        
        .result-error {
            border-left: 4px solid #e74c3c;
        }
        
        .result-content {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .result-card {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .result-card h4 {
            color: #2c3e50;
            margin-bottom: 15px;
        }
        
        .result-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding: 5px 0;
        }
        
        .result-label {
            font-weight: 600;
            color: #7f8c8d;
        }
        
        .result-value {
            color: #2c3e50;
        }
        
        .result-value a {
            color: #3498db;
            text-decoration: none;
        }
        
        .result-value a:hover {
            text-decoration: underline;
        }
        
        .qr-preview {
            text-align: center;
            margin-top: 20px;
        }
        
        .qr-preview img {
            max-width: 200px;
            border-radius: 8px;
            box-shadow: 0 3px 10px rgba(0,0,0,0.1);
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .api-info {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 8px;
            padding: 20px;
            margin-top: 30px;
        }
        
        .api-info h3 {
            color: #856404;
            margin-bottom: 15px;
        }
        
        .api-info code {
            background: #f8f9fa;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .header h1 {
                font-size: 2rem;
            }
            
            .content {
                padding: 20px;
            }
            
            .result-content {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📸 Image Upload & QR Generator</h1>
            <p>Upload an image and get a QR code linking to it</p>
        </div>
        
        <div class="content">
            <form id="uploadForm" enctype="multipart/form-data">
                <div class="upload-section" id="uploadSection">
                    <h3>📁 Select or Drop Your Image</h3>
                    <div class="file-input-wrapper">
                        <input type="file" id="imageFile" name="image" accept="image/*" class="file-input" required>
                        <label for="imageFile" class="file-input-button">Choose Image File</label>
                    </div>
                    <div class="upload-info">
                        Supported formats: JPEG, PNG, GIF, WebP<br>
                        Maximum file size: 10MB
                    </div>
                    <div id="selectedFile" class="selected-file" style="display: none;"></div>
                    <button type="submit" id="uploadButton" class="upload-button" disabled>
                        🚀 Upload & Generate QR Code
                    </button>
                </div>
            </form>
            
            <div id="loading" class="loading">
                <div class="spinner"></div>
                <p>Processing your image and generating QR code...</p>
            </div>
            
            <div id="resultSection" class="result-section">
                <h3 id="resultTitle"></h3>
                <div id="resultContent"></div>
            </div>
        </div>
    </div>

    <script>
        const uploadForm = document.getElementById('uploadForm');
        const imageFile = document.getElementById('imageFile');
        const uploadButton = document.getElementById('uploadButton');
        const selectedFile = document.getElementById('selectedFile');
        const uploadSection = document.getElementById('uploadSection');
        const loading = document.getElementById('loading');
        const resultSection = document.getElementById('resultSection');
        const resultTitle = document.getElementById('resultTitle');
        const resultContent = document.getElementById('resultContent');

        // File selection handler
        imageFile.addEventListener('change', function() {
            if (this.files && this.files[0]) {
                const file = this.files[0];
                selectedFile.innerHTML = `
                    <strong>Selected:</strong> ${file.name}<br>
                    <strong>Size:</strong> ${(file.size / 1024 / 1024).toFixed(2)} MB<br>
                    <strong>Type:</strong> ${file.type}
                `;
                selectedFile.style.display = 'block';
                uploadButton.disabled = false;
            } else {
                selectedFile.style.display = 'none';
                uploadButton.disabled = true;
            }
        });

        // Drag and drop handlers
        uploadSection.addEventListener('dragover', function(e) {
            e.preventDefault();
            this.classList.add('dragover');
        });

        uploadSection.addEventListener('dragleave', function(e) {
            e.preventDefault();
            this.classList.remove('dragover');
        });

        uploadSection.addEventListener('drop', function(e) {
            e.preventDefault();
            this.classList.remove('dragover');
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                imageFile.files = files;
                imageFile.dispatchEvent(new Event('change'));
            }
        });

        // Form submission handler
        uploadForm.addEventListener('submit', async function(e) {
            e.preventDefault();
            
            if (!imageFile.files[0]) {
                alert('Please select an image file');
                return;
            }

            // Show loading
            loading.style.display = 'block';
            resultSection.style.display = 'none';
            uploadButton.disabled = true;

            try {
                const formData = new FormData();
                formData.append('image', imageFile.files[0]);

                const response = await fetch('upload.php', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.json();

                // Hide loading
                loading.style.display = 'none';
                uploadButton.disabled = false;

                // Show result
                displayResult(result, response.ok);

            } catch (error) {
                console.error('Upload error:', error);
                loading.style.display = 'none';
                uploadButton.disabled = false;
                displayResult({
                    success: false,
                    error: 'Network error occurred'
                }, false);
            }
        });

        function displayResult(result, success) {
            resultSection.style.display = 'block';
            
            if (success && result.success) {
                resultSection.className = 'result-section result-success';
                resultTitle.textContent = '✅ Upload Successful';
                
                resultContent.innerHTML = `
                    <div class="result-card">
                        <h4>📱 QR Code Generated</h4>
                        <div class="result-item">
                            <span class="result-label">QR Code Link:</span>
                            <span class="result-value">
                                <a href="${result.qr_code_link}" target="_blank">View QR Code</a>
                            </span>
                        </div>
                        <div style="text-align: center; margin-top: 20px;">
                            <iframe src="${result.qr_code_link}" width="320" height="320" frameborder="0" style="border-radius: 8px;"></iframe>
                        </div>
                    </div>
                `;
            } else {
                resultSection.className = 'result-section result-error';
                resultTitle.textContent = '❌ Upload Failed';
                resultContent.innerHTML = `
                    <div class="result-card">
                        <h4>Error Details</h4>
                        <p style="color: #e74c3c;">${result.error || 'Unknown error occurred'}</p>
                    </div>
                `;
            }
        }
    </script>
</body>
</html>
