<?php 
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

    $country_data = json_decode(kirb_file_get_contents(KIRB_PLUGIN_DIR . '/assets/country-codes.json'), true);

    // Auto detect country code by visitor ip
    function kirb_getVisitorIpDetails() { 
        $ip = "";
        if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
            $ip = sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR']));
        }else if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP']));
        } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR']));
        }
        if(!empty($ip)) {
            $ipdat = @json_decode(kirb_file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip), true);
            return $ipdat;
        }
        return "";
    }

    $default_country_code = "";
    if(!$is_plugin_active) {
        $default_country_code = explode("_",determine_locale())[1];
        $vis_ip_details = kirb_getVisitorIpDetails();
        if(!empty($vis_ip_details) && isset($vis_ip_details['geoplugin_countryCode']) && $vis_ip_details['geoplugin_countryCode']) {
            $default_country_code = $vis_ip_details['geoplugin_countryCode'];
        }
    }

    $selected_country = '';
    $dial_codes_html = '';
    $countries_html = '';
    $idx = 0;
    foreach($country_data as $country){
        $is_active = '';

        if (($is_plugin_active && $country['name'] == esc_html($user_info['country'])) || ($country['code'] == $default_country_code)) {
            $selected_country = $country;
            $is_active = 'active';
        }

        $dial_codes_html .= '<div class="number-item '.$is_active.'" data-code="'.$country['dial_code'].'" data-idx="'.$idx.'">'.$country['dial_code'].' - '.$country['name'].'</div>';
        $countries_html .= '<div class="number-item '.$is_active.'" data-code="'.$country['name'].'" data-idx="'.$idx.'">'.$country['name'].'</div>';
        $idx++;
    }

    if(empty($selected_country)) {
        $selected_country = $country_data[0];
    }
?>
<div id="rightSidebar" class="sidebar contact-form-slider">
    <a href="javascript:void(0)" class="close-btn" onclick="kirb_activation_sidebar_toggle(false);">&times;</a>
    <div class='add-collection-slider contact-section'>
        <h2 class='mb-5'><?php echo esc_html($is_plugin_active ? "Edit Customer Profile" : "Customer Registration")?></h2>
        <div class="link-section">
            <p><?php echo esc_html('Need Help?'); ?> Schedule your <a target="_blank" href="https://meet.brevo.com/keyideas/jewelry-experts"><span>free consultation </span><img src="<?php echo esc_html(KIRB_PLUGIN_DIR) ?>/assets/images/share.svg" alt=""></a></p>
        </div>
        <p class="pera-head">Website: <?php echo esc_html(site_url());?></p>
        <form action="" method="post" id="kirb_plugin_activation_form" class="kirb-registration-form-styling">
            <div class="inner-form">
                <div class="form-element">
                    <label class="whitespace-nowrap">Name&#xFE61;</label>
                    <input type="text" placeholder="John Doe" name="name" value="<?php echo esc_html($is_plugin_active ? $user_info['name'] : ''); ?>">
                </div>
                <div class="form-element">
                    <label class="whitespace-nowrap">Company&#xFE61;</label>
                    <input type="text" placeholder="John's Jewelry" name="company_name" value="<?php echo esc_html($is_plugin_active ? $user_info['company_name'] : ''); ?>" >
                </div>
                <div class="form-element">
                    <label class="whitespace-nowrap">Email&#xFE61;</label>
                    <input type="text" placeholder="johndoe@gmail.com " name="email" value="<?php echo esc_html($is_plugin_active ? $user_info['email'] : ''); ?>">
                </div>

                <div class="form-element">
                    <label class="whitespace-nowrap">Country&#xFE61;</label>
                    <div class="input-drop-main kirb_country_dropdown">
                        <div class="dropdown-number dropdown-country" data-id="country">
                            <span class="dropdown-selected" ><?php echo esc_html($selected_country['name']) ?></span>           
                            <div class="number-dropdown-content">
                                <div class = "input-group mb-3">
                                    <input type="text" class="form-control border-size" id="country-search" placeholder="Search" aria-label="Search Country" aria-describedby="country-search" autocomplete="false">
                                </div>
                                <div class="kirb-country-scroll">
                                <div id="country-list"  class="kirb_country_dropdown_content"><div id="kirb-no-results" >No results found</div><?php echo wp_kses_post($countries_html); ?></div>

                                </div>
                            </div>
                              <img class="arrowIcon" src="<?php echo esc_html(KIRB_PLUGIN_DIR) ?>/assets/images/down-arrow.png" width="15px" height="" alt="">
                        </div>
                    </div>
                    <div class="country-error-container"></div>
                </div>

                <div class="form-element">
                    <label class="whitespace-nowrap">Phone&#xFE61;</label>
                    <div class="input-drop-main kirb_phone_number_dropdown">
                        <div class="dropdown-number" data-id="dial_code">
                            <span class="dropdown-selected"><?php echo esc_html($selected_country['dial_code']); ?></span>
                            <div class="number-dropdown-content kirb-phone-dropdown-border kirb_phone_number_dropdown_content" data-type="phone">
                                <?php echo wp_kses_post($dial_codes_html); ?>
                            </div>
                              <img class="arrowIcon" src="<?php echo esc_html(KIRB_PLUGIN_DIR) ?>/assets/images/down-arrow.png" width="15px" height="" alt="">
                        </div>
                        <input class="kirb-input-number" type="number" value="<?php echo esc_html($is_plugin_active ? (str_contains($user_info['phone'],"-") ? explode("-",$user_info['phone'])[1] : preg_replace('/[^0-9]/', '', $user_info['phone'])) : ''); ?>" id="temp_phone_kirb_act">
                        <input type="hidden" name="phone" value="<?php echo esc_html($is_plugin_active ? $user_info['phone'] : ''); ?>">
                    </div>
                    <div class="phone-error-container"></div>
                </div>
                <p class="kirb-valid-detail-para">Please provide your valid details to receive enhanced support.</p>
            </div>
            <button type="submit" class="save-btn key-btn text-white focus:outline-none focus:shadow-outline">Save</button>
            <input type="hidden" name="kirb_plugin_activation" value="true" />
            <input type="hidden" name="dial_code" value="<?php echo esc_html($selected_country['dial_code']) ?>" />
            <input type="hidden" name="country" value="<?php echo esc_html($selected_country['name']) ?>" />
            <?php wp_nonce_field('plugin_activation_tab_action', 'csrf_activation_nonce'); ?>
        </form>
    </div>
</div>
<div id="kirb_activation_form_overlay" class="overlay"></div>