<?php
include_once "global.php";

$msg = '';
$loginReturn = $webClass->userLogin();

if ($loginReturn === true) {
    if (isset($_GET['ref'])) {
        $reffer = $_GET['ref'];
        $loc = htmlentities(base64_decode($reffer));
    } else {
        $loc = WEB_URL;
    }

    // if user success login then refer on previous open page
    header("Location: $loc");
    exit;
} else if ($loginReturn != false) {
    $msg = $loginReturn;
}

$login = $webClass->userLoginCheck();
if ($login) {
    //if user already login then go to profile
    header("Location: " . WEB_URL);
    exit();
}

@$reffer = $_SERVER['HTTP_REFERER'];
@$reffer = str_replace(WEB_URL . '/', '', @$reffer);

if (!empty($reffer)) {
    //getting reffer link and set in url, when login success page redirect on location
    if (isset($_GET['ref'])) {
        $reffer = htmlentities(base64_decode($_GET['ref']));
    }
    $reffer = base64_encode($reffer);
}
?>
<!doctype html>
<html lang="en" data-bs-theme="dark">

<head>
  <!-- Meta Tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Login to access your dashboard at <?php echo $functions->ibms_setting('Site Name'); ?>">
  <meta name="author" content="Interactive Media Pakistan">

  <!-- Page Title -->
  <title>Login - <?php echo $functions->ibms_setting('Site Name'); ?></title>

  <!-- Favicon -->
  <link rel="icon" href="webImages/favicon.ico" type="image/x-icon">

  <!-- Preconnect for Google Fonts -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Material+Icons+Outlined" rel="stylesheet">

  <!-- Loader -->
  <link href="css/pace.min.css" rel="stylesheet">
  <script src="js/pace.min.js" defer></script>

  <!-- Core CSS -->
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link href="css/bootstrap-extended.css" rel="stylesheet">

  <!-- Plugins (if actually used on login page) -->
  <link href="plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="plugins/metismenu/metisMenu.min.css">
  <link rel="stylesheet" type="text/css" href="plugins/metismenu/mm-vertical.css">

  <!-- Custom Styles with Cache Busting -->
  <link href="sass/main.css?ver=<?php echo filemtime(__DIR__ . '/sass/main.css'); ?>" rel="stylesheet">
  <link href="sass/dark-theme.css?ver=<?php echo filemtime(__DIR__ . '/sass/dark-theme.css'); ?>" rel="stylesheet">
</head>

<body>
    <div id="spinner-overlay">
        <button class="btn btn-dark" type="button" disabled> <span class="spinner-border spinner-border-sm"
                role="status" aria-hidden="true"></span>
            Loading...</button>
    </div>
    <!--authentication-->
    <div class="auth-basic-wrapper d-flex align-items-center justify-content-center">
        <div class="container-fluid my-5 my-lg-0">
            <div class="row">
                <div class="col-12 col-md-8 col-lg-6 col-xl-5 col-xxl-4 mx-auto">
                    <div class="card mb-0 border-top border-4 border-primary border-gradient-1">
                        <div class="p-5">
                            <img src="webImages/logo1.png" class="mb-4" width="200" alt="logo">
                            <h4 class="fw-bold">Get Started Now</h4>
                            <p class="mb-0">Enter your credentials to login your account</p>
                            <?php if ($msg != '') { ?>
                                <div class="alert alert-dark border-0 bg-dark alert-dismissible fade show">
                                    <div class="text-white"><?php echo $msg; ?></div>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                            <?php } ?>
                            <div class="form-body my-5">
                                <form class="row g-3" action="" role="form" method="post">
                                    <?php $functions->setFormToken('signInUser'); ?>
                                    <div class="col-12">
                                        <label for="email" class="form-label">Email</label>
                                        <input type="email" class="form-control" id="email" name="email" required autocomplete="username">
                                    </div>
                                    <div class="col-12">
                                        <label for="inputChoosePassword" class="form-label">Password</label>
                                        <div class="input-group" id="show_hide_password">
                                            <input type="password" class="form-control border-end-0"
                                                id="inputChoosePassword" name="pass" required autocomplete="current-password">
                                            <a href="javascript:;" class="input-group-text bg-transparent"><i
                                                    class="bi bi-eye-slash-fill"></i></a>
                                        </div>
                                    </div>
                                    <div class="col-md-12 d-flex flex-column flex-sm-row align-items-start align-items-sm-center justify-content-between gap-2">
                                        <div class="form-check form-switch">
                                            <input class="form-check-input" type="checkbox" id="remember"
                                                name="remember">
                                            <label class="form-check-label" for="remember">Remember Me</label>
                                        </div>
                                        <a href="forgot_password">Forgot Password ?</a>
                                    </div>
                                    <div class="col-12">
                                        <div class="d-grid">
                                            <button type="submit" name="submit" class="btn default-btn default-btn-green">Login <span></span></button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!--end row-->
        </div>
    </div>
    <!--authentication-->

    <script src="js/bootstrap.bundle.min.js"></script>
    <!--plugins-->
    <script src="js/jquery.min.js"></script>

    <script>
        $(document).ready(function() {
            $("#spinner-overlay").hide();
            $("#show_hide_password a").on('click', function(event) {
                event.preventDefault();
                if ($('#show_hide_password input').attr("type") == "text") {
                    $('#show_hide_password input').attr('type', 'password');
                    $('#show_hide_password i').addClass("bi-eye-slash-fill");
                    $('#show_hide_password i').removeClass("bi-eye-fill");
                } else if ($('#show_hide_password input').attr("type") == "password") {
                    $('#show_hide_password input').attr('type', 'text');
                    $('#show_hide_password i').removeClass("bi-eye-slash-fill");
                    $('#show_hide_password i').addClass("bi-eye-fill");
                }
            });
        });
    </script>

</body>

</html>