<?php
session_start();
require_once 'includes/config.php';
require_once 'includes/auth.php';

if (isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = $_POST['username'] ?? '';
    $pass = $_POST['password'] ?? '';
    if (login($user, $pass)) {
        header('Location: index.php');
        exit;
    } else {
        $error = 'Username atau password salah.';
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login — Ship Network Admin</title>
<link rel="stylesheet" href="assets/css/tabler-icons.min.css">
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body class="login-body">
<div class="login-wrap">
  <div class="login-card">
    <div class="login-header">
      <div class="login-icon"><i class="ti ti-ship"></i></div>
      <h1>Ship Network Admin</h1>
      <p><?= htmlspecialchars(SHIP_NAME) ?></p>
    </div>
    <?php if ($error): ?>
    <div class="alert-error"><i class="ti ti-alert-circle"></i> <?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    <form method="POST" class="login-form">
      <div class="form-group">
        <label><i class="ti ti-user"></i> Username</label>
        <input type="text" name="username" placeholder="admin" required autofocus>
      </div>
      <div class="form-group">
        <label><i class="ti ti-lock"></i> Password</label>
        <input type="password" name="password" placeholder="••••••••" required>
      </div>
      <button type="submit" class="btn btn-primary btn-block">
        <i class="ti ti-login"></i> Masuk
      </button>
    </form>
    <div class="login-footer">
      Unified Network Dashboard v1.0 &mdash; LAN Kapal
    </div>
  </div>
</div>
</body>
</html>
