<?php session_start();require_once'../config/database.php';
function h($s){return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8');}
if(!isset($_SESSION['user_id'])||($_SESSION['user_role']!='admin'&&$_SESSION['user_role']!='teacher')){header('Location: ../login.php');exit();}
$sn=$_REQUEST['session']??'';$ci=$_REQUEST['class_id']??'';$gn=$_REQUEST['group_name']??'';$si=$_REQUEST['subject_id']??'';$ei=$_REQUEST['exam_id']??'';$ac=$_REQUEST['action']??'analyze';
$ses=$pdo->query("SELECT session_name FROM academic_sessions WHERE status='active' ORDER BY session_name DESC")->fetchAll();
$cls=$pdo->query("SELECT id,name,section FROM classes WHERE status='active' ORDER BY name,section")->fetchAll();
$grps=$pdo->query("SELECT group_name,group_code FROM student_groups WHERE status='active' ORDER BY group_name")->fetchAll();
$exm=[];if(!empty($ci)&&!empty($sn)){$s=$pdo->prepare("SELECT id,name,group_name FROM exams WHERE class_id=? AND session=? ORDER BY created_at DESC");$s->execute([$ci,$sn]);$exm=$s->fetchAll();}
$csb=[];if(!empty($ci)){$s=$pdo->prepare("SELECT s.id,s.name,s.code FROM class_subjects cs JOIN subjects s ON cs.subject_id=s.id WHERE cs.class_id=? AND s.status='active' ORDER BY s.name");$s->execute([$ci]);$csb=$s->fetchAll();}
$ws=["s.status='active'"];$ps=[];if($sn!==''){$ws[]="s.session=?";$ps[]=$sn;}if($ci!==''){$ws[]="s.class_id=?";$ps[]=$ci;}if($gn!==''){$ws[]="COALESCE(s.group_name,'')=?";$ps[]=$gn;}
$wc=implode(' AND ',$ws);$wc2=str_replace('s.','s2.',$wc);$ttl=0;if(!empty($ci)&&!empty($sn)){$s=$pdo->prepare("SELECT COUNT(*)AS c FROM students s WHERE $wc");$s->execute($ps);$ttl=intval($s->fetch()['c']??0);}
if($ac==='download'&&!empty($si)&&!empty($ci)&&!empty($sn)){header('Content-Type: text/csv; charset=UTF-8');header('Content-Disposition: attachment; filename="subject_list_'.date('Ymd_His').'.csv"');$o=fopen('php://output','w');fwrite($o,"\xEF\xBB\xBF");fputcsv($o,['Roll','Student ID','Name','Session','Class','Section','Group']);
$sql="SELECT s.roll_number,s.student_id,CONCAT(s.first_name,' ',s.last_name)AS name,s.session,c.name AS class_name,c.section,COALESCE(s.group_name,'')AS group_name FROM students s JOIN classes c ON s.class_id=c.id JOIN student_subjects ss ON ss.student_id=s.id AND ss.subject_id=? WHERE $wc ORDER BY s.roll_number";
$s=$pdo->prepare($sql);$s->execute(array_merge([$si],$ps));while($r=$s->fetch())fputcsv($o,[$r['roll_number'],$r['student_id'],$r['name'],$r['session'],$r['class_name'],$r['section'],$r['group_name']]);fclose($o);exit();}
$anl=[];$sl=[];$sbi=null;$dbg=[];$ea=isset($_REQUEST['exclude_absent'])&&$_REQUEST['exclude_absent']=='1'&&!empty($ei);
try{if($ac==='analyze'&&!empty($ci)&&!empty($sn)){$sql="SELECT ssub.id AS subject_id,ssub.name AS subject_name,ssub.code AS subject_code,COUNT(DISTINCT ss.student_id)AS enrolled_students FROM(SELECT s.id,s.name,s.code FROM class_subjects cs JOIN subjects s ON cs.subject_id=s.id WHERE cs.class_id=? AND s.status='active')ssub LEFT JOIN students s2 ON ($wc2) LEFT JOIN student_subjects ss ON(ss.student_id=s2.id AND ss.subject_id=ssub.id)GROUP BY ssub.id,ssub.name,ssub.code ORDER BY ssub.name";
$s=$pdo->prepare($sql);$s->execute(array_merge([$ci],$ps));$anl=$s->fetchAll();}
if(in_array($ac,['list','seatcards','seatcards_photo'],true)&&!empty($si)&&!empty($ci)&&!empty($sn)){$s=$pdo->prepare("SELECT id,name,code FROM subjects WHERE id=?");$s->execute([$si]);$sbi=$s->fetch();$sql=$ea?"SELECT s.id,s.student_id,s.first_name,s.last_name,s.roll_number,s.session,s.photo,c.name AS class_name,c.section,COALESCE(s.group_name,'')AS group_name FROM students s JOIN classes c ON s.class_id=c.id JOIN student_subjects ss ON ss.student_id=s.id AND ss.subject_id=? LEFT JOIN exam_attendance ea ON(ea.student_id=s.id AND ea.exam_id=?)WHERE $wc GROUP BY s.id,s.student_id,s.first_name,s.last_name,s.roll_number,s.session,s.photo,c.name,c.section,s.group_name HAVING SUM(CASE WHEN ea.attendance_status='absent' THEN 1 ELSE 0 END)=0 ORDER BY s.roll_number":"SELECT s.id,s.student_id,s.first_name,s.last_name,s.roll_number,s.session,s.photo,c.name AS class_name,c.section,COALESCE(s.group_name,'')AS group_name FROM students s JOIN classes c ON s.class_id=c.id JOIN student_subjects ss ON ss.student_id=s.id AND ss.subject_id=? WHERE $wc ORDER BY s.roll_number";
$s=$pdo->prepare($sql);$s->execute($ea?array_merge([$si,$ei],$ps):array_merge([$si],$ps));$sl=$s->fetchAll();}
}catch(Exception$e){$em='ডেটা লোড করতে সমস্যা: '.$e->getMessage();}
?><!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;--cols:3}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-s{background:rgba(148,163,184,.15);border:1px solid rgba(148,163,184,.3);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-s:hover{background:rgba(148,163,184,.25);color:#fff}
.btn-i{background:rgba(34,211,238,.12);border:1px solid rgba(34,211,238,.3);border-radius:8px;padding:5px 16px;font-weight:600;font-size:.82rem;color:#fff}.btn-i:hover{background:rgba(34,211,238,.2);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-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}
.form-check-input{background:rgba(255,255,255,.1);border:1px solid rgba(255,255,255,.2)}.form-check-input:checked{background:var(--p);border-color:var(--p)}.form-check-label{color:#fff;font-size:.82rem}.form-switch .form-check-input{background-image:url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e")}
.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-info{background:rgba(96,165,250,.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}.table-hover tbody tr:hover{background:rgba(255,255,255,.04)}.table-striped>tbody>tr:nth-of-type(odd){background:rgba(255,255,255,.04)}.table-striped>tbody>tr:nth-of-type(even){background:rgba(255,255,255,.01)}
hr{border-color:rgba(255,255,255,.08);margin:.75rem 0}.badge{font-weight:500;font-size:.7rem;padding:2px 8px;color:#fff}.tf{max-height:480px;overflow-y:auto}
.seat-grid{display:grid;grid-template-columns:repeat(var(--cols),1fr);gap:6mm}.seat-card{border:1.5px solid rgba(255,255,255,.15);border-radius:10px;padding:6mm;background:rgba(255,255,255,.04);display:flex;gap:8px;align-items:center;position:relative;overflow:hidden;break-inside:avoid}
.seat-card .logo-bg{position:absolute;inset:0;background-repeat:var(--logo-repeat,no-repeat);background-position:var(--logo-pos,center);background-size:var(--logo-size,70%);opacity:var(--logo-opacity,.08);pointer-events:none;transform:rotate(var(--logo-rotate,0deg));transform-origin:center}
.seat-card .photo{width:52px;height:52px;border-radius:6px;border:1px solid rgba(255,255,255,.2);object-fit:cover;background:rgba(255,255,255,.06)}
.seat-card .title{font-weight:700;font-size:15px;text-align:center;margin-bottom:4px}.seat-card .meta{font-size:11px;color:rgba(255,255,255,.65);text-align:center}
.seat-card .name{font-weight:700;font-size:14px;text-align:center;margin:6px 0;color:#fff}.seat-card .roll{font-weight:900;font-size:22px;text-align:center;color:#fff}
.compact .seat-card{padding:4mm}.compact .seat-card .title{font-size:13px;margin-bottom:2px}.compact .seat-card .meta{font-size:10px}.compact .seat-card .name{font-size:13px;margin:4px 0}.compact .seat-card .roll{font-size:18px}
#seatGrid2 .seat-card{flex-direction:column;align-items:center;text-align:center}#seatGrid2 .seat-card .photo{width:64px;height:64px;margin-bottom:6px}#seatGrid2 .seat-card .name{font-size:var(--name-size,14px)}#seatGrid2 .seat-card .roll{font-size:var(--roll-size,22px)}
body,.card,.card-body,.card-header,.btn,.form-label,.form-select,.form-control,.form-check,.nav-link,.list-group-item,.table,.table td,.table th,.table tr,td,th,tr,.btn i,a,a:hover,i,span,small,p,h1,h2,h3,h4,h5,h6,div,label,select,option,input,textarea,strong,b,em{color:#fff!important}
a:not(.btn):not(.nav-link):hover{color:#c4b5fd!important}
@media print{@page{size:A4;margin:6mm}.no-print{display:none!important}.print-only{display:block!important}.container-fluid,.fc,.cb,.table,.table td,.table th,.tf,.seat-card,.seat-card .content,.seat-card .name,.seat-card .roll,.seat-card .meta,.seat-card .title,.seat-card div,.seat-card span,.seat-card small,.seat-card strong,.table tr,td,th,tr,a{color:#000!important}body{background:none!important}.fc{background:#fff!important;border:none!important;box-shadow:none!important}.seat-card{break-inside:avoid;page-break-inside:avoid;background:#fff!important;border:1.5px solid #000!important}.seat-card .roll{font-size:22px!important}.compact .seat-card .roll{font-size:18px!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-graduation-cap"></i> বিষয়ভিত্তিক বিশ্লেষণ, লিস্ট ও সীটকার্ড</a>
<div class="navbar-nav ms-auto flex-row gap-1"><a class="nav-link text-white py-0 px-2" href="manage.php"><i class="fas fa-cog"></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-list-check me-1"></i>বিষয়ভিত্তিক বিশ্লেষণ, লিস্ট ও সীটকার্ড</div><div class="cb">
<?php if(isset($em)&&$em):?><div class="alert alert-danger py-1 small"><?=h($em)?></div><?php endif;?>
<?php if($ea):?><div class="alert alert-info py-1 small"><i class="fas fa-info-circle"></i> অনুপস্থিত শিক্ষার্থীদের বাদ দেওয়া হচ্ছে</div><?php endif;?>
<form method="GET" class="row g-2 align-items-end">
<div class="col-md-3"><label class="fl">সেশন *</label><select class="form-select fc-s" name="session" required><option value="">নির্বাচন</option><?php foreach($ses as$s):?><option value="<?=h($s['session_name'])?>" <?=$sn===$s['session_name']?'selected':''?>><?=h($s['session_name'])?></option><?php endforeach;?></select></div>
<div class="col-md-3"><label class="fl">ক্লাস *</label><select class="form-select fc-s" name="class_id" required><option value="">নির্বাচন</option><?php foreach($cls as$c):?><option value="<?=h($c['id'])?>" <?=$ci==$c['id']?'selected':''?>><?=h($c['name'].' - '.$c['section'])?></option><?php endforeach;?></select></div>
<div class="col-md-3"><label class="fl">গ্রুপ</label><select class="form-select fc-s" name="group_name"><option value="">সব</option><?php foreach($grps as$g):?><option value="<?=h($g['group_name'])?>" <?=$gn===$g['group_name']?'selected':''?>><?=h($g['group_name'].' ('.$g['group_code'].')')?></option><?php endforeach;?></select></div>
<div class="col-md-3"><label class="fl">বিষয়</label><select class="form-select fc-s" name="subject_id"><option value="">- নির্বাচন -</option><?php foreach($csb as$sb):?><option value="<?=h($sb['id'])?>" <?=$si==$sb['id']?'selected':''?>><?=h($sb['name'].' ('.$sb['code'].')')?></option><?php endforeach;?></select></div>
<div class="col-md-3"><label class="fl">পরীক্ষা</label><select class="form-select fc-s" name="exam_id"><option value="">- নির্বাচন -</option><?php foreach($exm as$ex):?><option value="<?=h($ex['id'])?>" <?=$ei==$ex['id']?'selected':''?>><?=h($ex['name'].(!empty($ex['group_name'])?' ('.$ex['group_name'].')':''))?></option><?php endforeach;?></select></div>
<div class="col-md-2"><div class="form-check mt-3"><input class="form-check-input" type="checkbox" name="exclude_absent" value="1" id="ea" <?=isset($_REQUEST['exclude_absent'])&&$_REQUEST['exclude_absent']=='1'?'checked':''?>><label class="form-check-label" for="ea">অনুপস্থিত বাদ</label></div></div>
<div class="col-md-12 d-flex flex-wrap gap-2">
<button type="submit" name="action" value="analyze" class="btn btn-p"><i class="fas fa-chart-pie"></i> বিশ্লেষণ</button>
<button type="submit" name="action" value="list" class="btn btn-s"><i class="fas fa-list"></i> লিস্ট</button>
<button type="submit" name="action" value="seatcards" class="btn btn-i"><i class="fas fa-id-card"></i> সীটকার্ড</button>
<button type="submit" name="action" value="seatcards_photo" class="btn btn-o"><i class="fas fa-id-badge"></i> সীটকার্ড (ছবি)</button>
<button type="button" class="btn btn-g" onclick="dl()"><i class="fas fa-download"></i> ডাউনলোড</button></div>
</form></div></div>

<?php if($ac==='analyze'&&!empty($anl)):?><div class="fc mt-2"><div class="ch d-flex justify-content-between"><span><i class="fas fa-chart-pie"></i> বিষয়ভিত্তিক শিক্ষার্থী সংখ্যা</span><span class="small">মোট: <?=(int)$ttl?></span></div>
<div class="tf"><table class="table table-hover table-striped"><thead><tr><th>বিষয়</th><th>কোড</th><th class="text-end">শিক্ষার্থী</th><th class="text-end no-print">অ্যাকশন</th></tr></thead>
<tbody><?php foreach($anl as$r):?><tr><td><?=h($r['subject_name'])?></td><td><span class="badge" style="background:var(--p)"><?=h($r['subject_code'])?></span></td><td class="text-end fw-bold"><?=(int)$r['enrolled_students']?></td>
<td class="text-end no-print"><?php $q="session=".urlencode($sn)."&class_id=".urlencode($ci)."&group_name=".urlencode($gn)."&subject_id=".urlencode($r['subject_id']);?>
<a class="btn btn-sm btn-s py-0" href="?<?=$q?>&action=list"><i class="fas fa-list"></i></a>
<a class="btn btn-sm btn-i py-0" href="?<?=$q?>&action=seatcards"><i class="fas fa-id-card"></i></a>
<a class="btn btn-sm btn-g py-0" href="?<?=$q?>&action=download"><i class="fas fa-download"></i></a></td></tr><?php endforeach;?></tbody></table></div></div><?php endif;?>

<?php if($ac==='list'&&!empty($sbi)):?><div class="fc mt-2"><div class="ch d-flex justify-content-between"><span><i class="fas fa-list"></i> লিস্ট: <?=h($sbi['name'])?> (<?=h($sbi['code'])?>)</span><button class="btn btn-sm btn-g no-print" onclick="window.print()"><i class="fas fa-print"></i></button></div>
<?php if(empty($sl)):?><div class="alert alert-warning m-2 small">এই বিষয়ে কোন শিক্ষার্থী নেই</div>
<?php else:?><div class="tf"><table class="table table-hover"><thead><tr><th>রোল</th><th>আইডি</th><th>নাম</th><th>সেশন</th><th>ক্লাস</th><th>সেকশন</th><th>গ্রুপ</th></tr></thead>
<tbody><?php foreach($sl as$st):?><tr><td><?=h($st['roll_number'])?></td><td><?=h($st['student_id'])?></td><td><?=h(trim($st['first_name'].' '.$st['last_name']))?></td><td><?=h($st['session'])?></td><td><?=h($st['class_name'])?></td><td><?=h($st['section'])?></td><td><?=h($st['group_name'])?></td></tr><?php endforeach;?></tbody></table></div><?php endif;?></div><?php endif;?>

<?php if($ac==='seatcards'&&!empty($sbi)):?><div class="fc mt-2"><div class="ch no-print d-flex justify-content-between flex-wrap"><span><i class="fas fa-id-card"></i> সীটকার্ড: <?=h($sbi['name'])?> (<?=h($sbi['code'])?>)<?php if($ea):?><span class="badge bg-info ms-2">অনুপস্থিত বাদ</span><?php endif;?></span>
<div class="d-flex gap-2 align-items-center flex-wrap"><label class="small mb-0">প্রতি সারি:</label><select class="form-select form-select-sm fc-s" style="width:70px" onchange="document.documentElement.style.setProperty('--cols',this.value)"><option value="2">2</option><option value="3" selected>3</option><option value="4">4</option></select>
<div class="form-check form-switch mb-0"><input class="form-check-input" type="checkbox" id="cmp" onchange="document.getElementById('sg').classList.toggle('compact',this.checked)"><label class="form-check-label small" for="cmp">কমপ্যাক্ট</label></div>
<button class="btn btn-g btn-sm" onclick="window.print()"><i class="fas fa-print"></i></button></div></div>
<?php if(empty($sl)):?><div class="alert alert-warning m-2 small">এই বিষয়ে কোন শিক্ষার্থী নেই</div>
<?php else:?><div class="cb"><div id="sg" class="seat-grid"><?php foreach($sl as$st):?><div class="seat-card"><div class="content text-center"><div class="meta">সেশন: <?=h($st['session'])?> | <?=h($st['class_name'].'-'.$st['section'])?><?=$st['group_name']?' | '.h($st['group_name']):''?></div><div class="meta"><?=h($sbi['name'])?> (<?=h($sbi['code'])?>)</div><div class="name"><?=h(trim($st['first_name'].' '.$st['last_name']))?></div><div class="roll">রোল: <?=h($st['roll_number'])?></div><div class="meta">আইডি: <?=h($st['student_id'])?></div></div></div><?php endforeach;?></div></div><?php endif;?></div><?php endif;?>

<?php if($ac==='seatcards_photo'&&!empty($sbi)):?><div class="fc mt-2"><div class="ch no-print d-flex justify-content-between flex-wrap"><span><i class="fas fa-id-badge"></i> সীটকার্ড (ছবি): <?=h($sbi['name'])?> (<?=h($sbi['code'])?>)<?php if($ea):?><span class="badge bg-info ms-2">অনুপস্থিত বাদ</span><?php endif;?></span>
<div class="d-flex gap-2 align-items-center flex-wrap"><label class="small mb-0">প্রতি সারি:</label><select class="form-select form-select-sm fc-s" style="width:70px" onchange="document.documentElement.style.setProperty('--cols',this.value)"><option value="2">2</option><option value="3" selected>3</option><option value="4">4</option></select>
<div class="form-check form-switch mb-0"><input class="form-check-input" type="checkbox" id="cmp2" onchange="document.getElementById('sg2').classList.toggle('compact',this.checked)"><label class="form-check-label small" for="cmp2">কমপ্যাক্ট</label></div>
<input type="file" accept="image/*" id="lf" class="form-control form-control-sm fc-s" style="width:140px;font-size:.75rem">
<label class="small mb-0">অপা:</label><input id="lo" type="range" min="0" max="1" step=".01" value=".08" class="form-range fc-s" style="width:80px;padding:0;height:4px">
<label class="small mb-0">সাইজ:</label><input id="ls" type="range" min="30" max="120" value="70" class="form-range fc-s" style="width:80px;padding:0;height:4px">
<select id="lp" class="form-select form-select-sm fc-s" style="width:100px"><option value="center">Center</option><option value="top left">T-Left</option><option value="top right">T-Right</option></select>
<select id="lr" class="form-select form-select-sm fc-s" style="width:90px"><option value="no-repeat">No</option><option value="repeat">Yes</option></select>
<label class="small mb-0">°:</label><input id="lrot" type="number" class="form-control form-control-sm fc-s" style="width:70px" value="0">
<label class="small mb-0">নাম:</label><input id="ns" type="number" class="form-control form-control-sm fc-s" style="width:65px" min="10" max="32" value="14">
<label class="small mb-0">রোল:</label><input id="rs" type="number" class="form-control form-control-sm fc-s" style="width:65px" min="14" max="40" value="22">
<button class="btn btn-o btn-sm" onclick="applyLogo()"><i class="fas fa-check"></i></button>
<button class="btn btn-g btn-sm" onclick="window.print()"><i class="fas fa-print"></i></button></div></div>
<?php if(empty($sl)):?><div class="alert alert-warning m-2 small">এই বিষয়ে কোন শিক্ষার্থী নেই</div>
<?php else:?><div class="cb"><div id="sg2" class="seat-grid"><?php foreach($sl as$st):
$ph=false;if(!empty($st['photo'])){$pp=__DIR__.'/../'.ltrim($st['photo'],'/');$ph=file_exists($pp);}?><div class="seat-card"><div class="logo-bg"></div>
<?php if($ph):?><img class="photo" src="../<?=h($st['photo'])?>" alt="<?=h($st['first_name'])?>"><?php else:?><div class="photo d-flex align-items-center justify-content-center" style="color:rgba(255,255,255,.3)"><i class="fas fa-user"></i></div><?php endif;?>
<div class="content text-center"><div class="meta"><?=h($st['session'])?> | <?=h($st['class_name'].'-'.$st['section'])?><?=$st['group_name']?' | '.h($st['group_name']):''?></div><div class="meta"><?=h($sbi['name'])?> (<?=h($sbi['code'])?>)</div><div class="name"><?=h(trim($st['first_name'].' '.$st['last_name']))?></div><div class="roll">রোল: <?=h($st['roll_number'])?></div><div class="meta"><?=h($st['student_id'])?></div></div></div><?php endforeach;?></div></div><?php endif;?></div><?php endif;?>
</div>
<script>
function dl(){const f=document.querySelector('form');const p=new URLSearchParams(new FormData(f));p.set('action','download');if(!p.get('subject_id')){alert('একটি বিষয় নির্বাচন করুন');return}window.location.href=window.location.pathname+'?'+p.toString()}
let lu='';
function applyLogo(){const g=document.getElementById('sg2');if(!g)return;const c=g.querySelectorAll('.logo-bg');c.forEach(e=>{e.style.setProperty('--logo-opacity',document.getElementById('lo')?.value||'.08');e.style.setProperty('--logo-size',(document.getElementById('ls')?.value||70)+'%');e.style.setProperty('--logo-pos',document.getElementById('lp')?.value||'center');e.style.setProperty('--logo-repeat',document.getElementById('lr')?.value||'no-repeat');e.style.setProperty('--logo-rotate',(document.getElementById('lrot')?.value||0)+'deg');e.style.backgroundImage=lu?`url("${lu}")`:'none'});const ns=document.getElementById('ns')?.value;if(ns)g.style.setProperty('--name-size',ns+'px');const rs=document.getElementById('rs')?.value;if(rs)g.style.setProperty('--roll-size',rs+'px')}
document.getElementById('lf')?.addEventListener('change',function(e){const f=e.target.files&&e.target.files[0];if(!f){lu='';return}if(lu&&lu.startsWith('blob:'))try{URL.revokeObjectURL(lu)}catch(_){}lu=URL.createObjectURL(f)});
document.querySelectorAll('select[name="class_id"],select[name="session"]').forEach(el=>el.addEventListener('change',function(){const f=document.querySelector('form');const p=new URLSearchParams(new FormData(f));p.set('action','analyze');window.location.href=window.location.pathname+'?'+p.toString()}));
</script></body></html>