<?php
session_start
();

// If user is already logged in, optionally redirect to cms.php
if (isset($_SESSION['logged_in_user'])) {
    
header("Location: cms.php");
    exit;
}

// Check if login form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['username'], $_POST['password'])) {
    
// Grab form data
    
$username $_POST['username'];
    
$password $_POST['password'];

    
// Database credentials
    
$servername "127.0.0.1";
    
$dbUsername "tyrfl709";
    
$dbPassword "[q8OXd*BEbxW6bM4";
    
$dbName     "tyrfl709";

    
// Connect to DB
    
$conn = new mysqli($servername$dbUsername$dbPassword$dbName);
    if (
$conn->connect_error) {
        die(
"DB connection failed: " $conn->connect_error);
    }

    
// Use prepared statement to query credentials
    // In production, you should store hashed passwords and use password_verify()
    
$stmt $conn->prepare("SELECT * FROM `User` WHERE userName = ? AND password = ? LIMIT 1");
    
$stmt->bind_param("ss"$username$password);
    
$stmt->execute();
    
$result $stmt->get_result();

    
// Check if exactly 1 user was found
    
if ($result && $result->num_rows === 1) {
        
// Valid credentials, store user in session
        
$_SESSION['logged_in_user'] = $username;

        
// Redirect to cms.php
        
header("Location: cms.php");
        exit;
    } else {
        
$error "Invalid username or password.";
    }

    
$stmt->close();
    
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Login - Cinema Staff</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f7f7f7;
      margin: 0; padding: 0;
    }
    .login-container {
      max-width: 400px;
      margin: 80px auto;
      background: #fff;
      padding: 20px;
      box-shadow: 0 0 6px rgba(0,0,0,0.1);
    }
    h2 {
      text-align: center;
      margin-bottom: 1em;
    }
    label {
      display: block;
      margin-top: 1em;
    }
    input[type="text"],
    input[type="password"] {
      width: 100%;
      padding: 8px 10px;
      box-sizing: border-box;
      margin-top: 5px;
    }
    .error {
      color: red;
      font-weight: bold;
      margin-bottom: 1em;
      text-align: center;
    }
    button {
      margin-top: 1em;
      width: 100%;
      padding: 10px;
      background: #007BFF;
      color: #fff;
      border: none; border-radius: 4px;
      cursor: pointer;
      font-size: 1em;
    }
    button:hover {
      background: #0056b3;
    }
  </style>
</head>
<body>
  <div class="login-container">
    <h2>Staff Login</h2>
    <?php if (!empty($error)) : ?>
      <div class="error"><?= htmlspecialchars($error?></div>
    <?php endif; ?>

    <form method="POST" action="">
      <label for="username">Username:</label>
      <input type="text" name="username" id="username" required />

      <label for="password">Password:</label>
      <input type="password" name="password" id="password" required />

      <button type="submit">Login</button>
    </form>
  </div>
</body>
</html>