<?php session_start();require_once'../config/database.php';require_once'../includes/site_settings.php';
$set=get_all_settings($pdo);$site=$set['site_name']??'ব‌িদ্যা নিকেতন';$addr=$set['site_address']??'ঠিকানা';$logo=$set['logo_image']??null;$ls=$set['logo_size']??100;
define('NO_FOURTH_CLS',[6,7,8]);
function gpa($r){$tp=0;$cn=0;$f4=false;$fa=false;$fg=0;foreach($r as$v){$g=$v['grade']??'';$gp=$v['grade_point']??0;if($g!='F'){if(!empty($v['is_fourth_subject'])){$f4=true;$fg=$gp;if($gp>2)$tp+=$gp-2;}else{$tp+=$gp;$cn++;}}elseif(empty($v['is_fourth_subject'])){$fa=true;}}return$fa?0:($cn?min(round($tp/$cn,2),5):0);}
function gf($g){if($g>=5)return'A+';if($g>=4)return'A';if($g>=3.5)return'A-';if($g>=3)return'B';if($g>=2)return'C';if($g>=1)return'D';return'F';}
function gp($g){$m=['A+'=>5,'A'=>4,'A-'=>3.5,'B'=>3,'C'=>2,'D'=>1,'F'=>0];return$m[$g]??0;}
function bg($cq,$mcq,$tm,$pm){$av=array_sum($cq)/max(count($cq),1);$p=(($av+$mcq)/$tm)*100;if($p>=80)return'A+';if($p>=70)return'A';if($p>=60)return'A-';if($p>=50)return'B';if($p>=40)return'C';if($p>=$pm/$tm*100)return'D';return'F';}
function eg($cq,$tm,$pm){$av=array_sum($cq)/max(count($cq),1);$p=($av/$tm)*100;if($p>=80)return'A+';if($p>=70)return'A';if($p>=60)return'A-';if($p>=50)return'B';if($p>=40)return'C';if($p>=$pm/$tm*100)return'D';return'F';}
if(!isset($_SESSION['user_id'])){header('Location: ../login.php');exit();}
$tid=null;$ac=[];
if($_SESSION['user_role']=='teacher'){$s=$pdo->prepare("SELECT id FROM teachers WHERE user_id=?");$s->execute([$_SESSION['user_id']]);$t=$s->fetch();if($t){$tid=$t['id'];$s=$pdo->prepare("SELECT DISTINCT c.id FROM classes c WHERE(c.class_teacher_id=? OR c.id IN(SELECT class_id FROM class_subjects WHERE teacher_id=?))AND c.status='active'");$s->execute([$tid,$tid]);$ac=array_column($s->fetchAll(),'id');}}
$ci=$_GET['class_id']??'';$sn=$_GET['session']??date('Y');$et=$_GET['exam_type']??'';$gn=$_GET['group_name']??'';
if($gn==='Arts')$gn='Humanities';$_GET['group_name']=$gn;
$mp=isset($_GET['show_merit_position'])?filter_var($_GET['show_merit_position'],FILTER_VALIDATE_BOOLEAN):true;
$lm=isset($_GET['landscape_mode'])?filter_var($_GET['landscape_mode'],FILTER_VALIDATE_BOOLEAN):false;
$sc=[];$sd=[];$mps=[];
if($_SESSION['user_role']=='teacher'&&!empty($ac)){$pl=str_repeat('?,',count($ac)-1).'?';$s=$pdo->prepare("SELECT id,name,section FROM classes WHERE id IN($pl)AND status='active' ORDER BY name,section");$s->execute($ac);$cls=$s->fetchAll();}else{$cls=$pdo->query("SELECT id,name,section FROM classes WHERE status='active' ORDER BY name,section")->fetchAll();}
$ss=$pdo->query("SELECT DISTINCT session FROM results ORDER BY session DESC")->fetchAll();
$ets=$pdo->query("SELECT DISTINCT exam_type FROM results ORDER BY exam_type")->fetchAll();
if($ci&&$sn&&$et){$q="SELECT s.id,s.student_id,s.first_name,s.last_name,s.roll_number FROM students s WHERE s.class_id=? AND s.status='active'";$p=[$ci];if($gn){$q.=" AND s.group_name=?";$p[]=$gn;}$q.=" AND EXISTS(SELECT 1 FROM results r WHERE r.student_id=s.id AND r.session=? AND r.exam_type=?)ORDER BY s.roll_number";$p[]=$sn;$p[]=$et;$s=$pdo->prepare($q);$s->execute($p);$al=$s->fetchAll();
$md=[];
foreach($al as$si){$s=$pdo->prepare("SELECT r.*,sub.name AS sn,sub.code AS sc,ss.is_fourth_subject FROM results r LEFT JOIN subjects sub ON r.subject_id=sub.id LEFT JOIN student_subjects ss ON r.subject_id=ss.subject_id AND r.student_id=ss.student_id WHERE r.student_id=? AND r.session=? AND r.exam_type=? AND r.id IN(SELECT MAX(id)FROM results WHERE student_id=? AND session=? AND exam_type=? GROUP BY subject_id)ORDER BY sub.name");$s->execute([$si['id'],$sn,$et,$si['id'],$sn,$et]);$rs=$s->fetchAll();
foreach($rs as&$rv){$rv['marks_obtained']=($rv['cq_marks']??0)+($rv['mcq_marks']??0)+($rv['practical_marks']??0)+($rv['assignment_marks']??0)+($rv['attendance_marks']??0);
$sd_=$pdo->prepare("SELECT total_marks,overall_pass_marks FROM subject_mark_distribution WHERE subject_id=? AND class_id=? AND session=? AND exam_type=? LIMIT 1");$sd_->execute([$rv['subject_id'],$ci,$sn,$et]);$smd_=$sd_->fetch();$rv['fm']=$tm_=$smd_?$smd_['total_marks']:100;$pm_=$smd_?$smd_['overall_pass_marks']:33;$ob=$rv['marks_obtained'];
if($ob>=$tm_*0.8)$eg_='A+';elseif($ob>=$tm_*0.7)$eg_='A';elseif($ob>=$tm_*0.6)$eg_='A-';elseif($ob>=$tm_*0.5)$eg_='B';elseif($ob>=$tm_*0.4)$eg_='C';elseif($ob>=$pm_)$eg_='D';else$eg_='F';
$sn_=$rv['sn']??'';if(stripos($sn_,'বা‌াংলা')!==false||stripos($sn_,'Bangla')!==false)$eg_=bg([$rv['cq_marks']??0],$rv['mcq_marks']??0,$tm_,$pm_);elseif(stripos($sn_,'ইংরেজি')!==false||stripos($sn_,'English')!==false)$eg_=eg([$rv['cq_marks']??0],$tm_,$pm_);
$rv['grade']=$eg_;$rv['grade_point']=gp($eg_);$rv['is_fourth_subject']=in_array((int)$ci,NO_FOURTH_CLS)?false:($rv['is_fourth_subject']??false);}unset($rv);
$to=0;$gv=false;foreach($rs as$rv){$to+=$rv['marks_obtained']??0;if(($rv['marks_obtained']??0)>0||($rv['cq_marks']??0)>0||($rv['mcq_marks']??0)>0)$gv=true;}
$md[$si['id']]=['total_obtained'=>$to,'gpa'=>gpa($rs),'has_valid_marks'=>$gv];}
$q="SELECT s.id,s.student_id,s.first_name,s.last_name,s.roll_number,s.class_id,c.name AS cn,c.section FROM students s JOIN classes c ON s.class_id=c.id WHERE s.class_id=? AND s.status='active'";$p=[$ci];if($gn){$q.=" AND s.group_name=?";$p[]=$gn;}$q.=" AND EXISTS(SELECT 1 FROM results r WHERE r.student_id=s.id AND r.session=? AND r.exam_type=?)ORDER BY s.roll_number";$p[]=$sn;$p[]=$et;$s=$pdo->prepare($q);$s->execute($p);$sc=$s->fetchAll();
foreach($sc as$si){$s=$pdo->prepare("SELECT r.*,sub.name AS sn,sub.code AS sc,ss.is_fourth_subject FROM results r LEFT JOIN subjects sub ON r.subject_id=sub.id LEFT JOIN student_subjects ss ON r.subject_id=ss.subject_id AND r.student_id=ss.student_id WHERE r.student_id=? AND r.session=? AND r.exam_type=? AND r.id IN(SELECT MAX(id)FROM results WHERE student_id=? AND session=? AND exam_type=? GROUP BY subject_id)ORDER BY sub.name");$s->execute([$si['id'],$sn,$et,$si['id'],$sn,$et]);$rs=$s->fetchAll();
foreach($rs as&$rv){$rv['marks_obtained']=($rv['cq_marks']??0)+($rv['mcq_marks']??0)+($rv['practical_marks']??0)+($rv['assignment_marks']??0)+($rv['attendance_marks']??0);
$sd_=$pdo->prepare("SELECT total_marks,overall_pass_marks FROM subject_mark_distribution WHERE subject_id=? AND class_id=? AND session=? AND exam_type=? LIMIT 1");$sd_->execute([$rv['subject_id'],$ci,$sn,$et]);$smd_=$sd_->fetch();$rv['fm']=$tm_=$smd_?$smd_['total_marks']:100;$pm_=$smd_?$smd_['overall_pass_marks']:33;$ob=$rv['marks_obtained'];
if($ob>=$tm_*0.8)$eg_='A+';elseif($ob>=$tm_*0.7)$eg_='A';elseif($ob>=$tm_*0.6)$eg_='A-';elseif($ob>=$tm_*0.5)$eg_='B';elseif($ob>=$tm_*0.4)$eg_='C';elseif($ob>=$pm_)$eg_='D';else$eg_='F';
$sn_=$rv['sn']??'';if(stripos($sn_,'বা‌াংলা')!==false||stripos($sn_,'Bangla')!==false)$eg_=bg([$rv['cq_marks']??0],$rv['mcq_marks']??0,$tm_,$pm_);elseif(stripos($sn_,'ইংরেজি')!==false||stripos($sn_,'English')!==false)$eg_=eg([$rv['cq_marks']??0],$tm_,$pm_);
$rv['grade']=$eg_;$rv['grade_point']=gp($eg_);$rv['is_fourth_subject']=in_array((int)$ci,NO_FOURTH_CLS)?false:($rv['is_fourth_subject']??false);}unset($rv);
$to=0;$tm=0;foreach($rs as$rv){$to+=$rv['marks_obtained']??0;$tm+=$rv['fm']??100;}$gv=gpa($rs);$si_data=['student'=>$si,'results'=>$rs,'total_obtained'=>$to,'total_marks'=>$tm,'gpa'=>$gv];
if($gv>0||$to>0)$si_data['has_valid_marks']=true;else$si_data['has_valid_marks']=false;
if(isset($md[$si['id']]))$si_data['merit_data']=$md[$si['id']];$sd[]=$si_data;}
usort($sd,function($a,$b){if(abs($b['gpa']-$a['gpa'])>0.001)return$b['gpa']<=>$a['gpa'];return$b['total_obtained']<=>$a['total_obtained'];});
$pos=1;$last_g=-1;$last_to=-1;$skip=0;foreach($sd as$i=>&$d){if($mp){if($d['gpa']==$last_g&&$d['total_obtained']==$last_to){$skip++;}else{$pos+=$skip;$skip=0;$last_g=$d['gpa'];$last_to=$d['total_obtained'];}$d['merit_position']=$pos;}}unset($d);
$all_subjects=[];if($ci&&$sn&&$et){$q="SELECT DISTINCT s.id,s.name AS subject_name,s.code AS subject_code,CASE WHEN ss.is_fourth_subject=1 THEN 1 ELSE 0 END AS is_fourth_subject,CASE WHEN(LOWER(s.name)LIKE'%বা‌াংলা%'OR LOWER(s.name)LIKE'%bangla%')AND LOWER(s.name)NOT LIKE'%bangladesh%'AND LOWER(s.name)NOT LIKE'%history%'THEN 1 WHEN(LOWER(s.name)LIKE'%ইংরেজি%'OR LOWER(s.name)LIKE'%english%')AND LOWER(s.name)NOT LIKE'%bangladesh%'AND LOWER(s.name)NOT LIKE'%history%'THEN 2 ELSE 3 END AS pri FROM subjects s JOIN results r ON s.id=r.subject_id JOIN students st ON r.student_id=st.id LEFT JOIN student_subjects ss ON s.id=ss.subject_id AND ss.student_id=st.id WHERE st.class_id=? AND r.session=? AND r.exam_type=?";$p=[$ci,$sn,$et];if($gn){$q.=" AND st.group_name=?";$p[]=$gn;}$q.=" ORDER BY pri ASC,is_fourth_subject ASC,s.name ASC";$s=$pdo->prepare($q);$s->execute($p);$all_subjects=$s->fetchAll();}}
function h($s){return htmlspecialchars((string)$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>
<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-g{background:rgba(52,211,153,.15);border:1px solid rgba(52,211,153,.3);border-radius:8px;padding:5px 16px;font-weight:600;color:#fff}.btn-g:hover{background:rgba(52,211,153,.25)}
.tsh{border:2px solid rgba(255,255,255,.15);padding:15px;margin:0 auto;background:rgba(255,255,255,.04);backdrop-filter:blur(16px);border-radius:14px;box-shadow:0 8px 32px rgba(0,0,0,.25);max-width:98vw;width:calc(98vw-40px);overflow-x:auto;overflow-y:visible}
.shd{text-align:center;border-bottom:2px double var(--p);padding-bottom:12px;margin-bottom:15px;border-radius:8px}.shd img{max-width:70px;max-height:70px;margin:0 10px}
.ti{background:rgba(255,255,255,.05);padding:10px;border-radius:8px;margin-bottom:12px;border:1px solid rgba(255,255,255,.1)}.ti strong{color:var(--p)}
.tt{width:100%;border-collapse:collapse;margin:10px 0;font-size:14px;border:1px solid rgba(255,255,255,.15)}
.tt th,.tt td{border:1px solid rgba(255,255,255,.15);padding:7px 9px;text-align:center;color:#fff!important}
.tt th{background:linear-gradient(135deg,rgba(108,99,255,.5),rgba(168,85,247,.4));color:#fff!important;font-weight:700;position:sticky;top:0}
.tt .sn{text-align:left;min-width:140px}.tt .sh{min-width:110px}
.tt .tc{background:rgba(52,211,153,.15)}.tt .gc{background:rgba(168,85,247,.15)}.tt .grc{background:rgba(251,191,36,.15)}
.tt .fh{background:rgba(233,30,99,.3)!important}.tt .fc{background:rgba(233,30,99,.1)}
.hc{display:none!important}.col-serial,.col-name,.col-roll,.col-group,.col-merit,.col-subjects,.col-total-marks,.col-obtained,.col-gpa,.col-grade{display:table-cell}
.hc{display:none!important}
.sm{background:rgba(52,211,153,.08);padding:10px;border-radius:8px;margin-top:12px;border:1px solid rgba(52,211,153,.15)}
.sg{margin-top:15px}.sg .sb{width:180px;padding:4px;border-top:1px solid rgba(255,255,255,.3);font-size:12px;color:#fff}
.cp{background:rgba(255,255,255,.05);padding:12px;border-radius:8px;margin-bottom:12px;border:1px solid rgba(255,255,255,.1);backdrop-filter:blur(8px)}
.cp h6{margin-bottom:10px;font-weight:600;border-bottom:1px solid rgba(255,255,255,.1);padding-bottom:6px}
.cp .fci{background:rgba(30,27,75,.8);border:1px solid rgba(255,255,255,.12);border-radius:5px;padding:6px 10px;margin-right:4px;color:#fff}.cp .fci:focus{border-color:var(--p);outline:none}
.cp .fci label{margin-left:4px;cursor:pointer;font-size:13px;user-select:none}
.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)}
@media(max-width:768px){.tt{font-size:10px}.tt th,.tt td{padding:3px 4px}.tt .sn{min-width:90px}.tsh{padding:8px}}
@media(max-width:576px){.tt{font-size:7px}.tt th,.tt td{padding:1px 2px}}
@media print{@page{size:A4 portrait;margin:8mm}*{-webkit-print-color-adjust:exact!important;color-adjust:exact!important;print-color-adjust:exact!important}body{background:#fff!important;margin:0;padding:0}.navbar,.cp,.btn,.no-print{display:none!important}.tsh{border:2px solid #000!important;box-shadow:none!important;margin:0;padding:6mm;width:100%!important;max-width:100%!important;overflow:visible;background:#fff!important;border-radius:0}.shd{page-break-inside:avoid;margin-bottom:8px;padding-bottom:8px;background:#fff!important;border-bottom:2px solid #000!important}.ti,.sm{page-break-inside:avoid;margin-bottom:6px;background:#fff!important;border:1px solid #000!important}.ti strong{color:#000!important}.tt{font-size:12px;width:100%!important;border-collapse:collapse;page-break-inside:auto}.tt th,.tt td{padding:4px 5px;font-size:11px;border:1px solid #000!important;background:#fff!important;color:#000!important}.tt th{background:#f0f0f0!important;color:#000!important}.tt .tc,.tt .gc,.tt .grc{background:#f0f0f0!important;color:#000!important}.tt .fh{background:#e91e63!important;color:#fff!important}.tt .fc{background:#fce4ec!important;color:#000!important}.sg .sb{border-top-color:#000!important;color:#000!important}}
<?php if($lm):?>@media print{@page{size:A4 landscape;margin:6mm}}<?php endif;?>
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}</style></head><body>
<nav class="navbar navbar-expand navbar-dark"><div class="container-fluid px-3"><a class="navbar-brand fw-bold" style="font-size:.9rem" href="../index.php"><i class="fas fa-table 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">
<?php if($ci&&$sn&&$et&&!empty($sd)):?>
<div class="mb-2 no-print"><a href="../index.php" class="btn btn-g btn-sm me-1"><i class="fas fa-home"></i> হোম</a><button class="btn btn-p btn-sm" onclick="window.print()"><i class="fas fa-print"></i> প্রিন্ট</button></div>
<div class="tsh">
<div class="shd d-flex align-items-center justify-content-center"><div class="d-flex align-items-center justify-content-between w-100">
<div><?php if($logo){$found=false;foreach(["../uploads/logos/$logo","uploads/logos/$logo","../$logo","$logo"]as$lp){if(file_exists($lp)){$lf=$lp;$found=true;break;}}if($found):?><img src="<?=$lf?>" alt="Logo"><?php else:?><div style="width:70px;height:70px;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.08);border-radius:50%;border:2px solid var(--p)"><i class="fas fa-school" style="font-size:22px;color:var(--p)"></i></div><?php endif;}else{?><div style="width:70px;height:70px;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.08);border-radius:50%;border:2px solid var(--p)"><i class="fas fa-school" style="font-size:22px;color:var(--p)"></i></div><?php }?></div>
<div class="text-center flex-grow-1 mx-2"><h5 class="mb-1 fw-bold" style="font-size:1.1rem"><?=h($site)?></h5><p class="mb-1 small"><?=h($addr)?></p><h6 class="mb-1">ট্যাবুলেশন শীট</h6><p class="mb-0 small">পরীক্ষা: <?=h(ucfirst($et))?>, সেশন: <?=h($sn)?></p></div></div></div>
<div class="ti row g-2"><div class="col-md-3"><strong>শ্রেণি:</strong> <?php foreach($cls as$c){if($c['id']==$ci){echo h($c['name'].' - '.$c['section']);break;}}?></div><div class="col-md-3"><strong>গ্রুপ:</strong> <?=$gn?h($gn):'সব'?></div><div class="col-md-3"><strong>মোট শিক্ষার্থী:</strong> <?=count($sd)?></div><div class="col-md-3"><strong>মোট বিষয়:</strong> <?=count($all_subjects)?></div></div>
<table class="tt"><thead><tr><th class="col-serial">ক্রমিক</th><th class="col-name">নাম</th><th class="col-roll">রোল</th><th class="col-group">গ্রুপ</th><?php foreach($all_subjects as$sb):?><th class="sh col-subjects col-subject-<?=$sb['id']?> <?=($sb['is_fourth_subject']??0)==1?'fh':''?>" style="font-size:11px"><?=h($sb['subject_name'])?></th><?php endforeach;?><th class="tc col-total-marks">মোট</th><th class="tc col-obtained">প্রাপ্ত</th><th class="gc col-gpa">জিপিএ</th><th class="grc col-grade">গ্রেড</th><?php if($mp):?><th class="col-merit">মেধাক্রম</th><?php endif;?></tr></thead><tbody>
<?php foreach($sd as$i=>$d):$st=$d['student'];$rm=[];foreach($d['results']as$r)$rm[$r['subject_id']]=$r;?>
<tr><td class="col-serial"><?=$i+1?></td><td class="sn col-name"><?=h($st['first_name'].' '.$st['last_name'])?></td><td class="col-roll"><?=$st['roll_number']?></td><td class="col-group"><?=h($st['group_name']??'')?></td>
<?php foreach($all_subjects as$sb):if(isset($rm[$sb['id']])){$r=$rm[$sb['id']];$cq=$r['cq_marks']??0;$mc=$r['mcq_marks']??0;$pr=$r['practical_marks']??0;$cells=[];if($pr>0)$cells=[round($cq),round($mc),round($pr),'=',round($cq+$mc+$pr)];elseif($mc>0)$cells=[round($cq),round($mc),'=',round($cq+$mc)];else$cells=[round($cq)];echo'<td class="col-subjects col-subject-'.$sb['id'].' '.(($sb['is_fourth_subject']??0)==1?'fc':'').'">'.implode(' + ',$cells).'</td>';}else{echo'<td class="col-subjects col-subject-'.$sb['id'].' '.(($sb['is_fourth_subject']??0)==1?'fc':'').'">-</td>';}endforeach;?>
<td class="tc col-total-marks"><?=$d['total_marks']?></td><td class="tc col-obtained"><?=$d['total_obtained']?></td><td class="gc col-gpa"><?=number_format($d['gpa'],2)?></td><td class="grc col-grade"><?=gf($d['gpa'])?></td><?php if($mp):?><td class="col-merit"><?=$d['merit_position']??'-'?></td><?php endif;?></tr><?php endforeach;?></tbody></table>
<div class="sm row g-2"><div class="col-md-3"><strong>মোট শিক্ষার্থী:</strong> <?=count($sd)?></div><div class="col-md-3"><strong>মোট বিষয়:</strong> <?=count($all_subjects)?></div><div class="col-md-3"><strong>পরীক্ষার ধরন:</strong> <?=h(ucfirst($et))?></div><div class="col-md-3"><strong>সেশন:</strong> <?=h($sn)?></div></div>
<div class="sg d-flex justify-content-between"><div class="sb text-start">প্রধান শিক্ষক</div><div class="sb text-center">শ্রেণি শিক্ষক</div><div class="sb text-end">পরীক্ষা নিয়ন্ত্রক</div></div></div>
<?php else:?>
<div class="fc"><div class="ch"><i class="fas fa-filter me-1"></i>ট্যাবুলেশন শীট তৈরি করুন</div><div class="cb">
<form method="GET" class="row g-2"><div class="col-md-2"><label class="fl">শ্রেণি</label><select class="form-select fc-s" name="class_id" required><option value="">নির্বাচন</option><?php foreach($cls as$c):?><option value="<?=$c['id']?>" <?=$ci==$c['id']?'selected':''?>><?=h($c['name'].' - '.$c['section'])?></option><?php endforeach;?></select></div>
<div class="col-md-2"><label class="fl">সেশন</label><select class="form-select fc-s" name="session" required><option value="">নির্বাচন</option><?php foreach($ss as$s):?><option value="<?=h($s['session'])?>" <?=$sn==$s['session']?'selected':''?>><?=h($s['session'])?></option><?php endforeach;?></select></div>
<div class="col-md-2"><label class="fl">গ্রুপ</label><select class="form-select fc-s" name="group_name"><option value="">সব</option><option value="Science" <?=$gn=='Science'?'selected':''?>>Science</option><option value="Commerce" <?=$gn=='Commerce'?'selected':''?>>Commerce</option><option value="Humanities" <?=$gn=='Humanities'||$gn=='Arts'?'selected':''?>>Humanities</option><option value="General" <?=$gn=='General'?'selected':''?>>General</option></select></div>
<div class="col-md-2"><label class="fl">পরীক্ষার ধরন</label><select class="form-select fc-s" name="exam_type" required><option value="">নির্বাচন</option><?php foreach($ets as$e):?><option value="<?=h($e['exam_type'])?>" <?=$et==$e['exam_type']?'selected':''?>><?=h(ucfirst($e['exam_type']))?></option><?php endforeach;?></select></div>
<div class="col-md-2"><label class="fl">মেধাক্রম</label><select class="form-select fc-s" name="show_merit_position"><option value="1" <?=$mp?'selected':''?>>দেখান</option><option value="0" <?=!$mp?'selected':''?>>লুকান</option></select></div>
<div class="col-md-2"><label class="fl">মোড</label><select class="form-select fc-s" name="landscape_mode"><option value="0" <?=!$lm?'selected':''?>>পোর্ট্রেট</option><option value="1" <?=$lm?'selected':''?>>ল্যান্ডস্কেপ</option></select></div>
<div class="col-12 d-flex align-items-end"><button type="submit" class="btn btn-p"><i class="fas fa-table me-1"></i>ট্যাবুলেশন শীট তৈরি করুন</button></div></form>
<?php if($ci&&$sn&&$et&&empty($sd)):?><div class="alert alert-warning mt-2 py-2 small">কোনো ফলাফল পাওয়া যায়নি।</div><?php endif;?></div></div>
<?php endif;?></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>document.querySelector('select[name="class_id"]')?.addEventListener('change',function(){this.closest('form')?.submit()});
document.querySelector('select[name="session"]')?.addEventListener('change',function(){this.closest('form')?.submit()});
document.querySelector('select[name="exam_type"]')?.addEventListener('change',function(){this.closest('form')?.submit()});</script>
</body></html>
