<?php
/*
Template Name: Login
*/

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

global $campoal_options;

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

if (is_user_logged_in()) {
    if (!empty($login_referer)) {
        $redirect_url = is_numeric($login_referer) ? get_permalink($login_referer) : esc_html($login_referer);
        wp_redirect($redirect_url);
        exit();
    }

    $account_page = isset($campoal_options['conikal_page_user_account_field']) ? $campoal_options['conikal_page_user_account_field'] : 0;
    if ($account_page != 0) {
        $redirect_url = get_permalink($account_page);
    } else {
        $redirect_url = home_url('/');
    }

    wp_redirect($redirect_url);
}

get_header();

$logo = isset($campoal_options['conikal_logo_field']['url']) ? $campoal_options['conikal_logo_field']['url'] : '';

$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 : '';
$reset_pass_link = $login_page ? add_query_arg(array('action' => 'reset'), $login_page) : '';
?>

<div class="login-page" id="wrapper">
    <div class="ui container mobile-full">
        <div class="ui hidden divider"></div>
        <div class="ui basic center aligned very padded segment">
            <a class="logo fixed-logo" href="<?php echo esc_url(home_url('/')); ?>" data-bjax>
                <?php
                if($logo != '') { ?>
                    <span>
                        <img src="<?php echo esc_url($logo) ?>" alt="<?php echo esc_attr(get_bloginfo('name')) ?>">
                    </span>
                <?php } else { ?>
                    <span>
                        <div class="ui header"><?php echo esc_html(get_bloginfo('name')) ?></div>
                    </span>
                <?php } ?>
            </a>
        </div>
        <div class="ui hidden divider"></div>
        <div class="ui centered grid login-section mobile-full">
            <div class="sixteen wide mobile eleven wide tablet seven wide computer column mobile-full">
                <div class="ui raised padded segment login-card">
                    <input type="hidden" name="login_page_link" id="login_page_link" value="<?php echo esc_attr($login_page_link) ?>">
                    <input type="hidden" name="reset_pass_link" id="reset_pass_link" value="<?php echo esc_attr($reset_pass_link) ?>">
                    <?php if ($action == 'signup') : ?>
                        <?php get_template_part( 'templates/form/form-signup' );  ?>
                    <?php endif; ?>

                    <?php if ($action == 'signin') : ?> 
                        <?php get_template_part( 'templates/form/form-signin' );  ?>
                    <?php endif; ?>

                    <?php if ($action == 'forgot') : ?>
                        <?php get_template_part( 'templates/form/form-forgot-password' );  ?>
                    <?php endif; ?>

                    <?php if ($action == 'reset') : ?>
                        <?php get_template_part( 'templates/form/form-reset-password' );  ?>
                    <?php endif; ?>

                    <?php if ($action == 'two_factor') : ?>
                        <?php get_template_part( 'templates/form/form-two-factor' );  ?>
                    <?php endif; ?>

                    <?php get_template_part( 'templates/form/form-social-login' );  ?>
                </div>

                <?php get_template_part( 'templates/form/form-footer-link' );  ?>

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

<?php get_footer(); ?>