<?php
session_start();
error_reporting(0);

if (strlen($_SESSION['adminlogin']) == 0) {
    // header('location:index.php');
} else {
    header('location:dashboard.php');
}
include('includes/config.php');

if (isset($_POST['login'])) {
    $email = $_POST['username'];
    $password = md5($_POST['password']);
    $sql = "SELECT UserName,Password FROM admin WHERE UserName=:email and Password=:password";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':password', $password, PDO::PARAM_STR);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);
    if ($query->rowCount() > 0) {
        $_SESSION['adminlogin'] = $_POST['username'];
        echo "<script type='text/javascript'> document.location = 'dashboard.php'; </script>";
    } else {
        echo "<script>alert('Invalid Details');</script>";
    }
}
?>
<!doctype html>
<html lang="en" class="no-js">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Add Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">

    <!-- Font Awesome for icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <!-- Custom CSS -->
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-page {
            background: linear-gradient(to right, #4e73df, #1cc88a);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            padding: 50px;
            max-width: 400px;
            width: 100%;
        }

        .form-content {
            background-color: white;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 2.5em;
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }

        .form-control {
            margin-bottom: 20px;
            padding: 15px;
            font-size: 16px;
            border-radius: 8px;
            border: 1px solid #ccc;
            width: 100%;
            display: flex;
            align-items: center;
        }

        .form-control:focus {
            border-color: #1cc88a;
            box-shadow: 0 0 5px rgba(28, 200, 138, 0.5);
        }

        .form-control i {
            margin-right: 10px;
            color: #ccc;
        }

        .form-control input {
            border: none;
            outline: none;
            flex: 1;
            padding: 10px;
        }

        .btn-primary {
            background-color: #1cc88a;
            border: none;
            padding: 15px;
            font-size: 16px;
            width: 100%;
            border-radius: 8px;
            cursor: pointer;
            color: white;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #17a673;
        }

        .alert {
            color: #e74a3b;
            background-color: #f8d7da;
            border: 1px solid #e74a3b;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
            font-weight: bold;
        }

    </style>
</head>

<body>
    <div class="login-page bk-img">
        <div class="form-content">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <h1 class="text-center text-bold mt-4x">Admin Login</h1>
                        <div class="well row pt-2x pb-3x bk-light">
                            <div class="col-md-12">
                                <form method="post">
                                    <div class="form-control">
                                        <i class="fas fa-user"></i>
                                        <input type="text" placeholder="Username" name="username" class="form-control mb" required>
                                    </div>

                                    <div class="form-control">
                                        <i class="fas fa-lock"></i>
                                        <input type="password" placeholder="Password" name="password" class="form-control mb" required>
                                    </div>

                                    <button class="btn btn-primary btn-block" name="login" type="submit">LOGIN</button>
                                </form>

                                <?php if (isset($results) && empty($results)) { ?>
                                    <div class="alert">
                                        Invalid Details. Please try again.
                                    </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Loading Scripts -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/chartData.js"></script>
    <script src="js/main.js"></script>
</body>

</html>
