<?php
if (session_status() === PHP_SESSION_NONE) session_start();
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    header('Location: dashboard.php'); exit;
}
require_once dirname(__DIR__) . '/api/konak.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = isset($_POST['username']) ? trim($_POST['username']) : '';
    $pass = isset($_POST['password']) ? trim($_POST['password']) : '';
    if ($user !== '' && $pass !== '') {
        $us = mysqli_real_escape_string($koneksi, $user);
        $q = mysqli_query($koneksi, "SELECT id,username,password FROM admin_users WHERE username='$us' LIMIT 1");
        if ($q && mysqli_num_rows($q) > 0) {
            $row = mysqli_fetch_assoc($q);
            if (password_verify($pass, $row['password'])) {
                $_SESSION['admin_logged_in'] = true;
                $_SESSION['admin_username'] = $row['username'];
                $_SESSION['admin_id'] = $row['id'];
                header('Location: dashboard.php'); exit;
            } else { $error = 'Invalid password.'; }
        } else { $error = 'Admin user not found.'; }
    } else { $error = 'Please fill in all fields.'; }
}
?>
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Login</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body class="auth-body">
<div class="auth-wrap">
  <div class="auth-card">
    <div class="auth-logo">
      <svg width="48" height="48" viewBox="0 0 48 48" fill="none">
        <rect width="48" height="48" rx="12" fill="var(--primary)"/>
        <path d="M14 34V20l10-8 10 8v14H28v-8h-8v8H14z" fill="white"/>
      </svg>
      <h1>Admin Panel</h1>
      <p>Sign in to your account</p>
    </div>
    <?php if ($error): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    <form method="POST">
      <div class="form-group">
        <label>Username</label>
        <input type="text" name="username" placeholder="Enter username" required autocomplete="username">
      </div>
      <div class="form-group">
        <label>Password</label>
        <div class="input-eye">
          <input type="password" name="password" id="pwdInput" placeholder="Enter password" required autocomplete="current-password">
          <button type="button" class="eye-btn" onclick="togglePwd()">
            <svg id="eyeIcon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
          </button>
        </div>
      </div>
      <button type="submit" class="btn btn-primary btn-full">Sign In</button>
    </form>
    <div class="theme-toggle-wrap">
      <button class="theme-btn" id="themeToggle" onclick="toggleTheme()">🌙 Dark Mode</button>
    </div>
  </div>
</div>
<script src="assets/script.js"></script>
<script>
function togglePwd(){
  const i=document.getElementById('pwdInput');
  i.type=i.type==='password'?'text':'password';
}
</script>
</body>
</html>
