<?php
declare(strict_types=1);
ini_set('display_errors','1');
error_reporting(E_ALL);

require_once __DIR__ . '/../lib/db.php';

start_session();
$db = db();

$err = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // Form alanlarını esnek oku (eski isimler kalmış olabilir)
  $username = trim((string)($_POST['username'] ?? $_POST['email'] ?? ''));
  $pass     = (string)($_POST['pass'] ?? $_POST['password'] ?? '');

  if ($username === '' || $pass === '') {
    $err = 'E-posta ve şifre gerekli.';
  } else {
    $stmt = $db->prepare("SELECT id, password_hash FROM users WHERE username=? LIMIT 1");
    if (!$stmt) {
      $err = 'DB hata: prepare başarısız.';
    } else {
      $stmt->bind_param('s', $username);
      $stmt->execute();
      $stmt->bind_result($uid, $hash);

      if ($stmt->fetch() && password_verify($pass, (string)$hash)) {
        $_SESSION['uid'] = (int)$uid;
        session_regenerate_id(true);
        header('Location: controller.php');
        exit;
      } else {
        $err = 'E-posta veya şifre yanlış.';
      }
      $stmt->close();
    }
  }
}
?>
<!doctype html>
<html lang="tr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <title>Giriş</title>
  <meta name="theme-color" content="#1d4ed8">
  <style>
    :root{--bg:#05070d;--bg2:#0b1220;--b:#1d4ed8;--b2:#60a5fa;--w:#fff;--m:rgba(255,255,255,.72);--line:rgba(255,255,255,.10)}
    *{box-sizing:border-box}
    body{margin:0;font-family:system-ui;color:var(--w);min-height:100vh;display:grid;place-items:center;padding:18px;
      background:radial-gradient(900px 600px at 20% 15%, rgba(29,78,216,.35), transparent 60%),
               linear-gradient(180deg,var(--bg),var(--bg2));}
    .card{width:min(520px,94vw);border-radius:22px;padding:18px;
      background:linear-gradient(180deg, rgba(255,255,255,.08), rgba(255,255,255,.04));
      border:1px solid rgba(255,255,255,.12);backdrop-filter:blur(10px)}
    .top{display:flex;align-items:center;gap:10px;margin-bottom:10px}
    .dot{width:10px;height:10px;border-radius:999px;background:linear-gradient(135deg,var(--b),var(--b2))}
    .ttl{font-weight:950;letter-spacing:.12em;font-size:12px}
    .h1{font-size:34px;font-weight:950;line-height:1.05;margin:10px 0}
    .p{color:var(--m);font-size:14px;line-height:1.55;margin:0 0 12px}
    input{width:100%;padding:14px;border-radius:16px;border:1px solid rgba(255,255,255,.12);background:rgba(0,0,0,.22);color:#fff;outline:none}
    button{width:100%;margin-top:10px;padding:14px;border-radius:16px;border:0;
      background:linear-gradient(135deg,var(--b),var(--b2));color:#fff;font-weight:950;letter-spacing:.06em;cursor:pointer}
    .err{margin-top:10px;color:#fca5a5;font-size:13px}
  </style>
</head>
<body>
  <div class="card">
    <div class="top"><div class="dot"></div><div class="ttl">TAHTA KONTROL</div></div>
    <div class="h1">Giriş</div>
    <div class="p">Mobil kontrol paneline erişmek için giriş yap.</div>

    <?php if($err): ?><div class="err"><?= e($err) ?></div><?php endif; ?>

    <form method="post" action="" autocomplete="off" novalidate>
      <div style="margin-top:10px">
        <input name="username" placeholder="E-posta" inputmode="email" autocomplete="username" required>
      </div>
      <div style="margin-top:10px">
        <input name="pass" type="password" placeholder="Şifre" autocomplete="current-password" required>
      </div>
      <button type="submit">GİRİŞ YAP</button>
    </form>
  </div>
</body>
</html>
