<?php
/**
 * Footer Layout — Gmind Institute
 *
 * Site-wide footer included by all pages.
 * Contains 4-column grid (About, Quick Links, Courses, Contact)
 * and a copyright bar. Supports multi-language via __() function.
 *
 * Expected PHP variables:
 *   $settings (optional) — key-value pairs from the settings table
 */

// Ensure $settings exists (may not be passed by all controllers)
$settings = $settings ?? [];

// Extract settings with sensible defaults (use correct DB key names)
$phone    = $settings['site_phone']       ?? '9305640290';
$email    = $settings['site_email']       ?? 'info@gmind.in';
$address  = $settings['site_address']     ?? '123 Education Street, New Delhi, India';
$facebook = $settings['social_facebook']  ?? 'https://facebook.com/gmindinstitute';
$twitter  = $settings['social_twitter']   ?? 'https://twitter.com/gmindinstitute';
$youtube  = $settings['social_youtube']   ?? 'https://youtube.com/@gmindinstitute';
$instagram= $settings['social_instagram'] ?? 'https://instagram.com/gmindinstitute';

// WhatsApp settings — admin-controllable
$whatsappEnabled = ($settings['whatsapp_enabled'] ?? '1') === '1';
$whatsapp        = $settings['whatsapp_number']  ?? '919305640290';
$whatsappMessage = $settings['whatsapp_message'] ?? 'Hello Gmind Institute! I have a query.';

// Live Chat settings — admin-controllable
$chatEnabled        = ($settings['chat_enabled'] ?? '1') === '1';
$chatTitle          = $settings['chat_title']           ?? 'Gmind Institute Support';
$chatSubtitle       = $settings['chat_subtitle']        ?? 'We typically reply in a few minutes';
$chatWelcomeMessage = $settings['chat_welcome_message'] ?? 'Hello! Welcome to Gmind Institute. How can we help you today?';

$basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind';

// Clean WhatsApp number (remove non-digits)
$whatsappClean = preg_replace('/[^0-9]/', '', $whatsapp);

// URL-encode the pre-filled WhatsApp message
$whatsappMessageEncoded = rawurlencode($whatsappMessage);
?>

    <!-- ============================================================
         Site Footer
         ============================================================ -->
    <footer class="site-footer" role="contentinfo" aria-label="Site footer">
        <div class="container">
            <div class="footer-grid">

                <!-- About Column -->
                <div class="footer-about">
                    <div class="footer-logo">
                        <?php
                        // Determine logo source — prefer admin-uploaded logo, fallback to default
                        $footerLogoSrc = $basePath . '/images/logo.png';
                        if (!empty($settings['site_logo'])) {
                            $footerLogoSrc = upload_url($settings['site_logo']);
                        }
                        ?>
                        <a href="<?= $basePath ?>/" style="display:flex; align-items:center; gap:10px; text-decoration:none;">
                            <img src="<?= htmlspecialchars($footerLogoSrc, ENT_QUOTES, 'UTF-8') ?>" alt="Gmind Institute Logo" style="height:48px; width:auto;" onerror="this.onerror=null;this.src='<?= $basePath ?>/images/logo.png';">
                            <div>
                                <span class="logo-text" style="display:block; color:#fff; font-family:'Poppins',sans-serif; font-weight:700; font-size:1.2rem; line-height:1.2;">GMIND</span>
                                <span class="logo-subtext" style="display:block; color:rgba(255,255,255,0.7); font-size:0.7rem; letter-spacing:2px; font-weight:500;">INSTITUTE</span>
                            </div>
                        </a>
                    </div>
                    <p class="footer-desc">
                        <?= __('footer.about_desc') ?>
                    </p>
                    <div class="footer-social" aria-label="Follow us on social media">
                        <!-- Facebook -->
                        <a href="<?= htmlspecialchars($facebook, ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer" aria-label="Follow us on Facebook">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z"/></svg>
                        </a>
                        <!-- Twitter / X -->
                        <a href="<?= htmlspecialchars($twitter, ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer" aria-label="Follow us on Twitter">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
                        </a>
                        <!-- YouTube -->
                        <a href="<?= htmlspecialchars($youtube, ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer" aria-label="Subscribe on YouTube">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M23.498 6.186a3.016 3.016 0 00-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 00.502 6.186C0 8.07 0 12 24 12s0 3.93.502 5.814a3.016 3.016 0 002.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 002.122-2.136C24 15.93 24 12 24 12s0-3.93.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z"/></svg>
                        </a>
                        <!-- Instagram -->
                        <a href="<?= htmlspecialchars($instagram, ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer" aria-label="Follow us on Instagram">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"/></svg>
                        </a>
                    </div>
                </div>

                <!-- Quick Links Column -->
                <div class="footer-col">
                    <h4><?= __('footer.quick_links') ?></h4>
                    <ul class="footer-links">
                        <li><a href="<?= $basePath ?>/"><?= __('nav.home') ?></a></li>
                        <li><a href="<?= $basePath ?>/about"><?= __('nav.about') ?></a></li>
                        <li><a href="<?= $basePath ?>/courses"><?= __('nav.courses') ?></a></li>
                        <li><a href="<?= $basePath ?>/admission"><?= __('nav.admission') ?></a></li>
                        <li><a href="<?= $basePath ?>/exam"><?= __('nav.exam') ?></a></li>
                        <li><a href="<?= $basePath ?>/toppers"><?= __('nav.toppers') ?></a></li>
                        <li><a href="<?= $basePath ?>/blog"><?= __('nav.blog') ?></a></li>
                        <li><a href="<?= $basePath ?>/gallery"><?= __('nav.gallery') ?></a></li>
                        <li><a href="<?= $basePath ?>/news"><?= __('nav.news') ?></a></li>
                        <li><a href="<?= $basePath ?>/contact"><?= __('nav.contact') ?></a></li>
                    </ul>
                </div>

                <!-- Courses Column -->
                <div class="footer-col">
                    <h4><?= __('footer.courses') ?></h4>
                    <ul class="footer-links">
                        <li><a href="<?= $basePath ?>/courses/adca">ADCA</a></li>
                        <li><a href="<?= $basePath ?>/courses/dca">DCA</a></li>
                        <li><a href="<?= $basePath ?>/courses/tally-prime">Tally Prime</a></li>
                        <li><a href="<?= $basePath ?>/courses/python"><?= __('courses.filter_programming') ?> - Python</a></li>
                        <li><a href="<?= $basePath ?>/courses/web-design">Web Design</a></li>
                        <li><a href="<?= $basePath ?>/courses/graphic-design">Graphic Design</a></li>
                        <li><a href="<?= $basePath ?>/courses/ccc">CCC</a></li>
                    </ul>
                </div>

                <!-- Contact Info Column -->
                <div class="footer-col">
                    <h4><?= __('footer.contact_info') ?></h4>
                    <div class="footer-contact-item">
                        <span class="icon" aria-hidden="true">
                            <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"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
                        </span>
                        <span><?= htmlspecialchars($address, ENT_QUOTES, 'UTF-8') ?></span>
                    </div>
                    <div class="footer-contact-item">
                        <span class="icon" aria-hidden="true">
                            <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"><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"/></svg>
                        </span>
                        <a href="tel:<?= htmlspecialchars($phone, ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($phone, ENT_QUOTES, 'UTF-8') ?></a>
                    </div>
                    <div class="footer-contact-item">
                        <span class="icon" aria-hidden="true">
                            <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"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                        </span>
                        <a href="mailto:<?= htmlspecialchars($email, ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($email, ENT_QUOTES, 'UTF-8') ?></a>
                    </div>
                    <div class="footer-contact-item">
                        <span class="icon" aria-hidden="true">
                            <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"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
                        </span>
                        <span><?= __('footer.working_hours') ?></span>
                    </div>
                </div>

            </div><!-- /.footer-grid -->
        </div><!-- /.container -->

        <!-- Copyright Bar -->
        <div class="footer-copyright">
            <div class="container">
                <p><?= __('footer.copyright') ?></p>
            </div>
        </div>
    </footer>

    <!-- ============================================================
         WhatsApp Floating Button (admin-controllable)
         ============================================================ -->
    <?php if ($whatsappEnabled && !empty($whatsappClean)): ?>
    <a href="https://wa.me/<?= $whatsappClean ?>?text=<?= $whatsappMessageEncoded ?>"
       class="whatsapp-float"
       target="_blank"
       rel="noopener noreferrer"
       aria-label="Chat on WhatsApp">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true">
            <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/>
        </svg>
        <span class="whatsapp-float-tooltip"><?= __('chat.whatsapp_tooltip') ?></span>
    </a>
    <?php endif; ?>

    <!-- ============================================================
         Live Chat Widget (admin-controllable)
         ============================================================ -->
    <?php if ($chatEnabled): ?>
    <button class="chat-widget-toggle" id="chatToggle" aria-label="Open Live Chat">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12z"/>
        </svg>
        <span class="chat-unread-badge" id="chatUnreadBadge" style="display:none">0</span>
    </button>

    <div class="chat-window" id="chatWindow">
        <!-- Chat Header -->
        <div class="chat-window-header">
            <div class="chat-window-header-avatar">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>
            </div>
            <div class="chat-window-header-info">
                <h4><?= htmlspecialchars($chatTitle, ENT_QUOTES, 'UTF-8') ?></h4>
                <p><?= htmlspecialchars($chatSubtitle, ENT_QUOTES, 'UTF-8') ?></p>
            </div>
            <button class="chat-window-close" id="chatWindowClose" aria-label="Close chat">&times;</button>
        </div>

        <!-- Pre-chat Form (shown first) -->
        <div class="chat-pre-form" id="chatPreForm" style="display:flex">
            <h3><?= __('chat.greeting') ?></h3>
            <p><?= __('chat.greeting_text') ?></p>
            <input type="text" id="chatNameInput" placeholder="<?= __('chat.name_placeholder') ?>" autocomplete="name">
            <input type="email" id="chatEmailInput" placeholder="<?= __('chat.email_placeholder') ?>" autocomplete="email">
            <button class="chat-start-btn" id="chatStartBtn"><?= __('chat.start_btn') ?></button>
        </div>

        <!-- Chat Main (shown after init) -->
        <div id="chatMain" style="display:none; flex-direction:column; flex:1; overflow:hidden;">
            <div class="chat-window-body" id="chatBody">
                <!-- Typing indicator -->
                <div class="chat-typing" id="chatTyping">
                    <span></span><span></span><span></span>
                </div>
            </div>
            <div class="chat-window-footer">
                <textarea class="chat-input" id="chatInput" placeholder="<?= __('chat.input_placeholder') ?>" rows="1"></textarea>
                <button class="chat-send-btn" id="chatSendBtn" aria-label="Send message">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg>
                </button>
            </div>
        </div>
    </div>

    <!-- Chat Widget Styles -->
    <link rel="stylesheet" href="<?= $basePath ?>/css/chat-widget.css">

    <!-- Chat Widget Script -->
    <script src="<?= $basePath ?>/js/chat-widget.js" defer></script>

    <!-- Pass admin-configured welcome message to chat-widget.js -->
    <script>
        window.GMIND_CHAT_CONFIG = {
            welcomeMessage: <?= json_encode($chatWelcomeMessage, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) ?>
        };
    </script>
    <?php endif; ?>

    <!-- Base path for JS -->
    <meta name="base-path" content="<?= $basePath ?>">

    <!-- Main JavaScript -->
    <script src="<?= $basePath ?>/js/main.js" defer></script>
