<?php
// Set session cookie domain for your specific website
ini_set('session.cookie_domain', 'digitalcardwale.com');

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();
}

if (isset($_POST['submit'])) {
    include_once 'csrf.php';
    if (!validate_csrf_token($_POST['csrf_token'])) {
        die('Invalid CSRF token!');
    }
    
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];

    // Check if user exists
    $query = "SELECT * FROM users WHERE email = '$email' AND role_id = 3 AND deleted_at IS NULL";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {
        $row = mysqli_fetch_assoc($result);

        // Verify password
        if (password_verify($password, $row['password'])) {
            // Login success
            $_SESSION['user_id'] = $row['user_id'];
            $_SESSION['username'] = $row['username'];
            $_SESSION['email'] = $row['email'];
            $_SESSION['mobile'] = $row['mobile'];
            $_SESSION['role_id'] = $row['role_id'];

             unset($_SESSION['csrf_token']);
            echo "<script>
                    document.addEventListener('DOMContentLoaded', function() {
                        Swal.fire({
                            title: 'Success!',
                            text: 'Login successful!',
                            icon: 'success',
                            confirmButtonText: 'OK'
                        }).then(() => {
                            window.location.href = 'index.php'; // redirect after login
                        });
                    });
                  </script>";
        } else {
            // Wrong password
            $_POST['password'] = '';
            echo "<script>
                    document.addEventListener('DOMContentLoaded', function() {
                        Swal.fire({
                            title: 'Error!',
                            text: 'Incorrect Password!',
                            icon: 'error',
                            confirmButtonText: 'Try Again'
                        });
                    });
                  </script>";
        }
    } else {
        // Email not found
        $_POST['email'] = '';
        $_POST['password'] = '';
        echo "<script>
                document.addEventListener('DOMContentLoaded', function() {
                    Swal.fire({
                        title: 'Error!',
                        text: 'Email not registered!',
                        icon: 'error',
                        confirmButtonText: 'Try Again'
                    });
                });
              </script>";
    }
}

?>
<!doctype html>
<html class="no-js" lang="zxx">
<head>
      <meta charset="utf-8">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      <title>Loveberi</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>


      <!-- 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.php">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-user-unlock"></i>
                              </div>
                              <div class="tptrack__item-content">
                                 <h4 class="tptrack__item-title">Login Here</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
                              if (isset($_GET['message']) && $_GET['message'] == 'login_required') {
                                 echo '<div class="alert alert-danger">Please login first to add products to your cart or in wishlist.</div>';
                              }
                              ?>


                                <?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="email" class="form-control" name="email" placeholder="email address" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
                               </div>
                               <div class="tptrack__email mb-10 position-relative">
                                     <input type="password" class="form-control" name="password" id="password" placeholder="Password" value="<?php echo isset($_POST['password']) ? htmlspecialchars($_POST['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="tpsign__remember d-flex align-items-center justify-content-between mb-15">
                                   <div class="form-check"></div>
                                  <div class="tpsign__pass">
                                     <a href="forgot_password.php">Forget Password</a>
                                  </div>
                               </div>
                               <div class="tptrack__btn">
                                  <button class="tptrack__submition active" type="submit" name="submit">Login Now<i class="fal fa-long-arrow-right"></i></button>
                               </div>
                            </form>
                          <br>
                           <div class="tpsign__account mb-15">
                              <a href="register.php">Dont Have Account? <span>Sign Up <i class="fal fa-long-arrow-right"></i></span></a>
                           </div>
                        </div>
                     </div>
                  </div>
                
               </div>
            </div>
         </section>
         <!-- track-area-end -->

     <?php 
     include 'footer.php'
     ?>
      </main>

          <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>

</html>
