<?php
if (!defined('ABSPATH')) exit;

class MWP_Floating_Button {
    public function __construct() {
        // Dashicons load karne ke liye (Mobile par zaruri hai)
        add_action('wp_enqueue_scripts', function() {
            wp_enqueue_style('dashicons');
        });
        add_action('wp_footer', [$this, 'render_button']);
    }

    public function render_button() {
        if (get_option('mwp_enable_floating_button') != '1') return;

        // Fetch Options
        $wa1_num = get_option('mwp_whatsapp_number');
        $wa1_lab = get_option('mwp_wa_1_label', 'Domestic Tours');
        $wa1_msg = get_option('mwp_wa_1_message');

        $wa2_num = get_option('mwp_whatsapp_number_2');
        $wa2_lab = get_option('mwp_wa_2_label', 'International');
        $wa2_msg = get_option('mwp_wa_2_message');

        $wa3_num = get_option('mwp_whatsapp_number_3');
        $wa3_lab = get_option('mwp_wa_3_label', 'Custom Packages');
        $wa3_msg = get_option('mwp_wa_3_message');

        $call_num = get_option('mwp_call_number');
        $enable_call = get_option('mwp_enable_call');
        ?>

        <style>
            /* --- Global Wrapper --- */
            #mwp-wrapper { 
                position: fixed; 
                bottom: 20px; 
                right: 20px; 
                z-index: 99999999 !important; 
                font-family: 'Segoe UI', Roboto, Arial, sans-serif;
                direction: ltr;
                pointer-events: none; /* Allows clicks to pass through empty space */
            }

            /* --- Main Button --- */
            #mwp-main { 
                background: #25d366 !important; 
                color: #ffffff !important; 
                padding: 0 20px;
                height: 55px; 
                border-radius: 50px; 
                display: flex; 
                align-items: center; 
                justify-content: center; 
                cursor: pointer !important; 
                box-shadow: 0 4px 15px rgba(0,0,0,0.3); 
                transition: all 0.3s ease;
                pointer-events: auto; /* Enables clicks on button */
                float: right;
                clear: both;
            }

            #mwp-main .dashicons { font-size: 24px; margin-right: 8px; width: 24px; height: 24px; line-height: 24px; color: #fff !important; }
            #mwp-main .mwp-text { font-weight: 600; font-size: 16px; color: #fff !important; }

            /* --- Menu Stack --- */
            #mwp-menu { 
                display: none; 
                flex-direction: column; 
                align-items: flex-end; 
                gap: 12px; 
                margin-bottom: 15px; 
            }
            #mwp-menu.show { display: flex; }

            .mwp-item { 
                display: flex; 
                align-items: center; 
                text-decoration: none; 
                cursor: pointer !important; 
                pointer-events: auto;
            }

            .mwp-label-text {
                background: #ffffff !important;
                color: #333333 !important;
                padding: 6px 14px;
                border-radius: 8px;
                margin-right: 12px;
                font-size: 13px;
                font-weight: 600;
                box-shadow: 0 4px 12px rgba(0,0,0,0.15);
                white-space: nowrap;
            }

            .mwp-icon-circle {
                width: 45px; height: 45px;
                border-radius: 50%;
                display: flex; align-items: center; justify-content: center;
                color: #fff !important;
                box-shadow: 0 4px 10px rgba(0,0,0,0.1);
            }
            .whatsapp-icon { background: #25d366 !important; }
            .call-icon { background: #007bff !important; }

            /* --- Lead Form --- */
            #mwp-lead-form {
                position: absolute;
                bottom: 80px;
                right: 0;
                width: 300px;
                background: #ffffff !important;
                border-radius: 15px;
                box-shadow: 0 10px 40px rgba(0,0,0,0.4);
                overflow: hidden;
                display: none;
                pointer-events: auto;
            }
            .mwp-form-header { background: #25d366 !important; color: #fff; padding: 15px; display: flex; justify-content: space-between; font-weight: 600; }
            .mwp-close-form { cursor: pointer; font-size: 20px; }
            .mwp-form-body { padding: 15px; }
            .mwp-form-body input { width: 100% !important; padding: 10px !important; margin-bottom: 10px !important; border: 1px solid #ddd !important; border-radius: 5px !important; display: block !important; }
            #mwp-submit-lead { width: 100%; background: #25d366 !important; color: #fff; border: none; padding: 12px; border-radius: 5px; font-weight: 700; cursor: pointer; }

            /* --- MOBILE FIXES --- */
            @media (max-width: 480px) {
                #mwp-wrapper { bottom: 15px; right: 15px; left: auto; }
                #mwp-main { height: 50px; padding: 0 15px; }
                #mwp-main .mwp-text { font-size: 14px; }
                #mwp-lead-form { 
                    position: fixed; 
                    width: 90vw; 
                    left: 5vw; 
                    right: 5vw; 
                    bottom: 80px; 
                }
            }
        </style>

        <div id="mwp-wrapper">
            <div id="mwp-menu">
                <?php if ($enable_call == '1' && !empty($call_num)): ?>
                    <a href="tel:<?php echo esc_attr($call_num); ?>" class="mwp-item">
                        <span class="mwp-label-text">Call Now</span>
                        <div class="mwp-icon-circle call-icon"><span class="dashicons dashicons-phone"></span></div>
                    </a>
                <?php endif; ?>

                <?php 
                $wa_options = [
                    ['num' => $wa1_num, 'lab' => $wa1_lab, 'msg' => $wa1_msg],
                    ['num' => $wa2_num, 'lab' => $wa2_lab, 'msg' => $wa2_msg],
                    ['num' => $wa3_num, 'lab' => $wa3_lab, 'msg' => $wa3_msg]
                ];
                foreach ($wa_options as $wa): 
                    if (!empty($wa['num'])): ?>
                    <div class="mwp-item mwp-trigger-form" data-num="<?php echo esc_attr($wa['num']); ?>" data-msg="<?php echo esc_attr($wa['msg']); ?>">
                        <span class="mwp-label-text"><?php echo esc_html($wa['lab']); ?></span>
                        <div class="mwp-icon-circle whatsapp-icon"><span class="dashicons dashicons-whatsapp"></span></div>
                    </div>
                <?php endif; endforeach; ?>
            </div>

            <div id="mwp-lead-form">
                <div class="mwp-form-header">
                    <span>Quick Inquiry</span>
                    <span class="mwp-close-form">&times;</span>
                </div>
                <div class="mwp-form-body">
                    <input type="text" id="mwp-user-name" placeholder="Full Name">
                    <input type="tel" id="mwp-user-phone" placeholder="WhatsApp Number">
                    <button id="mwp-submit-lead">Connect on WhatsApp</button>
                </div>
            </div>

            <div id="mwp-main">
                <span class="dashicons dashicons-whatsapp"></span>
                <span class="mwp-text">Chat with us</span>
            </div>
        </div>

        <script>
        document.addEventListener('DOMContentLoaded', function() {
            const mainBtn = document.getElementById('mwp-main');
            const menu = document.getElementById('mwp-menu');
            const form = document.getElementById('mwp-lead-form');
            const triggers = document.querySelectorAll('.mwp-trigger-form');
            const closeForm = document.querySelector('.mwp-close-form');
            
            let activeNum = '';
            let activeMsg = '';

            // Toggle Menu
            mainBtn.addEventListener('click', function(e) {
                menu.classList.toggle('show');
                form.style.display = 'none';
            });

            // Show Form on WhatsApp click
            triggers.forEach(trigger => {
                trigger.addEventListener('click', function() {
                    activeNum = this.getAttribute('data-num');
                    activeMsg = this.getAttribute('data-msg');
                    menu.classList.remove('show');
                    form.style.display = 'block';
                });
            });

            closeForm.addEventListener('click', () => form.style.display = 'none');

            // Handle Submit
            document.getElementById('mwp-submit-lead').addEventListener('click', function() {
                const name = document.getElementById('mwp-user-name').value;
                const phone = document.getElementById('mwp-user-phone').value;

                if(!name || !phone) { alert('Please fill details'); return; }

                const finalMsg = `Hi, I am ${name}. ${activeMsg}`;
                const waUrl = `https://wa.me/${activeNum}?text=${encodeURIComponent(finalMsg)}`;
                window.open(waUrl, '_blank');
                form.style.display = 'none';
            });
        });
        </script>
        <?php
    }
}