<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;

// Check user is logged in and redirect to user page
$login_referer          = isset($_GET['referer']) ? $_GET['referer'] : '';

$recaptcha_login        = isset($campoal_options['conikal_google_recaptcha_login_field']) ? $campoal_options['conikal_google_recaptcha_login_field'] : 1;
$condition_text         = isset($campoal_options['conikal_condition_text_field']) ? $campoal_options['conikal_condition_text_field'] : '';
$choose_username        = isset($campoal_options['conikal_choose_username_field']) ? $campoal_options['conikal_choose_username_field'] : 0;
$two_input_name         = isset($campoal_options['conikal_two_input_name_field']) ? $campoal_options['conikal_two_input_name_field'] : 0;
$repeat_password        = isset($campoal_options['conikal_repeat_password_field']) ? $campoal_options['conikal_repeat_password_field'] : 0;
$registration_gender    = isset($campoal_options['conikal_registration_gender_field']) ? $campoal_options['conikal_registration_gender_field'] : 0;
$registration_location  = isset($campoal_options['conikal_registration_location_field']) ? $campoal_options['conikal_registration_location_field'] : 0;
$registration_zip       = isset($campoal_options['conikal_registration_zip_field']) ? $campoal_options['conikal_registration_zip_field'] : 0;
$registration_country   = isset($campoal_options['conikal_registration_country_field']) ? $campoal_options['conikal_registration_country_field'] : 0;
$registration_phone     = isset($campoal_options['conikal_registration_phone_field']) ? $campoal_options['conikal_registration_phone_field'] : 0;
$registration_birthday  = isset($campoal_options['conikal_registration_birthday_field']) ? $campoal_options['conikal_registration_birthday_field'] : 0;
$signup_newsletters     = isset($campoal_options['conikal_signup_newsletters_field']) ? $campoal_options['conikal_signup_newsletters_field'] : 'checked';

// user requirement options
$user_gender_r          = isset($campoal_options['conikal_user_gender_r_field']) ? $campoal_options['conikal_user_gender_r_field'] : 0;
$user_birthday_r        = isset($campoal_options['conikal_user_birthday_r_field']) ? $campoal_options['conikal_user_birthday_r_field'] : 0;
$user_location_r        = isset($campoal_options['conikal_user_location_r_field']) ? $campoal_options['conikal_user_location_r_field'] : 0;
$user_zip_r 		    = isset($campoal_options['conikal_user_zip_r_field']) ? $campoal_options['conikal_user_zip_r_field'] : 0;
$user_country           = isset($campoal_options['conikal_user_country_field']) ? $campoal_options['conikal_user_country_field'] : 1;
$user_country_r         = isset($campoal_options['conikal_user_country_r_field']) ? $campoal_options['conikal_user_country_r_field'] : 0;
$user_phone_r           = isset($campoal_options['conikal_user_phone_r_field']) ? $campoal_options['conikal_user_phone_r_field'] : 0;
$user_gender_types      = isset($campoal_options['conikal_user_gender_types_field']) ? $campoal_options['conikal_user_gender_types_field'] : array();
$user_gender_label      = array(
    'male'          => esc_html__('Male', 'campoal'),
    'female'        => esc_html__('Female', 'campoal'),
    'transgender'   => esc_html__('Transgender', 'campoal'),
    'gender_neutra' => esc_html__('Gender neutra', 'campoal'),
    'non_binary'    => esc_html__('Non binary', 'campoal'),
    'agender'       => esc_html__('Agender', 'campoal'),
    'pangender'     => esc_html__('Pangender', 'campoal'),
    'genderqueer'   => esc_html__('Genderqueer', 'campoal'),
    'two_spirit'    => esc_html__('Two-spirit', 'campoal'),
    'third_gender'  => esc_html__('Third gender', 'campoal'),
    'two_spirit'    => esc_html__('Two-spirit', 'campoal'),
);

$login_page = isset($campoal_options['conikal_page_login_field']) ? $campoal_options['conikal_page_login_field'] : 0;
$login_page = get_permalink($login_page);
$login_page_link = $login_page ? $login_page : '';
$signin_link = $login_page ? add_query_arg(array('action' => 'signin', 'referer' => $login_referer), $login_page_link) : '';
$signup_link = $login_page ? add_query_arg(array('action' => 'signup', 'referer' => $login_referer), $login_page_link) : '';

// get location of user by GeoIp
$geoip_info = function_exists('conikal_client_geoip_info') ? conikal_client_geoip_info() : false;
?>

<h2 class="ui centered header"><?php esc_html_e('Sign Up', 'campoal') ?></h2>
<div class="respon-message" id="signinMessage"></div>

<form class="ui form sign-up" id="userSignupForm">
    <?php if ($two_input_name != 0) : ?>
    <div class="two fields">
        <div class="required large field">
            <label><?php esc_html_e('First Name', 'campoal') ?></label>
            <div class="ui right icon large input">
                <input type="text" name="firstnameSignup" id="firstnameSignup" placeholder="<?php esc_attr_e('First Name', 'campoal') ?>">
                <?php if (wp_is_mobile()) { ?>
                    <i class="user icon"></i>
                <?php } ?>
            </div>
        </div>
        <div class="required large field">
            <label><?php esc_html_e('Last Name', 'campoal') ?></label>
            <div class="ui right icon large input">
                <input type="text" name="lastnameSignup" id="lastnameSignup" placeholder="<?php esc_attr_e('Last Name', 'campoal') ?>">
                <i class="user icon"></i>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($two_input_name != 1) : ?>
    <div class="required field">
        <label><?php esc_html_e('Full Name', 'campoal') ?></label>
        <div class="ui right icon large input">
    <?php endif; ?>
        <input 
            type="<?php echo esc_attr($two_input_name == 0 ? 'text' : 'hidden') ?>" 
            name="nameSignup" 
            id="nameSignup"
            <?php if ($two_input_name == 0) : ?>
            placeholder="<?php echo esc_attr__('Enter your fullname', 'campoal') ?>" 
            <?php endif; ?>>
    <?php if ($two_input_name != 1) : ?>
            <i class="user icon"></i>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($choose_username == 1) : ?>
    <div class="required field">
        <label><?php esc_html_e('Username', 'campoal') ?></label>
        <div class="ui right large icon input">
            <input type="text" name="usernameSignup" id="usernameSignup" placeholder="<?php esc_attr_e('Enter your username', 'campoal') ?>">
            <i class="user icon"></i>
        </div>
    </div>
    <?php endif; ?>

    <div class="required field">
        <label><?php esc_html_e('Email', 'campoal') ?></label>
        <div class="ui right icon large input">
            <input type="text" name="emailSignup" id="emailSignup" placeholder="<?php esc_attr_e('Enter your e-mail', 'campoal') ?>">
            <i class="mail icon"></i>
        </div>
    </div>

    <?php if ($registration_phone == 1) : ?>
    <div class="field<?php echo esc_attr($user_phone_r == 1 ? ' required' : '') ?>">
        <label><?php esc_html_e('Phone Number', 'campoal') ?></label>
        <div class="ui large right icon left action input">
            <div class="ui search selection dropdown phone-code-dropdown">
                <input type="hidden" name="phone_code" id="phoneCode" value="<?php echo esc_attr($geoip_info ? $geoip_info->extra->tel : '1'); ?>">
                <div class="<?php echo esc_attr(empty($geoip_info) ? 'default ' : '') ?>text"><?php echo esc_html($geoip_info ? $geoip_info->extra->tel : __('+1', 'campoal')); ?></div>
                <i class="dropdown icon"></i>
            </div>
            <i class="phone icon"></i>
            <input type="number" name="phoneSignup" id="phoneSignup" placeholder="<?php esc_attr_e('122-332-234', 'campoal') ?>" value="">
        </div>
    </div>
    <?php endif; ?>

    <?php if ($registration_gender == 1) : ?>
        <div class="field<?php echo esc_attr($user_gender_r == 1 ? ' required' : '') ?>">
            <label><?php esc_html_e('Gender', 'campoal') ?></label>
            <select class="ui dropdown large input display-name" id="genderSignup" name="genderSignup">
                <option value="" selected><?php esc_html_e('Select a gender', 'campoal') ?></option>
                <?php foreach ($user_gender_types as $gender => $gender_checked) {
                    if ($gender_checked == '1') { ?>
                    <option value="<?php echo esc_attr($gender) ?>">
                        <?php echo esc_html($user_gender_label[$gender]) ?>
                    </option>
                <?php }
                } ?>
            </select>
        </div>
    <?php endif; ?>

    <?php if ($registration_birthday == 1) : ?>
    <div class="field<?php echo esc_attr($user_birthday_r == 1 ? ' required' : '') ?>">
        <label><?php esc_html_e('Birthday', 'campoal') ?></label>
        <div class="ui calendar" id="up_birthday">
            <div class="ui large right icon input">
                <i class="calendar icon"></i>
                <input type="text" name="birthdaySignup" id="birthdaySignup" placeholder="<?php esc_attr_e('Choose your birthday', 'campoal'); ?>" value="">
            </div>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($registration_location == 1) : ?>
    <div class="required field">
        <label><?php esc_html_e('Location', 'campoal') ?></label>
        <div class="ui right large icon input">
            <input type="text" name="addressSignup" id="addressSignup" placeholder="<?php esc_attr_e('Enter your address', 'campoal') ?>">
            <i class="map marker icon"></i>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($registration_zip == 1) : ?>
    <div class="field<?php echo esc_attr($user_zip_r == 1 ? ' required' : '') ?>">
        <label><?php esc_html_e('Zip code', 'campoal') ?></label>
        <div class="ui right large icon input">
            <input type="text" name="zipSignup" id="zipSignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->continent->geonameId : '') ?>" placeholder="<?php esc_attr_e('Enter your zip code', 'campoal') ?>">
            <i class="barcode icon"></i>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($registration_country == 1) : ?>
    <div class="field<?php echo esc_attr($user_country_r == 1 ? ' required' : '') ?>">
        <label><?php esc_html_e('Country', 'campoal') ?></label>
        <div class="ui fluid large search selection dropdown country-search">
            <input type="hidden" name="countrySignup" id="countrySignup" value="<?php echo esc_attr(!empty($up_country) ? $up_country : ($geoip_info ? $geoip_info->country->isoCode : '')); ?>">
            <i class="dropdown icon"></i>
            <div class="default text"><?php esc_html_e('Select country', 'campoal') ?></div>
            <div class="menu">
                <?php echo conikal_country_list_menu_html() ?>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <div class="required field">
        <label><?php esc_html_e('Password', 'campoal') ?></label>
        <div class="ui right icon large input">
            <input type="password" name="passSignup" class="password-strength" id="passSignup" placeholder="<?php esc_attr_e('Enter your password', 'campoal') ?>">
            <i class="toggle-password eye link icon"></i>
        </div>
    </div>
    <?php get_template_part('templates/form/password-strength') ?>
    
    <?php if ($repeat_password == 1) : ?>
    <div class="required field">
        <label><?php esc_html_e('Repeat Password', 'campoal') ?></label>
        <div class="ui right large icon input">
            <input type="password" name="repassSignup" id="repassSignup" placeholder="<?php esc_attr_e('Repeat your password', 'campoal') ?>">
            <i class="toggle-password eye link icon"></i>
        </div>
    </div>
    <?php endif; ?>

    <?php if (conikal_show_google_reCaptcha() && $recaptcha_login) { ?>
        <?php get_template_part('templates/google', 'recaptcha'); ?>
    <?php } ?>
    <div class="ui grid">
        <div class="eight wide column">
            <a class="ui primary large button password-strength-btn" id="submitSignup"><?php esc_html_e('Sign Up', 'campoal') ?></a>
        </div>
        <div class="eight wide right aligned column">
            <a href="<?php echo esc_url($signin_link) ?>">
                <?php esc_html_e('Sign In', 'campoal') ?>
            </a>
        </div>
    </div>

    <input type="hidden" id="citySignup" name="citySignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->city->name : '') ?>">
    <input type="hidden" id="stateSignup" name="stateSignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->mostSpecificSubdivision->name : '') ?>">
    <input type="hidden" id="neighborhoodSignup" name="neighborhoodSignup" value="">
    <input type="hidden" id="countrySignup" name="countrySignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->country->isoCode : '') ?>">
    <input type="hidden" id="latSignup" name="latSignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->latitude : '') ?>">
    <input type="hidden" id="lngSignup" name="lngSignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->longitude : '') ?>">
    <?php if ($registration_zip == 0) : ?>
        <input type="hidden" id="zipSignup" name="zipSignup" value="<?php echo esc_attr($geoip_info ? $geoip_info->continent->geonameId : '') ?>">
    <?php endif; ?>

    <input type="hidden" name="usernameSignin" id="usernameSignin" value="">
    <input type="hidden" name="passwordSignin" id="passwordSignin" value="">
    <input type="hidden" name="formReferrent" id="formReferrent" value="page">

    <div class="ui hidden divider"></div>

    <?php if ($signup_newsletters != 'none') : ?>
        <div class="field">
            <div class="ui checkbox<?php echo esc_attr($signup_newsletters == 'checked' ? ' checked' : '') ?>">
                <input type="checkbox" tabindex="0" value="1" name="signupNewsletter" id="signupNewsletter" class="hidden" <?php echo esc_attr($signup_newsletters == 'checked' ? 'checked' : '') ?>>
                <label><?php esc_html_e('Sign up for newsletters', 'campoal') ?></label>
            </div>
        </div>
    <?php endif; ?>

    <?php if ($condition_text != '') { ?>
        <div class="field">
            <?php echo wp_kses_post($condition_text); ?>
        </div>
    <?php } ?>
    <?php wp_nonce_field('signin_ajax_nonce', 'securitySignin', true); ?>
    <?php wp_nonce_field('signup_ajax_nonce', 'securitySignup', true); ?>
</form>