<?php
/**
 * Address Form Widget — Render Functions
 *
 * Standalone functions used by the widget's render() method.
 * $widget is the widget instance (replaces $this).
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Get the countries list.
 *
 * @return array<string, string>
 */
function ggmc_address_form_get_countries_list() {
    return [
        'US' => 'United States',
        'CA' => 'Canada',
        'GB' => 'United Kingdom',
        'AU' => 'Australia',
        'DE' => 'Germany',
        'FR' => 'France',
        'IT' => 'Italy',
        'ES' => 'Spain',
        'NL' => 'Netherlands',
        'BE' => 'Belgium',
        'CH' => 'Switzerland',
        'AT' => 'Austria',
        'SE' => 'Sweden',
        'NO' => 'Norway',
        'DK' => 'Denmark',
        'FI' => 'Finland',
        'JP' => 'Japan',
        'KR' => 'South Korea',
        'SG' => 'Singapore',
        'HK' => 'Hong Kong',
        'NZ' => 'New Zealand',
        'MX' => 'Mexico',
        'BR' => 'Brazil',
        'AR' => 'Argentina',
        'CL' => 'Chile',
        'IN' => 'India',
        'CN' => 'China',
        'RU' => 'Russia',
        'ZA' => 'South Africa',
        'EG' => 'Egypt',
        'NG' => 'Nigeria',
        'KE' => 'Kenya',
        'MA' => 'Morocco',
        'TN' => 'Tunisia',
        'GH' => 'Ghana',
        'UG' => 'Uganda',
        'TZ' => 'Tanzania',
        'ZW' => 'Zimbabwe',
        'BW' => 'Botswana',
        'ZM' => 'Zambia',
        'MW' => 'Malawi',
        'MZ' => 'Mozambique',
        'AO' => 'Angola',
        'NA' => 'Namibia',
        'SZ' => 'Eswatini',
        'LS' => 'Lesotho',
    ];
}

/**
 * Main render entry point.
 *
 * @param \Elementor\Widget_Base $widget The widget instance.
 */
function ggmc_address_form_render( $widget ) {
    $settings  = $widget->get_settings_for_display();
    $countries = ggmc_address_form_get_countries_list();
    $title_tag = isset( $settings['title_tag'] ) ? $settings['title_tag'] : 'h3';
    ?>
    <div class="ggmc-address-form-wrapper">
        <!-- Hidden fields for JS -->
        <input type="hidden" id="address_id">
        <input type="hidden" id="address_type">

        <!-- Title with dynamic text -->
        <<?php echo esc_attr( $title_tag ); ?> id="modal-title" class="ggmc-address-form-title"><?php echo esc_html( $settings['form_title'] ); ?></<?php echo esc_attr( $title_tag ); ?>>
        <p id="address-type-display" class="ggmc-address-type-display"></p>

        <!-- Address Label -->
        <?php if ( $settings['show_address_label'] === 'yes' ) : ?>
        <div class="ggmc-form-field">
            <label for="address_label"><?php esc_html_e( 'Address Label', 'ggm-commerce' ); ?></label>
            <input type="text" id="address_label" placeholder="<?php esc_attr_e( 'e.g., Home, Office', 'ggm-commerce' ); ?>">
        </div>
        <?php endif; ?>

        <!-- Name Fields Row -->
        <div class="ggmc-form-row">
            <div class="ggmc-form-field ggmc-form-field-half">
                <label for="first_name"><?php esc_html_e( 'First Name', 'ggm-commerce' ); ?></label>
                <input type="text" id="first_name">
            </div>
            <div class="ggmc-form-field ggmc-form-field-half">
                <label for="last_name"><?php esc_html_e( 'Last Name', 'ggm-commerce' ); ?></label>
                <input type="text" id="last_name">
            </div>
        </div>

        <!-- Company -->
        <?php if ( $settings['show_company'] === 'yes' ) : ?>
        <div class="ggmc-form-field">
            <label for="company"><?php esc_html_e( 'Company', 'ggm-commerce' ); ?></label>
            <input type="text" id="company">
        </div>
        <?php endif; ?>

        <!-- Address Line 1 -->
        <div class="ggmc-form-field">
            <label for="address_1"><?php esc_html_e( 'Address Line 1', 'ggm-commerce' ); ?> <span class="required">*</span></label>
            <input type="text" id="address_1" required>
        </div>

        <!-- Address Line 2 -->
        <div class="ggmc-form-field">
            <label for="address_2"><?php esc_html_e( 'Address Line 2', 'ggm-commerce' ); ?></label>
            <input type="text" id="address_2">
        </div>

        <!-- City, State, Postcode Row -->
        <div class="ggmc-form-row">
            <div class="ggmc-form-field ggmc-form-field-third">
                <label for="city"><?php esc_html_e( 'City', 'ggm-commerce' ); ?> <span class="required">*</span></label>
                <input type="text" id="city" required>
            </div>
            <div class="ggmc-form-field ggmc-form-field-third">
                <label for="state"><?php esc_html_e( 'State / Province / Region', 'ggm-commerce' ); ?></label>
                <input type="text" id="state">
            </div>
            <div class="ggmc-form-field ggmc-form-field-third">
                <label for="postcode"><?php esc_html_e( 'Postcode', 'ggm-commerce' ); ?> <span class="required">*</span></label>
                <input type="text" id="postcode" required>
            </div>
        </div>

        <!-- Country -->
        <div class="ggmc-form-field">
            <label for="country"><?php esc_html_e( 'Country', 'ggm-commerce' ); ?> <span class="required">*</span></label>
            <select id="country" required>
                <option value=""><?php esc_html_e( 'Select Country', 'ggm-commerce' ); ?></option>
                <?php foreach ( $countries as $code => $name ) : ?>
                    <option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $name ); ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <!-- Phone -->
        <div class="ggmc-form-field">
            <label for="phone"><?php esc_html_e( 'Phone', 'ggm-commerce' ); ?></label>
            <input type="tel" id="phone">
        </div>

        <!-- Default Checkbox -->
        <div class="ggmc-form-field">
            <label class="ggmc-checkbox-label">
                <input type="checkbox" id="is_default">
                <?php esc_html_e( 'Set as default address', 'ggm-commerce' ); ?>
            </label>
        </div>

        <!-- Buttons -->
        <div class="ggmc-form-buttons">
            <button type="button" id="btn-cancel-address" class="ggmc-form-button secondary"><?php esc_html_e( 'Cancel', 'ggm-commerce' ); ?></button>
            <button type="button" id="btn-save-address" class="ggmc-form-button primary"><?php esc_html_e( 'Save Address', 'ggm-commerce' ); ?></button>
        </div>
    </div>
    <?php
}
