<?php
session_start();
error_reporting(0);

include('includes/config.php');
include('includes/whatsapp.php');

// If already logged in, redirect to the dashboard
if (strlen($_SESSION['alogin']) > 0) {
    header('Location: index.php');
    exit;
}

// Get the user's IP address
$user_ip = $_SERVER['REMOTE_ADDR'];  // This retrieves the user's IP address

// Login handling
if (isset($_POST['submit'])) {
    // Get the form data
    $email = $_POST['email'];
    $password = md5($_POST['password']); // Encrypt password

    // Query to check the user's credentials
    $sql = "SELECT id, name, email, password FROM users WHERE email = :email AND password = :password";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':password', $password, PDO::PARAM_STR);
    $query->execute();
    
    $user = $query->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        // Store session details
        $_SESSION['alogin'] = $user['email'];
        $_SESSION['id'] = $user['id'];
        $_SESSION['name'] = $user['name'];

        // Update the user's last login IP address in the database
        $update_ip = "UPDATE users SET last_login_ip = :user_ip WHERE email = :email";
        $update_query = $dbh->prepare($update_ip);
        $update_query->bindParam(':user_ip', $user_ip, PDO::PARAM_STR);
        $update_query->bindParam(':email', $email, PDO::PARAM_STR);
        $update_query->execute();

        // Redirect to dashboard after successful login
        echo "<script type='text/javascript'> document.location = 'index.php'; </script>";
    } else {
        $error = "Invalid email or password. Please try again.";
    }
}
?>

<!doctype html>
<html lang="bn">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>নগর ডিজিটাল সেবা - Login</title>
  <link rel="shortcut icon" type="image/png" href="assets/images/logos/grameen2.png" />
  
  <!-- Google Fonts: Anek Bangla -->
  <link href="https://fonts.googleapis.com/css2?family=Anek+Bangla:wght@400;500;600;700&display=swap" rel="stylesheet">
  
  <link rel="stylesheet" href="assets/css/styles.min.css" />
  
  <style>
    body, .form-label, .btn, .fs-4 {
      font-family: 'Anek Bangla', sans-serif;
    }
  </style>
</head>

<body>
  <!-- Body Wrapper -->
  <div class="page-wrapper" id="main-wrapper" data-layout="vertical" data-navbarbg="skin6" data-sidebartype="full"
    data-sidebar-position="fixed" data-header-position="fixed">
    <div
      class="position-relative overflow-hidden radial-gradient min-vh-100 d-flex align-items-center justify-content-center">
      <div class="d-flex align-items-center justify-content-center w-100">
        <div class="row justify-content-center w-100">
          <div class="col-md-8 col-lg-6 col-xxl-3">
            <div class="card mb-0">
              <div class="card-body">
                <a href="#" class="text-nowrap logo-img text-center d-block py-3 w-100">
                  <img src="assets/images/logos/1.png" width="200" alt="">
                </a>
                <p class="text-center"></p>
                <form method="POST" autocomplete="on">
                  <div class="mb-3">
                    <label for="email" class="form-label">ইমেইলঃ</label>
                    <input type="email" class="form-control" id="email" name="email" required autocomplete="email">
                  </div>
                  <div class="mb-3">
                    <label for="password" class="form-label">পাসওয়ার্ডঃ</label>
                    <input type="password" class="form-control" id="password" name="password" required autocomplete="current-password">
                  </div>

                  <?php if(isset($error)) { echo "<div class='alert alert-danger'>$error</div>"; } ?>

                  <button type="submit" name="submit" class="btn btn-primary w-100 py-2 fs-4 mb-4 rounded-2">লগ ইন করুন</button>
                  <div class="d-flex align-items-center justify-content-center">
                    <p class="fs-4 mb-0 fw-bold">একাউন্ট নেই?</p>
                    <a class="text-primary fw-bold ms-2" href="register.php">রেজিস্টার করুন।</a>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="assets/libs/jquery/dist/jquery.min.js"></script>
  <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  
  <script>
    function openTelegramBot() {
      // Open the @chatIDrobot bot directly
      window.open('https://t.me/chatIDrobot', '_blank');
    }
  </script>
</body>

</html>
