<!DOCTYPE html>
<html lang="en" dir="ltr" data-bs-theme="light" data-color-theme="Blue_Theme" data-layout="vertical">

<head>
    <!-- Required meta tags -->
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Favicon icon-->
    <link rel="shortcut icon" type="image/png" href="../sglogo.png" />

    <!-- Core Css -->
    <link rel="stylesheet" href="assets/css/styles.css" />
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <title>Admin Login</title>
</head>

<style>
    .img-fluid {
    max-width: 100%;
    height: auto;
    width: 100%;
    margin-top: 50px;
}
</style>

<body>
    <!-- Preloader -->

<?php

session_start();

include "db.php";
$showError = false;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    $query = "SELECT * FROM user WHERE email = ? LIMIT 1";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, "s", $email);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    if ($row = mysqli_fetch_assoc($result)) {
        if ($row['password'] === $password) { 
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['email'] = $row['email'];
            ?>
            <script>
              Swal.fire({
                icon: 'success',
                title: 'Login Successful!',
                showConfirmButton: false,
                timer: 1500
              }).then(() => {
                window.location.href = 'index.php';
              });
            </script>
            <?php
            exit();
        } else {
            $showError = "Invalid password.";
        }
    } else {
        $showError = "User not found.";
    }
}

if ($showError) {
    ?>
    <script>
      Swal.fire({
        icon: 'error',
        title: 'Oops...',
        text: '<?php echo $showError; ?>'
      });
    </script>
    <?php
}

?>

    <div id="main-wrapper">
        <div
            class="position-relative overflow-hidden auth-bg min-vh-100 w-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 my-5 my-xl-0">
                    <div class="col-md-9 d-flex flex-column justify-content-center">
                        <div class="card mb-0 bg-body auth-login m-auto w-100">
                            <div class="row gx-0">
                                <!-- ------------------------------------------------- -->
                                <!-- Part 1 -->
                                <!-- ------------------------------------------------- -->
                                <div class="col-xl-6 border-end">
                                    <div class="row justify-content-center py-4">
                                        <div class="col-lg-11">
                                            <div class="card-body">
                                                <a href="index.php"
                                                    class="text-nowrap logo-img d-block mb-4 w-100">
                                                    <img src="../sglogo.png" style="width: 130px;" class="dark-logo" alt="Logo-Dark" />
                                                </a>
                                                <h2 class="lh-base mb-4">Let's get you signed in</h2>

                                                <div class="position-relative text-center my-4">
                                                    <p class="mb-0 fs-12 px-3 d-inline-block bg-body z-index-5 position-relative">
                                                        Or sign in with email
                                                    </p>
                                                    <span class="border-top w-100 position-absolute top-50 start-50 translate-middle"></span>
                                                </div>
                                                <form method="post">
                                                    <div class="mb-3">
                                                        <label for="exampleInputEmail1" class="form-label">Email Address</label>
                                                        <input type="email" class="form-control" name="email" placeholder="Enter your email" />
                                                    </div>
                                                    <div class="mb-4">
                                                        <div class="d-flex align-items-center justify-content-between">
                                                            <label for="exampleInputPassword1" class="form-label">Password</label>
                                                        </div>
                                                        <input type="password" class="form-control" name="password" placeholder="Enter your password" />
                                                    </div>

                                                    <button class="btn btn-dark w-100 py-8 mb-4 rounded-1">Sign In</button>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- ------------------------------------------------- -->
                                <!-- Part 2 -->
                                <!-- ------------------------------------------------- -->
                                <div class="col-xl-6 d-none d-xl-block">
                                    <div class="row justify-content-center align-items-start h-100">
                                        <div class="col-lg-9">
                                            <div id="auth-login" class="carousel slide auth-carousel mt-5 pt-4"
                                                data-bs-ride="carousel">
                                                <div class="carousel-indicators">
                                                    <button type="button" data-bs-target="#auth-login"
                                                        data-bs-slide-to="0" class="active" aria-current="true"
                                                        aria-label="Slide 1"></button>
                                                    <button type="button" data-bs-target="#auth-login"
                                                        data-bs-slide-to="1" aria-label="Slide 2"></button>
                                                    <button type="button" data-bs-target="#auth-login"
                                                        data-bs-slide-to="2" aria-label="Slide 3"></button>
                                                </div>
                                                <div class="carousel-inner">
                                                    <div class="carousel-item active">
                                                        <div
                                                            class="d-flex align-items-center justify-content-center w-100 h-100 flex-column gap-9 text-center">
                                                            <img src="../images/1.jpeg"
                                                                alt="login-side-img" width="300" class="img-fluid" />
                                                            
                                                        </div>
                                                    </div>
                                                    <div class="carousel-item">
                                                        <div
                                                            class="d-flex align-items-center justify-content-center w-100 h-100 flex-column gap-9 text-center">
                                                            <img src="../images/4.jpeg"
                                                                alt="login-side-img" width="300" class="img-fluid" />
                                                            
                                                        </div>
                                                    </div>
                                                    <div class="carousel-item">
                                                        <div
                                                            class="d-flex align-items-center justify-content-center w-100 h-100 flex-column gap-9 text-center">
                                                            <img src="../images/10.jpg"
                                                                alt="login-side-img" width="300" class="img-fluid" />
                                                            
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="dark-transparent sidebartoggler"></div>
    <!-- Import Js Files -->
    <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/simplebar/dist/simplebar.min.js"></script>
    <script src="assets/js/theme/app.init.js"></script>
    <script src="assets/js/theme/theme.js"></script>
    <script src="assets/js/theme/app.min.js"></script>

    <!-- solar icons -->
    <script src="https://cdn.jsdelivr.net/npm/iconify-icon@1.0.8/dist/iconify-icon.min.js"></script>
</body>

</html>