<?php

// Output buffering to prevent premature output
ob_start();

include("db/cn.php");

if ($_POST) {
    // Securely fetch POST data
    $name = mysqli_real_escape_string($connection, $_POST["name"]);
    $password = mysqli_real_escape_string($connection, $_POST["password"]);
    $outlet_name = mysqli_real_escape_string($connection, $_POST["outlet_name"]);
    $outlet_address = mysqli_real_escape_string($connection, $_POST["outlet_address"]);

    // Query the database
    $query = "SELECT name, password, role, outlet_name, outlet_address FROM r_login WHERE name='$name'";
    $result = mysqli_query($connection, $query);

    // Check if user exists
    if ($result && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);

        // Assuming passwords are stored as plaintext in the database
        if ($password == $row['password']) {
            // Store user information in session variables
            $_SESSION['user_name'] = $row['name'];
            $_SESSION['user_role'] = $row['role'];
            $_SESSION['outlet_name'] = $row['outlet_name'];
            $_SESSION['outlet_address'] = $row['outlet_address'];

            // Debugging: print session variables
            echo "<pre>";
            print_r($_SESSION);
            echo "</pre>";

            // Redirect based on role
            if ($row['role'] == 'superadmin') {
                header('Location: dashboard.php');
                ob_end_flush();
                exit();
            } elseif ($row['role'] == 'admin') {
                header('Location: log/addproduct.php');
                ob_end_flush();
                exit();
            } else {
                echo "Unknown role";
            }
        } else {
            echo "Invalid password";
        }
    } else {
        echo "User not found";
    }
} else {
    echo "";
}
ob_end_flush(); // End output buffering and send the output
?>


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

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login - UNITED SALES CORPORATION</title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

</head>
<style>
  .animated-gradient {
           
            background: linear-gradient(90deg, #2E4556, #0A4F63, #060606);
            background-size: 300% 300%;
            animation: gradientAnimation 8s ease infinite;
        }

        @keyframes gradientAnimation {
            0% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
            100% {
                background-position: 0% 50%;
            }
        }
.btn-primary{
	background: linear-gradient(270deg, #2E4556, #0A5064);
	color:#fff;
	border:none;
	
	}
.btn-primary:hover{
   background: linear-gradient(270deg, #0A5064, #2E4556);
   color:#fff;
   border:1px solid #000;	
	}	
	
	.logo-container {
    display: inline-block;
    animation: heartbeat 5s infinite;
}

@keyframes heartbeat {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
    }
}
</style>
<body class="animated-gradient">

    <div class="container" style="margin-top:150px;">

        <!-- Outer Row -->
        <div class="row justify-content-center">

            <div class="col-xl-10 col-lg-12 col-md-9">

                <div class="card o-hidden border-0 shadow-lg my-5">
                    <div class="card-body p-0">
                        <!-- Nested Row within Card Body -->
                        <div class="row">
                           <div class="col-lg-7" style="background-color:none;">
                                <div class="p-5" style="background-color:none;">
                                    <div class="text-center">
                                        <h1 class="h4 text-gray-900 mb-3" style="text-transform:uppercase; font-size:25px;font-weight:600;">UNITED SALES CORPORATION</h1>
                                        </div>
         
		                            <form class="user" action="index.php" method="post" enctype="multipart/form-data">
                                        <div class="form-group">
                                            <input type="text" class="form-control form-control-user"
                                                name="name"
                                                placeholder="Login Name">
                                        </div>
                                        <div class="form-group">
                                            <input type="password" class="form-control form-control-user"
                                                 name="password" placeholder="Password">
                                        </div>
                                        <div class="form-group">
                                            <div class="custom-control custom-checkbox small">
                                                <input type="checkbox" class="custom-control-input" id="customCheck">
                                                <label class="custom-control-label" for="customCheck">Remember
                                                    Me</label>
                                            </div>
                                        </div>
                                        <button type="submit" class="btn btn-primary btn-user btn-block">
                                            Login</button>
                                        
                                        <hr>
                                        
                                        
                                    </form>
                                  
                                    
                                    
                                </div>
                            </div>
                            <div class="col-lg-5 d-none d-lg-block ">
                            <br><br>
                            <center><div class="logo-container"><img src="log/img/united sales corporation 2.png" class="img-fluid" width="200" width="200" ></div>
                          <br><br><br>
                           <p style="color:#B18B1F; font-size:16px; font-weight:600;">Powered By ATR Sales & Marketing Services</p>
                           
                            </center>
                            </div>
                            
                        </div>
                    </div>
                </div>

            </div>

        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

</body>

</html>
<?php

?>