 <?php 

include "config/connect.php";
include 'list_data/sweetalert.php';
include 'config/tanggal_indo.php';

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ManjaPro</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="plugins/fontawesome-free/css/all.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
  <!-- Icon Aplikasi -->
  <link rel="icon" href="dist/img/logo-sm.png" type="image/gif" />
  <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
</head>
<body class="hold-transition login-page">
<div class="login-box" style="zoom : 115%;">
  <!-- /.login-logo -->
  <div class="card card-outline card-primary">
    <div class="card-header text-center">
      <img src="dist/img/logo-login.png" width="150px" class="mt-4"><br>
      <p><small>Managemen Jaringan Propesional</small></p>
    </div>
    <div class="card-body">
      <p class="login-box-msg">Masuk Aplikasi</p>

      <form method="post" enctype="multipart/fomr-data">
        <div class="input-group mb-3">
          <input type="text" class="form-control" id="username" name="username" placeholder="Username / Email">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-envelope"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-4">
          <input type="password" class="form-control" 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 class="row">
          <div class="col-8">
            <small>
              <a href="lupa_password" class="mb-3">Lupa Password ?</a><br>
              <a href="../">Kembali ke Halaman Utama</a>
            </small>
          </div>
          <!-- /.col -->
          <div class="col-4">
            <input type="submit" name="login" class="btn btn-primary btn-block" value="Masuk"/>
          </div>
          <!-- /.col -->
        </div>
      </form><br>
      <?php

        if (isset($_POST['login'])) {
            // Ambil data dari form
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $waktu = date('Y-m-d H:i:s');

            // Pastikan username dan password tidak kosong
            if (empty($username) || empty($password)) {
                sweetAlert("Username dan Password harus diisi", "error");
            } else {
                try {
                    // Buat koneksi PDO jika belum dibuat
                    // Pastikan $pdo sudah didefinisikan sebelumnya

                    // Query untuk mencari user di data_server
                    $stmt = $pdo->prepare("SELECT id, nama_pemilik, akses, profile, password FROM data_server WHERE username = :username OR email = :username");
                    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                    $stmt->execute();
                    $user = $stmt->fetch(PDO::FETCH_ASSOC);

                    if ($user) {
                        // Verifikasi password untuk data_server
                        if (password_verify($password, $user['password'])) {
                            // Set session untuk user data_server
                            $_SESSION['master'] = $user['id'];

                            // Update last_login di data_server
                            $stmt = $pdo->prepare("UPDATE data_server SET last_login = :waktu WHERE username = :username OR email = :username");
                            $stmt->bindParam(':waktu', $waktu);
                            $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                            $stmt->execute();

                            sweetAlert("Login Berhasil", "success", baseurl());
                        } else {
                            sweetAlert("Login Gagal.. Username atau Password Tidak terdaftar", "error");
                        }
                    } else {
                        // Jika user tidak ditemukan, periksa di user_akses
                        $stmt = $pdo->prepare("SELECT id, password FROM user_akses WHERE username = :username OR email = :username");
                        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                        $stmt->execute();
                        $user1 = $stmt->fetch(PDO::FETCH_ASSOC);

                        if ($user1) {
                            // Verifikasi password untuk user_akses
                            if (password_verify($password, $user1['password'])) {
                                // Set session untuk user_akses
                                $_SESSION['user'] = $user1['id'];

                                // Update last_login di user_akses
                                $stmt = $pdo->prepare("UPDATE user_akses SET last_login = :waktu WHERE username = :username OR email = :username");
                                $stmt->bindParam(':waktu', $waktu);
                                $stmt->bindParam(':username', $username, PDO::PARAM_STR);
                                $stmt->execute();

                                sweetAlert("Login Berhasil", "success", baseurl());
                            } else {
                                sweetAlert("Login Gagal.. Username atau Password Tidak terdaftar", "error");
                            }
                        } else {
                            sweetAlert("Login Gagal.. Username atau Password Tidak terdaftar", "error");
                        }
                    }
                } catch (PDOException $e) {
                    // Tangani kesalahan koneksi atau query
                    sweetAlert("Login Gagal.. Terjadi kesalahan pada sistem", "error");
                    // Jika diperlukan, Anda bisa log kesalahan ke file log atau mengirim email kepada admin
                    // error_log($e->getMessage()); // Untuk log kesalahan
                }
            }
        }
      ?>
    </div>
    <!-- /.card-body -->
  </div>
  <!-- /.card -->
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="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>
