<?php
require __DIR__ . '/security_headers.php';
require __DIR__ . '/auth_guard.php';
require_student();
require __DIR__ . '/db.php';

$student = $_SESSION['student'] ?? null;
if (!$student || empty($student['id'])) {
  header('Location: /index.php'); exit;
}

$stmt = $pdo->prepare('SELECT id, id_number, student_number, first_name, last_name,
                              place_of_birth, date_of_birth, nationality, email,
                              document_text, document_path, created_at
                       FROM students
                       WHERE id = ?
                       LIMIT 1');
$stmt->execute([$student['id']]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) { http_response_code(404); die('Student not found'); }

function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

$docText = trim((string)($row['document_text'] ?? ''));
$docPath = trim((string)($row['document_path'] ?? ''));
$downloadHref = $docPath !== '' ? ('/public/uploads/' . rawurlencode(basename($docPath))) : null;

$fullName = trim(($row['first_name'] ?? '').' '.($row['last_name'] ?? ''));
$enrollmentSince = $row['created_at'] ?? '—';
$nationality = $row['nationality'] ?: '—';
$status = 'Active';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Student Portal</title>
  <link rel="icon" type="image/svg+xml" href="/public/img/umis_logo.svg">


  <link rel="stylesheet" href="/public/css/styles.css">
  <link rel="stylesheet" href="/public/css/admin.css">

  <style>
    .topbar{
      position:sticky; top:0; z-index:40; background:#fff;
      border-bottom:1px solid var(--border);
    }
    .topbar .inner{
      max-width:1100px; margin:0 auto; padding:12px 16px;
      display:flex; align-items:center; justify-content:space-between; gap:12px;
    }
    .brand-inline{ display:flex; align-items:center; gap:10px; font-weight:800; }
    .brand-inline img{ height:22px }
    .portal{ max-width:1100px; margin:0 auto; padding:22px 16px; }

    .profile-header{ display:flex; align-items:center; gap:12px; margin:0 0 6px; }
    .avatar{ width:44px; height:44px; border-radius:50%; background:linear-gradient(135deg,#dbeafe,#eff6ff);
      display:flex; align-items:center; justify-content:center; font-weight:800; color:#1d4ed8; border:1px solid var(--border); }
    .name{ font-size:22px; font-weight:800; }
    .sub{ color:var(--muted); font-size:13px; }

    .chip{ display:inline-flex; align-items:center; gap:8px; padding:6px 10px;
      border:1px solid var(--border); border-radius:999px; background:#fff; font-weight:600; font-size:12px; }
    .pill{ display:inline-block; padding:6px 10px; border-radius:999px; background:#ecfdf5; color:#065f46;
      font-weight:700; font-size:12px; border:1px solid #a7f3d0; }
  </style>
</head>
<body>

  <header class="topbar">
    <div class="inner">
      <div class="brand-inline">
        <img src="/public/img/umis_logo.svg" alt="logo">
        <span>Student Portal</span>
      </div>
    </div>
  </header>

  <main class="portal">
      <?php if (!empty($_GET['msg'])): ?>
  <div class="alert" style="color:green; margin-bottom:15px;">
    <?php echo htmlspecialchars($_GET['msg']); ?>
  </div>
<?php endif; ?>

    <section class="card">
      <div class="profile-header">
        <div class="avatar">
          <?php
            $initials = strtoupper(mb_substr($row['first_name'] ?? '',0,1).mb_substr($row['last_name'] ?? '',0,1));
            echo h($initials ?: 'ST');
          ?>
        </div>
        <div>
          <div class="name"><?php echo h($fullName ?: 'Student'); ?></div>
          <div class="sub">Student #: <?php echo h($row['student_number'] ?: '—'); ?></div>
        </div>
      </div>

      <div class="kv" style="margin-top:10px">
        <div class="k">ID Number:</div>        <div class="v"><?php echo h($row['id_number']); ?></div>
        <div class="k">Student Number:</div>   <div class="v"><?php echo h($row['student_number']); ?></div>
        <div class="k">First Name:</div>       <div class="v"><?php echo h($row['first_name']); ?></div>
        <div class="k">Last Name:</div>        <div class="v"><?php echo h($row['last_name']); ?></div>
        <div class="k">Place of Birth:</div>   <div class="v"><?php echo h($row['place_of_birth']); ?></div>
        <div class="k">Date of Birth:</div>    <div class="v"><?php echo h($row['date_of_birth']); ?></div>
        <div class="k">Nationality:</div>      <div class="v"><span class="chip"><?php echo h($nationality); ?></span></div>
        <div class="k">Email:</div>            <div class="v"><?php echo h($row['email']); ?></div>

        <div class="k">Document / Notes:</div>
        <div class="v">
          <?php if ($docText !== ''): ?>
            <?php echo nl2br(h($docText)); ?>
          <?php elseif ($downloadHref): ?>
            <a class="btn" href="<?php echo h($downloadHref); ?>" download>Download document</a>
          <?php else: ?>
            <span class="link-muted">—</span>
          <?php endif; ?>
        </div>
      </div>
    </section>

    <section class="panel-grid" style="margin-top:16px">
      <div class="card">
        <div class="card-title">Enrollment Snapshot</div>
        <div class="kv">
          <div class="k">Status:</div>      <div class="v"><span class="pill"><?php echo h($status); ?></span></div>
          <div class="k">Since:</div>       <div class="v"><?php echo h($enrollmentSince); ?></div>
          <div class="k">Nationality:</div> <div class="v"><span class="chip"><?php echo h($nationality); ?></span></div>
        </div>
      </div>

      <div class="card">
        <div class="card-title">Quick Actions</div>
        <div class="list">
          <div class="list-row">
            <div class="title">Change password</div>
            <a class="btn btn-small" href="/student_change_password.php">Go</a>
          </div>
          <div class="list-row">
            <div class="title">Logout</div>
            <a class="btn btn-small" href="/student_logout.php">Logout</a>
          </div>
        </div>
      </div>
    </section>
  </main>

</body>
</html>
