<?php
session_start();require_once'../config/database.php';
if(!isset($_SESSION['user_id'])||$_SESSION['user_role']!=='admin'){header('Location: ../login.php');exit;}
$s=$e='';
if($_SERVER['REQUEST_METHOD']=='POST'){
  $n=trim($_POST['name']??'');$sc=trim($_POST['section']??'');$c=intval($_POST['capacity']??0);$d=trim($_POST['description']??'');$st=$_POST['status']??'active';
  if(!$n)$e='ক্লাসের নাম আবশ্যক';
  elseif($c<=0)$e='ধারণক্ষমতা ০ এর চেয়ে বেশি হতে হবে';
  else{try{$q=$pdo->prepare("SELECT id FROM classes WHERE name=? AND (section=? OR (section='' AND ?=''))");$q->execute([$n,$sc,$sc]);
    if($q->fetch())$e='এই নাম ও সেকশনের ক্লাস ইতিমধ্যে বিদ্যমান';
    else{$q=$pdo->prepare("INSERT INTO classes(name,section,capacity,description,status)VALUES(?,?,?,?,?)");
      if($q->execute([$n,$sc,$c,$d,$st])){$s='নতুন ক্লাস সফলভাবে যোগ করা হয়েছে!';$_POST=[];}
      else $e='ক্লাস যোগ করতে সমস্যা হয়েছে';
    }
  }catch(Exception$x){$e='ডাটাবেস ত্রুটি: '.$x->getMessage();}
  }
}
$cls=$pdo->query("SELECT name,section,capacity,status,created_at,(SELECT COUNT(*)FROM students WHERE class_id=classes.id AND status='active')sc FROM classes ORDER BY name,section")->fetchAll();
$stt=$pdo->query("SELECT COUNT(*)tc,SUM(CASE WHEN status='active' THEN 1 ELSE 0 END)ac,SUM(capacity)tp,(SELECT COUNT(*)FROM students WHERE status='active')ts FROM classes")->fetch();
$u=$stt['tp']>0?min(round($stt['ts']/$stt['tp']*100,1),100):0;
function h($s){return htmlspecialchars($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>
<style>
*,body{margin:0;padding:0;box-sizing:border-box}
body{font-family:'Hind Siliguri',Arial,'Siyam Rupali','Noto Sans Bengali',sans-serif;background:#1a1818;height:100vh;overflow:hidden;color:#d4ccc4;display:flex;flex-direction:column}
.h{background:#2a2725;color:#d4ccc4;padding:8px 16px;display:flex;align-items:center;justify-content:space-between;font-size:.82rem;flex-shrink:0;border-bottom:1px solid #3a3735}
.h a{color:#a09888;text-decoration:none;font-size:.76rem;padding:4px 10px;border-radius:6px;transition:all .2s}
.h a:hover{background:rgba(255,255,255,.06);color:#d4ccc4}
.w{flex:1;overflow:hidden;max-width:100%;margin:0 auto}
.g{display:grid;grid-template-columns:2fr 3fr 2fr;gap:10px;height:calc(100vh - 56px)}
@media(max-width:1100px){.g{grid-template-columns:1fr 1fr}}@media(max-width:700px){.g{grid-template-columns:1fr}}
.c{background:#252220;border-radius:10px;border:1px solid #3a3735;display:flex;flex-direction:column;overflow:hidden}
.ct{background:#2f2c2a;padding:7px 12px;font-size:.78rem;font-weight:600;display:flex;align-items:center;gap:5px;border-bottom:1px solid #3a3735;flex-shrink:0}
.cb{padding:6px 10px;flex:1;overflow-y:auto}
.cb::-webkit-scrollbar{width:3px}.cb::-webkit-scrollbar-thumb{background:#3a3735;border-radius:3px}
label{font-size:.68rem;font-weight:600;color:#a09888;display:block;margin-bottom:1px}
input,select,textarea{width:100%;padding:4px 7px;border:1px solid #3a3735;border-radius:5px;font-size:.73rem;outline:none;font-family:inherit;background:#1e1c1b;transition:all .2s;color:#d4ccc4}
input:focus,select:focus,textarea:focus{border-color:#6a6058;background:#22201f}
textarea{resize:none;min-height:28px;height:28px}
.btn{display:inline-flex;align-items:center;gap:3px;padding:4px 10px;border:none;border-radius:5px;font-size:.72rem;font-weight:600;cursor:pointer;text-decoration:none;font-family:inherit;transition:all .2s}
.btn-g{background:linear-gradient(135deg,#6a7a60,#5a6a50);color:#e8e4dc}.btn-g:hover{box-shadow:0 2px 6px rgba(74,90,60,.3)}
.btn-s{background:#3a3735;color:#a09888}.btn-s:hover{background:#4a4745}
.a{padding:5px 8px;border-radius:5px;font-size:.7rem;margin-bottom:4px;display:flex;align-items:center;gap:3px}
.a-s{background:#1a2a18;color:#7aaa6a;border:1px solid #2a3a28}.a-d{background:#2a1818;color:#ca7a6a;border:1px solid #3a2828}
.t{width:100%;border-collapse:collapse;font-size:.7rem}
.t th{text-align:left;padding:4px 6px;background:#2a2725;border-bottom:1px solid #3a3735;font-weight:600;color:#a09888;white-space:nowrap;font-size:.68rem}
.t td{padding:3px 6px;border-bottom:1px solid #2a2725}
.t tr:hover td{background:#2a2725}
.bdg{display:inline-block;padding:1px 6px;border-radius:10px;font-size:.65rem;font-weight:600}
.bdg-g{background:#1a2a18;color:#7aaa6a}.bdg-s{background:#2a2725;color:#706860}.bdg-r{background:#2a1818;color:#ca7a6a}
.mt{margin-top:4px}.mb{margin-bottom:3px}
.f2{display:grid;grid-template-columns:1fr 1fr;gap:4px}
.tg{display:flex;flex-wrap:wrap;gap:2px;margin-top:3px}
.tg s{background:#2a2725;padding:1px 6px;border-radius:4px;font-size:.68rem;color:#888078}
</style></head><body>
<nav class=h><div>🎓 স্কুল ম্যানেজমেন্ট</div><div><a href=../index.php>🏠 হোম</a><a href=manage.php>📋 ক্লাস তালিকা</a></div></nav>
<div class=w>
<div class=g>
<div class=c>
<div class=ct>✨ নতুন ক্লাস</div>
<div class=cb>
<?php if($s):?><div class="a a-s">✓ <?=h($s)?></div><?php endif;if($e):?><div class="a a-d">⚠ <?=h($e)?></div><?php endif;?>
<form method=post action="">
<div class=f2><div class=mb><label>ক্লাসের নাম *</label><input type=text name=name value="<?=h($_POST['name']??'')?>" placeholder='যেমন: ষষ্ঠ' required></div>
<div class=mb><label>সেকশন</label><input type=text name=section value="<?=h($_POST['section']??'')?>" placeholder='যেমন: ক'></div></div>
<div class=f2><div class=mb><label>ধারণক্ষমতা *</label><input type=number name=capacity value="<?=h($_POST['capacity']??'50')?>" min=1 max=1000 required></div>
<div class=mb><label>স্ট্যাটাস</label><select name=status required><option value=active <?=($_POST['status']??'active')=='active'?'selected':''?>>সক্রিয়</option><option value=inactive <?=($_POST['status']??'')=='inactive'?'selected':''?>>নিষ্ক্রিয়</option></select></div></div>
<div class=mb><label>বিবরণ</label><textarea name=description><?=h($_POST['description']??'')?></textarea></div>
<div class=mt><button class="btn btn-g">💾 যোগ করুন</button> <a href=manage.php class="btn btn-s">📋 তালিকা</a></div>
</form>
<div style="margin-top:6px;padding-top:5px;border-top:1px solid #3a3735">
<div class=tg><strong style=font-size:.68rem;color:#888078>📛 উদাহরণ:</strong><s>প্রথম, দ্বিতীয়</s><s>ষষ্ঠ, সপ্তম</s><s>নবম, দশম</s></div>
<div class=tg><strong style=font-size:.68rem;color:#888078>ℹ️ টিপস:</strong><s>একই নাম ও সেকশন দ্বিগুণ নয়</s><s>ধারণক্ষমতা পরিবর্তন যোগ্য</s></div>
</div>
</div></div>

<div class=c>
<div class=ct>📋 ক্লাসের তালিকা <span style=margin-left:auto;font-size:.65rem;background:#3a3735;color:#a09888;padding:0 8px;border-radius:10px><?=count($cls)?></span></div>
<div class=cb>
<?php if(empty($cls)):?><div style=text-align:center;padding:12px;color:#706860;font-size:.75rem>কোন ক্লাস নেই</div>
<?php else:?>
<table class=t><thead><tr><th>ক্লাস</th><th>সেকশন</th><th>ধারণ</th><th>শিক্ষার্থী</th><th>স্ট্যাটাস</th></tr></thead>
<tbody><?php foreach($cls as$x):?><tr><td><strong><?=h($x['name'])?></strong></td><td><?=h($x['section']?:'—')?></td><td><?=$x['capacity']?></td><td><span class="bdg <?=$x['sc']>$x['capacity']?'bdg-r':'bdg-g'?>"><?=$x['sc']?></span></td><td><?=$x['status']=='active'?'<span class="bdg bdg-g">সক্রিয়</span>':'<span class="bdg bdg-s">নিষ্ক্রিয়</span>'?></td></tr><?php endforeach;?></tbody></table>
<?php endif;?>
</div></div>

<div class=c>
<div class=ct>📊 পরিসংখ্যান</div>
<div class=cb>
<div style=display:grid;grid-template-columns:1fr 1fr;gap:0;text-align:center>
<div style=padding:6px 0;border-right:1px solid #3a3735;border-bottom:1px solid #3a3735><div style=font-size:1.1rem;font-weight:700;color:#7aaa6a><?=$stt['tc']?></div><small style=font-size:.65rem;color:#706860>মোট</small></div>
<div style=padding:6px 0;border-bottom:1px solid #3a3735><div style=font-size:1.1rem;font-weight:700;color:#8aba7a><?=$stt['ac']?></div><small style=font-size:.65rem;color:#706860>সক্রিয়</small></div>
<div style=padding:6px 0;border-right:1px solid #3a3735><div style=font-size:1.1rem;font-weight:700;color:#6a9a8a><?=$stt['tp']?:0?></div><small style=font-size:.65rem;color:#706860>ধারণক্ষমতা</small></div>
<div style=padding:6px 0><div style=font-size:1.1rem;font-weight:700;color:#caaa6a><?=$stt['ts']?></div><small style=font-size:.65rem;color:#706860>শিক্ষার্থী</small></div>
</div>
<div style=height:4px;background:#2a2725;border-radius:4px;overflow:hidden;margin-top:6px><div style=height:100%;border-radius:4px;width:<?=$u?>%;background:<?=$u>90?'#ca7a6a':($u>75?'#caaa6a':'#7aaa6a')?>></div></div>
<div style=display:flex;justify-content:space-between;font-size:.65rem;color:#706860;margin-top:2px><span>ব্যবহার</span><span><?=$u?>%</span></div>
</div></div>
</div></div>
</body></html>
