<?php 
session_start();
include "connection.php"; 

if (isset($_SESSION['userid']) || (isset($_COOKIE['userid']) && !empty($_COOKIE['userid']))) {
   
    if (isset($_COOKIE['userid'])) {
        $_SESSION['userid'] = $_COOKIE['userid'];
    }
    header("Location: UserPanel/index.php");
    exit();
}

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $number = $_POST["number"];
    $password = $_POST["password"];
    $pin = $_POST["pin"];

    // Validate user credentials
    $query = "SELECT * FROM student WHERE number = '$number' AND password = '$password' AND pin = '$pin'";
    $result = mysqli_query($con, $query);
    
    if (mysqli_num_rows($result) > 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: UserPanel/index.php");
        exit();
    } else {
        
		$_SESSION['error_message'] = " সঠিক তথ্য দিয়ে পুনরায় চেষ্টা করুন! ";
        header("Location:login.php");
        exit();
    }

    mysqli_close($con);
}
?>


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

    <main>
	
        <section>   
			<div class="container mt">
				<div class="row justify-content-center">
					<div class="col-md-6">
						<div class="card custom-card">
							<div class="card-body">
							    
								<h3 class="card-title text-center d-block text-primary">
								    <img  src="UserPanel/img/emt-w.png" height="110px" width="110px" > <br>
								    Easy Money Transfer</h3>
									<h6 class="d-block text-center my-4"> অ্যাকাউন্ট     নেই?    <a href="user-register.php">  নতুন অ্যাকাউন্ট তৈরি করুন  </a>
									
									</h6>
									<form class="form" action="" method="POST">
										<div class="mb-3">
											<label for="inputField" class="form-label"> মোবাইল নং   </label>
											<input type="number" class="form-control" name="number" id="inputField" placeholder="  মোবাইল নং ... " minlength="11" required>
										</div>
										
										<div class="mb-3">
											<label for="inputField" class="form-label"> পাসওয়ার্ড  </label>
											<input type="password" class="form-control" name="password" id="inputField" placeholder=" পাসওয়ার্ড ... " required>
										</div>

										<div class="mb-3">
											<label for="inputField" class="form-label"> পিন নং  </label>
											<input type="number" class="form-control" name="pin" id="inputField" placeholder=" পিন নং ... " required>
										</div>

                                        <div class="d-grid">
										<button type="submit" class="btn custom-button  w-50 mx-auto"> লগইন </button>
										</div>
										
									</form>
									<a href="#" class="d-block text-center my-4"> পাসওয়ার্ড মনে নেই?  </a>
									
									<?php if (isset($_SESSION['success_message'])): ?>
                                <div class="alert alert-success mt-3 text-center" role="alert">
                                    <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
                                </div>
                            <?php elseif (isset($_SESSION['error_message'])): ?>
                                <div class="alert alert-danger mt-3 text-center" role="alert">
                                    <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
                                </div>
                            <?php endif; ?>
							
							
							</div>
						</div>
					</div>
				</div>
			</div>
		</section>
		

		
    </main>

    
</body>
</html>
