<?php
session_start();
require_once __DIR__ . '/parent_portal_common.php';
$conn = pp_connect();
parent_portal_install($conn);
$success=''; $error='';
if($_SERVER['REQUEST_METHOD']=='POST'){
    $action=isset($_POST['action'])?$_POST['action']:'';
    if($action=='create'){
        $title=isset($_POST['title'])?trim($_POST['title']):''; $subject=isset($_POST['subject'])?trim($_POST['subject']):''; $stage=isset($_POST['stage'])?trim($_POST['stage']):''; $class_name=isset($_POST['class_name'])?trim($_POST['class_name']):''; $exam_date=isset($_POST['exam_date'])?trim($_POST['exam_date']):''; $exam_time=isset($_POST['exam_time'])?trim($_POST['exam_time']):''; $notes=isset($_POST['notes'])?trim($_POST['notes']):'';
        if($title==''||$stage==''||$exam_date=='') $error='يرجى إدخال العنوان والمرحلة والتاريخ.';
        else { $stmt=mysqli_prepare($conn,"INSERT INTO parent_exam_schedules (title, subject, stage, class_name, exam_date, exam_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?)"); if($stmt){ mysqli_stmt_bind_param($stmt,'sssssss',$title,$subject,$stage,$class_name,$exam_date,$exam_time,$notes); if(mysqli_stmt_execute($stmt)) $success='تم إضافة جدول الامتحان بنجاح.'; else $error='خطأ: '.mysqli_stmt_error($stmt); mysqli_stmt_close($stmt);} }
    }
    if($action=='delete'){ $id=intval($_POST['id']); if(mysqli_query($conn,"DELETE FROM parent_exam_schedules WHERE id='$id'")) $success='تم حذف الجدول.'; else $error='خطأ: '.mysqli_error($conn); }
}
$schedules=mysqli_query($conn,"SELECT * FROM parent_exam_schedules ORDER BY exam_date ASC, id DESC");
?>
<!DOCTYPE html><html lang="ar" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>جداول امتحانات أولياء الأمور</title><link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet"><style>
:root{--navy:#0b2d4d;--dark:#061b30;--gold:#d7a328;--line:#e2e8f0}*{box-sizing:border-box}body{margin:0;font-family:Cairo,Arial;background:radial-gradient(circle at 10% 10%,rgba(215,163,40,.20),transparent 25%),linear-gradient(135deg,#f8fafc,#eef4fb,#f7f2e8);color:#132238}.container{width:95%;max-width:1250px;margin:25px auto}.hero{background:linear-gradient(135deg,var(--dark),var(--navy));color:#fff;border-radius:30px;padding:24px;display:grid;grid-template-columns:90px 1fr auto;gap:16px;align-items:center;box-shadow:0 25px 70px rgba(6,27,48,.22)}.logo{width:88px;height:88px;background:#fff;border:3px solid var(--gold);border-radius:24px;padding:9px}.logo img{width:100%;height:100%;object-fit:contain}.hero h1{margin:0}.hero p{margin:6px 0 0;color:#dbeafe}.top-link{background:#fff7df;color:#6e5207;text-decoration:none;border-radius:15px;padding:12px 16px;font-weight:900}.card{background:#fff;border-radius:26px;padding:20px;box-shadow:0 18px 42px rgba(15,23,42,.08);margin:18px 0}.card h2{margin:0 0 14px;color:var(--navy)}.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:13px}.full{grid-column:1/-1}.field label{display:block;font-weight:900;margin-bottom:7px}.field input,.field select,.field textarea{width:100%;padding:13px;border:1px solid #dbe3ef;border-radius:16px;font-family:Cairo;outline:none}.field textarea{min-height:100px}.btn{border:none;border-radius:16px;padding:12px 15px;font-family:Cairo;font-weight:900;cursor:pointer}.main{background:linear-gradient(135deg,var(--navy),#174f80,var(--gold));color:#fff}.red{background:#fee2e2;color:#991b1b}.alert{padding:14px;border-radius:16px;text-align:center;font-weight:900;margin-bottom:15px}.success{background:#dcfce7;color:#166534}.error{background:#fee2e2;color:#991b1b}.table-wrap{overflow-x:auto;border:1px solid #edf2f7;border-radius:20px}table{width:100%;border-collapse:collapse;min-width:850px}th,td{text-align:center;padding:11px;border-bottom:1px solid #edf2f7;white-space:nowrap}th{background:var(--navy);color:#fff}.date-badge{display:inline-block;background:#fff7df;color:#7a580b;border:1px solid #f5d77b;border-radius:999px;padding:6px 10px;font-weight:900}@media(max-width:850px){.hero,.grid{grid-template-columns:1fr;text-align:center}.logo{margin:auto}.full{grid-column:auto}}
</style></head><body><div class="container"><div class="hero"><div class="logo"><img src="/sy/school_system/logo.png"></div><div><h1>إدارة جداول الامتحانات</h1><p>إضافة وتنظيم جداول الامتحانات التي تظهر في بوابة ولي الأمر.</p></div><a class="top-link" href="admin_dashboard.php">لوحة الإدارة</a></div><?php if($success!=''){ ?><div class="alert success"><?php echo h($success); ?></div><?php } ?><?php if($error!=''){ ?><div class="alert error"><?php echo h($error); ?></div><?php } ?><div class="card"><h2>إضافة جدول امتحان</h2><form method="POST"><input type="hidden" name="action" value="create"><div class="grid"><div class="field"><label>عنوان الجدول</label><input type="text" name="title" required placeholder="مثال: امتحان شهر أول"></div><div class="field"><label>المادة</label><input type="text" name="subject" placeholder="مثال: الرياضيات"></div><div class="field"><label>المرحلة</label><select name="stage" required><option value="">اختر المرحلة</option><?php echo pp_stage_options(''); ?></select></div><div class="field"><label>الشعبة</label><input type="text" name="class_name" placeholder="مثال: أ أو جميع الشعب"></div><div class="field"><label>تاريخ الامتحان</label><input type="date" name="exam_date" required></div><div class="field"><label>الوقت</label><input type="text" name="exam_time" placeholder="مثال: 8:30 صباحاً"></div><div class="field full"><label>ملاحظات</label><textarea name="notes" placeholder="أي تفاصيل إضافية"></textarea></div></div><br><button class="btn main" type="submit">إضافة الجدول</button></form></div><div class="card"><h2>الجداول الحالية</h2><div class="table-wrap"><table><tr><th>#</th><th>العنوان</th><th>المادة</th><th>المرحلة</th><th>الشعبة</th><th>التاريخ</th><th>الوقت</th><th>الملاحظات</th><th>إجراء</th></tr><?php if($schedules && mysqli_num_rows($schedules)>0){ while($s=mysqli_fetch_assoc($schedules)){ ?><tr><td><?php echo intval($s['id']); ?></td><td><?php echo h($s['title']); ?></td><td><?php echo h($s['subject']); ?></td><td><?php echo h($s['stage']); ?></td><td><?php echo h($s['class_name']); ?></td><td><span class="date-badge"><?php echo h($s['exam_date']); ?></span></td><td><?php echo h($s['exam_time']); ?></td><td><?php echo h($s['notes']); ?></td><td><form method="POST" onsubmit="return confirm('هل تريد حذف هذا الجدول؟')"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?php echo intval($s['id']); ?>"><button class="btn red" type="submit">حذف</button></form></td></tr><?php }} else { ?><tr><td colspan="9">لا توجد جداول بعد.</td></tr><?php } ?></table></div></div></div></body></html>
