<?php
if(session_status() === PHP_SESSION_NONE) session_start();
require "../config/config.php";
if(!defined("APPURL"))  define("APPURL",  "http://localhost/hotel-booking/");
if(!defined("ADMINURL")) define("ADMINURL", "http://localhost/hotel-booking/admin/");

if(isset($_SESSION['admin_username'])){
    header("location: " . ADMINURL . "index.php");
    exit;
}

$error = '';
if(isset($_POST['submit'])){
    if(empty($_POST['email']) || empty($_POST['password'])){
        $error = 'All fields are required.';
    } else {
        $email    = $_POST['email'];
        $password = $_POST['password'];

        $login = $conn->prepare("SELECT * FROM admins WHERE email = :email");
        $login->bindParam(':email', $email);
        $login->execute();

        if($login->rowCount() > 0){
            $fetch = $login->fetch(PDO::FETCH_ASSOC);
            if(password_verify($password, $fetch['mypassword'])){
                $_SESSION['admin_username'] = $fetch['username'];
                $_SESSION['admin_id']       = $fetch['id'];
                header("location: " . ADMINURL . "index.php");
                exit;
            } else {
                $error = 'Incorrect email or password.';
            }
        } else {
            $error = 'Email does not exist.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Admin Login — Hotel Booking</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <style>
    * { font-family: 'Inter', sans-serif; }
    body {
      min-height: 100vh; display: flex; align-items: center; justify-content: center;
      background: linear-gradient(135deg, #0a1628 0%, #1a2d5a 50%, #0a1628 100%);
    }
    .login-wrapper { width: 100%; max-width: 440px; padding: 20px; }
    .login-logo {
      text-align: center; margin-bottom: 32px;
    }
    .login-logo h1 { font-size: 28px; font-weight: 700; color: #fff; }
    .login-logo h1 span { color: #4e9af1; }
    .login-logo p { color: rgba(255,255,255,0.5); font-size: 14px; }

    .login-card {
      background: rgba(255,255,255,0.04);
      backdrop-filter: blur(20px);
      border: 1px solid rgba(255,255,255,0.1);
      border-radius: 20px; padding: 40px;
      box-shadow: 0 24px 60px rgba(0,0,0,0.5);
    }
    .login-card label { color: rgba(255,255,255,0.7); font-size: 13px; font-weight: 500; margin-bottom: 6px; }
    .login-card .form-control {
      background: rgba(255,255,255,0.06); border: 1.5px solid rgba(255,255,255,0.12);
      color: #fff; border-radius: 10px; padding: 12px 16px; font-size: 14px;
    }
    .login-card .form-control::placeholder { color: rgba(255,255,255,0.3); }
    .login-card .form-control:focus {
      background: rgba(255,255,255,0.1); border-color: #4e9af1;
      box-shadow: 0 0 0 3px rgba(78,154,241,0.2); color: #fff;
    }
    .input-icon { position: relative; }
    .input-icon i {
      position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
      color: rgba(255,255,255,0.3); font-size: 15px;
    }
    .input-icon .form-control { padding-left: 42px; }
    .btn-login {
      width: 100%; padding: 14px;
      background: linear-gradient(135deg, #4e9af1, #1565c0);
      color: #fff; border: none; border-radius: 10px;
      font-size: 15px; font-weight: 600; letter-spacing: 0.5px;
      transition: all 0.3s; cursor: pointer;
    }
    .btn-login:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(78,154,241,0.4); }
    .error-msg { background: rgba(220,53,69,0.15); border: 1px solid rgba(220,53,69,0.3); color: #ff8a8a; border-radius: 8px; padding: 12px 16px; font-size: 13px; margin-bottom: 20px; }
    .back-link { text-align: center; margin-top: 20px; }
    .back-link a { color: rgba(255,255,255,0.4); font-size: 13px; text-decoration: none; }
    .back-link a:hover { color: #4e9af1; }
  </style>
</head>
<body>
  <div class="login-wrapper">
    <div class="login-logo">
      <h1>Vacation<span>Admin</span></h1>
      <p>Hotel Management System</p>
    </div>

    <div class="login-card">
      <h4 style="color:#fff; font-weight:700; margin-bottom:8px;">Welcome back</h4>
      <p style="color:rgba(255,255,255,0.4); font-size:14px; margin-bottom:28px;">Sign in to your admin account</p>

      <?php if($error): ?>
        <div class="error-msg"><i class="fa fa-exclamation-circle mr-2"></i><?php echo $error; ?></div>
      <?php endif; ?>

      <form method="POST" action="login.php">
        <div class="form-group">
          <label>Email Address</label>
          <div class="input-icon">
            <i class="fa fa-envelope"></i>
            <input type="email" name="email" class="form-control" placeholder="admin@hotel.com" required>
          </div>
        </div>
        <div class="form-group mb-4">
          <label>Password</label>
          <div class="input-icon">
            <i class="fa fa-lock"></i>
            <input type="password" name="password" class="form-control" placeholder="••••••••" required>
          </div>
        </div>
        <button type="submit" name="submit" class="btn-login">
          <i class="fa fa-sign-in mr-2"></i> Sign In
        </button>
      </form>
    </div>

    <div class="back-link">
      <a href="<?php echo APPURL; ?>"><i class="fa fa-arrow-left mr-1"></i> Back to website</a>
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
