<?php 
session_destroy();
$logo = 'images/white_logo.png';
?>
<!DOCTYPE html>
<html lang="en">
  <head>
        <!-- basic -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- mobile metas -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <!-- site metas -->
    <title>Agen Hotspot</title>

    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="css/fontawesome-free/css/all.min.css">
    <!-- icheck bootstrap -->
    <link rel="stylesheet" href="https://bilspro.com/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="https://bilspro.com/dist/css/adminlte.min.css">
    <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
   </head>
<body style="zoom: 85%;">
<!--d-md-none d-lg-none d-xl-none-->
<div class="container p-0" style="height: 100%">
  <div class="card" style="height: 600px; background-color: #fca503; border-bottom-left-radius: 40px; border-bottom-right-radius: 40px;" >
    <center>
        <img src="<?php echo $logo; ?>" alt="logo" width="150" style="margin-top: 80px; margin-bottom: 10px;">
        <h3  style="margin-bottom: 120px; color: white;">Selamat Datang!</h3>
      </center>
    <div class="card" style="margin: 20px; border-radius: 10px;">
      <div class="card-body">
        <center>
          <h4><b>Login Agen</b></h4><hr>
        </center>
        <form method="post" enctype="multipart/form-data">
          <div class="form-group">
            <label>Nomor Handphone</label>
            <input type="number" class="form-control form-control-lg" name="nohp" placeholder="Nomor Handphone">
          </div>
          <div class="form-group">
            <label style="font-style: normal; ">Password</label>
            <div class="input-group mb-4">
              <input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="Password">
              <div class="input-group-append">
                  <div class="input-group-text">
                      <span class="fas fa-eye" id="toggle-password"></span>
                  </div>
              </div>
            </div>
          </div>
          <a href="?lupa_password">Lupa Password ?</a>
       </div></div>
          <div style="margin: 30px;">
            <input type="submit" class="btn btn-block btn-lg font-weight-medium auth-form-btn text-light" name="login" style="border-radius: 40px; background-color: #fca503;" value="LOGIN">
          </div>
        </form>
        <?php 
          if (isset($_POST['login'])) {
            // Mengatur nomor WhatsApp
            if (substr($_POST['nohp'], 0, 2) == '62') {
                $handphone = $_POST['nohp'];
            } else {
                $handphone = '62' . substr($_POST['nohp'], 1, 15);
            }

            try {
                // Ambil password dari database
                $stmt = $pdo->prepare("SELECT id, password, status FROM agen_voucher WHERE nomor_whatsapp = :handphone");
                $stmt->bindParam(':handphone', $handphone, PDO::PARAM_STR);
                $stmt->execute();
                $user = $stmt->fetch(PDO::FETCH_ASSOC);

                if ($user) {
                    // Verifikasi password
                    if (password_verify($_POST['password'], $user['password'])) {
                        if ($user['status'] == "Aktif") {
                            session_start();
                            $_SESSION['agen'] = $user['id'];
                            $waktu = date('Y-m-d H:i:s');
                            $stmt = $pdo->prepare("UPDATE agen_voucher SET last_login = :last WHERE id = :id");
                            $stmt->bindParam(':last', $waktu);
                            $stmt->bindParam(':id', $user['id']);
                            $stmt->execute();
                            sweetAlert("Login Berhasil", "success", baseurl());
                        } else {
                            sweetAlert("Login Gagal.. Status Agen anda tidak aktif, silahkan hubungi admin", "error", baseurl());
                        }
                    } else {
                        sweetAlert("Login Gagal.. Username atau Password Tidak terdaftar", "error", baseurl());
                    }
                } else {
                    sweetAlert("Login Gagal.. Username atau Password Tidak terdaftar", "error", baseurl());
                }
            } catch (PDOException $e) {
                // Tangani kesalahan koneksi atau query
                sweetAlert("Login Gagal.. Terjadi kesalahan pada server", "error", baseurl());
                // echo "Error: " . $e->getMessage();
            }
          }
        ?>
<div class="text-center">
    <br><br><br>
    <p><i>Versi 2.1.1 @2024</i></p>
</div>
</div></div>
<!-- jQuery -->
<script src="https://bilspro.com/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="https://bilspro.com/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="https://bilspro.com/dist/js/adminlte.min.js"></script>

    <script>
        document.getElementById('toggle-password').addEventListener('click', function() {
            var passwordInput = document.getElementById('password');
            var icon = this;
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
            }
        });
    </script>
</body>
</html> 