<?php session_start();require_once'../config/database.php';
function h($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}
if(!isset($_SESSION['user_id'])){header('Location: ../login.php');exit();}
$ei=$_GET['exam_id']??'';$sn=$_GET['session']??'';$ci=$_GET['class_id']??'';$gn=$_GET['group_name']??'';$si=$_GET['subject_id']??'';
// Build student query from direct params (session/class/group/subject) or from exam_id
$ed=[];$es=[];$st=[];$sa=[];$sm='';$em='';
if($ei){$s=$pdo->prepare("SELECT e.*,c.name AS class_name,c.section FROM exams e JOIN classes c ON e.class_id=c.id WHERE e.id=?");$s->execute([$ei]);$ed=$s->fetch();
if($ed){$s=$pdo->prepare("SELECT es.*,s.name AS subject_name,s.code AS subject_code FROM exam_subjects es JOIN subjects s ON es.subject_id=s.id WHERE es.exam_id=? ORDER BY es.exam_date,es.start_time");$s->execute([$ei]);$es=$s->fetchAll();
$s=$pdo->prepare("SELECT s.id,s.student_id,s.first_name,s.last_name,s.roll_number,s.guardian_name FROM students s WHERE s.class_id=? AND s.session=? AND s.status='active' ORDER BY s.roll_number");$s->execute([$ed['class_id'],$ed['session']]);$st=$s->fetchAll();
$s=$pdo->prepare("SELECT sa.*,s.student_id,s.first_name,s.last_name,s.roll_number,s.guardian_name,subj.name AS subject_name,subj.code AS subject_code,es.exam_date,es.start_time,es.end_time FROM seat_arrangements sa JOIN students s ON sa.student_id=s.id JOIN subjects subj ON sa.subject_id=subj.id JOIN exam_subjects es ON(sa.exam_id=es.exam_id AND sa.subject_id=es.subject_id)WHERE sa.exam_id=? ORDER BY sa.subject_id,sa.seat_number");$s->execute([$ei]);$sa=$s->fetchAll();}}
if($_SERVER['REQUEST_METHOD']=='POST'&&isset($_POST['generate_seats'])){$eid=$_POST['exam_id'];$rc=$_POST['room_config']??[];try{$pdo->beginTransaction();$pdo->prepare("DELETE FROM seat_arrangements WHERE exam_id=?")->execute([$eid]);$c=1;
foreach($es as$sub){$rn=$rc[$sub['subject_id']]['room']??'Room-1';$spr=intval($rc[$sub['subject_id']]['seats_per_row']??10);
foreach($st as$i=>$s){$r=intval($i/$spr)+1;$col=($i%$spr)+1;$sn='S-'.str_pad($c,3,'0',STR_PAD_LEFT);$pdo->prepare("INSERT INTO seat_arrangements(exam_id,subject_id,student_id,room_number,seat_number,row_number,column_number,exam_date)VALUES(?,?,?,?,?,?,?,?)")->execute([$eid,$sub['subject_id'],$s['id'],$rn,$sn,$r,$col,$sub['exam_date']]);$c++;}}$pdo->commit();$sm='সীট বিন্যাস তৈরি হয়েছে!';}catch(Exception$e){$pdo->rollBack();$em='ত্রুটি: '.$e->getMessage();}}
$ae=$pdo->query("SELECT e.id,e.name,c.name AS class_name,c.section,e.session,e.group_name FROM exams e JOIN classes c ON e.class_id=c.id ORDER BY e.created_at DESC")->fetchAll();
?><!DOCTYPE html><html lang="bn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>সীট কার্ড</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}body{font-family:'Hind Siliguri',sans-serif;background:linear-gradient(135deg,#0F0C29,#302B63,#24243E);min-height:100vh;font-size:.85rem;color:#fff}
.navbar{background:linear-gradient(135deg,#1a1a3e,#2d1b69)!important;padding:5px 0!important;border-bottom:1px solid rgba(168,85,247,.2)}
.fc{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}
.fc .ch{background:linear-gradient(135deg,rgba(108,99,255,.35),rgba(168,85,247,.25));color:#fff;border:none;padding:7px 12px;font-weight:700;font-size:.88rem;backdrop-filter:blur(4px)}
.fc .cb{padding:10px 12px}.fl{font-weight:600;color:#fff;margin-bottom:2px;font-size:.78rem}
.fc-s{background:#1e1b4b;border:1px solid rgba(255,255,255,.12);border-radius:8px;padding:5px 9px;font-size:.82rem;transition:.3s;width:100%;color:#fff}
.fc-s:focus{border-color:var(--p);box-shadow:0 0 0 3px rgba(108,99,255,.2);outline:none}.fc-s option{background:#1e1b4b;color:#fff}
.btn-p{background:linear-gradient(135deg,var(--p),var(--s));border:none;border-radius:8px;padding:5px 16px;font-weight:700;font-size:.82rem;box-shadow:0 4px 14px rgba(108,99,255,.3);color:#fff}.btn-p:hover{transform:translateY(-1px);box-shadow:0 6px 20px rgba(108,99,255,.4);color:#fff}
.btn-w{background:rgba(251,191,36,.15);border:1px solid rgba(251,191,36,.3);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-w:hover{background:rgba(251,191,36,.25)}
.btn-g{background:rgba(52,211,153,.15);border:1px solid rgba(52,211,153,.3);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-g:hover{background:rgba(52,211,153,.25);color:#fff}
.btn-o{background:transparent;border:1px solid rgba(255,255,255,.15);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-o:hover{background:rgba(255,255,255,.08);color:#fff}
.btn-dr{background:rgba(239,68,68,.12);border:1px solid rgba(239,68,68,.25);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-dr:hover{background:rgba(239,68,68,.2)}
.text-muted{color:rgba(255,255,255,.55)!important}.alert{backdrop-filter:blur(8px);border:none;border-radius:8px;color:#fff}.alert-success{background:rgba(52,211,153,.12)}.alert-danger{background:rgba(239,68,68,.12)}.alert-warning{background:rgba(251,191,36,.12)}
.table{color:#fff!important;margin-bottom:0}.table th{background:linear-gradient(135deg,rgba(108,99,255,.4),rgba(168,85,247,.3));color:#fff!important;font-weight:600;font-size:.75rem;padding:6px 8px!important;border:none}
.table td{padding:5px 8px!important;vertical-align:middle;font-size:.8rem;border-color:rgba(255,255,255,.06);color:#fff!important}
.sc{border:1.5px solid rgba(255,255,255,.15);border-radius:14px;padding:15px;margin-bottom:16px;background:rgba(255,255,255,.04)}
.sc .sph{width:80px;height:80px;border-radius:8px;border:2px solid rgba(255,255,255,.15);background:rgba(255,255,255,.06);display:flex;align-items:center;justify-content:center}
.si{background:rgba(108,99,255,.3);padding:3px 10px;border-radius:6px;font-weight:700;font-size:.8rem;color:#fff}
body,.card,.card-body,.card-header,.btn,.form-label,.form-select,.form-control,.form-check,.nav-link,.table,.table td,.table th,.table tr,td,th,tr,.btn i,a,i,span,small,p,h1,h2,h3,h4,h5,h6,div,label,select,option,input,textarea,strong,b,em{color:#fff!important}
@media print{@page{size:A4 portrait;margin:6mm}.no-print{display:none!important}body{background:none!important}.sc{border:1.5px solid #000!important;background:#fff!important;color:#000!important;break-inside:avoid;page-break-inside:avoid;width:48%;float:left;margin:1%}.sc,.sc div,.sc span,.sc small,.sc strong,.sc p,.sc table,.sc td,.sc th,.sc h5,.sc h6{color:#000!important}.si{background:#e0e0e0!important;color:#000!important}.table td,.table th{color:#000!important;border-color:#ddd!important}.table th{background:#e0e0e0!important}}
</style></head><body>
<nav class="navbar navbar-expand navbar-dark no-print"><div class="container-fluid px-3"><a class="navbar-brand fw-bold" style="font-size:.9rem" href="../index.php"><i class="fas fa-id-card 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="../index.php"><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="fc no-print"><div class="ch"><i class="fas fa-id-card me-1"></i>সীট কার্ড ব্যবস্থাপনা</div><div class="cb">
<?php if($sm):?><div class="alert alert-success py-1 small"><?=$sm?></div><?php endif;?><?php if($em):?><div class="alert alert-danger py-1 small"><?=$em?></div><?php endif;?>
<form method="GET" class="row g-2 align-items-end"><div class="col-md-8"><label class="fl">পরীক্ষা নির্বাচন</label><select class="form-select fc-s" name="exam_id" required><option value="">পরীক্ষা নির্বাচন</option><?php foreach($ae as$e):?><option value="<?=$e['id']?>" <?=$ei==$e['id']?'selected':''?>><?=h($e['name'].' - '.$e['class_name'].'('.$e['section'].')'.($e['group_name']?' - '.$e['group_name']:''))?></option><?php endforeach;?></select></div>
<div class="col-md-4 d-flex gap-2 align-items-end"><button type="submit" class="btn btn-p"><i class="fas fa-search"></i> লোড</button><?php if($ei&&!empty($sa)):?><button type="button" class="btn btn-g" onclick="window.print()"><i class="fas fa-print"></i> প্রিন্ট</button><?php endif;?></div></form>
<?php if($ed&&!empty($st)&&empty($sa)):?><hr><h6 style="color:#a5b4fc;font-size:.82rem"><i class="fas fa-cog"></i> সীট বিন্যাস তৈরি</h6>
<form method="POST"><input type="hidden" name="generate_seats" value="1"><input type="hidden" name="exam_id" value="<?=$ei?>">
<div class="row"><?php foreach($es as$sub):?><div class="col-md-6 mb-2"><div class="fc"><div class="ch" style="font-size:.82rem"><?=h($sub['subject_name'])?></div><div class="cb">
<div class="mb-1 small text-muted">একাধিক রুম যোগ করতে "+" চাপুন</div><div class="rw" data-sid="<?=(int)$sub['subject_id']?>"><div class="rr row g-1 mb-1"><div class="col-4"><label class="fl" style="font-size:.72rem">রুম</label><input type="text" class="form-control fc-s" name="room_config[<?=$sub['subject_id']?>][room]" value="Room-1"></div>
<div class="col-3"><label class="fl" style="font-size:.72rem">Seats/Row</label><input type="number" class="form-control fc-s" name="room_config[<?=$sub['subject_id']?>][seats_per_row]" value="10" min="1" max="50"></div></div></div></div></div></div><?php endforeach;?></div>
<button type="submit" class="btn btn-w"><i class="fas fa-magic"></i> সীট বিন্যাস তৈরি</button></form><?php endif;?>
</div></div>

<?php if($ed&&(!empty($sa)||!empty($st))):$groups=[];if(!empty($sa)){foreach($sa as$s){$groups[$s['student_id']][]=$s;}}?>
<div class="row"><div class="col-12"><h5 class="no-print" style="color:#a5b4fc;font-size:.9rem">সীট কার্ডসমূহ (<?=count(!empty($groups)?$groups:$st)?> জন)</h5></div>
<?php if(!empty($groups)):foreach($groups as$sid=>$sd):$f=$sd[0];?>
<div class="sc"><div class="text-center mb-2"><h5 style="font-size:.95rem;font-weight:700">পরীক্ষার সীট কার্ড</h5><h6 style="color:#a5b4fc;font-size:.85rem"><?=h($ed['name'])?></h6></div>
<div class="row"><div class="col-8"><table class="table table-sm" style="border:none"><tr><td style="width:100px;border:none;font-weight:600;padding:2px 4px">নাম:</td><td style="border:none;padding:2px 4px"><?=h($f['first_name'].' '.$f['last_name'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">আইডি:</td><td style="border:none;padding:2px 4px"><?=h($f['student_id'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">রোল:</td><td style="border:none;padding:2px 4px"><?=h($f['roll_number'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">ক্লাস:</td><td style="border:none;padding:2px 4px"><?=h($ed['class_name'].' - '.$ed['section'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">সেশন:</td><td style="border:none;padding:2px 4px"><?=h($ed['session'])?></td></tr>
<?php if($ed['group_name']):?><tr><td style="border:none;padding:2px 4px;font-weight:600">গ্রুপ:</td><td style="border:none;padding:2px 4px"><?=h($ed['group_name'])?></td></tr><?php endif;?>
<tr><td style="border:none;padding:2px 4px;font-weight:600">অভিভাবক:</td><td style="border:none;padding:2px 4px"><?=h($f['guardian_name']??'')?></td></tr></table></div>
<div class="col-4 text-center"><div class="sph mx-auto"><i class="fas fa-user fa-2x" style="color:rgba(255,255,255,.3)"></i></div><small style="color:rgba(255,255,255,.45)">ছবি</small></div></div>
<div class="mt-2"><h6 style="font-size:.82rem;color:#a5b4fc"><i class="fas fa-chair"></i> সীট তথ্য</h6>
<table class="table table-sm"><thead><tr><th>বিষয়</th><th>রুম</th><th>সীট</th></tr></thead><tbody><?php foreach($sd as$s):?><tr><td><?=h($s['subject_name'])?></td><td><?=h($s['room_number'])?></td><td><span class="si"><?=h($s['seat_number'])?></span></td></tr><?php endforeach;?></tbody></table></div>
<div class="mt-2"><h6 style="font-size:.82rem;color:#a5b4fc"><i class="fas fa-exclamation-triangle"></i> নির্দেশনা</h6><ul style="font-size:.75rem;color:rgba(255,255,255,.65)"><li>পরীক্ষার সময় এই কার্ড সাথে রাখুন</li><li>নির্ধারিত সীটে বসুন</li><li>১৫ মিনিট আগে হলে প্রবেশ করুন</li></ul></div>
<div class="text-center mt-2 pt-2" style="border-top:1px solid rgba(255,255,255,.08);font-size:.72rem;color:rgba(255,255,255,.45)"><strong>প্রতিষ্ঠানের নাম</strong><br>ঠিকানা</div></div>
<?php endforeach;else:foreach($st as$s):?>
<div class="sc"><div class="text-center mb-2"><h5 style="font-size:.95rem;font-weight:700">পরীক্ষার সীট কার্ড</h5><h6 style="color:#a5b4fc;font-size:.85rem"><?=h($ed['name'])?></h6></div>
<div class="row"><div class="col-8"><table class="table table-sm" style="border:none"><tr><td style="width:100px;border:none;padding:2px 4px;font-weight:600">নাম:</td><td style="border:none;padding:2px 4px"><?=h($s['first_name'].' '.$s['last_name'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">আইডি:</td><td style="border:none;padding:2px 4px"><?=h($s['student_id'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">রোল:</td><td style="border:none;padding:2px 4px"><?=h($s['roll_number'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">ক্লাস:</td><td style="border:none;padding:2px 4px"><?=h($ed['class_name'].' - '.$ed['section'])?></td></tr>
<tr><td style="border:none;padding:2px 4px;font-weight:600">সেশন:</td><td style="border:none;padding:2px 4px"><?=h($ed['session'])?></td></tr>
<?php if($ed['group_name']):?><tr><td style="border:none;padding:2px 4px;font-weight:600">গ্রুপ:</td><td style="border:none;padding:2px 4px"><?=h($ed['group_name'])?></td></tr><?php endif;?>
<tr><td style="border:none;padding:2px 4px;font-weight:600">অভিভাবক:</td><td style="border:none;padding:2px 4px"><?=h($s['guardian_name']??'')?></td></tr></table></div>
<div class="col-4 text-center"><div class="sph mx-auto"><i class="fas fa-user fa-2x" style="color:rgba(255,255,255,.3)"></i></div><small style="color:rgba(255,255,255,.45)">ছবি</small></div></div>
<div class="mt-2"><div class="alert alert-warning py-1 small">সীট বিন্যাস এখনো তৈরি হয়নি</div></div>
<div class="mt-2"><h6 style="font-size:.82rem;color:#a5b4fc"><i class="fas fa-exclamation-triangle"></i> নির্দেশনা</h6><ul style="font-size:.75rem;color:rgba(255,255,255,.65)"><li>পরীক্ষার সময় এই কার্ড সাথে রাখুন</li><li>নির্ধারিত সীটে বসুন</li><li>১৫ মিনিট আগে হলে প্রবেশ করুন</li></ul></div>
<div class="text-center mt-2 pt-2" style="border-top:1px solid rgba(255,255,255,.08);font-size:.72rem;color:rgba(255,255,255,.45)"><strong>প্রতিষ্ঠানের নাম</strong><br>ঠিকানা</div></div>
<?php endforeach;endif;elseif($ei):?><div class="alert alert-warning py-1 small mt-2">এই পরীক্ষার জন্য কোনো শিক্ষার্থী পাওয়া যায়নি</div><?php endif;?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script></body></html>