<?php
session_start();require_once'../config/database.php';
if(!isset($_SESSION['user_id'])||$_SESSION['user_role']!='admin'){header('Location: ../login.php');exit;}
try{$pdo->query("SELECT designation FROM teachers LIMIT 1");}catch(Exception$e){try{$pdo->exec("ALTER TABLE teachers ADD COLUMN designation VARCHAR(150) NULL AFTER last_name");}catch(Exception$e2){}}
$s=$e='';
if($_SERVER['REQUEST_METHOD']=='POST'){
  $fn=trim($_POST['first_name']??'');$ln=trim($_POST['last_name']??'');$em=trim($_POST['email']??'');$ph=trim($_POST['phone']??'');
  $ad=trim($_POST['address']??'');$dob=$_POST['date_of_birth']??'';$g=$_POST['gender']??'';$q=trim($_POST['qualification']??'');
  $ey=(int)($_POST['experience_years']??0);$jd=$_POST['joining_date']??'';$sa=(float)($_POST['salary']??0);$ds=trim($_POST['designation']??'');
  if(!$fn||!$ln||!$em)$e='প্রয়োজনীয় ক্ষেত্রগুলো পূরণ করুন';
  else{try{$pdo->beginTransaction();
    $yr=date('Y');$q=$pdo->prepare("SELECT COUNT(*)FROM teachers WHERE employee_id LIKE ?");$q->execute(['EMP'.$yr.'%']);$c=$q->fetchColumn();$eid='EMP'.$yr.str_pad($c+1,4,'0',STR_PAD_LEFT);
    if(!filter_var($em,FILTER_VALIDATE_EMAIL))throw new Exception('ইমেইল সঠিক নয়');
    $q=$pdo->prepare("SELECT COUNT(*)FROM teachers WHERE email=? AND email IS NOT NULL AND email!=''");$q->execute([$em]);if($q->fetchColumn()>0)throw new Exception('ইমেইল ইতিমধ্যে ব্যবহৃত');
    $q=$pdo->prepare("SELECT COUNT(*)FROM users WHERE email=? AND email IS NOT NULL AND email!=''");$q->execute([$em]);if($q->fetchColumn()>0)throw new Exception('ইমেইল ইতিমধ্যে ব্যবহৃত');
    $un=strtolower($fn.$ln.rand(100,999));$pw=password_hash('teacher123',PASSWORD_DEFAULT);
    try{$q=$pdo->prepare("INSERT INTO users(username,password,email,role)VALUES(?,?,?,'teacher')");$q->execute([$un,$pw,$em]);$uid=$pdo->lastInsertId();}
    catch(Exception$x){if(strpos($x->getMessage(),'Duplicate')!==false){$q=$pdo->prepare("INSERT INTO users(username,password,email,role)VALUES(?,?,NULL,'teacher')");$q->execute([$un,$pw]);$uid=$pdo->lastInsertId();$em=null;}else throw$x;}
    $pp=null;
    if(isset($_FILES['photo'])&&$_FILES['photo']['error']==0){$d='../uploads/teachers/';if(!is_dir($d))mkdir($d,0777,true);$ex=pathinfo($_FILES['photo']['name'],PATHINFO_EXTENSION);$pf=$eid.'.'.$ex;$p=$d.$pf;if(move_uploaded_file($_FILES['photo']['tmp_name'],$p))$pp='uploads/teachers/'.$pf;}
    $q=$pdo->prepare("INSERT INTO teachers(user_id,employee_id,first_name,last_name,designation,email,phone,address,date_of_birth,gender,qualification,experience_years,joining_date,salary,photo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    $q->execute([$uid,$eid,$fn,$ln,$ds?:null,$em,$ph,$ad,$dob,$g,$q,$ey,$jd,$sa,$pp]);
    $pdo->commit();$s='শিক্ষক যোগ করা হয়েছে। আইডি: '.$eid.', ইউজারনেম: '.$un;$_POST=[];
  }catch(Exception$x){$pdo->rollBack();$e=$x->getMessage();}
}}
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}
.c{background:#252220;border-radius:10px;border:1px solid #3a3735;height:calc(100vh - 56px);display:flex;flex-direction:column}
.ct{background:#2f2c2a;padding:8px 14px;font-size:.82rem;font-weight:600;display:flex;align-items:center;gap:6px;border-bottom:1px solid #3a3735;flex-shrink:0}
.cb{padding:8px 14px;flex:1;overflow-y:auto}
.cb::-webkit-scrollbar{width:4px}.cb::-webkit-scrollbar-thumb{background:#3a3735;border-radius:4px}
label{font-size:.7rem;font-weight:600;color:#a09888;display:block;margin-bottom:1px}
input,select,textarea{width:100%;padding:5px 8px;border:1px solid #3a3735;border-radius:6px;font-size:.76rem;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:32px;height:32px}
.hint{font-size:.65rem;color:#706860}
.btn{display:inline-flex;align-items:center;gap:3px;padding:5px 12px;border:none;border-radius:6px;font-size:.76rem;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 8px rgba(74,90,60,.3)}
.btn-s{background:#3a3735;color:#a09888}.btn-s:hover{background:#4a4745}
.btn-o{background:linear-gradient(135deg,#8a7a5a,#7a6a4a);color:#e8e4dc}.btn-o:hover{box-shadow:0 2px 8px rgba(90,70,40,.3)}
.a{padding:6px 10px;border-radius:6px;font-size:.74rem;margin-bottom:6px;display:flex;align-items:center;gap:4px;flex-shrink:0}
.a-s{background:#1a2a18;color:#7aaa6a;border:1px solid #2a3a28}.a-d{background:#2a1818;color:#ca7a6a;border:1px solid #3a2828}
.g2{display:grid;grid-template-columns:1fr 1fr;gap:6px}@media(max-width:480px){.g2{grid-template-columns:1fr}}
.r{display:flex;gap:6px;flex-wrap:wrap;align-items:center;justify-content:space-between;border-top:1px solid #3a3735;padding:6px 0 0;margin-top:4px;flex-shrink:0}
.sh{font-size:.76rem;color:#888078;display:flex;align-items:center;gap:4px;margin:4px 0 3px}
.sh:first-of-type{margin-top:0}
.tg{padding:6px 10px;border-radius:6px;background:#1e2220;border:1px solid #2a3830;color:#7aaa6a;margin:4px 0;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap}
.tg h6{font-size:.74rem;color:#8aba7a;margin:0}.tg p{font-size:.7rem;margin:0;color:#6a9a5a}
.mb{margin-bottom:4px}
</style></head><body>
<nav class=h><div>🎓 স্কুল ম্যানেজমেন্ট</div><div><a href=../index.php>🏠 হোম</a><a href=list.php>📋 তালিকা</a></div></nav>
<div class=w>
<div class=c>
<div class=ct>👨‍🏫 শিক্ষক যোগ করুন</div>
<?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;?>
<div class=cb>
<form method=post action="" enctype=multipart/form-data>
<div class=sh>👤 ব্যক্তিগত তথ্য</div>
<div class=g2><div class=mb><label>প্রথম নাম *</label><input type=text name=first_name value="<?=h($_POST['first_name']??'')?>" required></div><div class=mb><label>শেষ নাম *</label><input type=text name=last_name value="<?=h($_POST['last_name']??'')?>" required></div></div>
<div class=g2><div class=mb><label>ইমেইল *</label><input type=email name=email value="<?=h($_POST['email']??'')?>" required></div><div class=mb><label>পদবী</label><input type=text name=designation value="<?=h($_POST['designation']??'')?>" placeholder='সহকারী শিক্ষক'></div></div>
<div class=g2><div class=mb><label>ফোন</label><input type=tel name=phone value="<?=h($_POST['phone']??'')?>"></div><div class=mb><label>জন্ম তারিখ</label><input type=date name=date_of_birth value="<?=h($_POST['date_of_birth']??'')?>"></div></div>
<div class=g2><div class=mb><label>লিঙ্গ</label><select name=gender><option value="">নির্বাচন</option><option value=male <?=($_POST['gender']??'')=='male'?'selected':''?>>পুরুষ</option><option value=female <?=($_POST['gender']??'')=='female'?'selected':''?>>মহিলা</option><option value=other <?=($_POST['gender']??'')=='other'?'selected':''?>>অন্যান্য</option></select></div><div class=mb><label>ছবি</label><input type=file name=photo accept=image/* onchange="p(this)"><img id=pr style=display:none;max-width:60px;border-radius:4px;vertical-align:top></div></div>
<div class=mb><label>ঠিকানা</label><textarea name=address><?=h($_POST['address']??'')?></textarea></div>
<div class=sh>💼 পেশাগত তথ্য</div>
<div class=g2><div class=mb><label>শিক্ষাগত যোগ্যতা</label><input type=text name=qualification value="<?=h($_POST['qualification']??'')?>" placeholder='এমএ, বিএড'></div><div class=mb><label>অভিজ্ঞতা (বছর)</label><input type=number name=experience_years min=0 max=50 value="<?=h($_POST['experience_years']??'0')?>"></div></div>
<div class=g2><div class=mb><label>যোগদান</label><input type=date name=joining_date value="<?=h($_POST['joining_date']??date('Y-m-d'))?>"></div><div class=mb><label>বেতন (টাকা)</label><input type=number name=salary min=0 step=0.01 value="<?=h($_POST['salary']??'')?>"></div></div>
<div class=tg><h6>🔑 লগইন</h6><p>পাস: <strong>teacher123</strong> | ইউজারনেম: স্বয়ংক্রিয়</p></div>
<div class=r><a href=list.php class="btn btn-s">⬅ ফিরে যান</a><div><button type=reset class="btn btn-o">🔄 রিসেট</button><button class="btn btn-g" style=margin-left:4px>💾 সংরক্ষণ</button></div></div>
</form>
</div></div></div>
<script>
function p(e){var f=e.files[0];if(f){var r=new FileReader();r.onload=function(ev){var i=document.getElementById('pr');i.style.display='inline';i.src=ev.target.result};r.readAsDataURL(f)}}
</script></body></html>
