<?php require_once __DIR__.'/../config/config.php';
if(current_user()){$u=current_user(); redirect(BASE_URL.($u['role']==='admin'?'/admin/dashboard.php':'/user/dashboard.php'));}
$err=null;
if($_SERVER['REQUEST_METHOD']==='POST'){csrf_check();
  $email=trim($_POST['email']??''); $pass=$_POST['password']??'';
  $remember = isset($_POST['remember']) ? 1 : 1; // always on — user requested persistent 30-day login
  $s=$pdo->prepare("SELECT * FROM users WHERE email=?"); $s->execute([$email]); $u=$s->fetch();
  if($u && password_verify($pass,$u['password_hash'])){
    if($u['status']==='banned') $err='আপনার একাউন্ট বন্ধ।';
    else{
      $_SESSION['uid']=$u['id'];
      // 30-day remember cookie
      try { remember_issue($pdo, (int)$u['id']); } catch(Throwable $e){}
      try{ activity_log_login($pdo,$u['id']); }catch(Throwable $e){}
      redirect(BASE_URL.($u['role']==='admin'?'/admin/dashboard.php':'/user/dashboard.php'));
    }
  } else $err='ভুল ইমেইল বা পাসওয়ার্ড';
}
?><!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Login</title><link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../assets/css/style.css"></head>
<body><div class="auth-wrap"><div class="auth-card">
<h1>🔐 লগইন</h1><p class="sub">আপনার একাউন্টে প্রবেশ করুন (৩০ দিন লগইন থাকবে)</p>
<?php if($err):?><div class="alert err"><?=e($err)?></div><?php endif;?>
<form method="post"><?=csrf_field()?>
<div class="field"><label>ইমেইল</label><input class="input" type="email" name="email" required autofocus></div>
<div class="field"><label>পাসওয়ার্ড</label><input class="input" type="password" name="password" required></div>
<label style="display:flex;gap:8px;align-items:center;margin:8px 0 12px;font-size:14px;color:#475569">
  <input type="checkbox" name="remember" value="1" checked> ৩০ দিন পর্যন্ত লগইন রাখুন
</label>
<button class="btn">লগইন</button>
</form>
<div class="link">নতুন? <a href="register.php">একাউন্ট তৈরি করুন</a></div>
</div></div></body></html>
