<?php
/**
 * Template for the quick order popup.
 *
 * @since      1.0.0
 * @package    DevNajmusOneClickOrderPopup
 */

defined('ABSPATH') || exit;
?>

<div id="DOPW-popup" class="DOPW-popup" style="display: none;" role="dialog" aria-modal="true">
    <div class="DOPW-popup-overlay"></div>
    <div class="DOPW-popup-content">
        <div class="DOPW-popup-header">
            <h3><?php esc_html_e('Quick Order', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></h3>
            <button type="button" class="DOPW-close">&times;</button>
        </div>

        <form id="DOPW-order-form" class="DOPW-form">
            <input type="hidden" id="DOPW-nonce" name="DOPW_nonce" value="<?php echo esc_attr(wp_create_nonce('DOPW_nonce')); ?>">
            <!-- Customer Information -->
            <div class="DOPW-customer-info">
                <h4><?php esc_html_e('Customer Information', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></h4>
                <div class="DOPW-form-row">
                    <div class="DOPW-form-group">
                        <label for="DOPW-name"><?php esc_html_e('Name', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?> *</label>
                        <input type="text" id="DOPW-name" name="customer_name" required>
                    </div>
                    <div class="DOPW-form-group">
                        <label for="DOPW-phone"><?php esc_html_e('Phone', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?> *</label>
                        <input type="tel" id="DOPW-phone" name="customer_phone" required>
                    </div>
                </div>
                <div class="DOPW-form-row">
                    <div class="DOPW-form-group">
                        <label for="DOPW-email"><?php esc_html_e('Email', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></label>
                        <input type="email" id="DOPW-email" name="customer_email">
                    </div>
                    <div class="DOPW-form-group">
                        <label for="DOPW-address"><?php esc_html_e('Address', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?> *</label>
                        <textarea id="DOPW-address" name="customer_address" required></textarea>
                    </div>
                </div>
            </div>

            <!-- Product Information -->
            <div class="DOPW-products">
                <h4><?php esc_html_e('Product Information', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></h4>
                <div id="DOPW-product-list">
                    <!-- Product rows will be dynamically added here -->
                </div>
                <div id="DOPW-variation-selectors" aria-live="polite"></div>
                <div class="DOPW-subtotal">
                    <span><?php esc_html_e('Subtotal:', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></span>
                    <span class="DOPW-subtotal-amount">0.00</span>
                </div>
            </div>

            <!-- Shipping Method -->
            <div class="DOPW-shipping">
                <h4><?php esc_html_e('Shipping Method', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></h4>
                <select id="DOPW-shipping-method" name="shipping_method[0]" required>
                    <option value=""><?php esc_html_e('Select shipping method', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></option>
                    <?php
                    if (function_exists('WC') && (class_exists('WC_Shipping') || WC()->shipping)) {
                        $dopw_packages = [];
                        if (isset(WC()->cart) && method_exists(WC()->cart, 'get_shipping_packages')) {
                            $dopw_packages = WC()->cart->get_shipping_packages();
                        } elseif (WC()->shipping && method_exists(WC()->shipping(), 'get_packages')) {
                            $dopw_packages = WC()->shipping()->get_packages();
                        }

                        $dopw_package = !empty($dopw_packages) ? reset($dopw_packages) : [];
                        $dopw_rates = !empty($dopw_package['rates']) ? $dopw_package['rates'] : [];

                        if (!empty($dopw_rates)) {
                            $dopw_first = true;
                            foreach ($dopw_rates as $dopw_rate_id => $dopw_rate) {
                                $dopw_value = $dopw_rate->get_id();
                                $dopw_label = $dopw_rate->get_label();
                                $dopw_cost = (float)$dopw_rate->get_cost();
                                $dopw_cost_raw = number_format($dopw_cost, wc_get_price_decimals(), '.', '');
                                $dopw_cost_html = wc_price($dopw_cost);
                                $dopw_selected_attr = $dopw_first ? ' selected="selected"' : '';
                                printf(
                                    '<option value="%s" data-cost="%s"%s>%s - %s</option>',
                                    esc_attr($dopw_value),
                                    esc_attr($dopw_cost_raw),
                                    esc_attr($dopw_selected_attr),
                                    esc_html($dopw_label),
                                    wp_kses_post($dopw_cost_html)
                                );
                                $dopw_first = false;
                            }
                        } else {
                            $dopw_zones = WC_Shipping_Zones::get_zones();
                            $dopw_found = false;
                            foreach ($dopw_zones as $dopw_zone) {
                                $dopw_zone_obj = new WC_Shipping_Zone($dopw_zone['zone_id']);
                                $dopw_methods = $dopw_zone_obj->get_shipping_methods(true);
                                if (!empty($dopw_methods)) {
                                    foreach ($dopw_methods as $dopw_method) {
                                        if (!empty($dopw_method->enabled) && $dopw_method->enabled === 'yes') {
                                            $dopw_found = true;
                                            $dopw_method_id = $dopw_method->id;
                                            $dopw_instance_id = isset($dopw_method->instance_id) ? $dopw_method->instance_id : '';
                                            $dopw_value = $dopw_instance_id ? $dopw_method_id . ':' . $dopw_instance_id : $dopw_method_id;
                                            if (method_exists($dopw_method, 'get_method_title')) {
                                                $dopw_label = $dopw_method->get_method_title();
                                            } else {
                                                $dopw_label = !empty($dopw_method->title) ? $dopw_method->title : ($dopw_method->id ?? '');
                                            }
                                            $dopw_cost_opt = $dopw_method->get_option('cost', '');
                                            $dopw_cost_num = is_string($dopw_cost_opt) && $dopw_cost_opt !== '' ? floatval(preg_replace('/[^0-9\.\-]/', '', $dopw_cost_opt)) : 0;
                                            $dopw_cost_raw = number_format($dopw_cost_num, wc_get_price_decimals(), '.', '');
                                            $dopw_cost_html = wc_price($dopw_cost_num);
                                            printf(
                                                '<option value="%s" data-cost="%s">%s - %s</option>',
                                                esc_attr($dopw_value),
                                                esc_attr($dopw_cost_raw),
                                                esc_html($dopw_label),
                                                wp_kses_post($dopw_cost_html)
                                            );
                                        }
                                    }
                                }
                            }
                            if (!$dopw_found) {
                                echo '<option value="">' . esc_html__('No shipping methods available', 'devnajmus-oneclick-order-popup-for-woocommerce') . '</option>';
                            }
                        }
                    } else {
                        echo '<option value="">' . esc_html__('WooCommerce shipping not available', 'devnajmus-oneclick-order-popup-for-woocommerce') . '</option>';
                    }
                    ?>
                </select>
                <div class="DOPW-shipping-cost">
                    <span><?php esc_html_e('Shipping Cost:', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></span>
                    <span class="DOPW-shipping-amount">0.00</span>
                </div>

                <!-- Payment Method -->
                <?php
                if (function_exists('WC') && class_exists('WC_Payment_Gateways')) {
                    $dopw_gateways = WC()->payment_gateways()->get_available_payment_gateways();
                    if (empty($dopw_gateways)) {
                        $dopw_all = WC()->payment_gateways()->payment_gateways();
                        $dopw_gateways = array_filter($dopw_all, function ($dopw_g) {
                            return isset($dopw_g->enabled) && 'yes' === $dopw_g->enabled;
                        });
                    }

                    if (!empty($dopw_gateways)) {
                        echo '<fieldset class="DOPW-payment-methods"><legend>' . esc_html__('Payment Method', 'devnajmus-oneclick-order-popup-for-woocommerce') . '</legend>';
                        $dopw_first = true;
                        foreach ($dopw_gateways as $dopw_gateway_id => $dopw_gateway) {
                            $dopw_id = esc_attr($dopw_gateway->id);
                            $dopw_title = esc_html($dopw_gateway->get_title());
                            $dopw_desc = wp_kses_post($dopw_gateway->get_description());
                            $dopw_checked = $dopw_first ? ' checked="checked"' : '';
                            printf(
                                '<label class="DOPW-payment-method"><input type="radio" name="DOPW_payment_method" value="%s" %s> <span class="DOPW-payment-title">%s</span> <span class="DOPW-payment-desc">%s</span></label>',
                                esc_attr($dopw_id),
                                esc_attr($dopw_checked),
                                esc_html($dopw_title),
                                wp_kses_post($dopw_desc)
                            );
                            $dopw_first = false;
                        }
                        echo '</fieldset>';
                    } else {
                        echo '<div class="DOPW-payment-methods"><p>' . esc_html__('No payment methods available.', 'devnajmus-oneclick-order-popup-for-woocommerce') . '</p></div>';
                    }
                } else {
                    echo '<div class="DOPW-payment-methods"><p>' . esc_html__('WooCommerce payment system not available.', 'devnajmus-oneclick-order-popup-for-woocommerce') . '</p></div>';
                }
                ?>

                <!-- Stripe Elements container (hidden by default) -->
                <div id="DOPW-stripe-container" style="display:none; margin-top:15px;">
                    <label><?php esc_html_e('Card Details', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></label>
                    <div id="DOPW-stripe-element"></div>
                    <div id="DOPW-stripe-errors" role="alert" style="color:#a00;margin-top:8px;"></div>
                    <div class="DOPW-save-payment">
                        <label><input type="checkbox" id="DOPW-save-payment-method" name="DOPW_save_payment_method" value="1"> <?php esc_html_e('Save payment information to my account for future purchases.', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></label>
                    </div>
                </div>

                <!-- Payment Request button (Apple Pay / Google Pay) -->
                <div id="DOPW-payment-request-button" style="margin-top:12px; display:none;"></div>

                <!-- Generic container for gateway-specific payment fields (e.g., PayPal smart buttons) -->
                <div id="DOPW-gateway-fields" style="margin-top:15px;">
                    <!-- AJAX-inserted gateway payment fields will appear here -->
                </div>

            </div>

            <!-- Total -->
            <div class="DOPW-total">
                <strong><?php esc_html_e('Total:', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></strong>
                <strong class="DOPW-total-amount">0.00</strong>
            </div>

            <!-- Footer Actions -->
            <div class="DOPW-footer">
                <button type="button" class="DOPW-cancel"><?php esc_html_e('Cancel', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></button>
                <button type="submit" class="DOPW-place-order"><?php esc_html_e('Place Order', 'devnajmus-oneclick-order-popup-for-woocommerce'); ?></button>
            </div>
        </form>
    </div>
</div>
