<?php session_start();require_once'../config/database.php';
$sid=(int)($_GET['id']??0);
if(!$sid||!isset($_SESSION['user_id'])){header('Location: list.php');exit;}
$st=$pdo->prepare("SELECT s.*,c.name AS cn,c.section AS cs,u.username,u.email AS ue FROM students s LEFT JOIN classes c ON s.class_id=c.id LEFT JOIN users u ON s.user_id=u.id WHERE s.id=?");
$st->execute([$sid]);$st=$st->fetch();if(!$st){header('Location: list.php');exit;}
function h($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}
$ra=$pdo->prepare("SELECT date,status,remarks FROM attendance WHERE student_id=? ORDER BY date DESC LIMIT 10");$ra->execute([$sid]);$ra=$ra->fetchAll();
$rr=$pdo->prepare("SELECT e.name AS en,s.name AS sn,r.marks_obtained,es.max_marks,r.grade,r.created_at FROM results r JOIN exams e ON r.exam_id=e.id JOIN subjects s ON r.subject_id=s.id JOIN exam_subjects es ON r.exam_id=es.exam_id AND r.subject_id=es.subject_id WHERE r.student_id=? ORDER BY r.created_at DESC LIMIT 10");$rr->execute([$sid]);$rr=$rr->fetchAll();
$fe=$pdo->prepare("SELECT fee_type,amount,paid_amount,status,due_date FROM fees WHERE student_id=? ORDER BY due_date DESC LIMIT 5");$fe->execute([$sid]);$fe=$fe->fetchAll();
$sel_subj=null;if($st['selected_subjects']){$sj=json_decode($st['selected_subjects'],true);if(is_array($sj)&&!empty($sj)){$pl=str_repeat('?,',count($sj)-1).'?';$s=$pdo->prepare("SELECT name FROM subjects WHERE id IN($pl)AND status='active'");$s->execute($sj);$sel_subj=$s->fetchAll(PDO::FETCH_COLUMN);}}
function gs($s){switch($s){case'active':return['success','সক্রিয়'];case'inactive':return['danger','নিষ্ক্রিয়'];case'graduated':return['info','স্নাতক'];case'transferred':return['warning','বদলি'];default:return['secondary',$s];}}
function gg($g){switch($g){case'male':return'পুরুষ';case'female':return'মহিলা';case'other':return'অন্যান্য';default:return'নির্দিষ্ট নয়';}}
function ga($a){switch($a){case'present':return['success','উপস্থিত'];case'absent':return['danger','অনুপস্থিত'];case'late':return['warning','দেরি'];case'excused':return['info','ছুটি'];default:return['secondary',$a];}}
function gf($f){switch($f){case'paid':return['success','পরিশোধিত'];case'partial':return['warning','আংশিক'];case'pending':return['info','অপেক্ষমান'];case'overdue':return['danger','বকেয়া'];default:return['secondary',$f];}}
$role=$_SESSION['user_role'];$back=$role=='teacher'?'../teachers/dashboard.php':'../index.php';
?><!DOCTYPE html><html lang="bn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title><?=h($st['first_name'].' '.$st['last_name'])?> - ছাত্র বিস্তারিত</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
<style>:root{--p:#6C63FF;--s:#A855F7}*{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}body{font-family:'Hind Siliguri',sans-serif;background:linear-gradient(135deg,#0F0C29,#302B63,#24243E);min-height:100vh;color:#fff;font-size:.85rem}
.navbar{background:linear-gradient(135deg,#1a1a3e,#2d1b69)!important;padding:5px 0!important;border-bottom:1px solid rgba(168,85,247,.2)}.navbar-brand{font-size:.9rem!important}
.cd{background:rgba(255,255,255,.06);backdrop-filter:blur(16px);border-radius:14px;box-shadow:0 8px 32px rgba(0,0,0,.25);border:1px solid rgba(255,255,255,.08);overflow:hidden;margin-bottom:14px}
.cd .ch{background:linear-gradient(135deg,rgba(108,99,255,.35),rgba(168,85,247,.25));padding:7px 12px;font-weight:700;font-size:.88rem;backdrop-filter:blur(4px)}.cd .cb{padding:10px 12px}
.dh{font-size:.78rem;font-weight:600;margin-bottom:6px}.dv{font-size:.82rem;color:rgba(255,255,255,.9)}
.pp{width:130px;height:130px;border-radius:50%;object-fit:cover;border:3px solid rgba(168,85,247,.4);box-shadow:0 0 20px rgba(168,85,247,.2)}.pp-n{width:130px;height:130px;border-radius:50%;background:rgba(168,85,247,.15);display:flex;align-items:center;justify-content:center;border:3px solid rgba(168,85,247,.3)}
.bdg{padding:3px 10px;font-size:.72rem;font-weight:600;border-radius:6px}
.tb{width:100%;border-collapse:collapse;font-size:.78rem}.tb th{padding:5px 7px;font-weight:700;font-size:.72rem;border-bottom:1px solid rgba(255,255,255,.08);background:rgba(108,99,255,.12)}.tb td{padding:5px 7px;border-bottom:1px solid rgba(255,255,255,.04)}
.btn-v{background:linear-gradient(135deg,var(--p),var(--s));border:none;border-radius:8px;padding:4px 14px;font-weight:600;font-size:.78rem;box-shadow:0 4px 14px rgba(108,99,255,.25);color:#fff!important}.btn-v:hover{transform:translateY(-1px);box-shadow:0 6px 20px rgba(108,99,255,.35);color:#fff!important}
.btn-o{background:rgba(108,99,255,.12);border:1px solid rgba(108,99,255,.3);border-radius:8px;padding:4px 10px;font-size:.72rem;color:#fff!important}.btn-o:hover{background:rgba(108,99,255,.25);color:#fff!important}
.badge{color:#fff!important;font-weight:600;font-size:.72rem;padding:3px 8px}
.text-muted{color:rgba(255,255,255,.5)!important}
@media(max-width:768px){.tb{font-size:.72rem}.pp{width:90px;height:90px}.pp-n{width:90px;height:90px}}
@media print{body{background:#fff!important}.navbar,.no-print{display:none!important}.cd{border:1px solid #000!important;box-shadow:none!important;backdrop-filter:none!important;background:#fff!important}.cd .ch{background:#f0f0f0!important;color:#000!important}.cd .cb{color:#000!important}.badge{color:#000!important;border:1px solid #000!important}*{color:#000!important}}
</style></head><body>
<nav class="navbar navbar-expand navbar-dark"><div class="container-fluid px-3"><a class="navbar-brand fw-bold" href="<?=$back?>"><i class="fas fa-graduation-cap me-1"></i>ছাত্র বিস্তারিত</a>
<div class="navbar-nav ms-auto flex-row gap-1"><a class="nav-link text-white py-0 px-2" href="<?=$back?>"><i class="fas fa-home"></i></a><a class="nav-link text-white py-0 px-2" href="../logout.php"><i class="fas fa-sign-out-alt"></i></a></div></div></nav>
<div class="container-fluid py-2">
<div class="d-flex justify-content-between align-items-center mb-2 no-print"><div><h4 class="mb-0" style="font-size:1rem"><i class="fas fa-user-graduate me-1"></i><?=h($st['first_name'].' '.$st['last_name'])?></h4></div>
<div class="d-flex gap-1"><a href="list.php" class="btn btn-o btn-sm"><i class="fas fa-arrow-left"></i> তালিকা</a><?php if($role=='admin'||$role=='teacher'):?><a href="edit.php?id=<?=$st['id']?>" class="btn btn-o btn-sm"><i class="fas fa-edit"></i> সম্পাদনা</a><?php endif;?><button onclick="window.print()" class="btn btn-v btn-sm"><i class="fas fa-print"></i> প্রিন্ট</button></div></div>
<div class="row g-2">
<div class="col-md-4"><div class="cd"><div class="ch"><i class="fas fa-user me-1"></i>ব্যক্তিগত তথ্য</div><div class="cb text-center">
<?php if($st['photo']&&file_exists(__DIR__.'/../'.ltrim($st['photo'],'/'))):?><img src="../<?=$st['photo']?>" alt="<?=h($st['first_name'])?>" class="pp mb-2"><?php else:?><div class="pp-n mx-auto mb-2"><i class="fas fa-user fa-3x" style="color:rgba(168,85,247,.6)"></i></div><?php endif;?>
<h5 class="mb-1 fw-bold" style="font-size:.95rem"><?=h($st['first_name'].' '.$st['last_name'])?></h5>
<p class="text-muted mb-2" style="font-size:.78rem">ছাত্র আইডি: <strong class="text-white"><?=h($st['student_id'])?></strong></p>
<?php list($sc,$stx)=gs($st['status']);?><span class="badge bg-<?=$sc?> bdg mb-2"><?=$stx?></span>
<div class="row g-1 mt-2 text-start"><div class="col-6"><div class="dh">পিতার নাম</div><div class="dv"><?=h($st['father_name']??'নেই')?></div></div>
<div class="col-6"><div class="dh">মাতার নাম</div><div class="dv"><?=h($st['mother_name']??'নেই')?></div></div>
<div class="col-6"><div class="dh">ইমেইল</div><div class="dv"><?=h($st['email']?:'নেই')?></div></div>
<div class="col-6"><div class="dh">ফোন</div><div class="dv"><?=h($st['phone']?:'নেই')?></div></div>
<div class="col-6"><div class="dh">জন্ম তারিখ</div><div class="dv"><?=$st['date_of_birth']?date('d/m/Y',strtotime($st['date_of_birth'])):'নেই'?></div></div>
<div class="col-6"><div class="dh">লিঙ্গ</div><div class="dv"><?=gg($st['gender'])?></div></div>
<div class="col-6"><div class="dh">উপবৃত্তি</div><div class="dv"><span class="badge bg-<?=$st['scholarship']==='yes'?'success':'secondary'?>"><?=$st['scholarship']==='yes'?'হ্যাঁ':'না'?></span></div></div>
<div class="col-6"><div class="dh">ভর্তির তারিখ</div><div class="dv"><?=$st['admission_date']?date('d/m/Y',strtotime($st['admission_date'])):'নেই'?></div></div></div></div></div></div>
<div class="col-md-8"><div class="row g-2">
<div class="col-md-6"><div class="cd"><div class="ch"><i class="fas fa-school me-1"></i>একাডেমিক তথ্য</div><div class="cb">
<div class="dh">ক্লাস</div><div class="dv mb-2"><?=$st['cn']?'<span class="badge bg-primary">'.h($st['cn'].' - '.$st['cs']).'</span>':'<span class="text-muted">নির্ধারিত নয়</span>'?></div>
<div class="dh">রোল নম্বর</div><div class="dv mb-2"><span class="badge bg-info"><?=h($st['roll_number'])?></span></div>
<?php if($sel_subj):?><div class="dh">নির্বাচিত বিষয়</div><div class="dv"><?php foreach($sel_subj as$sn):?><span class="badge bg-success me-1"><?=h($sn)?></span><?php endforeach;?></div><?php endif;?>
<?php if($st['username']):?><div class="dh mt-2">ইউজারনেম</div><div class="dv"><?=h($st['username'])?></div><?php endif;?>
</div></div></div>
<div class="col-md-6"><div class="cd"><div class="ch"><i class="fas fa-users me-1"></i>অভিভাবকের তথ্য</div><div class="cb">
<div class="row g-1"><div class="col-6"><div class="dh">নাম</div><div class="dv"><?=h($st['guardian_name']?:'নেই')?></div></div>
<div class="col-6"><div class="dh">ফোন</div><div class="dv"><?=h($st['guardian_phone']?:'নেই')?></div></div>
<div class="col-6"><div class="dh">ইমেইল</div><div class="dv"><?=h($st['guardian_email']?:'নেই')?></div></div>
<div class="col-6"><div class="dh">ঠিকানা</div><div class="dv"><?=$st['guardian_address']?nl2br(h($st['guardian_address'])):'নেই'?></div></div>
</div></div></div></div>
<div class="col-12"><div class="cd"><div class="ch"><i class="fas fa-map-marker-alt me-1"></i>ঠিকানা</div><div class="cb"><?=$st['address']?nl2br(h($st['address'])):'ঠিকানা প্রদান করা হয়নি'?></div></div></div>
<div class="col-md-4"><div class="cd"><div class="ch"><i class="fas fa-calendar-check me-1"></i>সাম্প্রতিক উপস্থিতি</div><div class="cb p-0"><?php if(empty($ra)):?><div class="text-center py-3 text-muted" style="font-size:.78rem">কোন উপস্থিতির তথ্য নেই</div><?php else:?><div style="overflow-x:auto"><table class="tb"><thead><tr><th>তারিখ</th><th>অবস্থা</th></tr></thead><tbody><?php foreach($ra as$a):list($ac,$at)=ga($a['status']);?><tr><td><?=date('d/m/Y',strtotime($a['date']))?></td><td><span class="text-<?=$ac?> fw-bold" style="font-size:.78rem"><?=$at?></span></td></tr><?php endforeach;?></tbody></table></div><?php endif;?></div></div></div>
<div class="col-md-4"><div class="cd"><div class="ch"><i class="fas fa-chart-line me-1"></i>সাম্প্রতিক ফলাফল</div><div class="cb p-0"><?php if(empty($rr)):?><div class="text-center py-3 text-muted" style="font-size:.78rem">কোন ফলাফল নেই</div><?php else:?><div style="overflow-x:auto"><table class="tb"><thead><tr><th>পরীক্ষা</th><th>বিষয়</th><th>নম্বর</th><th>গ্রেড</th></tr></thead><tbody><?php foreach($rr as$r):?><tr><td><?=h($r['en'])?></td><td><?=h($r['sn'])?></td><td><?=$r['marks_obtained'].'/'.$r['max_marks']?></td><td><span class="badge bg-primary"><?=h($r['grade'])?></span></td></tr><?php endforeach;?></tbody></table></div><?php endif;?></div></div></div>
<div class="col-md-4"><div class="cd"><div class="ch"><i class="fas fa-money-bill me-1"></i>ফি অবস্থা</div><div class="cb p-0"><?php if(empty($fe)):?><div class="text-center py-3 text-muted" style="font-size:.78rem">কোন ফি তথ্য নেই</div><?php else:$ta=0;$tp=0;$td=0;?><div style="overflow-x:auto"><table class="tb"><thead><tr><th>ধরন</th><th>মোট</th><th>জমা</th><th>বাকি</th><th>অবস্থা</th></tr></thead><tbody><?php foreach($fe as$f):$du=$f['amount']-$f['paid_amount'];$ta+=$f['amount'];$tp+=$f['paid_amount'];$td+=$du;list($fc,$ft)=gf($f['status']);?><tr><td><?=h($f['fee_type'])?></td><td>৳<?=number_format($f['amount'],2)?></td><td>৳<?=number_format($f['paid_amount'],2)?></td><td>৳<?=number_format($du,2)?></td><td><span class="badge bg-<?=$fc?>"><?=$ft?></span></td></tr><?php endforeach;?><tr style="background:rgba(108,99,255,.1);font-weight:700"><td>মোট</td><td>৳<?=number_format($ta,2)?></td><td>৳<?=number_format($tp,2)?></td><td>৳<?=number_format($td,2)?></td><td></td></tr></tbody></table></div><?php endif;?></div></div></div>
</div></div></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body></html>