<?php
require('connection.inc.php');
require('security.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if the user is logged in by verifying session variables
if (isset($_SESSION['user_id'])) {
    // If not logged in, redirect to the login page (index.php)
    header('Location: index.php');
    exit();
}

// Handle Insert/Update
if (isset($_POST['submit'])) {
    include_once 'csrf.php';
    if (!validate_csrf_token($_POST['csrf_token'])) {
        die('Invalid CSRF token!');
    }

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $role_id = mysqli_real_escape_string($conn, $_POST['role_id']);
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
  
        // Step 2: Check if email exists
        $checkEmailQuery = "SELECT * FROM users WHERE email = '$email' AND role_id = 3 AND deleted_at IS NULL";
        $checkEmailResult = mysqli_query($conn, $checkEmailQuery);

        if (mysqli_num_rows($checkEmailResult) > 0) {
            $_POST['email'] = '';
            // If email exists, show SweetAlert
            echo "<script>
                    document.addEventListener('DOMContentLoaded', function() {
                        Swal.fire({
                            title: 'Duplicate Email!',
                            text: 'The email \"$email\" already exists. Please use a different email.',
                            icon: 'error',
                            confirmButtonText: 'Try Again'
                        });
                    });
                  </script>";
        } else {
            // Step 3: Check if mobile exists
            $checkMobileQuery = "SELECT * FROM users WHERE mobile = '$mobile' AND role_id = 3 AND deleted_at IS NULL";
            $checkMobileResult = mysqli_query($conn, $checkMobileQuery);

            if (mysqli_num_rows($checkMobileResult) > 0) {
                $_POST['mobile'] = '';
                // If mobile exists, show SweetAlert
                echo "<script>
                        document.addEventListener('DOMContentLoaded', function() {
                            Swal.fire({
                                title: 'Duplicate Mobile!',
                                text: 'The mobile number \"$mobile\" already exists. Please use a different number.',
                                icon: 'error',
                                confirmButtonText: 'Try Again'
                            });
                        });
                      </script>";
            } else {
                // If all checks pass, insert the new user
                $insertQuery = "INSERT INTO users (username, email, mobile, password, role_id, created_at) 
                                VALUES ('$username', '$email', '$mobile', '$password', '$role_id', NOW())";
                $result = mysqli_query($conn, $insertQuery);
                unset($_SESSION['csrf_token']);
                if ($result) {
                    echo "<script>
                            document.addEventListener('DOMContentLoaded', function() {
                                Swal.fire({
                                    title: 'Success!',
                                    text: 'Register successfully!',
                                    icon: 'success',
                                    confirmButtonText: 'OK'
                                }).then(() => {
                                    window.location.href = 'log-in.php'; // Redirect after confirmation
                                });
                            });
                          </script>";
                } else {
                    echo "<script>
                            document.addEventListener('DOMContentLoaded', function() {
                                Swal.fire({
                                    title: 'Error!',
                                    text: 'Failed to add the user.',
                                    icon: 'error',
                                    confirmButtonText: 'Try Again'
                                });
                            });
                          </script>";
                }
            }
        }
    }
?>

<!doctype html>
<html class="no-js" lang="zxx">
   
<!-- Mirrored from html.hixstudio.net/orfarm/log-in.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 19 Apr 2025 07:11:22 GMT -->
<head>
      <meta charset="utf-8">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      <title>Love berri</title>
      <meta name="description" content="">
      <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- Place favicon.ico in the root directory -->
      <link rel="shortcut icon" type="image/x-icon" href="assets/img/allnew/logo.png">

      <!-- CSS here -->
      <link rel="stylesheet" href="assets/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/css/animate.css">
      <link rel="stylesheet" href="assets/css/swiper-bundle.css">
      <link rel="stylesheet" href="assets/css/slick.css">
      <link rel="stylesheet" href="assets/css/magnific-popup.css">
      <link rel="stylesheet" href="assets/css/spacing.css">
      <link rel="stylesheet" href="assets/css/meanmenu.css">
      <link rel="stylesheet" href="assets/css/nice-select.css">
      <link rel="stylesheet" href="assets/css/fontawesome.min.css">
      <link rel="stylesheet" href="assets/css/icon-dukamarket.css">
      <link rel="stylesheet" href="assets/css/jquery-ui.css">
      <link rel="stylesheet" href="assets/css/main.css">
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
   </head>
   <body>

<style>
    .tptrack__submition {
    background: #96ae00;}
</style>
      <!-- Scroll-top -->
      <button class="scroll-top scroll-to-target" data-target="html">
         <i class="icon-chevrons-up"></i>
      </button>
      <!-- Scroll-top-end-->


      <!-- header-area-start -->
      <?php 
      include 'header.php'
      ?>
      <!-- header-area-end -->

      <main>

         <!-- breadcrumb-area-start -->
         <div class="breadcrumb__area pt-5 pb-5">
            <div class="container">
               <div class="row">
                  <div class="col-lg-12">
                     <div class="tp-breadcrumb__content">
                        <div class="tp-breadcrumb__list">
                           <span class="tp-breadcrumb__active"><a href="index.html">Home</a></span>
                           <span class="dvdr">/</span>
                           <span>Sign in</span>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
         <!-- breadcrumb-area-end -->

         <!-- track-area-start -->
         <section class="track-area pb-40">
            <div class="container">
               <div class="row justify-content-center">
               <div class="col-lg-6 col-sm-12">
                     <div class="tptrack__product mb-40">
                        <div class="tptrack__content grey-bg">
                           <div class="tptrack__item d-flex mb-20">
                              <div class="tptrack__item-icon">
                                 <i class="fal fa-lock"></i>
                              </div>
                              <div class="tptrack__item-content">
                                 <h4 class="tptrack__item-title">Sign Up</h4>
                                 <p>Your personal data will be used to support your experience throughout this website, to manage access to your account.</p>
                              </div>
                           </div>
                                <form action="#" method="POST">
                                    <?php include_once 'csrf.php'; ?>
                                    <input type="hidden" name="csrf_token" value="<?php echo generate_csrf_token(); ?>">
                                   <div class="tptrack__id mb-10">
                                         <input type="name" class="form-control" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" placeholder="Your Name" required>
                                         <input type="hidden" class="form-control" name="role_id" value="3" required>
                                   </div>
                                   <div class="tptrack__id mb-10">
                                         <input type="email" class="form-control" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" placeholder="Email address" required>
                                   </div>
                                   <div class="tptrack__id mb-10">
                                         <input type="number" class="form-control" name="mobile"  value="<?php echo isset($_POST['mobile']) ? htmlspecialchars($_POST['mobile']) : ''; ?>" placeholder="Mobile No" required>
                                   </div>
                                     <div class="tptrack__email mb-10 position-relative">
                                       <input type="password" class="form-control" name="password" id="password" placeholder="Password" required style="padding-right: 40px;">
                                       <span class="toggle-password" onclick="togglePassword()" style="position: absolute; left: 92%; top: 50%; transform: translateY(-50%); cursor: pointer;">
                                          <i class="fa-solid fa-eye-slash" id="toggleIcon"></i>
                                       </span>
                                    </div>
                                   <div class="tptrack__btn">
                                      <button class="tptrack__submition tpsign__reg" type="submit" name="submit">Register Now<i class="fal fa-long-arrow-right"></i></button>
                                   </div>
                                </form>
                           <br>
                            <div class="tpsign__account mb-15">
                              <a href="log-in.php">Already Have Account?Log In <i class="fal fa-long-arrow-right"></i></a>
                           </div>
                        </div>
                     </div>
                  </div>
                
               </div>
            </div>
         </section>
         <!-- track-area-end -->

         
         <!-- feature-area-start -->
     <?php 
     include 'footer.php'
     ?>
         <!-- feature-area-end -->

      </main>

      <!-- footer-area-start -->
  
      <!-- footer-area-end -->
      
    <script>
    function togglePassword() {
       const passwordInput = document.getElementById("password");
       const toggleIcon = document.getElementById("toggleIcon");
    
       if (passwordInput.type === "password") {
          passwordInput.type = "text";
          toggleIcon.classList.remove("fa-eye-slash");
          toggleIcon.classList.add("fa-eye");
       } else {
          passwordInput.type = "password";
          toggleIcon.classList.remove("fa-eye");
          toggleIcon.classList.add("fa-eye-slash");
       }
    }
    </script>

      <!-- JS here -->
      <script src="assets/js/jquery.js"></script>
      <script src="assets/js/waypoints.js"></script>
      <script src="assets/js/bootstrap.bundle.min.js"></script>
      <script src="assets/js/swiper-bundle.js"></script>
      <script src="assets/js/nice-select.js"></script>
      <script src="assets/js/slick.js"></script>
      <script src="assets/js/magnific-popup.js"></script>
      <script src="assets/js/counterup.js"></script>
      <script src="assets/js/wow.js"></script>
      <script src="assets/js/isotope-pkgd.js"></script>
      <script src="assets/js/imagesloaded-pkgd.js"></script>
      <script src="assets/js/countdown.js"></script>
      <script src="assets/js/ajax-form.js"></script>
      <script src="assets/js/jquery-ui.js"></script>
      <script src="assets/js/meanmenu.js"></script>
      <script src="assets/js/main.js"></script>
   </body>

<!-- Mirrored from html.hixstudio.net/orfarm/log-in.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 19 Apr 2025 07:11:22 GMT -->
</html>
