<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" type="image/ico" sizes="16x16" href="<?php echo  base_url();?>admin-assets/images/favicon.ico">

    <title>Plex | Admin Login</title>
    <link href="<?php echo base_url(); ?>admin-assets/css/pages/login-register-lock.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>admin-assets/css/style.min.css" rel="stylesheet">
    <link href="<?php echo  base_url();?>admin-assets/node_modules/sweetalert/sweetalert.css" rel="stylesheet" type="text/css">
    
    <script> var CI_ROOT = "<?php echo base_url(); ?>"; </script>
    
    <style>
      .error {
        color: #F00;
        font-size: 12px;
        display: none;
      }
    
      .form-control{
        border-color: #BCB7B7;
      }

      .login-box{
        -webkit-box-shadow: 10px 10px 17px -8px rgba(0,0,0,0.75);
        -moz-box-shadow: 10px 10px 17px -8px rgba(0,0,0,0.75);
        box-shadow: 10px 10px 17px -8px rgba(0,0,0,0.75);
      }
    </style>
  </head>
  <body class="skin-blue card-no-border">
    
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <div class="preloader">
      <div class="loader">
        <div class="loader__figure"></div>
        <p class="loader__label">LOADING...</p>
      </div>
    </div>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <section id="wrapper">
      <div class="login-register"
        style="background-image:url(<?php echo base_url();?>admin-assets/images/login.jpg);">
        <div class="login-box card">
          <div class="card-body login-box">
            <form id="adminLogin" class="form-material" autocomplete="off">
              <input name="hash" id="hash" type="hidden"  />
              <h3 class="box-title m-b-20" style="text-align:center;">PLEX ADMIN PANEL  </h3>
              <div class="form-group ">
                <div class="col-xs-12">
                  <input class="form-control" type="text" id="uname" name="uname" placeholder="Enter Username">
                </div>
                <div><label for="uname" generated="true" class="error"></label></div>
              </div>
              <div class="form-group">
                <div class="col-xs-12">
                  <input class="form-control" type="password" id="pword" name="pword" placeholder="Enter Password">
                </div>
                <div><label for="pword" generated="true" class="error"></label></div>
              </div>
              <div class="form-group">
                <div class="col-xs-12">
                  <input class="form-control" type="text" name="captcha" placeholder="Enter Captcha" />
                  <div><label for="captcha" generated="true" class="error"></label></div>
                  
                  <div id="image_captcha">
                    <?php
                      echo $captcha;
                    ?>
                  </div>
                  <div>
                    <a href="javascript:void(0);" class="captcha-refresh"><img style="width: 10%;" src="<?php echo base_url().'admin-assets/images/refresh.png'; ?>"/></a>
                  </div>
                </div>
              </div>
              
              <?php $_SESSION["token"] = md5(uniqid(mt_rand(), true)); ?>
              <input type="hidden" name="csrfToken" id="csrfreg" value="<?php echo $_SESSION["token"];?>">
              
              <div class="form-group text-center">
                <div class="col-xs-12">
                  <input type="submit" class="btn btn-block btn-info btn-rounded" value="Log In">
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </section>
    
    <!-- ============================================================== -->
    <!-- End Wrapper -->
    <!-- ============================================================== -->
    
    <!-- ============================================================== -->
    <!-- All Jquery -->
    <!-- ============================================================== -->
    <script src="<?php echo base_url(); ?>admin-assets/node_modules/jquery/jquery-3.2.1.min.js"></script>
    <script src="<?php echo base_url(); ?>admin-assets/node_modules/popper/popper.min.js"></script>
    <script src="<?php echo base_url(); ?>admin-assets/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="<?php echo base_url(); ?>public/custom/js/sweetalert.min.js"></script>

    <script src="<?php echo base_url(); ?>admin-assets/js/aes.js">/* AES JavaScript implementation */</script>
    <script src="<?php echo base_url(); ?>admin-assets/js/aes-ctr.js">/* AES Counter Mode implementation */</script>
    <script src="<?php echo base_url(); ?>admin-assets/js/base64.js">/* Base64 encoding */</script>
    <script src="<?php echo base_url(); ?>admin-assets/js/utf8.js">/* UTF-8 encoding */</script>
    <script src="<?php echo base_url(); ?>admin-assets/js/md5-min.js"></script>
    <!--Custom JavaScript -->
    <script type="text/javascript">
    $(function() {
      $(".preloader").fadeOut();
    });
    $(function() {
      $('[data-toggle="tooltip"]').tooltip()
    });

    function  crypt(password = ''){
      let pass= password;

      if (pass != ''){
        let p = hex_md5(pass);
        var k = '<?=$salt?>';
        var h = hex_md5(k + p);

        $("#pword").val(h);
        $("#hash").val(h);
      }
    }
    // ==============================================================
    // Login and Recover Password
    // ==============================================================
    $('#to-recover').on("click", function() {
    $("#loginform").slideUp();
    $("#recoverform").fadeIn();
    });
      
      $("#adminLogin").on("submit",function(e){
        e.preventDefault();
        $(".error").html("");
        $(".error").css("display","none");
        
        crypt($('#pword').val());
        var formdata = $(this).serialize();

        $.ajax({
          type:'POST',
          data:formdata,
          url:CI_ROOT+"admin/loginAction",
          dataType: "json",
          beforeSend:function(){
            $(".preloader").fadeIn();
          },
          success:function(result){
            $(".preloader").fadeOut();
            
            if(result['status'] == "success"){
              window.location.href = CI_ROOT + 'admin/dashboard';
            }else if(result['status'] == "invalid"){
              swal({
                title: "Invalid Request",
                icon: "error",
                text: "Something went wrong."
              });
            }else if(result['status'] == "fail"){
              swal({
                title: "Error",
                icon: "error",
                text: result['message']
              });
            }else{
              $.each(result, function(i, v) {
                $("label[for='"+i+"']").html(v);
              });
                      
              var keys = Object.keys(result);
              $(".error").css("display","block");
              $('input[name="'+keys[0]+'"]').focus();
            }
          }
        });
      });
    </script>

    <script>
      $(document).ready(function(){
        $('.captcha-refresh').on('click', function(){
              
          $.ajax({
            type:'POST',
            data:{req : 1},
            url:CI_ROOT+"admin/captcha-refresh",
            dataType: "json",
            beforeSend:function(){
              $(".preloader").fadeIn();
            },
            success:function(result){
              $(".preloader").fadeOut();

              if(result['status'] == "success"){
                $('#image_captcha').html('');
                $('#image_captcha').html(result['res']);
              }else if(result['status'] == "fail"){
                swal({
                  title: "Error",
                  icon: "error",
                  text: result['message']
                });
              }
              
            }
          });
        });
      });
   </script>
  </body>
</html>