<?php
/**
 * Plantilla de Checkout Minimalista v6.3 (Métodos de envío con AJAX)
 */
defined('ABSPATH') || exit;
?>
<div class="ckfylt25-minimalista-wrapper ckfylt25-template-minimalista">
    <div class="ckfylt25-minimalista-container">
        <form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url(wc_get_checkout_url()); ?>" enctype="multipart/form-data">
            <!-- Columna izquierda -->
            <div class="ckfylt25-minimalista-main">
                <!-- Métodos de Pago Express -->
                <?php 
                global $ckfylt25_checkout_override;
                if ($ckfylt25_checkout_override && method_exists($ckfylt25_checkout_override, 'render_express_checkout_section')) {
                    $ckfylt25_checkout_override->render_express_checkout_section();
                }
                ?>
                
                <h2 class="ckfylt25-main-title"><?php esc_html_e( 'Shipping Information', 'checkoutify-lite' ); ?></h2>
                <?php do_action('woocommerce_checkout_before_customer_details'); ?>
                
                <div id="customer_details" class="ckfylt25-billing-container">
                    <div class="ckfylt25-billing-fields">
                        <?php do_action('woocommerce_checkout_billing'); ?>
                    </div>
                    <?php $needs_shipping = ( function_exists('WC') && WC()->cart ) ? WC()->cart->needs_shipping() : false; ?>
                    <div class="ckfylt25-shipping-fields" style="<?php echo $needs_shipping ? '' : 'display: none;'; ?>">
                        <?php do_action('woocommerce_checkout_shipping'); ?>
                    </div>
                </div>
                <?php do_action('woocommerce_checkout_after_customer_details'); ?>
                <!-- Métodos de Envío -->
                <?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
                <div class="ckfylt25-shipping-section">
                    <h3 class="ckfylt25-section-title"><?php esc_html_e( 'Shipping Method', 'checkoutify-lite' ); ?></h3>
                    <ul class="ckfylt25-shipping-methods" id="shipping_methods_list">
                        <?php
                        // Forzar el cálculo de shipping si no está disponible
                        WC()->cart->calculate_shipping();
                        $packages = WC()->shipping()->get_packages();
                        
                        foreach ( $packages as $i => $package ) {
                            $chosen_method = isset( WC()->session->get( 'chosen_shipping_methods' )[ $i ] ) ? WC()->session->get( 'chosen_shipping_methods' )[ $i ] : '';
                            
                            if ( empty( $package['rates'] ) ) {
                                echo '<li>' . esc_html__( 'No shipping methods available for your address.', 'checkoutify-lite' ) . '</li>';
                            } else {
                                $total_methods = count($package['rates']);
                                $auto_select = ($total_methods === 1); // Solo auto-seleccionar si hay exactamente 1 método
                                
                                foreach ( $package['rates'] as $rate_id => $rate ) {
                                    $is_checked = $auto_select || ($rate_id === $chosen_method);
                                    echo '<li>';
                                    echo '<input type="radio" name="shipping_method[' . esc_attr( $i ) . ']" id="shipping_method_' . esc_attr( $i ) . '_' . esc_attr( $rate_id ) . '" value="' . esc_attr( $rate_id ) . '"' . checked( $is_checked, true, false ) . ' />';
                                    echo '<label for="shipping_method_' . esc_attr( $i ) . '_' . esc_attr( $rate_id ) . '">' . esc_html( $rate->get_label() ) . ' <span class="shipping-price">' . wp_kses_post( wc_price( $rate->get_cost() ) ) . '</span></label>';
                                    echo '</li>';
                                }
                            }
                        }
                        ?>
                    </ul>
                </div>
                <?php endif; ?>
                
                <!-- Order Bump -->
                <?php do_action('ckfylt25_before_testimonials'); ?>
                
                <!-- Métodos de Pago en la columna derecha -->
                <div id="payment" class="woocommerce-checkout-payment">
                    <?php if (WC()->cart->needs_payment()) : ?>
                        <h3 class="ckfylt25-payment-title"><?php esc_html_e( 'Payment Method', 'checkoutify-lite' ); ?></h3>
                        <ul class="wc_payment_methods payment_methods methods">
                            <?php
                            if (!empty($available_gateways = WC()->payment_gateways()->get_available_payment_gateways())) {
                                foreach ($available_gateways as $gateway) {
                                    wc_get_template('checkout/payment-method.php', array('gateway' => $gateway));
                                }
                            } else {
                                echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">' . wp_kses_post( apply_filters('woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__('No payment methods available for your location.', 'checkoutify-lite') : esc_html__('Complete your address to see payment methods.', 'checkoutify-lite')) ) . '</li>';
                            }
                            ?>
                        </ul>
                        <div class="form-row place-order">
                            <?php wc_get_template('checkout/terms.php'); ?>
                            <?php do_action('woocommerce_review_order_before_submit'); ?>
                            <button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="<?php esc_attr_e('Place order', 'checkoutify-lite'); ?>" data-value="<?php esc_attr_e('Place order', 'checkoutify-lite'); ?>">
                                <?php esc_html_e('Complete Order', 'checkoutify-lite'); ?>
                            </button>
                            <?php do_action('woocommerce_review_order_after_submit'); ?>
                            <?php wp_nonce_field('woocommerce-process_checkout', 'woocommerce-process-checkout-nonce'); ?>
                        </div>
                    <?php endif; ?>
                </div>
                
                <?php do_action('ckfylt25_after_order_summary'); ?>
            </div>
            <!-- Columna derecha -->
            <div class="ckfylt25-minimalista-sidebar">
                <div class="ckfylt25-sidebar-content">
                    <h3 class="ckfylt25-sidebar-title"><?php esc_html_e( 'Order Summary', 'checkoutify-lite' ); ?></h3>
                    <div class="ckfylt25-order-summary">
                        <ul class="ckfylt25-order-products" id="order_products_list">
                            <?php foreach (WC()->cart->get_cart() as $cart_item) :
                                $product = $cart_item['data'];
                                if (!$product || !$product->exists() || $cart_item['quantity'] <= 0) continue;
                                $thumbnail = get_the_post_thumbnail_url($product->get_id(), 'thumbnail');
                                if (!$thumbnail) {
                                    $thumbnail = wc_placeholder_img_src('thumbnail');
                                }
                            ?>
                            <li class="ckfylt25-order-product" data-price="<?php echo esc_attr($product->get_price()); ?>" data-qty="<?php echo esc_attr($cart_item['quantity']); ?>">
                                <div class="ckfylt25-product-image">
                                    <img src="<?php echo esc_url($thumbnail); ?>" alt="<?php echo esc_attr($product->get_name()); ?>" class="ckfylt25-product-thumb" />
                                    <span class="ckfylt25-product-qty"><?php echo esc_html($cart_item['quantity']); ?></span>
                                </div>
                                <div class="ckfylt25-product-details">
                                    <span class="ckfylt25-product-name"><?php echo esc_html($product->get_name()); ?></span>
                                    <span class="ckfylt25-product-price"><?php echo wp_kses_post( wc_price($product->get_price()) ); ?></span>
                                </div>
                                <div class="ckfylt25-product-total">
                                    <?php echo wp_kses_post( wc_price($product->get_price() * $cart_item['quantity']) ); ?>
                                </div>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                        <div class="ckfylt25-order-totals">
                            <div class="ckfylt25-total-line ckfylt25-subtotal">
                                <span class="ckfylt25-total-label"><?php esc_html_e( 'Subtotal', 'checkoutify-lite' ); ?></span>
                                <span class="ckfylt25-total-amount" id="subtotal_amount"></span>
                            </div>
                            <div class="ckfylt25-total-line ckfylt25-shipping">
                                <span class="ckfylt25-total-label"><?php esc_html_e( 'Shipping', 'checkoutify-lite' ); ?></span>
                                <span class="ckfylt25-total-amount shipping-total" id="shipping_amount"></span>
                            </div>
                            <div class="ckfylt25-total-line ckfylt25-total">
                                <span class="ckfylt25-total-label"><?php esc_html_e( 'Total', 'checkoutify-lite' ); ?></span>
                                <span class="ckfylt25-total-amount" id="total_amount"></span>
                            </div>
                        </div>
                    </div>
                    <!-- Beneficios similares a Frisbee -->
                    <div class="ckfylt25-benefits-section">
                        <h4><?php echo esc_html__( "Here's what you get", 'checkoutify-lite' ); ?></h4>
                        <ul class="ckfylt25-benefits-list">
                            <li>
                                <span class="ckfylt25-benefit-icon">✓</span>
                                <span><?php echo esc_html__( 'Money Back Guarantee - We stand behind our products with confidence', 'checkoutify-lite' ); ?></span>
                            </li>
                            <li>
                                <span class="ckfylt25-benefit-icon">✓</span>
                                <span><?php echo esc_html__( 'Fast & Secure Processing - Your order is protected with bank-level security', 'checkoutify-lite' ); ?></span>
                            </li>
                            <li>
                                <span class="ckfylt25-benefit-icon">✓</span>
                                <span><?php echo esc_html__( 'Full Support - We provide high quality post purchase support for all our customers', 'checkoutify-lite' ); ?></span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
