<?php 
    if ( ! defined( 'ABSPATH' ) ) exit;
    $country_data = json_decode(krfsapp_file_get_contents(KRFSAPP_PLUGIN_DIR . '/assets/country-codes.json'), true);

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

    $default_country_code = "";
    $is_plugin_active = true;
    if(!$is_plugin_active) {
        $default_country_code = explode("_",krfsapp_determine_locale())[1];
        $vis_ip_details = krfsapp_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 && isset($user_info['country']) && $country['name'] == $user_info['country']) || ($country['code'] == $default_country_code)) {
            $selected_country = $country;
            $is_active = 'active';
        }

        $dial_codes_html .= '<div class="number-item '.esc_attr($is_active).'" data-code="'.esc_attr($country['dial_code']).'" data-idx="'.esc_attr($idx).'">'.esc_html($country['dial_code']).' - '.esc_html($country['name']).'</div>';
        $countries_html .= '<div class="number-item '.esc_attr($is_active).'" data-code="'.esc_attr($country['name']).'" data-idx="'.esc_attr($idx).'">'.esc_html($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="krfs_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>Need Help? Schedule your <a target="_blank" href="https://meet.brevo.com/keyideas/jewelry-experts"><span>free consultation </span><img src="<?php echo esc_url(KRFSAPP_PLUGIN_DIR); ?>/assets/images/share.svg" alt=""></a></p>
        </div>
        <p class="pera-head">Website: <?php echo esc_url(site_url());?></p>
        <form action="" method="post" id="krfs_plugin_activation_form">
            <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 $is_plugin_active && isset($user_info['name']) ? esc_attr($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 $is_plugin_active && isset($user_info['company_name']) ? esc_attr($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 $is_plugin_active && isset($user_info['email']) ? esc_attr($user_info['email']) : ''; ?>">
                </div>

                <div class="form-element">
                    <label class="whitespace-nowrap">Country&#xFE61;</label>
                    <div class="input-drop-main krfs_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="krfs-country-scroll">
                                <div id="country-list" class="krfs_country_dropdown_content"><div id="krfs-no-results" >No results found</div><?php echo wp_kses_post($countries_html); ?></div>
                                </div>
                            </div>
                            <img class="arrowIcon" src="<?php echo esc_url(KRFSAPP_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 krfs_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 krfs-phone-dropdown-border krfs_phone_number_dropdown_content" data-type="phone">
                                <?php echo wp_kses_post($dial_codes_html); ?>
                            </div>
                            <img class="arrowIcon" src="<?php echo esc_url(KRFSAPP_PLUGIN_DIR); ?>/assets/images/down-arrow.png" width="15px" height="" alt="">
                        </div>
                        <input class="krfs-input-number" type="number" value="<?php echo $is_plugin_active && isset($user_info['phone']) ? esc_attr(str_replace($selected_country['dial_code'].'-','',$user_info['phone'])) : ''; ?>" id="temp_phone_krfs_act">
                        <input type="hidden" name="phone" value="<?php echo $is_plugin_active && isset($user_info['phone']) ? esc_attr($user_info['phone']) : ''; ?>">
                    </div>
                    <div class="phone-error-container"></div>
                </div>
                <!-- <div class="form-element">
                    <label class="whitespace-nowrap website-label">Website URL</label>
                    <input type="text" name="website_url" value="<?php echo esc_url(site_url());?>" disabled>
                </div> -->
            </div>
            <p class="krfs-registration-form-para" >Please provide your valid details to receive enhanced support.</p>
            <button type="submit" class="save-btn key-btn text-white focus:outline-none focus:shadow-outline">Save</button>
            <input type="hidden" name="krfs_plugin_activation" value="true" />
            <input type="hidden" name="dial_code" value="<?php echo esc_attr($selected_country['dial_code']); ?>" />
            <input type="hidden" name="country" value="<?php echo esc_attr($selected_country['name']); ?>" />
            <input type="hidden" name="phone" value="" />
            <input type="hidden" name="hidden_site_url" value="<?php echo esc_url(site_url());?>" />
            <?php wp_nonce_field('plugin_activation_tab_action', 'csrf_activation_nonce'); ?>
        </form>
    </div>
</div>
<div id="krfs_activation_form_overlay" class="overlay"></div>
