<?php
require_once __DIR__ . '/../config/helpers.php';
require_once __DIR__ . '/../config/db.php';
if(!empty($_SESSION['user'])){ header('Location: index.php'); exit; }
$err=null;
if(is_post()){
  $u=trim($_POST['username']??''); $p=trim($_POST['password']??'');
  $pdo=db(); $st=$pdo->prepare("SELECT * FROM users WHERE username=?"); $st->execute([$u]);
  $user=$st->fetch();
  if($user && password_verify($p,$user['password_hash'])){ $_SESSION['user']=['id'=>$user['id'],'username'=>$user['username']]; header('Location:index.php'); exit; }
  else{ $err="Username atau password salah."; }
}
?><!doctype html><html lang="id"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Admin</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
<style>body{font-family:Inter,ui-sans-serif,system-ui}</style>
</head><body class="min-h-screen grid place-items-center bg-gray-50">
  <form method="post" class="bg-white border border-gray-200 rounded-2xl p-6 w-full max-w-md shadow-sm">
    <div class="flex items-center gap-2 mb-4">
      <div class="w-10 h-10 rounded-xl bg-blue-50 grid place-content-center text-blue-600 font-bold">F</div>
      <div class="font-semibold text-gray-900">Fortopolio Admin</div>
    </div>
    <h1 class="text-xl font-semibold mb-4">Masuk</h1>
    <?php if($err): ?><div class="mb-3 text-sm text-red-600 bg-red-50 border border-red-100 rounded-lg p-2"><?php echo e($err) ?></div><?php endif; ?>
    <label class="block text-sm font-medium text-gray-700">Username</label>
    <input name="username" class="mt-1 w-full border rounded-lg px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-200" required autofocus>
    <label class="block text-sm font-medium text-gray-700 mt-3">Password</label>
    <input type="password" name="password" class="mt-1 w-full border rounded-lg px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-200" required>
    <button class="mt-5 w-full bg-blue-600 hover:bg-blue-700 text-white rounded-lg py-2 font-semibold">Masuk</button>
  </form>
</body></html>
