<?php
session_start();
include '../functions.php';

// Already logged in — go straight to dashboard
if (isset($_SESSION['username'])) {
    header("location:./");
    exit;
}

$login_error = '';

if (isset($_POST['login'])) {
    $username_input = trim($_POST['username'] ?? '');
    $password_input = $_POST['password'] ?? '';

    $stmt = $conn->prepare("SELECT * FROM administrator WHERE username = ?");
    $stmt->bind_param("s", $username_input);
    $stmt->execute();
    $admin = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    $authenticated = false;
    if ($admin) {
        if (password_verify($password_input, $admin['password'])) {
            // Modern bcrypt hash — direct match
            $authenticated = true;
        } elseif ($admin['password'] === sha1($password_input)) {
            // Legacy SHA1 hash — upgrade to bcrypt transparently on login
            $new_hash = password_hash($password_input, PASSWORD_DEFAULT);
            $upd = $conn->prepare("UPDATE administrator SET password = ? WHERE username = ?");
            $upd->bind_param("ss", $new_hash, $username_input);
            $upd->execute();
            $upd->close();
            $authenticated = true;
        }
    }

    if ($authenticated) {
        $_SESSION['username'] = $username_input;
        header("location:./");
        exit;
    } else {
        $login_error = 'Incorrect password or username.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
	<title>Syndash - Admin Template</title>
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
	<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&amp;family=Roboto&amp;display=swap" />
	<!-- Icons CSS -->
	<link rel="stylesheet" href="assets/css/icons.css" />
	<!-- App CSS -->
	<link rel="stylesheet" href="assets/css/app.css" />
	<script type="text/javascript" src="../js/sweetalert.min.js"></script>
</head>

<body class="bg-login">
	<!-- wrapper -->
	<div class="wrapper">
		<div class="section-authentication-login d-flex align-items-center justify-content-center">
			<div class="row">
				<div class="col-12 col-lg-10 mx-auto">
					<div class="card radius-15">
						<div class="row no-gutters">
							<div class="col-lg-6">
								<div class="card-body p-md-5">
									<div class="text-center">
										<img src="assets/images/logo-icon.png" width="80" alt="">
										<h3 class="mt-4 font-weight-bold">Welcome Back</h3>
									</div>
									<div class="login-separater text-center"> <span>LOGIN WITH USERNAME</span>
										<hr/>
									</div>
									<?php if ($login_error): ?>
									<div class="alert alert-danger mt-3 text-center" role="alert">
										<?php echo htmlspecialchars($login_error); ?>
									</div>
									<?php endif; ?>
									<form action="" method="post">
									<div class="form-group mt-4">
										<label>Username</label>
										<input type="text" class="form-control" name="username" placeholder="Enter your username" />
									</div>
									<div class="form-group">
										<label>Password</label>
										<input type="password" class="form-control" name="password" placeholder="Enter your password" />
									</div>
									<div class="form-row">
										<div class="form-group col">
											<div class="custom-control custom-switch">
												<input type="checkbox" class="custom-control-input" id="customSwitch1" checked>
												<label class="custom-control-label" for="customSwitch1">Remember Me</label>
											</div>
										</div>
										<div class="form-group col text-right"> <a href="forgot-password"><i class='bx bxs-key mr-2'></i>Forgot Password?</a>
										</div>
									</div>
									<div class="btn-group mt-3 w-100">
										<button type="submit" class="btn btn-primary btn-block" name="login">Log In</button>
									</div>
									</form>
								</div>
							</div>
							<div class="col-lg-6">
								<img src="assets/images/login-images/login-frent-img.jpg" class="card-img login-img h-100" alt="...">
							</div>
						</div>
						<!--end row-->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- end wrapper -->
</body>
</html>
