<?php
ob_start();
session_start();

require 'db-connection.php';


      if (isset($_POST['userEmail']) && isset($_POST['userPassword'])) 
      {
            
            
            $email = $_POST['userEmail'];
            $password = $_POST['userPassword'];
           
            
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $response['message'] = 'Invalid email format.';
                echo json_encode($response);
                exit();
            }
            
            
            $query = "SELECT id, u_email,u_name, u_password FROM tbl_admin WHERE u_email='$email' and u_password='$password' ";
            
            
            $result = mysqli_query($link, $query) or die("Error " . mysqli_error($link));
            
            
            if ($row = mysqli_fetch_assoc($result)) {
                
              
                $_SESSION['admin_id'] = $row['id'];
                $_SESSION['admin_email'] = $row['u_email'];
                $_SESSION['admin_name'] = $row['u_name'];
                
               
                
                //  echo "<pre>";
                // print_r($_SESSION);
                
                // exit();
                
                
                $response['status'] = true;
                $response['message'] = 'Login successful.';
                
                
               // echo $_SESSION["favanimal"];
                
                if(isset($_SESSION['admin_id']) && isset($_SESSION['admin_email']) && isset($_SESSION['admin_name']))
                {
                 header("Location: dashboard.php");
                 ob_end_flush();
                 exit;
               // echo '<script> window.location.href="https://heightdetox.com/admin/dashboard.php" </script>'; 
                }
                
            //echo '<script> window.location.href="https://heightdetox.com/admin/dashboard.php" </script>';   
                
                
            } 
            
            else {
                $response['status'] = false;
                $response['message'] = 'Invalid email or password.';
            }
            
            
            // Output response
            //echo json_encode($response);
            
        
            
        }

?>


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="description" content="Height Detox">
  <meta name="author" content="vHeight Detox">
  <meta name="keywords" content="Height Detox">
  <title>Admin Login || Shrichitt Ayurveda</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="assets/css/demo1/style.css">
  <link rel="shortcut icon" href="assets/images/favicon.png" />
</head>

<body>
  <div class="main-wrapper">
    <div class="page-wrapper full-page">
      <div class="page-content d-flex align-items-center justify-content-center">.
        <div class="row w-100 mx-0 auth-page">
          <div class="col-md-10 col-lg-8 col-xl-6 mx-auto">
            <div class="card">
              <div class="row">
                <div class="col-md-4 pe-md-0">
                  <div class="auth-side-wrapper" style="background-image: url(assets/images/photos/img6.jpg);"></div>
                </div>
                
                <div class="col-md-8 ps-md-0" style="background: #d3c3c3;">
                <?php
                // echo "<pre>";
                // echo "abhay";
                // print_r($_SESSION);
                ?>
                  <div class="auth-form-wrapper px-4 py-5">
                    <a href="#" class="nobleui-logo d-block mb-2"><img src="assets/images/logo-white.png" alt="Logo" style="width:100px;"></a>
                    <h5 class="text-secondary fw-normal mb-4">Welcome back! Log in to your account.</h5>
                    <form class="forms-sample" method="post">
                      <div class="mb-3">
                        <label for="userEmail" class="form-label">Email address</label>
                        <input type="email" class="form-control" id="userEmail" name="userEmail" placeholder="Email" required>
                      </div>
                      <div class="mb-3">
                        <label for="userPassword" class="form-label">Password</label>
                        <input type="password" class="form-control" id="userPassword" name="userPassword" autocomplete="current-password" placeholder="Password" required minlength="6">
                      </div>
                      <div>
                        <button type="submit" class="btn btn-primary btn-icon-text mb-2 mb-md-0">Login</button>
                      </div>
                      <div id="error-message" class="text-danger mt-2"></div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>




  <script>
    $(document).on('submit', '#loginform', function(event) {
      event.preventDefault();
      var form = $(this);
      var form_data = new FormData(form[0]);
      form_data.append('admin-login', true);

      $.ajax({
        url: 'controllers-abhay.php',
        type: 'POST',
        data: form_data,
        contentType: false, // Important for FormData
        processData: false, // Important for FormData
        dataType: 'json',
        beforeSend: function() {
          $('#error-message').html(''); // Clear previous error messages
        },
        success: function(resp) {
            console.log(resp);
          if (resp.status) {
           // window.location.replace("dashboard.php"); // Redirect on success
          } else {
            $('#error-message').html(resp.message); // Show error message
          }
        },
        error: function(xhr, status, error) {
          $('#error-message').html('An error occurred. Please try again.');
        }
      });
    });
  </script>
</body>

</html>