<?php
require_once __DIR__.'/../inc/config.php';
require_once __DIR__.'/../inc/db.php';
require_once __DIR__.'/../inc/csrf.php';

$err='';
if($_SERVER['REQUEST_METHOD']==='POST'){
  if(!csrf_verify()) $err='CSRF gagal';
  else{
    $u=$_POST['username']??''; $p=$_POST['password']??'';
    $st=db()->prepare("SELECT * FROM admins WHERE username=? LIMIT 1"); $st->execute([$u]); $a=$st->fetch();
    if($a && password_verify($p,$a['password_hash'])){ $_SESSION['admin_id']=$a['id']; $_SESSION['admin_role'] = $a['role']; header('Location: dashboard.php'); exit; }
    else $err='Login gagal';
  }
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../public/assets/css/style.css">
    <title>Admin Login</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background-color: #f8fafc;
        }
        .login-card {
            max-width: 420px;
            width: 100%;
        }
        .login-card form label {
            display: block;
            margin-bottom: 16px;
            font-weight: 600;
        }
        .login-card form input {
            width: 100%;
            padding: 10px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            margin-top: 6px;
            box-sizing: border-box;
        }
        .login-card form button {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            margin-top: 8px;
        }
    </style>
</head>
<body>
<div class="card login-card">
    <h2 style="text-align: center;">Admin Login</h2>
    <?php if($err):?><p style="color:#b91c1c; text-align:center;"><?=$err?></p><?php endif;?>
    <form method="post">
        <?=csrf_field()?>
        <label>Username
            <input name="username" required>
        </label>
        <label>Password
            <input type="password" name="password" required>
        </label>
        <button class="cta">Masuk</button>
    </form>
</div>
</body>
</html>