<?php

class Mainwp_WPvivid_Extension_LoginPage
{
    public function __construct()
    {

    }

    public function render()
    {
        $login_options = Mainwp_WPvivid_Extension_Option::get_instance()->get_login_addon();
        ?>
        <div id="mwp_wpvivid_pro_notice">
            <?php
            if(isset($_REQUEST['login_success']))
            {
                ?>
                <script>
                location.href='<?php echo apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page=Extensions-Wpvivid-Backup-Mainwp&tab=dashboard&sync=1'?>';
                </script>
                <?php
                return;
            }
            else if(isset($_REQUEST['active_success']))
            {
                _e('<div class="notice notice-success is-dismissible inline" style="margin: 0; padding-top: 10px;"><p>Your license has been activated successfully.</p></div>');
            }
            else if(isset($_REQUEST['error']))
            {
                _e('<div class="notice notice-error inline is-dismissible" style="margin: 0; padding-top: 10px;"><p>'.$_REQUEST['error'].'</p></div>');
                if($login_options !== false){
                    if(isset($login_options['connect_server_last_error']) && !empty($login_options['connect_server_last_error'])){
                        unset($login_options['connect_server_last_error']);
                        Mainwp_WPvivid_Extension_Option::get_instance()->update_login_addon($login_options);
                    }
                }
            }
            else{
                if($login_options !== false){
                    if(isset($login_options['connect_server_last_error']) && !empty($login_options['connect_server_last_error'])){
                        $last_error = $login_options['connect_server_last_error'];
                        if(is_string($last_error)) {
                            _e('<div class="notice notice-error is-dismissible" style="margin: 0; padding-top: 10px;"><p>' . $last_error . '</p></div>');
                        }
                        unset($login_options['connect_server_last_error']);
                        Mainwp_WPvivid_Extension_Option::get_instance()->update_login_addon($login_options);
                    }
                }
            }
            ?>
        </div>
        <div style="margin-top: 10px;">
            This tab allows you to login to your WPvivid Backup Pro account. Once logged in, you can install , claim, and update WPvivid Backup Pro on child sites in bulk from MainWP dashboard.
        </div>
        <?php
        if(isset($_REQUEST['switch'])) {
            $this->output_login_page();
        }
        else{
            if(isset($login_options['wpvivid_pro_login_cache']['pro_user']) && !empty($login_options['wpvivid_pro_login_cache']['pro_user'])){
                $this->output_user_info_page();
            }
            else{
                $this->output_login_page();
            }
        }
        ?>
        <script>
            function mwp_wpvivid_display_pro_notice(notice_type, notice_message){
                if(notice_type === 'Success'){
                    var div = "<div class='notice notice-success is-dismissible inline'><p>" + notice_message + "</p>" +
                        "<button type='button' class='notice-dismiss' onclick='mwp_click_dismiss_pro_notice(this);'>" +
                        "<span class='screen-reader-text'>Dismiss this notice.</span>" +
                        "</button>" +
                        "</div>";
                }
                else{
                    var div = "<div class=\"notice notice-error inline\"><p>Error: " + notice_message + "</p></div>";
                }
                jQuery('#mwp_wpvivid_pro_notice').show();
                jQuery('#mwp_wpvivid_pro_notice').html(div);
            }

            function mwp_click_dismiss_pro_notice(obj){
                jQuery(obj).parent().remove();
            }

            function mwp_wpvivid_lock_login(lock,error='') {
                if(lock) {
                    jQuery('#mwp_wpvivid_active_btn').css({'pointer-events': 'none', 'opacity': '0.4'});
                    jQuery('#mwp_wpvivid_login_box_progress').show();
                    jQuery('#mwp_wpvivid_login_box_progress').addClass('is-active');
                    jQuery('#mwp_wpvivid_connect_result').hide();
                    jQuery('#mwp_wpvivid_connect_result').html('');
                }
                else {
                    jQuery('#mwp_wpvivid_log_progress_text').html('');
                    jQuery('#mwp_wpvivid_login_box_progress').hide();
                    jQuery('#mwp_wpvivid_login_box_progress').removeClass('is-active');
                    jQuery('#mwp_wpvivid_active_btn').css({'pointer-events': 'auto', 'opacity': '1'});
                    if(error!=='') {
                        mwp_wpvivid_display_pro_notice('Error', error);
                    }
                }
            }

            function mwp_wpvivid_login_progress(log) {
                jQuery('#mwp_wpvivid_log_progress_text').html(log);
            }

            function mwp_wpvivid_is_running(is_running){
                if(is_running){
                    jQuery('.mwp-wpvivid-login-btn').css({'pointer-events': 'none', 'opacity': '0.4'});
                }
                else{
                    jQuery('.mwp-wpvivid-login-btn').css({'pointer-events': 'auto', 'opacity': '1'});
                }
            }

            jQuery('#mwp_wpvivid_active_btn').click(function(){
                jQuery('#mwp_wpvivid_pro_notice').hide();
                mwp_wpvivid_connect_account_and_active();
            });

            function mwp_wpvivid_connect_account_and_active(){
                var user = jQuery('#mwp_wpvivid_account_user').val();
                var pass = jQuery('#mwp_wpvivid_account_pw').val();
                var ajax_data = {
                    'action': 'mwp_wpvivid_connect_account',
                    'user': user,
                    'password': pass,
                    'auto_login': false
                };
                mwp_wpvivid_lock_login(true);
                mwp_wpvivid_login_progress('Logging in to your WPvivid Backup Pro account');
                mwp_wpvivid_post_request(ajax_data, function(data) {
                    var jsonarray = jQuery.parseJSON(data);
                    if (jsonarray.result === 'success') {
                        if(jsonarray.pro_user===1) {
                            mwp_wpvivid_login_progress('You have successfully logged in');
                            location.href='<?php echo apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page=Extensions-Wpvivid-Backup-Mainwp&tab=login&login_success'?>';
                        }
                        else {
                            mwp_wpvivid_login_progress('You have successfully logged in');
                            location.href='<?php echo apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page=Extensions-Wpvivid-Backup-Mainwp&tab=login&login_success'?>';
                        }
                    }
                    else {
                        mwp_wpvivid_lock_login(false,jsonarray.error);
                    }
                }, function(XMLHttpRequest, textStatus, errorThrown) {
                    var error_message = mwp_wpvivid_output_ajaxerror('connect server and active', textStatus, errorThrown);
                    mwp_wpvivid_lock_login(false,error_message);
                });
            }
        </script>
        <?php
    }

    public function output_login_page(){
        $membership = 'N/A';
        $expire = 'N/A';
        $current_version = 'N/A';
        ?>
        <div style="margin-top: 10px;">
            <div class="postbox" id="mwp_wpvivid_login_box">
                <table class="wp-list-table widefat plugins" style="width: 100%;">
                    <tbody>
                    <tr>
                        <td class="column-primary" style="margin: 10px;">
                            <div>
                                <img src="<?php echo esc_url(MAINWP_WPVIVID_EXTENSION_PLUGIN_URL.'/admin/images/pro.png'); ?>" style="width:100px; height:100px;">
                            </div>
                        </td>
                        <td class="column-description desc">
                            <div>
                                <form action="">
                                    <div style="margin-top: 10px; margin-bottom: 15px;"><input type="text" class="regular-text" id="mwp_wpvivid_account_user" placeholder="Email" autocomplete="off" required /></div>
                                    <div style="margin-bottom: 15px;"><input type="password" class="regular-text" id="mwp_wpvivid_account_pw" placeholder="Password" autocomplete="new-password" required /></div>
                                    <div style="margin-bottom: 10px; float: left; margin-left: 0; margin-right: 10px;"><input class="button-primary mwp-wpvivid-login-btn" id="mwp_wpvivid_active_btn" type="button" value="Login"/></div>
                                    <div class="spinner" id="mwp_wpvivid_login_box_progress" style="float: left; margin-left: 0; margin-right: 10px;"></div>
                                    <div style="float: left; margin-top: 4px;"><span id="mwp_wpvivid_log_progress_text"></span></div>
                                    <div style="clear: both;"></div>
                                </form>
                                <div id="mwp_wpvivid_connect_result" style="display: none; margin-bottom: 10px;"></div>
                                <div style="background-color:#f5f5f5; padding:5px;">
                                    <i>Tips: The email and password are the same as your registration on <a href="https://pro.wpvivid.com" target="_blank">pro.wpvivid.com</a>. You have to activate the installation to get update and support</i>
                                </div>
                            </div>
                        </td>
                        <td class="column-description desc">
                            <div style="padding-left: 5px;">
                                <div style="margin-bottom: 10px;"><strong>WPvivid Backup Pro</strong></div>
                                <div style="margin-bottom: 10px;"><i>WPvivid Backup Pro works on top of free version, providing full flexibility and a series of robust customization options to meet your diverse website backup and migration needs.</i></div>
                            </div>
                            <div style="border-left:4px solid #00a0d2;padding-left:10px;">
                                <div>
                                    <div style="margin-right: 5px; float: left; margin-bottom: 5px;">Current Version: </div><div style="float: left; margin-bottom: 5px;"><?php echo $current_version; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                                <div>
                                    <div style="margin-right: 5px; float: left; margin-bottom: 5px;">Membership Plan: </div><div style="float: left; margin-bottom: 5px;"><?php echo $membership; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                                <div>
                                    <div style="margin-right: 5px; float: left;">Expiration Date: </div><div style="float: left;"><?php echo $expire; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <?php
    }

    public function output_user_info_page(){
        $login_options = Mainwp_WPvivid_Extension_Option::get_instance()->get_login_addon();
        $addons_cache = isset($login_options['wpvivid_pro_login_cache']) ? $login_options['wpvivid_pro_login_cache'] : false;
        $membership = 'N/A';
        $expire = 'N/A';
        $current_version = 'N/A';

        if(isset($addons_cache['pro']['version'])){
            $current_version = $addons_cache['pro']['version'];
        }
        if(isset($addons_cache['memberships'])) {
            foreach ($addons_cache['memberships'] as $member => $value) {
                $membership = $value['name'];
                if($membership === 'WPvivid Backup Pro - Beta') {
                    $membership = 'WPvivid Backup Pro (Beta)';
                    $expire = $value['end_date'];
                    if($expire === 0){
                        $expire = '12-31-2019';
                    }
                }
                else {
                    $expire = $value['end_date'];
                    if($expire === 0){
                        $expire = 'N/A';
                    }
                }

            }
        }
        ?>
        <div style="margin-top: 10px;">
            <div class="postbox">
                <table class="wp-list-table widefat plugins" style="width: 100%;">
                    <tbody>
                    <tr>
                        <td class="column-primary" style="margin: 10px;">
                            <div>
                                <img src="<?php echo esc_url(MAINWP_WPVIVID_EXTENSION_PLUGIN_URL.'/admin/images/pro.png'); ?>" style="width:100px; height:100px;">
                            </div>
                        </td>
                        <td class="column-description desc">
                            <div style="margin-top: 10px;">
                                <?php
                                if($addons_cache['pro_user']==1) {
                                    ?>
                                    <div style="float: left; margin-bottom: 10px; margin-right: 10px;">You are WPvivid Pro user<a href="https://pro.wpvivid.com" target="_blank"> Go to My Account</a></div>
                                    <div style="float: left; margin-bottom: 10px;">
                                        <a href="#" class="mwp-wpvivid-login-btn" id="mwp_wpvivid_change_btn">Switch Accounts</a>
                                    </div>
                                    <div style="clear: both;"></div>

                                    <div style="margin-bottom: 10px; float: left;">
                                        <input id="mwp_wpvivid_switch_dashboard_page" type="button" class="ui green mini button mwp-wpvivid-login-btn" value="Install & Claim WPvivid Backup Pro on Child Sites">
                                    </div>
                                    <?php
                                }
                                else {
                                    ?>
                                    <div style="margin-bottom: 10px; margin-right: 10px; float: left;">You are not a Pro user yet, <a href="https://pro.wpvivid.com/pricing" target="_blank">get it now!</a></div>
                                    <div style="float: left; margin-bottom: 10px;">
                                        <a href="#" class="mwp-wpvivid-login-btn" id="mwp_wpvivid_change_btn">Switch Accounts</a>
                                    </div>
                                    <div style="clear: both;"></div>
                                    <?php
                                }
                                ?>
                                <div class="spinner" id="mwp_wpvivid_user_info_box_progress" style="float: left;"></div>
                                <div style="float: left; margin-top: 4px;"><span id="mwp_wpvivid_user_info_log_progress_text"></span></div>
                                <div style="clear: both;"></div>
                                <div id="mwp_wpvivid_action_result" style="display: none; margin-bottom: 10px;"></div>
                                <div style="background-color:#f5f5f5; padding:5px;">
                                    <i>Tips: The email and password are the same as your registration on <a href="https://pro.wpvivid.com" target="_blank">pro.wpvivid.com</a>.</i>
                                </div>
                            </div>
                        </td>
                        <td class="column-description desc">
                            <div style="padding-left: 5px;">
                                <div style="margin-bottom: 10px;"><strong>WPvivid Backup Pro</strong></div>
                                <div style="margin-bottom: 10px;"><i>WPvivid Backup Pro works on top of free version, providing full flexibility and a series of robust customization options to meet your diverse website backup and migration needs.</i></div>
                            </div>
                            <div style="border-left:4px solid #00a0d2;padding-left:10px;">
                                <div>
                                    <div style="margin-right: 5px; float: left; margin-bottom: 5px;">Current Version: </div><div style="float: left; margin-bottom: 5px;"><?php echo $current_version; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                                <div>
                                    <div style="margin-right: 5px; float: left; margin-bottom: 5px;">Membership Plan: </div><div style="float: left; margin-bottom: 5px;"><?php echo $membership; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                                <div>
                                    <div style="margin-right: 5px; float: left;">Expiration Date: </div><div style="float: left;"><?php echo $expire; ?></div>
                                    <div style="clear: both;"></div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <script>
            jQuery( '#mwp_wpvivid_login_table' ).DataTable( {
                "columnDefs": [ { "orderable": false, "targets": "no-sort" } ],
                "order": [ [ 1, "asc" ] ],
                "language": { "emptyTable": "No websites were found with the WPvivid Backup plugin installed." },
                "drawCallback": function( settings ) {
                    jQuery('#mwp_wpvivid_login_table .ui.checkbox').checkbox();
                    jQuery( '#mwp_wpvivid_login_table .ui.dropdown').dropdown();
                },
            } );

            function mwp_wpvivid_user_info_lock_login(lock, error = ''){
                if(lock){
                    jQuery('.mwp-wpvivid-login-btn').css({'pointer-events': 'none', 'opacity': '0.4'});
                    jQuery('#mwp_wpvivid_user_info_box_progress').show();
                    jQuery('#mwp_wpvivid_user_info_box_progress').addClass('is-active');
                    jQuery('#mwp_wpvivid_action_result').hide();
                    jQuery('#mwp_wpvivid_action_result').html('');
                }
                else{
                    jQuery('.mwp-wpvivid-login-btn').css({'pointer-events': 'auto', 'opacity': '1'});
                    jQuery('#mwp_wpvivid_user_info_box_progress').hide();
                    jQuery('#mwp_wpvivid_user_info_box_progress').removeClass('is-active');
                    jQuery('#mwp_wpvivid_user_info_log_progress_text').html('');
                    if(error!=='') {
                        mwp_wpvivid_display_pro_notice('Error', error);
                    }
                }
            }

            function mwp_wpvivid_user_info_progress(log) {
                jQuery('#mwp_wpvivid_user_info_log_progress_text').html(log);
            }

            jQuery('#mwp_wpvivid_change_btn').on('click', function(){
                var descript = 'Are you sure switch accounts?';
                var ret = confirm(descript);
                if(ret === true) {
                    location.href='<?php echo apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page=Extensions-Wpvivid-Backup-Mainwp&tab=login&switch=1'?>';
                }
            });

            jQuery('#mwp_wpvivid_switch_dashboard_page').on('click', function(){
                location.href='<?php echo apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page=Extensions-Wpvivid-Backup-Mainwp&tab=dashboard'; ?>';
            });
        </script>
        <?php
    }
}