<?php $this->layout('layouts/main', ['pageTitle' => $pageTitle ?? 'QR Codes & Liens']); ?>

<div class="space-y-6">
    <!-- Header -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">📱 QR Codes & Liens directs</h1>
            <p class="text-gray-500 mt-1">Partagez facilement l'accès à votre support client</p>
        </div>
    </div>

    <!-- Widget Chat -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="p-6 border-b border-gray-100 bg-gradient-to-r from-cyan-500 to-emerald-500">
            <h2 class="text-xl font-bold text-white flex items-center gap-2">
                <i class="fas fa-comment-dots"></i>
                Widget Chat Web
            </h2>
            <p class="text-white/80 text-sm mt-1">QR Code et lien pour accéder au chat depuis n'importe quel appareil</p>
        </div>
        
        <div class="p-6">
            <div class="grid md:grid-cols-2 gap-8">
                <!-- QR Code -->
                <div class="text-center">
                    <div class="bg-white p-4 rounded-xl border-2 border-dashed border-gray-200 inline-block">
                        <img src="<?= htmlspecialchars($links['widget']['qr_code']) ?>" 
                             alt="QR Code Widget" 
                             class="w-64 h-64 mx-auto"
                             id="widget-qr">
                    </div>
                    <p class="text-sm text-gray-500 mt-3">Scannez pour ouvrir le chat</p>
                    <a href="<?= htmlspecialchars($links['widget']['qr_code']) ?>" 
                       download="qrcode-widget-<?= htmlspecialchars($entreprise['slug']) ?>.png"
                       class="inline-flex items-center gap-2 mt-3 px-4 py-2 bg-gray-100 hover:bg-gray-200 rounded-lg text-sm font-medium text-gray-700 transition">
                        <i class="fas fa-download"></i>
                        Télécharger le QR Code
                    </a>
                </div>
                
                <!-- Liens et code -->
                <div class="space-y-4">
                    <!-- Lien direct -->
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            <i class="fas fa-link mr-1"></i> Lien direct
                        </label>
                        <div class="flex gap-2">
                            <input type="text" 
                                   value="<?= htmlspecialchars($links['widget']['url']) ?>" 
                                   readonly
                                   class="flex-1 px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-sm"
                                   id="widget-url">
                            <button onclick="copyToClipboard('widget-url')" 
                                    class="px-4 py-2 bg-cyan-500 hover:bg-cyan-600 text-white rounded-lg transition">
                                <i class="fas fa-copy"></i>
                            </button>
                        </div>
                        <p class="text-xs text-gray-500 mt-1">Partagez ce lien sur vos réseaux sociaux, emails, SMS...</p>
                    </div>
                    
                    <!-- Code d'intégration -->
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            <i class="fas fa-code mr-1"></i> Code d'intégration (site web)
                        </label>
                        <div class="relative">
                            <textarea readonly 
                                      class="w-full px-4 py-3 border border-gray-300 rounded-lg bg-gray-900 text-green-400 text-sm font-mono resize-none"
                                      rows="3"
                                      id="embed-code"><?= htmlspecialchars($links['widget']['embed_code']) ?></textarea>
                            <button onclick="copyToClipboard('embed-code')" 
                                    class="absolute top-2 right-2 px-3 py-1 bg-gray-700 hover:bg-gray-600 text-white text-xs rounded transition">
                                Copier
                            </button>
                        </div>
                        <p class="text-xs text-gray-500 mt-1">Collez ce code juste avant &lt;/body&gt; sur votre site</p>
                    </div>
                    
                    <!-- Tester -->
                    <a href="<?= htmlspecialchars($links['widget']['url']) ?>" 
                       target="_blank"
                       class="inline-flex items-center gap-2 px-4 py-2 bg-emerald-500 hover:bg-emerald-600 text-white rounded-lg transition">
                        <i class="fas fa-external-link-alt"></i>
                        Tester le widget
                    </a>
                </div>
            </div>
        </div>
    </div>

    <?php if (isset($links['whatsapp'])): ?>
    <!-- WhatsApp -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="p-6 border-b border-gray-100 bg-gradient-to-r from-green-500 to-green-600">
            <h2 class="text-xl font-bold text-white flex items-center gap-2">
                <i class="fab fa-whatsapp"></i>
                WhatsApp Direct
            </h2>
            <p class="text-white/80 text-sm mt-1">QR Code et lien pour démarrer une conversation WhatsApp</p>
        </div>
        
        <div class="p-6">
            <div class="grid md:grid-cols-2 gap-8">
                <!-- QR Code WhatsApp -->
                <div class="text-center">
                    <div class="bg-white p-4 rounded-xl border-2 border-dashed border-green-200 inline-block">
                        <img src="<?= htmlspecialchars($links['whatsapp']['qr_code']) ?>" 
                             alt="QR Code WhatsApp" 
                             class="w-64 h-64 mx-auto">
                    </div>
                    <p class="text-sm text-gray-500 mt-3">Scannez pour ouvrir WhatsApp</p>
                    <a href="<?= htmlspecialchars($links['whatsapp']['qr_code']) ?>" 
                       download="qrcode-whatsapp-<?= htmlspecialchars($entreprise['slug']) ?>.png"
                       class="inline-flex items-center gap-2 mt-3 px-4 py-2 bg-gray-100 hover:bg-gray-200 rounded-lg text-sm font-medium text-gray-700 transition">
                        <i class="fas fa-download"></i>
                        Télécharger le QR Code
                    </a>
                </div>
                
                <!-- Lien WhatsApp -->
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            <i class="fab fa-whatsapp mr-1"></i> Lien WhatsApp direct
                        </label>
                        <div class="flex gap-2">
                            <input type="text" 
                                   value="<?= htmlspecialchars($links['whatsapp']['url']) ?>" 
                                   readonly
                                   class="flex-1 px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-sm"
                                   id="whatsapp-url">
                            <button onclick="copyToClipboard('whatsapp-url')" 
                                    class="px-4 py-2 bg-green-500 hover:bg-green-600 text-white rounded-lg transition">
                                <i class="fas fa-copy"></i>
                            </button>
                        </div>
                    </div>
                    
                    <a href="<?= htmlspecialchars($links['whatsapp']['url']) ?>" 
                       target="_blank"
                       class="inline-flex items-center gap-2 px-4 py-2 bg-green-500 hover:bg-green-600 text-white rounded-lg transition">
                        <i class="fab fa-whatsapp"></i>
                        Ouvrir WhatsApp
                    </a>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Instructions d'utilisation -->
    <div class="bg-gradient-to-br from-indigo-50 to-purple-50 rounded-xl p-6 border border-indigo-100">
        <h3 class="text-lg font-bold text-indigo-900 mb-4">
            <i class="fas fa-lightbulb mr-2"></i>
            Comment utiliser ces QR Codes ?
        </h3>
        
        <div class="grid md:grid-cols-3 gap-4">
            <div class="bg-white rounded-lg p-4 shadow-sm">
                <div class="w-10 h-10 bg-indigo-100 rounded-lg flex items-center justify-center mb-3">
                    <i class="fas fa-file-invoice text-indigo-600"></i>
                </div>
                <h4 class="font-semibold text-gray-900">Sur vos factures</h4>
                <p class="text-sm text-gray-600 mt-1">Imprimez le QR Code sur vos factures pour un accès direct au support</p>
            </div>
            
            <div class="bg-white rounded-lg p-4 shadow-sm">
                <div class="w-10 h-10 bg-indigo-100 rounded-lg flex items-center justify-center mb-3">
                    <i class="fas fa-store text-indigo-600"></i>
                </div>
                <h4 class="font-semibold text-gray-900">Dans vos agences</h4>
                <p class="text-sm text-gray-600 mt-1">Affichez le QR Code pour réduire les files d'attente</p>
            </div>
            
            <div class="bg-white rounded-lg p-4 shadow-sm">
                <div class="w-10 h-10 bg-indigo-100 rounded-lg flex items-center justify-center mb-3">
                    <i class="fas fa-box text-indigo-600"></i>
                </div>
                <h4 class="font-semibold text-gray-900">Sur vos produits</h4>
                <p class="text-sm text-gray-600 mt-1">Intégrez le QR Code sur les emballages pour un SAV immédiat</p>
            </div>
        </div>
    </div>

    <!-- Statistiques d'utilisation -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
        <h3 class="text-lg font-bold text-gray-900 mb-4">
            <i class="fas fa-chart-bar mr-2"></i>
            Canaux activés
        </h3>
        
        <?php 
        $canaux = json_decode($entreprise['canaux_actifs'] ?? '[]', true);
        ?>
        
        <div class="flex flex-wrap gap-3">
            <span class="inline-flex items-center gap-2 px-4 py-2 rounded-full text-sm font-medium <?= in_array('web', $canaux) ? 'bg-cyan-100 text-cyan-700' : 'bg-gray-100 text-gray-400' ?>">
                <i class="fas fa-globe"></i>
                Web Chat
                <?= in_array('web', $canaux) ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-times-circle"></i>' ?>
            </span>
            
            <span class="inline-flex items-center gap-2 px-4 py-2 rounded-full text-sm font-medium <?= in_array('whatsapp', $canaux) ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-400' ?>">
                <i class="fab fa-whatsapp"></i>
                WhatsApp
                <?= in_array('whatsapp', $canaux) ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-times-circle"></i>' ?>
            </span>
            
            <span class="inline-flex items-center gap-2 px-4 py-2 rounded-full text-sm font-medium <?= in_array('voice', $canaux) ? 'bg-purple-100 text-purple-700' : 'bg-gray-100 text-gray-400' ?>">
                <i class="fas fa-phone"></i>
                Appels Vocaux
                <?= in_array('voice', $canaux) ? '<i class="fas fa-check-circle"></i>' : '<i class="fas fa-times-circle"></i>' ?>
            </span>
        </div>
        
        <p class="text-sm text-gray-500 mt-4">
            <i class="fas fa-info-circle mr-1"></i>
            Pour activer d'autres canaux, contactez votre administrateur ou accédez aux <a href="<?= BASE_URL ?>/admin/widget" class="text-cyan-600 hover:underline">Paramètres du Widget</a>.
        </p>
    </div>
</div>

<script>
function copyToClipboard(elementId) {
    const element = document.getElementById(elementId);
    const text = element.value || element.textContent;
    
    navigator.clipboard.writeText(text).then(() => {
        // Feedback visuel
        const btn = event.target.closest('button');
        const originalHtml = btn.innerHTML;
        btn.innerHTML = '<i class="fas fa-check"></i>';
        btn.classList.add('bg-green-500');
        
        setTimeout(() => {
            btn.innerHTML = originalHtml;
            btn.classList.remove('bg-green-500');
        }, 2000);
    }).catch(err => {
        // Fallback pour les anciens navigateurs
        element.select();
        document.execCommand('copy');
        alert('Copié !');
    });
}
</script>
