<?php

/**
 * Checkout Form
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.5.0
 */

if (!defined('ABSPATH')) {
    exit;
}
if (is_null(WC()->cart)) {
    return;
}
?>
<div data-block-name="woocommerce-checkout/checkout-block" class="woocommerce-checkout-block" bis_skin_checked="1">
    <div class="woocommerce-checkout-block">
        <?php
        do_action('woocommerce_before_checkout_form', $checkout);

        // If checkout registration is disabled and not logged in, the user cannot checkout.
        if (!$checkout->is_registration_enabled() && $checkout->is_registration_required() && !is_user_logged_in()) {
            echo esc_html(apply_filters('woocommerce_checkout_must_be_logged_in_message', __('You must be logged in to checkout.', 'fluxtore')));
            return;
        }

        global $post;

        // Get the custom billing field labels and settings from __fluxtore__billing_fields
        $billing_fields = get_post_meta($post->ID, FLUXTORE_PREFIX . 'billing_fields', true);
        ?>

        <form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url(wc_get_checkout_url()); ?>" enctype="multipart/form-data">
            <input type="hidden" name="fluxtore_checkout" value="1" />
            <input type="hidden" name="fluxtore_step" value="<?php echo $post->ID ?>" />

            <?php if ($checkout->get_checkout_fields()) : ?>

                <?php do_action('woocommerce_checkout_before_customer_details'); ?>

                <div class="woocommerce-checkout-steps">
                    <div class="woocommerce-checkout-steps__section woocommerce-billing-fields__section" id="customer_details">
                        <div class="woocommerce-checkout-steps__column woocommerce-checkout-steps__column--billing">
                            <?php
                                // FLUXTORE FIX: If no custom billing fields are configured, use default WooCommerce fields
                                if ($billing_fields && is_array($billing_fields)) :
                                    // Loop through the custom billing fields (including 'billing_country')
                                    foreach ($billing_fields as $field_key => $field_data) :
                                        // Check if the field is visible; if it's empty or 0, skip the field
                                        if (empty($field_data['visible']) || $field_data['visible'] === '0') {
                                            // If the field is not visible (empty or 0), skip it.
                                            continue;
                                        }

                                        // Get the WooCommerce billing field value
                                        $field_value = WC()->checkout()->get_value($field_key);

                                        // Check if this field is required
                                        $is_required = (isset($field_data['required']) && $field_data['required'] === '1') ? true : false;

                                        // Check if the field is billing_country and conditionally display the dropdown
                                        if ('billing_country' === $field_key) :
                                            // Display the country dropdown
                                            ?>
                                            <p class="woocommerce-form-row woocommerce-form-row--wide form-row <?php echo esc_attr(implode(' ', $field_data['class'])); ?>" id="<?php echo esc_attr($field_key); ?>_field">
                                                <label for="<?php echo esc_attr($field_key); ?>" class="required_field">
                                                    <?php echo esc_html($field_data['label']); ?>
                                                    <?php if ($is_required) : ?>
                                                        <span class="required" aria-hidden="true">*</span>
                                                    <?php endif; ?>
                                                </label>
                                                <span class="woocommerce-input-wrapper">
                                                    <select name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>" class="country_to_state country_select" aria-required="true">
                                                        <option value=""><?php esc_html_e( 'Select a country / region…', 'fluxtore' ); ?></option>
                                                        <?php
                                                            // Get the countries
                                                            $countries = WC()->countries->get_countries();
                                                            foreach ($countries as $country_code => $country_name) :
                                                                $selected = selected($country_code, $field_value, false);
                                                        ?>
                                                            <option value="<?php echo esc_attr($country_code); ?>" <?php echo $selected; ?>>
                                                                <?php echo esc_html($country_name); ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </span>
                                            </p>
                                            <?php
                                        else :
                                            // For other fields, we render them as normal if visible
                                            ?>
                                            <p class="woocommerce-form-row woocommerce-form-row--wide form-row <?php echo esc_attr(implode(' ', $field_data['class'])); ?>" id="<?php echo esc_attr($field_key); ?>_field">
                                                <label for="<?php echo esc_attr($field_key); ?>" class="required_field">
                                                    <?php echo esc_html($field_data['label']); ?>
                                                    <?php if ($is_required) : ?>
                                                        <span class="required" aria-hidden="true">*</span>
                                                    <?php endif; ?>
                                                </label>
                                                <span class="woocommerce-input-wrapper">
                                                    <input
                                                        type="text"
                                                        name="<?php echo esc_attr($field_key); ?>"
                                                        id="<?php echo esc_attr($field_key); ?>"
                                                        value="<?php echo esc_attr($field_value); ?>"
                                                        <?php echo $is_required ? 'required' : ''; ?>
                                                        class="input-text woocommerce-input <?php echo esc_attr(implode(' ', $field_data['class'])); ?>"
                                                        placeholder="<?php echo isset($field_data['placeholder']) ? esc_attr($field_data['placeholder']) : ''; ?>"
                                                        aria-required="true"
                                                        autocomplete="<?php echo isset($field_data['autocomplete']) ? esc_attr($field_data['autocomplete']) : 'off'; ?>"
                                                    />
                                                </span>
                                            </p>
                                            <?php
                                        endif;
                                    endforeach;
                                else :
                                    // FLUXTORE FIX: Show default WooCommerce billing fields when no custom fields are configured
                                    do_action('woocommerce_before_checkout_billing_form', $checkout);
                                    foreach ($checkout->get_checkout_fields('billing') as $key => $field) :
                                        woocommerce_form_field($key, $field, $checkout->get_value($key));
                                    endforeach;
                                    do_action('woocommerce_after_checkout_billing_form', $checkout);
                                endif;
                            ?>

                        </div>

                        <div class="woocommerce-checkout-steps__column woocommerce-checkout-steps__column--order">
                            <?php do_action('fluxtore_before_checkout') ?>

                            <?php do_action('woocommerce_checkout_before_order_review_heading'); ?>

                            <h3 id="order_review_heading">
                                <?php echo wp_kses_post(esc_html__('Your order', 'fluxtore')); ?>
                            </h3>

                            <?php do_action('woocommerce_checkout_before_order_review'); ?>

                            <div id="order_review" class="woocommerce-checkout-review-order">
                                <?php do_action('woocommerce_checkout_order_review'); ?>
                            </div>

                            <?php do_action('woocommerce_checkout_after_order_review'); ?>

                        </div>

                    </div>

                    <?php do_action('woocommerce_checkout_after_customer_details'); ?>

                </div>

            <?php endif; ?>

        </form>

        <?php do_action('woocommerce_after_checkout_form', $checkout); ?>
    </div>
</div>


<style type="text/css">
	span.woocommerce-input-wrapper {
    display: block;
    width: 100%;
}

#billing_first_name, #billing_last_name {
    width: 100%;
}

</style>