<?php
session_start();
include '../config/db.php';
include '../config/config.php';
include '../config/function.php';

// Initialize variables
$email = $password = '';
$alert_script = '';

// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Validate email
    if (empty($_POST['email'])) {
        $errors[] = 'Email is required';
    } else {
        $email = text_input($_POST['email']);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = 'Invalid email format';
        }
    }

    // Validate password
    if (empty($_POST['password'])) {
        $errors[] = 'Password is required';
    } else {
        $password = text_input($_POST['password']);
    }

    // If no validation errors, proceed with login
    if (empty($errors)) {
        // Check if user exists
        $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows === 1) {
            $user = $result->fetch_assoc();
            
            // Verify password (assuming passwords are hashed)
            if (password_verify($password, $user['password'])) {
                // Successful login
                $_SESSION['USER_LOG'] = $user['email'];
                $_SESSION['USER_ID'] = $user['id'];
                
                // Use show_success like in the first code
                $alert_script = show_success('Login successful! Redirecting to dashboard...');
                $redirect_script = "<script>
                    setTimeout(function() {
                        window.location.href = '../user/user.php';
                    }, 2000);
                </script>";
            } else {
                $errors[] = 'Incorrect password';
            }
        } else {
            $errors[] = 'Email not found';
        }
        $stmt->close();
    }

    // If there are errors, display them
    if (!empty($errors)) {
        $error_content = implode('<br>', $errors);
        $alert_script = show_error($error_content);
    }
}


function show_error($message) {
    return "<script>
        document.addEventListener('DOMContentLoaded', function() {
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: '".addslashes($message)."'
            });
        });
    </script>";
}

function show_success($message) {
    return "<script>
        document.addEventListener('DOMContentLoaded', function() {
            Swal.fire({
                icon: 'success',
                title: 'Success',
                text: '".addslashes($message)."'
            }).then((result) => {
                if (result.isConfirmed) {
                    window.location.href = '../user/dashboard.php';
                }
            });
        });
    </script>";
}
?>

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

<head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Links of CSS files -->
        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/css/aos.css">
        <link rel="stylesheet" href="assets/css/animate.min.css">
        <link rel="stylesheet" href="assets/css/meanmenu.min.css">
        <link rel="stylesheet" href="assets/css/remixicon.css">
        <link rel="stylesheet" href="assets/css/flaticon.css">
        <link rel="stylesheet" href="assets/css/odometer.min.css">
        <link rel="stylesheet" href="assets/css/owl.carousel.min.css">
        <link rel="stylesheet" href="assets/css/owl.theme.default.min.css">
        <link rel="stylesheet" href="assets/css/magnific-popup.min.css">
        <link rel="stylesheet" href="assets/css/style.css">
        <link rel="stylesheet" href="assets/css/navbar.css">
        <link rel="stylesheet" href="assets/css/footer.css">
        <link rel="stylesheet" href="assets/css/dark.css">
		<link rel="stylesheet" href="assets/css/responsive.css">
		
		<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
		
        <!--=== Title & Favicon ===-->
		<title><?php echo $sitename ?></title>
        <link rel="icon" type="image/png" href="assets/images/favicon.png">
    </head>
    <body>
        
        <!-- Start Login Area -->
        <div class="login-area ptb-100">
            <div class="container">
                <div class="login-form">
                    <h2>Login Here</h2>
                    <p>Welcome Back, Login To Your Account</p>

                    <form method="post" >
                        <div class="form-group">
                            <label>Your email address</label>
                            <input type="text" class="form-control" name="email" placeholder="Your email address">
                        </div>

                        <div class="form-group">
                            <label>Your password</label>
                            <input type="password" class="form-control" name="password" placeholder="Your password">
                        </div>

                        <div class="row align-items-center">
                            <div class="col-lg-6 col-md-6 col-sm-6 remember-me-wrap">
                                <p>
                                    <input type="checkbox" id="test2">
                                    <label for="test2">Remember me</label>
                                </p>
                            </div>

                        </div>

                        <button type="submit" class="default-btn">Login Now</button>

                        <div class="account-text">
                            <span>Don’t have an account? <a href="register.php">Create Account</a></span>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- End Login Area -->
 <?php if (!empty($alert_script)) { echo $alert_script; } ?>
       
        <!-- Links of JS files -->
        <script src="assets/js/jquery.min.js"></script>
        <script src="assets/js/bootstrap.bundle.min.js"></script>
        <script src="assets/js/jquery.meanmenu.js"></script> 
        <script src="assets/js/owl.carousel.min.js"></script>
        <script src="assets/js/jquery.appear.js"></script>
        <script src="assets/js/odometer.min.js"></script>
        <script src="assets/js/jquery.magnific-popup.min.js"></script>
        <script src="assets/js/TweenMax.min.js"></script>
        <script src="assets/js/ScrollMagic.min.js"></script>
        <script src="assets/js/aos.js"></script>
		<script src="assets/js/jquery.ajaxchimp.min.js"></script>
		<script src="assets/js/form-validator.min.js"></script>
        <script src="assets/js/contact-form-script.js"></script>
        <script src="assets/js/wow.min.js"></script>
        <script src="assets/js/main.js"></script>
       
    </body>


</html>