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

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

// Fetch control settings from the database
$stmt = $dbh->prepare('SELECT * FROM control WHERE id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$id = 1;
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$status = $data['reg'];

// If registration is closed, redirect to regOff.php
if ($status == 1) {
    header('Location: regOff.php');
    die();
}

// If the user is already logged in, redirect to nid.php
if (strlen($_SESSION['alogin']) == 0) {
    // header('location:index.php'); // Uncomment if you want to redirect when not logged in
} else {
    header('Location: nid.php');
}

// Registration handling
if (isset($_POST['submit'])) {
    // Get form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = md5($_POST['password']); // Encrypt password
    $gender = "male";  // Default gender (can be dynamic)
    $mobileno = $_POST['mobileno'];
    $image = "image.png";  // Default image
    $telegram_username = $_POST['telegram_username'];

    $balance = 0;  // Default balance is 0
    $signup_ip = $_SERVER['REMOTE_ADDR']; // Get user IP for sign up
    $login_ip = $_SERVER['REMOTE_ADDR'];  // Initial login IP

    $notitype = 'Create Account';
    $reciver = 'Admin';
    $sender = $email;

    // Insert notification for account creation
    $sqlnoti = "INSERT INTO notification (notiuser, notireciver, notitype) VALUES (:notiuser, :notireciver, :notitype)";
    $querynoti = $dbh->prepare($sqlnoti);
    $querynoti->bindParam(':notiuser', $sender, PDO::PARAM_STR);
    $querynoti->bindParam(':notireciver', $reciver, PDO::PARAM_STR);
    $querynoti->bindParam(':notitype', $notitype, PDO::PARAM_STR);
    $querynoti->execute();

    // Insert user details into the database
    $sql = "INSERT INTO users (name, email, password, gender, mobile, balance, image, status, signup_ip, login_ip, telegram_username) 
            VALUES (:name, :email, :password, :gender, :mobileno, :balance, :image, 1, :signup_ip, :login_ip, :telegram_username)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':name', $name, PDO::PARAM_STR);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':password', $password, PDO::PARAM_STR);
    $query->bindParam(':gender', $gender, PDO::PARAM_STR);
    $query->bindParam(':mobileno', $mobileno, PDO::PARAM_STR);
    $query->bindParam(':balance', $balance, PDO::PARAM_INT);
    $query->bindParam(':image', $image, PDO::PARAM_STR);
    $query->bindParam(':signup_ip', $signup_ip, PDO::PARAM_STR);
    $query->bindParam(':login_ip', $login_ip, PDO::PARAM_STR);
    $query->bindParam(':telegram_username', $telegram_username, PDO::PARAM_STR);
    $query->execute();
    
    $lastInsertId = $dbh->lastInsertId();
    if ($lastInsertId) {
        echo "<script type='text/javascript'>alert('Registration Successful!');</script>";
        echo "<script type='text/javascript'> document.location = 'login.php'; </script>";
    } else {
        $error = "Something went wrong. Please try again";
    }
}
?>

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

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>নগর ডিজিটাল সেবা </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" />
  <link rel="stylesheet" href="assets/bensen/stylesheet.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="name" class="form-label">নামঃ</label>
                    <input type="text" class="form-control" id="name" name="name" required autocomplete="name">
                  </div>
                  <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="new-password">
                  </div>
                  <div class="mb-4">
                    <label for="mobileno" class="form-label">মোবাইল নাম্বারঃ</label>
                    <input type="number" id="mobileno" name="mobileno" class="form-control" min="11" required autocomplete="tel">
                  </div>
                  
                  <div class="mb-4">
                    <label for="telegram_username" class="form-label">টেলিগ্রাম ইউজারনেমঃ</label>
                    <input type="text" id="telegram_username" name="telegram_username" class="form-control" required autocomplete="off">
                  </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="login.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>
