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

global $campoal_options;
global $post;

$citation_instant_add   = isset($campoal_options['conikal_citation_instant_add_field']) ? $campoal_options['conikal_citation_instant_add_field'] : 'none';
$receive_update         = isset($campoal_options['conikal_receive_update_field']) ? $campoal_options['conikal_receive_update_field'] : 'checked';
$sign_anonymous         = isset($campoal_options['conikal_sign_anonymous_field']) ? $campoal_options['conikal_sign_anonymous_field'] : 'checked';
$condition_text_position = isset($campoal_options['conikal_condition_text_position_field']) ? $campoal_options['conikal_condition_text_position_field'] : 'desktop';
$condition_text         = isset($campoal_options['conikal_condition_text_field']) ? $campoal_options['conikal_condition_text_field'] : '';
$two_input_name         = isset($campoal_options['conikal_two_input_name_field']) ? $campoal_options['conikal_two_input_name_field'] : 0;
$recaptcha_sign         = isset($campoal_options['conikal_google_recaptcha_sign_field']) ? $campoal_options['conikal_google_recaptcha_sign_field'] : 1;
$reason_sign_box        = isset($campoal_options['conikal_reason_sign_box_field']) ? $campoal_options['conikal_reason_sign_box_field'] : 0;
$mobile_sign_style      = isset($campoal_options['conikal_mobile_sign_style_field']) ? $campoal_options['conikal_mobile_sign_style_field'] : 'button';
$mobile_progress_bar    = isset($campoal_options['conikal_mobile_progress_bar_field']) ? $campoal_options['conikal_mobile_progress_bar_field'] : 'sidebar';
$sign_show_all          = isset($campoal_options['conikal_sign_show_all_field']) ? $campoal_options['conikal_sign_show_all_field'] : 0;
$guest_location         = isset($campoal_options['conikal_guest_location_field']) ? $campoal_options['conikal_guest_location_field'] : 1;
$guest_phone            = isset($campoal_options['conikal_guest_phone_field']) ? $campoal_options['conikal_guest_phone_field'] : 0;
$guest_zip              = isset($campoal_options['conikal_guest_zip_field']) ? $campoal_options['conikal_guest_zip_field'] : 0;
$guest_zip_r            = isset($campoal_options['conikal_guest_zip_r_field']) ? $campoal_options['conikal_guest_zip_r_field'] : 0;
$guest_country          = isset($campoal_options['conikal_guest_country_field']) ? $campoal_options['conikal_guest_country_field'] : 0;
$guest_country_r        = isset($campoal_options['conikal_guest_country_r_field']) ? $campoal_options['conikal_guest_country_r_field'] : 0;
$guest_location_r       = isset($campoal_options['conikal_guest_location_r_field']) ? $campoal_options['conikal_guest_location_r_field'] : 0;
$guest_phone_r          = isset($campoal_options['conikal_guest_phone_r_field']) ? $campoal_options['conikal_guest_phone_r_field'] : 0;

$post_type = get_post_type($post->ID);
if ($post_type != 'petition') {
    $petition_id = get_post_meta($post->ID, $post_type . '_post_id', true);
} else {
    $petition_id = $post->ID;
}

// get custom sign button label
$sign_button_text        = conikal_custom_sign_label_button($petition_id);

// get petition citation status
$citation = conikal_citation_status($petition_id);

// get location of user by GeoIp
$geoip_info = function_exists('conikal_client_geoip_info') ? conikal_client_geoip_info() : false;
?>
<?php if ( !wp_is_mobile() ) { ?>
    <div class="respon-message signMessage<?php echo esc_attr($petition_id) ?>" id="signMessage<?php echo esc_attr($petition_id) ?>"></div>
    <form class="ui form guest-sign" id="guestSignForm<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>">
        <?php if ($two_input_name != 0) : ?>
        <div class="two fields">
            <div class="field">
                <div class="ui right large input">
                    <input type="text" name="firstnameSign" id="firstnameSign<?php echo esc_attr($petition_id) ?>" class="firstnameSign" placeholder="<?php esc_attr_e('First Name', 'campoal') ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                </div>
            </div>
            <div class="field">
                <div class="ui right icon large input">
                    <input type="text" name="lastnameSign" id="lastnameSign<?php echo esc_attr($petition_id) ?>" class="lastnameSign" placeholder="<?php esc_attr_e('Last Name', 'campoal') ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                    <i class="user icon"></i>
                </div>
            </div>
        </div>
        <?php endif; ?>
        <?php if ($two_input_name == 0) : ?>
        <div class="field">
        	<div class="ui right icon large input">
        <?php endif; ?>
            <input 
                type="<?php echo esc_attr($two_input_name == 0 ? 'text' : 'hidden') ?>" 
                name="nameSign" 
                id="nameSign"
                <?php if ($two_input_name == 0) : ?>
                placeholder="<?php echo esc_attr__('Full name', 'campoal') ?>" 
                <?php endif; ?>>
        <?php if ($two_input_name == 0) : ?>
                <i class="user icon"></i>
            </div>
        </div>
        <?php endif; ?>
        <div class="required field">
            <div class="ui right icon large input">
                <input type="text" name="emailSign" id="emailSign" placeholder="<?php esc_attr_e('Email', 'campoal') ?>">
                <i class="mail icon"></i>
            </div>
        </div>

        <?php if ($guest_location == 1) : ?>
        <div class="field<?php echo esc_attr($guest_location_r == 1 ? ' required' : '') ?>">
            <div class="ui right icon large input">
                <input type="text" name="addressSign" id="addressSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('Address', 'campoal') ?>">
                <i class="map marker icon"></i>
            </div>
        </div>
        <?php endif; ?>

        <?php if ($guest_zip == 1) : ?>
        <div class="field<?php echo esc_attr($guest_zip_r == 1 ? ' required' : '') ?>">
            <div class="ui right icon large input">
                <input type="text" name="zipSign" id="zipSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('Zip code', 'campoal') ?>">
                <i class="barcode icon"></i>
            </div>
        </div>
        <?php endif; ?>
        
        <input type="hidden" name="passSign" id="passSign" value="<?php echo wp_generate_password(6, false); ?>">
        <input type="hidden" id="citySign<?php echo esc_attr($petition_id) ?>" name="citySign" value="<?php echo esc_attr($geoip_info ? $geoip_info->city->name : '') ?>">
        <input type="hidden" id="stateSign<?php echo esc_attr($petition_id) ?>" name="stateSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->mostSpecificSubdivision->name : '') ?>">
        <input type="hidden" id="neighborhoodSign<?php echo esc_attr($petition_id) ?>" name="neighborhoodSign" value="">
        <?php if ($guest_country == 0) : ?>
            <input type="hidden" id="countrySign<?php echo esc_attr($petition_id) ?>" name="countrySign" value="<?php echo esc_attr($geoip_info ? $geoip_info->country->isoCode : '') ?>">
        <?php endif; ?>
        <input type="hidden" id="latSign<?php echo esc_attr($petition_id) ?>" name="latSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->latitude : '') ?>">
        <input type="hidden" id="lngSign<?php echo esc_attr($petition_id) ?>" name="lngSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->longitude : '') ?>">
        <?php if ($guest_zip == 0) : ?>
            <input type="hidden" id="zipSign<?php echo esc_attr($petition_id) ?>" name="zipSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->continent->geonameId : '') ?>">
        <?php endif; ?>

        <?php if ($guest_country == 1) : ?>
            <div class="field<?php echo esc_attr($guest_country_r == 1 ? ' required' : '') ?>">
                <div class="ui fluid large search selection dropdown country-search" id="country-search<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                    <input type="hidden" name="countrySign" id="countrySign<?php echo esc_attr($petition_id) ?>" 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; ?>

        <?php if ($guest_phone == 1) : ?>
        <div class="field<?php echo esc_attr($guest_phone_r == 1 ? ' required' : '') ?>">
            <div class="ui large right icon left action input">
                <div class="ui search selection dropdown phone-code-dropdown">
                    <input type="hidden" name="phonecodeSign" id="phonecodeSign" 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="phoneSign" class="phoneSign" id="phoneSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('(123) 456-7890', 'campoal') ?>" value="" data-id="<?php echo esc_attr($petition_id) ?>">
            </div>
        </div>
        <?php endif; ?>

        <?php if ($reason_sign_box) : ?>
        <div class="field">
        	<div class="ui pointing below fluid basic large label sign-comment-label">
        		<textarea name="signComment" class="sign-comment" id="signComment" rows="3" placeholder="<?php esc_attr_e('I am signing because...', 'campoal') ?>"></textarea>
        	</div>
    		<div class="ui message sign-option facebook-share-option">
    			<div class="ui toggle checkbox checked">
    				<input type="checkbox" name="fbPublish" class="hidden" checked>
    				<label><i class="facebook icon"></i><?php esc_html_e('Share on Facebook', 'campoal') ?></label>
    			</div>
    		</div>
    	</div>
        <?php endif; ?>
        <?php if ($citation) {
            get_template_part('templates/citation-opinion', 'add'); 
        } else { ?>
        <div class="field">
            <a href="javascript:void(0);" class="ui primary fluid big button submitSign" id="submitSign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('sign') ?><?php echo esc_html($sign_button_text) ?></a>
        </div>
        <?php } ?>
        <?php if ($citation_instant_add != 'none' && $citation) { ?>
            <div class="field">
                <div class="citation-instant ui checkbox <?php echo esc_attr($citation_instant_add == 'checked' ? 'checked' : ''); ?>" >
                  <input type="checkbox" name="citationInstant" <?php echo esc_attr($citation_instant_add == 'checked' ? 'checked' : ''); ?>>
                  <label><?php esc_html_e('Add citation instant on this petition', 'campoal') ?></label>
                </div>
            </div>
        <?php } ?>
        <?php if ($sign_anonymous != 'none') { ?>
            <div class="field">
                <div class="sign-anonymous ui checkbox <?php echo esc_attr($sign_anonymous == 'checked' ? 'checked' : ''); ?>">
                  <input type="checkbox" name="signAnonymous" <?php echo esc_attr($sign_anonymous == 'checked' ? 'checked' : ''); ?>>
                  <label><?php esc_html_e('Display my name on this petition', 'campoal') ?></label>
                </div>
            </div>
        <?php } ?>
        <?php if ($receive_update != 'none') { ?>
            <div class="field">
                <div class="email-notice ui checkbox <?php echo esc_attr($receive_update == 'checked' ? 'checked' : ''); ?>">
                  <input type="checkbox" name="emailNotice" <?php echo esc_attr($receive_update == 'checked' ? 'checked' : ''); ?>>
                  <label><?php esc_html_e('Receive updates from this petition', 'campoal') ?></label>
                </div>
            </div>
        <?php } ?>
        <?php if ($condition_text != '' && in_array($condition_text_position, array('all', 'desktop')) ) { ?>
            <div class="field condition-text">
                <?php echo wp_kses_post($condition_text); ?>
            </div>
        <?php } ?>
        <?php if (conikal_show_google_reCaptcha() && $recaptcha_sign == 1) { ?>
            <?php get_template_part('templates/google', 'recaptcha'); ?>
        <?php } ?>
    </form>

<?php } else { ?>
    <div class="respon-message signMessage<?php echo esc_attr($petition_id) ?>" id="signMessage<?php echo esc_attr($petition_id) ?>"></div>
    <?php if ($mobile_sign_style == 'sidebar' && $sign_show_all == 0) { ?>
     <div class="ui accordion accord-guest-sign" id="accord-guest-sign">
        <div class="title">
            <h4 class="ui header">
            <i class="dropdown icon"></i>
                <?php esc_html_e('Enter your name and email', 'campoal') ?>
            </h4>
        </div>
        <div class="content">
    <?php } ?>
    <form class="ui form guest-sign" id="guestSignForm<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>">
        <?php if ($two_input_name != 0) : ?>
            <div class="two fields">
                <div class="field">
                    <div class="ui right icon large input">
                        <input type="text" name="firstnameSign" class="firstnameSign" id="firstnameSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('First Name', 'campoal') ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                        <i class="user icon"></i>
                    </div>
                </div>
                <div class="field">
                    <div class="ui right icon large input">
                        <input type="text" name="lastnameSign" class="firstnameSign" id="lastnameSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('Last Name', 'campoal') ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                        <i class="user icon"></i>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        <?php if ($two_input_name != 1) : ?>
        <div class="field">
            <div class="ui right icon large input">
        <?php endif; ?>
            <input 
                type="<?php echo esc_attr($two_input_name == 0 ? 'text' : 'hidden') ?>" 
                name="nameSign" 
                id="nameSign"
                <?php if ($two_input_name == 0) : ?>
                placeholder="<?php echo esc_attr__('Full name', 'campoal') ?>" 
                <?php endif; ?>>
        <?php if ($two_input_name != 1) : ?>
                <i class="user icon"></i>
            </div>
        </div>
        <?php endif; ?>
        <div class="required field">
            <div class="ui right icon large input">
                <input type="text" name="emailSign" id="emailSign" placeholder="<?php esc_attr_e('Email', 'campoal') ?>">
                <i class="mail icon"></i>
            </div>
        </div>

        <?php if ($guest_location == 1) : ?>
        <div class="field<?php echo esc_attr($guest_location_r == 1 ? ' required' : '') ?>">
            <div class="ui right icon large input">
                <input type="text" name="addressSign" id="addressSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('Address', 'campoal') ?>">
                <i class="map marker icon"></i>
            </div>
        </div>
        <?php endif; ?>

        <?php if ($guest_zip == 1) : ?>
        <div class="field<?php echo esc_attr($guest_zip_r == 1 ? ' required' : '') ?>">
            <div class="ui right icon large input">
                <input type="text" name="zipSign" id="zipSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('Zip code', 'campoal') ?>">
                <i class="barcode icon"></i>
            </div>
        </div>
        <?php endif; ?>
        
        <input type="hidden" name="passSign" id="passSign" value="<?php echo wp_generate_password(6, false); ?>">
        <input type="hidden" id="citySign<?php echo esc_attr($petition_id) ?>" name="citySign" value="<?php echo esc_attr($geoip_info ? $geoip_info->city->name : '') ?>">
        <input type="hidden" id="stateSign<?php echo esc_attr($petition_id) ?>" name="stateSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->mostSpecificSubdivision->name : '') ?>">
        <input type="hidden" id="neighborhoodSign<?php echo esc_attr($petition_id) ?>" name="neighborhoodSign" value="">
        <?php if ($guest_country == 0) : ?>
            <input type="hidden" id="countrySign" name="countrySign<?php echo esc_attr($petition_id) ?>" value="<?php echo esc_attr($geoip_info ? $geoip_info->country->isoCode : '') ?>">
        <?php endif; ?>
        <input type="hidden" id="latSign<?php echo esc_attr($petition_id) ?>" name="latSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->latitude : '') ?>">
        <input type="hidden" id="lngSign<?php echo esc_attr($petition_id) ?>" name="lngSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->location->longitude : '') ?>">
        <?php if ($guest_zip == 0) : ?>
            <input type="hidden" id="zipSign<?php echo esc_attr($petition_id) ?>" name="zipSign" value="<?php echo esc_attr($geoip_info ? $geoip_info->continent->geonameId : '') ?>">
        <?php endif; ?>

        <?php if ($guest_country == 1) : ?>
            <div class="field<?php echo esc_attr($guest_country_r == 1 ? ' required' : '') ?>">
                <div class="ui fluid large search selection dropdown country-search" id="country-search<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                    <input type="hidden" name="countrySign" id="countrySign<?php echo esc_attr($petition_id) ?>" 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; ?>

        <?php if ($guest_phone == 1) : ?>
        <div class="field<?php echo esc_attr($guest_phone_r == 1 ? ' required' : '') ?>">
            <div class="ui large right icon left action input">
                <div class="ui search selection dropdown phone-code-dropdown">
                    <input type="hidden" name="phonecodeSign" id="phonecodeSign" 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="phoneSign" class="phoneSign" id="phoneSign<?php echo esc_attr($petition_id) ?>" placeholder="<?php esc_attr_e('(123) 456-7890', 'campoal') ?>" value="" data-id="<?php echo esc_attr($petition_id) ?>">
            </div>
        </div>
        <?php endif; ?>

        <?php if ($reason_sign_box) : ?>
        <div class="field">
            <div class="ui pointing below fluid basic large label sign-comment-label">
                <textarea name="signComment" id="signComment" class="sign-comment" rows="3" placeholder="<?php esc_attr_e('I am signing because...', 'campoal') ?>"></textarea>
            </div>
            <div class="ui message sign-option facebook-share-option">
                <div class="fb-publish ui toggle checkbox checked">
                    <input type="checkbox" name="fbPublish" class="hidden" checked>
                    <label><i class="facebook icon"></i><?php esc_html_e('Share on Facebook', 'campoal') ?></label>
                </div>
            </div>
        </div>
        <?php endif; ?>
        <?php if ($citation_instant_add != 'none' && $citation) { ?>
            <div class="field">
                <div class="ui message sign-option">
                    <div class="citation-instant ui toggle checkbox <?php echo esc_attr($citation_instant_add == 'checked' ? 'checked' : ''); ?>">
                      <input type="checkbox" name="citationInstant" <?php echo esc_attr($citation_instant_add == 'checked' ? 'checked' : ''); ?>>
                      <label><?php esc_html_e('Add citation instantly', 'campoal') ?></label>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($sign_anonymous != 'none') { ?>
            <div class="field">
                <div class="ui message sign-option">
                    <div class="sign-anonymous ui toggle checkbox <?php echo esc_attr($sign_anonymous == 'checked' ? 'checked' : ''); ?>" >
                      <input type="checkbox" name="signAnonymous" <?php echo esc_attr($sign_anonymous == 'checked' ? 'checked' : ''); ?>>
                      <label><?php esc_html_e('Display my name', 'campoal') ?></label>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($receive_update != 'none') { ?>
            <div class="field">
                <div class="ui message sign-option">
                    <div class="email-notice ui toggle checkbox <?php echo esc_attr($receive_update == 'checked' ? 'checked' : ''); ?>">
                      <input type="checkbox" name="emailNotice" <?php echo esc_attr($receive_update == 'checked' ? 'checked' : ''); ?>>
                      <label><?php esc_html_e('Receive updates', 'campoal') ?></label>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($mobile_sign_style != 'sidebar') {
        if ($citation) {
            get_template_part('templates/citation-opinion', 'add'); 
        } else { ?>
        <div class="field">
            <a href="javascript:void(0);" class="ui primary fluid big button submitSign" id="submitSign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('sign') ?><?php echo esc_html($sign_button_text) ?></a>
        </div>
        <?php }
        } ?>

        <?php if ($condition_text != '' && in_array($condition_text_position, array('all', 'mobile'))) { ?>
            <div class="field condition-text">
                <?php echo wp_kses_post($condition_text); ?>
            </div>
        <?php } ?>
        <?php if (conikal_show_google_reCaptcha() && $recaptcha_sign == 1) { ?>
            <?php get_template_part('templates/google', 'recaptcha'); ?>
        <?php } ?>
    </form>
    <?php if ($mobile_sign_style == 'sidebar' && $sign_show_all == 0) { ?>
        </div>  
    </div>
    <?php } ?>
    <?php if ($mobile_sign_style == 'sidebar') { ?>
        <div class="ui form citation-sidebar">
            <?php if ($citation) {
                get_template_part('templates/citation-opinion', 'add'); 
            } else { ?>
                <div class="field">
                    <a href="javascript:void(0);" class="ui primary fluid big button submitSign" id="submitSign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('sign') ?><?php echo esc_html($sign_button_text) ?></a>
                </div>
            <?php } ?>
        </div>
    <?php } ?>
<?php } ?>