<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once dirname(__DIR__) . '/config/config.php';

if (isset($_SESSION['admin_id'])) { header('Location: dashboard.php'); exit; }

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    if ($username && $password) {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM admins WHERE username = ?");
        $stmt->execute([$username]);
        $admin = $stmt->fetch();
        if ($admin && password_verify($password, $admin['password'])) {
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_name'] = $admin['display_name'];
            header('Location: dashboard.php');
            exit;
        }
    }
    $error = 'ভুল ইউজারনেম বা পাসওয়ার্ড!';
}
?>
<!DOCTYPE html>
<html lang="bn" data-theme="dark">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>অ্যাডমিন লগইন — তজিয়া গ্রুপ</title>
  <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
  <link rel="stylesheet" href="<?= SITE_URL ?>/assets/css/style.css">
</head>
<body>
  <div class="login-page">
    <div class="login-card glass">
      <h2 style="text-align:center;">🔐 অ্যাডমিন লগইন</h2>
      <p style="text-align:center;color:var(--text-muted);margin-bottom:24px;">তজিয়া গ্রুপ কন্ট্রোল প্যানেল</p>
      <?php if ($error): ?>
      <div class="alert alert-error"><?= e($error) ?></div>
      <?php endif; ?>
      <form method="POST">
        <div class="form-group">
          <label><i class="fas fa-user"></i> ইউজারনেম</label>
          <input type="text" name="username" class="form-control" required autofocus placeholder="admin">
        </div>
        <div class="form-group">
          <label><i class="fas fa-lock"></i> পাসওয়ার্ড</label>
          <input type="password" name="password" class="form-control" required placeholder="••••••">
        </div>
        <button type="submit" class="btn btn-primary" style="width:100%;justify-content:center;">
          <i class="fas fa-sign-in-alt"></i> লগইন
        </button>
      </form>
      <p style="text-align:center;margin-top:16px;"><a href="<?= SITE_URL ?>"><i class="fas fa-arrow-left"></i> হোমে ফিরে যান</a></p>
    </div>
  </div>
</body>
</html>
