<?php

if(!defined('ABSPATH')) die();

?>
<div class="w3eden">
    <div id="wpdmlogin" <?php if(wpdm_query_var('action') == 'lostpassword') echo 'class="lostpass"'; ?>>
        <?php if(isset($params['logo']) && $params['logo'] != ''){ ?>
            <div class="text-center wpdmlogin-logo">
                <a href="<?php echo home_url('/'); ?>"><img alt="Logo" src="<?php echo esc_attr($params['logo']);?>" /></a>
            </div>
        <?php } ?>






        <?php do_action("wpdm_before_login_form"); ?>


        <form name="loginform" id="loginform" action="" method="post" class="login-form" >

            <input type="hidden" name="permalink" value="<?php the_permalink(); ?>" />

            <div id="__signin_msg"><?php
                $wpdm_signup_success = \WPDM\__\Session::get('__wpdm_signup_success');
                if(isset($_GET['signedup'])){
                    if($wpdm_signup_success == '') $wpdm_signup_success = apply_filters("wpdm_signup_success", __("Your account has been created successfully.", "download-manager"));
                    ?>
                    <div class="alert alert-success dismis-on-click">
                        <?php echo  $wpdm_signup_success; ?>
                    </div>
                    <?php
                }
                ?></div>


            <?php
            if(isset($params['note_before']) && $params['note_before'] !== '') {  ?>
                <div class="alert alert-info alert-note-before mb-3" >
                    <?php echo  esc_attr($params['note_before']); ?>
                </div>
            <?php } ?>

            <?php echo  $this->formFields($params); ?>


            <?php  if(isset($params['note_after']) && $params['note_before'] !== '') {  ?>
                <div class="alert alert-info alter-note-after mb-3" >
                    <?php echo  esc_attr($params['note_after']); ?>
                </div>
            <?php } ?>

            <?php do_action("wpdm_login_form"); ?>
            <?php do_action("login_form"); ?>

            <div class="row login-form-meta-text text-muted mb-3" style="font-size: 10px">
                <div class="col-md-5"><label><input class="wpdm-checkbox" name="rememberme" type="checkbox" id="rememberme" value="forever" /><?php _e( "Remember Me" , "download-manager" ); ?></label></div>
                <div class="col-md-7 text-right"><a class="color-blue" href="<?php echo wpdm_lostpassword_url(); ?>"><?php _e( "Forgot Password?" , "download-manager" ); ?></a></div>
            </div>

            <div class="row">
                <div class="col-md-12"><button type="submit" name="wp-submit" id="loginform-submit" class="btn btn-block btn-primary btn-lg"><i class="fas fa-user-shield"></i> &nbsp;<?php _e( "Login" , "download-manager" ); ?></button></div>
                <?php if(isset($regurl) && $regurl != ''){ ?>
                    <div class="col-md-12"><br/><a href="<?php echo esc_attr($regurl); ?>" class="btn btn-block btn-link btn-xs wpdm-reg-link  color-primary"><?php _e( "Don't have an account yet?" , "download-manager" ); ?> <i class="fas fa-user-plus"></i> <?php _e( "Register Now" , "download-manager" ); ?></a></div>
                <?php } ?>
            </div>


            <input type="hidden" name="redirect_to" value="<?php echo $log_redirect; ?>" />



        </form>



        <?php do_action("wpdm_after_login_form"); ?>

    </div>


</div>
<script>
    jQuery(function ($) {
        <?php if(!isset($params['form_submit_handler']) || $params['form_submit_handler'] !== false){ ?>
        var llbl = $('#loginform-submit').html();
        $('#loginform').submit(function () {
            $('#loginform-submit').html("<i class='fa fa-spin fa-sync'></i> <?php _e( "Logging In..." , "download-manager" ); ?>").attr('disabled', 'disabled');
            WPDM.blockUI('#loginform');
            $(this).ajaxSubmit({
                error: function(error) {
                    WPDM.unblockUI('#loginform');
                    $('#loginform').prepend("<div class='alert alert-danger' data-title='<?php _e( "LOGIN FAILED!" , "download-manager" ); ?>'>"+error.responseJSON.message+"</div>");
                    $('#loginform-submit').html(llbl).removeAttr('disabled');
                    <?php if((int)get_option('__wpdm_recaptcha_loginform', 0) === 1 && get_option('_wpdm_recaptcha_site_key') != ''){ ?>
                    try {
                        if(typeof grecaptcha !== "undefined")
                            grecaptcha.reset();
                    } catch (e) {

                    }
                    <?php } ?>
                },
                success: async function (res) {
                    WPDM.unblockUI('#loginform');
                    if (!res.success) {
                        $('form .alert-danger').hide();
                        $('#loginform').prepend("<div class='alert alert-danger' data-title='<?php _e( "LOGIN FAILED!" , "download-manager" ); ?>'>"+res.message+"</div>");
                        $('#loginform-submit').html(llbl).removeAttr('disabled');
                        <?php if((int)get_option('__wpdm_recaptcha_loginform', 0) === 1 && get_option('_wpdm_recaptcha_site_key') != ''){ ?>
                        try {
                            if(typeof grecaptcha !== "undefined")
                                grecaptcha.reset();
                        } catch (e) {

                        }
                        <?php } ?>
                    } else {
                        await WPDM.doAction("wpdm_user_login", res);
                        $('#loginform-submit').html("<i class='fa fa-sun fa-spider'></i> "+res.message);
                        $('#loginform-submit').html(WPDM.html("i", "", "fa fa-sun fa-spider") + " " + res.message);
                        setTimeout(function () {
                            location.href = "<?= wp_sanitize_redirect(htmlspecialchars_decode($log_redirect)); ?>";
                        }, 1000);
                        location.href = "<?php echo $log_redirect; ?>";
                    }
                }
            });
            return false;
        });
        <?php } ?>
        $('body').on('click', 'form .alert-danger', function(){
            $(this).slideUp();
        });

    });
</script>
