<?php


include('config.php');


$inactive = false;
$wrongpass = false;
$nodata = false;
$success = false;


if(isset($_POST['sub']) && $_POST['sub'] =="1"){
   $username = mysqli_real_escape_string($conn,$_POST['username']);
   $password = mysqli_real_escape_string($conn,$_POST['password']);
   $usertype = mysqli_real_escape_string($conn,$_POST['usertype']);
   $res = mysqli_query($conn,"SELECT * FROM users WHERE username='$username' AND usertype='$usertype'");
   if(mysqli_num_rows($res)==1){
      $data = mysqli_fetch_assoc($res);
      if($data['status'] =="active"){
         if($_POST['password'] == $data['password'] ){
            $success = true;
            $_SESSION['username'] = $data['username'];
            if($data['usertype'] == "Retailer"){
               ?>
            <script>
               window.location.href='dashboard.php';
            </script>
            <?php
            }else if($data['usertype'] == "Superadmin"){
               ?>
            <script>
               window.location.href='admin.php';
            </script>
            <?php
            }else if($data['usertype'] == "Admin"){
               ?>
            <script>
               window.location.href='main-admin.php';
            </script>
            <?php
            }else if($data['usertype'] == "User"){
               ?>
            <script>
               window.location.href='user-dashboard.php';
            </script>
            <?php
            }
         }else{
            $wrongpass = true;
         }
      }else{
         $inactive = true;
      }
   }else{
      $nodata = true;
   }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/scss/style.css" rel="stylesheet">

    <style>
        body {
            background-color: #f4f7fc;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }

        .login-container .logo {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-container .form-group {
            margin-bottom: 20px;
        }

        .login-container input[type="text"],
        .login-container input[type="password"],
        .login-container select {
            border-radius: 5px;
            padding: 10px;
            width: 100%;
            border: 1px solid #ccc;
        }

        .btn-login {
            background-color: #007bff;
            color: #fff;
            border-radius: 5px;
            padding: 10px 20px;
            width: 100%;
            border: none;
            font-size: 16px;
        }

        .btn-login:hover {
            background-color: #0056b3;
        }

        .login-footer {
            text-align: center;
            margin-top: 20px;
        }

        .alert {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <div class="logo">
          
        </div>
        <h3 class="text-center">Login to Your Account</h3>

        <?php if ($success) : ?>
            <div class="alert alert-success">
                Welcome, <?php echo $data['name']; ?>! You have logged in successfully.
            </div>
        <?php endif; ?>
        <?php if ($inactive) : ?>
            <div class="alert alert-danger">
                Your account is inactive. Please contact the admin.
            </div>
        <?php endif; ?>
        <?php if ($wrongpass) : ?>
            <div class="alert alert-danger">
                Incorrect username or password.
            </div>
        <?php endif; ?>
        <?php if ($nodata) : ?>
            <div class="alert alert-danger">
                Username does not exist. Please register before logging in.
            </div>
        <?php endif; ?>

        <form method="POST" action="" autocomplete="off">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" name="username" id="username" placeholder="Enter Username" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" name="password" id="password" placeholder="Enter Password" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="role">Role</label>
                <select name="usertype" id="role" class="form-control" required>
                    <option value="Superadmin">Admin Login</option>
                    <option value="Admin">Distributor Login</option>
                    <option value="Retailer">Retailer Login</option>
                    <option value="User">Back Office</option>
                </select>
            </div>
            <div class="form-group">
                <button type="submit" class="btn-login">Login</button>
            </div>
            <div class="form-group">
                <a href="forgot-password.php" class="btn btn-link">Forgot Password?</a>
            </div>
            <input type="hidden" name="sub" value="1">
        </form>

        <div class="login-footer">
            <p>&copy;  All Rights Reserved.</p>
        </div>
    </div>

    <!-- Bootstrap JS and jQuery -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>

</html>
