<?php
session_start();
require_once 'config/database.php';
if (isset($_SESSION['user_id'])) { header('Location: index.php'); exit; }
$e = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $u = trim($_POST['username'] ?? '');
  $p = $_POST['password'] ?? '';
  if (!$u || !$p) {
    $e = 'ইউজারনেম এবং পাসওয়ার্ড প্রয়োজন';
  } else {
    try {
      $s = $pdo->prepare("SELECT id,username,password,email,role FROM users WHERE username=? AND status='active'");
      $s->execute([$u]);
      $r = $s->fetch();
      if ($r && password_verify($p, $r['password'])) {
        $_SESSION['user_id'] = $r['id'];
        $_SESSION['username'] = $r['username'];
        $_SESSION['user_role'] = $r['role'];
        $_SESSION['role'] = $r['role'];
        $_SESSION['user_email'] = $r['email'];
        if ($r['role'] === 'teacher') {
          $s = $pdo->prepare("SELECT CONCAT(first_name,' ',last_name) n FROM teachers WHERE user_id=?");
          $s->execute([$r['id']]);
          $n = $s->fetch();
          $_SESSION['user_name'] = $n ? $n['n'] : $r['username'];
        } elseif ($r['role'] === 'student') {
          $s = $pdo->prepare("SELECT CONCAT(first_name,' ',last_name) n FROM students WHERE user_id=?");
          $s->execute([$r['id']]);
          $n = $s->fetch();
          $_SESSION['user_name'] = $n ? $n['n'] : $r['username'];
        } else {
          $_SESSION['user_name'] = $r['username'];
        }
        header('Location: ' . ($r['role'] === 'teacher' ? 'teachers/dashboard.php' : 'index.php'));
        exit;
      } else {
        $e = 'ভুল ইউজারনেম অথবা পাসওয়ার্ড';
      }
    } catch (PDOException $_) {
      $e = 'লগইনে সমস্যা হয়েছে। আবার চেষ্টা করুন।';
    }
  }
}
function h($s) { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }
?><!DOCTYPE html><html lang="bn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>লগইন</title>
<style>
*,body{margin:0;padding:0;box-sizing:border-box}
body{height:100vh;display:flex;align-items:center;justify-content:center;font-family:'Hind Siliguri',Arial,'Siyam Rupali','Noto Sans Bengali',sans-serif;background:#1a1818;color:#d4ccc4}
.c{background:#252220;border-radius:12px;border:1px solid #3a3735;width:100%;max-width:340px;margin:20px;box-shadow:0 8px 32px rgba(0,0,0,.4)}@keyframes u{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:translateY(0)}}
.h{padding:16px 18px 0;text-align:center}
.h i{font-size:1.3rem;width:40px;height:40px;border-radius:10px;display:inline-flex;align-items:center;justify-content:center;margin-bottom:5px;background:#2f2c2a;border:1px solid #3a3735}
.h h3{font-size:.88rem;color:#d4ccc4;margin:0 0 2px}
.h p{font-size:.7rem;color:#888078;margin:0 0 10px}
.b{padding:0 18px 16px}
.b label{display:block;font-size:.7rem;font-weight:600;color:#a09888;margin-bottom:2px}
.g{display:flex;border:1px solid #3a3735;border-radius:6px;overflow:hidden;transition:border-color .2s;background:#1e1c1b}
.g:focus-within{border-color:#6a6058}
.g i{width:30px;display:flex;align-items:center;justify-content:center;color:#706860;font-size:.72rem;border-right:1px solid #3a3735;flex-shrink:0}
.g input{flex:1;border:none;padding:6px 8px;font-size:.78rem;outline:none;font-family:inherit;background:transparent;color:#d4ccc4}
.g .t{width:30px;border:none;background:none;cursor:pointer;color:#706860;font-size:.72rem;display:flex;align-items:center;justify-content:center;font-family:inherit}
.g .t:hover{color:#a09888}
.btn{display:block;width:100%;border:none;border-radius:6px;padding:7px;font-size:.78rem;font-weight:600;cursor:pointer;background:linear-gradient(135deg,#6a7a60,#5a6a50);color:#e8e4dc;font-family:inherit;transition:all .2s}
.btn:hover{box-shadow:0 4px 12px rgba(74,90,60,.3)}
.mb{margin-bottom:6px}
.a{padding:5px 8px;border-radius:5px;font-size:.7rem;margin-bottom:8px}
.a-s{background:#1a2a18;color:#7aaa6a;border:1px solid #2a3a28}.a-d{background:#2a1818;color:#ca7a6a;border:1px solid #3a2828}
.f{font-size:.65rem;color:#706860;text-align:center;padding:8px 0 0;border-top:1px solid #3a3735;margin-top:8px}
.f s{color:#a09888}
</style></head><body>
<div class=c>
<div class=h><i>🎓</i><h3>স্কুল ম্যানেজমেন্ট</h3><p>লগইন করুন</p></div>
<div class=b>
<?php if (isset($_GET['logout_success'])): ?><div class="a a-s">✓ আপনি সফলভাবে লগআউট করেছেন।</div><?php endif; ?>
<?php if ($e): ?><div class="a a-d">⚠ <?=h($e)?></div><?php endif; ?>
<form method=post action="">
<div class=mb><label>ইউজারনেম</label><div class=g><i>👤</i><input type=text name=username placeholder="ইউজারনেম" required value="<?=h($_POST['username']??'')?>"></div></div>
<div class=mb><label>পাসওয়ার্ড</label><div class=g><i>🔒</i><input type=password id=p name=password placeholder="পাসওয়ার্ড" required><button class=t type=button onclick="pt()" id=eb>👁</button></div></div>
<button class=btn>▶ লগইন করুন</button>
</form>
<div class=f>ডিফল্ট: <s>admin</s> / <s>admin123</s></div>
</div></div>
<script>
function pt(){var e=document.getElementById('p'),b=document.getElementById('eb');e.type=e.type==='password'?'text':'password';b.textContent=e.type==='password'?'👁':'🙈'}
setTimeout(function(){document.querySelectorAll('.a').forEach(function(e){e.remove()})},5000);
</script></body></html>
