	<?php include 'header-link.php' ?>

    <main>
	
        <section>   
			<div class="container mt-3">
				<div class="row justify-content-center">
					<div class="col-md-6">
						<div class="card custom-card">
							<div class="card-body">
								<h5 class="card-title text-center d-block"> Admin Log In</h5>
								<?php 
									// Check connection
							if ($con->connect_error) {
								die("Connection failed: " . $con->connect_error);
							}

							// Handle form submission
							if ($_SERVER["REQUEST_METHOD"] == "POST") {
								$username = $_POST['user'];
								$password = $_POST['password'];

								// Prepare and bind
								$stmt = $con->prepare("SELECT * FROM admin_login WHERE admin_name = ? AND password = ?");
								$stmt->bind_param("ss", $username, $password);
								$stmt->execute();
								$result = $stmt->get_result();

								
								if ($result->num_rows > 0) {
									 $user = mysqli_fetch_assoc($result);
									$_SESSION['userid'] = $user['userid']; // Store only the userid in the session

									// Set a cookie that lasts for 30 days
									setcookie("userid", $user['userid'], time() + (30 * 24 * 60 * 60), "/");

									header("Location:ViewPanel/");
									exit();
								} else {
									echo "<p class='text-danger'>Invalid username or password.</p>";
								}
								$stmt->close();
							}

							$con->close();
						?>
									
									<form class="form" action="" method="POST">
										<div class="mb-3">
											<label for="inputField" class="form-label"> Username </label>
											<input type="text" class="form-control" name="user" id="inputField" placeholder="Your Username...">
										</div>
										
										<div class="mb-3">
											<label for="inputField" class="form-label"> Password </label>
											<input type="password" class="form-control" name="password" id="inputField" placeholder="Your Password...">
										</div>
										<button type="submit" class="btn custom-button">Submit</button>
										
										
									</form>
									
							</div>
						</div>
					</div>
				</div>
			</div>
		</section>
		

		
    </main>

    
</body>
</html>
