<?php
/*
Template Name: User Account
*/

/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;
global $wp;

$country_default        = isset($campoal_options['conikal_country_field']) ? $campoal_options['conikal_country_field'] : 0;
$sidebar_position       = isset($campoal_options['conikal_sidebar_field']) ? $campoal_options['conikal_sidebar_field'] : 'right';
$show_bc                = isset($campoal_options['conikal_breadcrumbs_field']) ? $campoal_options['conikal_breadcrumbs_field'] : 0;
$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';
$fb_login               = isset($campoal_options['conikal_fb_login_field']) ? $campoal_options['conikal_fb_login_field'] : 0;
$fb_app_id              = isset($campoal_options['conikal_fb_id_field']) ? $campoal_options['conikal_fb_id_field'] : '';
$google_login           = isset($campoal_options['conikal_google_login_field']) ? $campoal_options['conikal_google_login_field'] : 0;
$google_client_id       = isset($campoal_options['conikal_google_id_field']) ? $campoal_options['conikal_google_id_field'] : '';
$profile_petitioner     = isset($campoal_options['conikal_profile_petitioner_field']) ? $campoal_options['conikal_profile_petitioner_field'] : 1;
$profile_decision_maker = isset($campoal_options['conikal_profile_decision_maker_field']) ? $campoal_options['conikal_profile_decision_maker_field'] : 1;
$profile_organization   = isset($campoal_options['conikal_profile_organization_field']) ? $campoal_options['conikal_profile_organization_field'] : 1;

// 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           = isset($campoal_options['conikal_user_zip_field']) ? $campoal_options['conikal_user_zip_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'),
);

$user_meta              = get_user_meta($current_user->ID);
$up_nickname            = $current_user->user_login;
$up_full_name           = $current_user->display_name;
$up_firstname           = $current_user->user_firstname ;
$up_lastname            = $current_user->user_lastname ;
$up_email               = $current_user->user_email;
$up_bio                 = $current_user->description;
$up_website             = $current_user->user_url;
$up_type                = get_user_meta($current_user->ID, 'user_type', true);
$up_type                = $up_type != '' ? $up_type : 'petitioner';
$up_birthday            = get_user_meta($current_user->ID, 'user_birthday', true);
$up_gender              = get_user_meta($current_user->ID, 'user_gender', true);
$up_address             = get_user_meta($current_user->ID, 'user_address', true);
$up_neighborhood        = get_user_meta($current_user->ID, 'user_neighborhood', true);
$up_state               = get_user_meta($current_user->ID, 'user_state', true);
$up_city                = get_user_meta($current_user->ID, 'user_city', true);
$up_country             = get_user_meta($current_user->ID, 'user_country', true);
$up_lat                 = get_user_meta($current_user->ID, 'user_lat', true);
$up_lng                 = get_user_meta($current_user->ID, 'user_lng', true);
$up_zip                 = get_user_meta($current_user->ID, 'user_zip', true);
$up_phone_code          = get_user_meta($current_user->ID, 'user_phone_code', true);
$up_phone               = get_user_meta($current_user->ID, 'user_phone', true);
$up_organization_name   = get_user_meta($current_user->ID, 'user_organization_name', true);
$facebook_connection    = get_user_meta($current_user->ID, 'facebook_connection', true);
$google_connection      = get_user_meta($current_user->ID, 'google_connection', true);
$account_activation     = get_user_meta($current_user->ID, 'is_activated', true);
$temporary_pass         = get_user_meta($current_user->ID, 'user_temporary_pass', true);

$display_names = array(
    $up_firstname, 
    $up_lastname, 
    $up_firstname . ' ' . $up_lastname, 
    $up_lastname . ' ' . $up_firstname, 
    $up_firstname . ' ' . substr($up_lastname, 0, 1), 
    $up_lastname . ' ' . substr($up_firstname, 0, 1), 
    substr($up_firstname, 0, 1) . ' ' . $up_lastname, 
    substr($up_lastname, 0, 1) . ' ' . $up_firstname,
);

$decision_id = get_user_meta($current_user->ID, 'user_decision', true);
if ($decision_id) {
    $prefix = 'decisionmaker_';

    $decision_status        = get_post_status( $decision_id );
    $decision_title         = wp_get_post_terms( $decision_id, 'decisionmakers_title' );
    $decision_title         = ($decision_title ? $decision_title[0]->name : '');
    $decision_organization  = wp_get_post_terms($decision_id, 'decisionmakers_organization');
    $decision_organization  = ($decision_organization ? $decision_organization[0]->name : '');

    // get indentify
    $indentify_verified         = get_post_meta($decision_id, $prefix . 'indentify_verified', true);
    $indentify_type             = get_post_meta($decision_id, $prefix . 'indentify_type', true);
    $indentify_name             = get_post_meta($decision_id, $prefix . 'indentify_name', true);
    $indentify_id               = get_post_meta($decision_id, $prefix . 'indentify_id', true);
    $indentify_country_issue    = get_post_meta($decision_id, $prefix . 'indentify_country_issue', true);
    $indentify_frontside_photo  = get_post_meta($decision_id, $prefix . 'indentify_frontside_photo', true);
    $indentify_backside_photo   = get_post_meta($decision_id, $prefix . 'indentify_backside_photo', true);
    $indentify_other_photo      = get_post_meta($decision_id, $prefix . 'indentify_other_photo', true);
} else {
    $decision_status        = '';
    $decision_title         = '';
    $decision_organization  = '';
}

$decision_title_option = get_terms( 'decisionmakers_title', array(
    'hide_empty' => false,
) );

$indentify_types = array(
    'national_id'    => esc_html__( 'National ID', 'campoal' ),
    'driver_license' => esc_html__( 'Driver\'s license', 'campoal' ),
    'passport'       => esc_html__( 'Passport', 'campoal' ),
);

$tab = isset($_GET['tab']) ? $_GET['tab'] : 'profile';
$upload_tab = isset($_GET['upload']) ? $_GET['upload'] : 'front';

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

<form class="ui form update-profile">

    <?php if ($profile_petitioner || $profile_decision_maker || $profile_organization) : ?>
    <div class="field">
        <div class="radio-flex">
            <div class="radio-container">
            <div class="radio-options typeUser">
            <?php if ($profile_petitioner) : ?>
                <input class="radio-input" id="petitioner" type="radio" name="typeUser" value="petitioner" <?php echo esc_attr(isset($up_type) && $up_type == 'petitioner' ? 'checked' : ''); ?>/>
                <label class="radio-label" for="petitioner">
                    <?php echo conikal_custom_icon('profile_activist') ?>
                    <?php esc_html_e('Activist', 'campoal') ?>
                </label>
            <?php endif; ?>
            <?php if ($profile_organization) : ?>
                <input class="radio-input" id="organization" type="radio" name="typeUser" value="organization" <?php echo esc_attr(isset($up_type) && $up_type == 'organization' ? 'checked' : ''); ?>/>
                <label class="radio-label" for="organization">
                    <?php echo conikal_custom_icon('profile_organization') ?>
                    <?php esc_html_e('Organization', 'campoal') ?>
                </label>
            <?php endif; ?>
            <?php if ($profile_decision_maker) : ?>
                <input class="radio-input" id="decisioner" type="radio" name="typeUser" value="decisioner" <?php echo esc_attr(isset($up_type) && $up_type == 'decisioner' ? 'checked' : ''); ?>/>
                <label class="radio-label" for="decisioner">
                    <?php echo conikal_custom_icon('profile_legislator') ?>
                    <?php esc_html_e('Legislator', 'campoal') ?>
                </label>
            <?php endif; ?>
            </div>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <input type="hidden" name="decision_id" id="decision_id" value="<?php echo esc_attr(isset($decision_id) && $decision_id != '' ? $decision_id : ''); ?>">

    <div class="equal width fields">
        <div class="field required">
            <label><?php esc_html_e('First Name', 'campoal'); ?></label>
            <div class="ui large right icon input">
                <input class="text-input" name="firstnameUser" type="text" id="firstnameUser" value="<?php echo esc_attr(isset($up_firstname) ? esc_attr($up_firstname) : ''); ?>" />
            </div>
        </div>
        <div class="field required">
            <label><?php esc_html_e('Last Name', 'campoal'); ?></label>
            <div class="ui large right icon input">
                <input class="text-input" name="lastnameUser" type="text" id="lastnameUser" value="<?php echo esc_attr(isset($up_lastname) ? esc_attr($up_lastname) : ''); ?>" />
            </div>
        </div>
    </div>

    <div class="equal width fields">
        <div class="field required">
            <label><?php esc_html_e('Display name', 'campoal'); ?></label>
            <select class="ui dropdown large input display-name" id="nameUser" name="nameUser">
                <?php foreach ($display_names as $name) { ?>
                    <option value="<?php echo esc_attr($name) ?>"<?php echo esc_attr($up_full_name == $name ? ' selected' : ''); ?>><?php echo esc_html($name) ?></option>
                <?php } ?>
            </select>
        </div>
        <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" id="genderUser" name="genderUser">
                <?php foreach ($user_gender_types as $gender => $gender_checked) {
                    if ($gender_checked == '1') { ?>
                    <option value="<?php echo esc_attr($gender) ?>"<?php echo esc_attr($up_gender == $gender ? ' selected' : '') ?>>
                        <?php echo esc_html($user_gender_label[$gender]) ?>
                    </option>
                <?php }
                } ?>
            </select>
        </div>
    </div>

    <?php if ($profile_decision_maker) : ?>
    <div class="equal width fields ui <?php echo esc_attr(isset($decision_status) && $decision_status == 'publish' ? '' : 'warning'); ?> message <?php echo esc_attr(isset($up_type) && $up_type == 'decisioner' ? 'display flex' : 'display none'); ?>" id="decision-fields">
        <div class="field required">
            <label><?php esc_html_e('Organization', 'campoal'); ?></label>
            <div class="ui fluid multiple search selection dropdown" id="organization-search">
                <input name="organizationUser" id="organizationUser" type="hidden">
                <i class="dropdown icon"></i>
                <div class="default text"><?php esc_html_e('Organization', 'campoal') ?></div>
                <div class="menu">
                </div>
            </div>
            <?php if(isset($decision_organization)) { ?>
                <input type="hidden" id="decision_organization_name" value="<?php echo esc_attr(isset($decision_organization) ? esc_attr($decision_organization) : ''); ?>">
            <?php } ?>
        </div>
        <div class="field required">
            <label><?php esc_html_e('Position', 'campoal'); ?></label>
            <div class="ui fluid multiple search selection dropdown decision-title-select" id="position-search">
                <input name="titleUser" id="titleUser" type="hidden">
                <i class="dropdown icon"></i>
                <div class="default text"><?php esc_html_e('Position', 'campoal') ?></div>
                <div class="menu">
                </div>
            </div>
            <?php if(isset($decision_title)) { ?>
                <input type="hidden" id="decision_title_id" value="<?php echo esc_attr(isset($decision_title) ? esc_attr($decision_title) : ''); ?>">
            <?php } ?>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($profile_organization) : ?>
    <div class="field required ui message <?php echo esc_attr(isset($up_type) && $up_type == 'organization' ? 'display block' : 'display none'); ?>" id="organization-fields">
        <label><?php esc_html_e('Organization name', 'campoal'); ?></label>
        <div class="ui large right icon input">
            <input class="text-input" name="organizationName" type="text" id="organizationName" value="<?php echo esc_attr(isset($up_organization_name) ? $up_organization_name : ''); ?>" placeholder="<?php esc_attr_e('Enter organization name', 'campoal') ?>">
        </div>
    </div>
    <?php endif; ?>

    <div class="field required <?php echo esc_attr($account_activation == 1 ? '' : 'warning') ?>">
        <label><?php esc_html_e('E-mail', 'campoal'); ?></label>
        <div class="ui large right icon input">
            <i class="<?php echo esc_attr($account_activation == 1 ? 'checkmark' : 'envelope') ?> icon"></i>
            <input type="text" id="emailUser" name="emailUser" placeholder="<?php esc_attr_e('Enter your e-mail address', 'campoal'); ?>" value="<?php echo esc_attr(isset($up_email) ? esc_attr($up_email) : ''); ?>">
            <input type="hidden" id="oldemailUser" name="oldemailUser" value="<?php echo esc_attr(isset($up_email) ? esc_attr($up_email) : ''); ?>">
        </div>
    </div>
    <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="birthdayUser" id="birthdayUser" placeholder="<?php esc_attr_e('Choose your birthday', 'campoal'); ?>" value="<?php echo esc_attr(isset($up_birthday) ? esc_attr($up_birthday) : ''); ?>">
            </div>
        </div>
    </div>
    <div class="field<?php echo esc_attr($user_location_r == 1 ? ' required' : '') ?>">
        <label><?php esc_html_e('Location', 'campoal') ?></label>
        <div class="ui large right icon input">
            <i class="map marker icon"></i>
            <input type="text" name="addressUser" id="addressUser" placeholder="<?php esc_attr_e('Enter your address', 'campoal'); ?>" value="<?php echo esc_attr(!empty($up_address) ? esc_attr($up_address) : ''); ?>"></input>
        </div>
        <input type="hidden" name="neighborhoodUser" id="neighborhoodUser" value="<?php echo esc_attr(!empty($up_neighborhood) ? $up_neighborhood : ''); ?>"></input>
        <input type="hidden" name="stateUser" id="stateUser" value="<?php echo esc_attr(!empty($up_state) ? $up_state : ($geoip_info ? $geoip_info->mostSpecificSubdivision->name : '')); ?>"></input>
        <input type="hidden" name="cityUser" id="cityUser" value="<?php echo esc_attr(!empty($up_city) ? $up_city : ($geoip_info ? $geoip_info->city->name : '')); ?>"></input>
        <?php if ($user_country == 0) : ?>
            <input type="hidden" name="countryUser" id="countryUser" value="<?php echo esc_attr(!empty($up_country) ? $up_country : ($geoip_info ? $geoip_info->country->isoCode : '')); ?>"></input>
        <?php endif; ?>
        <input type="hidden" name="latUser" id="latUser" value="<?php echo esc_attr(!empty($up_lat) ? $up_lat : ($geoip_info ? $geoip_info->location->latitude : '')); ?>"></input>
        <input type="hidden" name="lngUser" id="lngUser" value="<?php echo esc_attr(!empty($up_lng) ? $up_lng : ($geoip_info ? $geoip_info->location->longitude : '')); ?>"></input>
        <?php if ($user_zip == 0) : ?>
            <input type="hidden" name="zipUser" id="zipUser" value="<?php echo esc_attr(!empty($up_zip) ? $up_zip : ($geoip_info ? $geoip_info->continent->geonameId : '')); ?>"></input>
        <?php endif; ?>
    </div>

    <?php if ($user_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 large right icon input">
                <i class="barcode icon"></i>
                <input type="text" name="zipUser" id="zipUser" placeholder="<?php esc_attr_e('Enter zip code', 'campoal'); ?>" value="<?php echo esc_attr(!empty($up_zip) ? esc_attr($up_zip) : ''); ?>"></input>
            </div>
        </div>
    <?php endif; ?>

    <?php if ($user_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="countryUser" id="countryUser" 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="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(!empty($up_phone_code) ? $up_phone_code : ($geoip_info ? $geoip_info->extra->tel : '1')); ?>">
                <?php if (empty($up_phone_code) && empty($geoip_info)) { ?>
                    <div class="default text"><?php esc_html_e('Code', 'campoal') ?></div>
                <?php } else { ?>
                    <div class="text"><?php echo esc_html(!empty($up_phone_code) ? '+' . $up_phone_code : ($geoip_info ? $geoip_info->extra->tel : '+1')); ?></div>
                <?php } ?>
                <i class="dropdown icon"></i>
            </div>
            <i class="phone icon"></i>
            <input type="<?php echo esc_attr(!empty($up_phone_code) ? 'text' : 'number') ?>" name="phoneUser" class="phoneUser" id="phoneUser" placeholder="<?php esc_attr_e('(123) 456-7890', 'campoal') ?>" value="<?php echo esc_attr(isset($up_phone) ? esc_attr($up_phone) : ''); ?>" data-id="">
        </div>
    </div>
    <div class="field">
        <label><?php esc_html_e('Website', 'campoal') ?></label>
        <div class="ui large right icon input">
            <i class="linkify icon"></i>
            <input type="text" name="websiteUser" id="websiteUser" placeholder="<?php esc_attr_e('http://', 'campoal') ?>" value="<?php echo esc_attr(isset($up_website) ? esc_url($up_website) : ''); ?>">
        </div>
    </div>
    <div class="field">
        <label><?php esc_html_e('Biographical', 'campoal') ?></label>
        <div class="ui large right icon input">
            <textarea id="bioUser" rows="5" maxlength="300" placeholder="<?php esc_attr_e('Introduce about yourself', 'campoal'); ?>"><?php echo isset($up_bio) ? $up_bio : ''; ?></textarea>
        </div>
    </div>
    <div class="field required">
        <label><?php esc_html_e('URL profile', 'campoal'); ?></label>
        <div class="ui labeled large input">
            <div class="ui label"><?php echo '/author/' ?></div>
            <input type="text" id="nicknameUser" name="nicknameUser" placeholder="<?php esc_attr_e('Enter your nickname', 'campoal'); ?>" value="<?php echo esc_attr(isset($up_nickname) ? esc_attr($up_nickname) : ''); ?>" disabled>
        </div>
    </div>
</form>

<!-- save button -->
<div class="ui basic vertical right aligned segment">
    <button class="ui right labeled icon large primary button updateProfileBtn"><i class="save icon"></i><?php esc_html_e('Save Profile', 'campoal'); ?></button>
</div>