<?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']):''; $body=isset($_POST['body'])?trim($_POST['body']):''; $target_type=isset($_POST['target_type'])?trim($_POST['target_type']):'all'; $student_id=isset($_POST['student_id'])?intval($_POST['student_id']):0; $stage=isset($_POST['stage'])?trim($_POST['stage']):''; $class_name=isset($_POST['class_name'])?trim($_POST['class_name']):'';
        if($title=='' || $body=='') $error='يرجى كتابة عنوان ونص الإشعار.';
        else {
            $stmt=mysqli_prepare($conn,"INSERT INTO parent_notifications (title, body, target_type, student_id, stage, class_name, is_active) VALUES (?, ?, ?, ?, ?, ?, 1)");
            if($stmt){ mysqli_stmt_bind_param($stmt,'sssiss',$title,$body,$target_type,$student_id,$stage,$class_name); if(mysqli_stmt_execute($stmt)) $success='تم إرسال الإشعار بنجاح.'; else $error='خطأ: '.mysqli_stmt_error($stmt); mysqli_stmt_close($stmt); }
        }
    }
    if($action=='toggle'){ $id=intval($_POST['id']); $st=intval($_POST['new_status']); if(mysqli_query($conn,"UPDATE parent_notifications SET is_active='$st' WHERE id='$id'")) $success='تم تحديث حالة الإشعار.'; else $error='خطأ: '.mysqli_error($conn); }
    if($action=='delete'){ $id=intval($_POST['id']); if(mysqli_query($conn,"DELETE FROM parent_notifications WHERE id='$id'")) $success='تم حذف الإشعار.'; else $error='خطأ: '.mysqli_error($conn); }
}
$students=mysqli_query($conn,"SELECT id,name,stage,class FROM students ORDER BY stage ASC,class ASC,name ASC");
$notifications=mysqli_query($conn,"SELECT pn.*, s.name AS student_name FROM parent_notifications pn LEFT JOIN students s ON s.id=pn.student_id ORDER BY pn.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(2,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:120px}.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}.green{background:#dcfce7;color:#166534}.red{background:#fee2e2;color:#991b1b}.blue{background:#eef5ff;color:#0b2d4d}.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}.notice-card{border:1px solid #edf2f7;border-radius:18px;padding:15px;margin-bottom:12px;background:#f8fafc}.notice-head{display:flex;justify-content:space-between;gap:10px;align-items:start}.badge{display:inline-block;padding:6px 10px;border-radius:999px;font-size:12px;font-weight:900;background:#fff7df;color:#7a580b}.actions{display:flex;gap:8px;flex-wrap:wrap;margin-top:10px}@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></div><div class="field"><label>نوع الاستهداف</label><select name="target_type"><option value="all">كل أولياء الأمور</option><option value="student">طالب محدد</option><option value="stage">مرحلة محددة</option><option value="class">مرحلة وشعبة</option></select></div><div class="field"><label>الطالب المحدد</label><select name="student_id"><option value="0">بدون</option><?php if($students){ while($s=mysqli_fetch_assoc($students)){ ?><option value="<?php echo intval($s['id']); ?>"><?php echo h($s['name'].' - '.$s['stage'].' - '.$s['class']); ?></option><?php }} ?></select></div><div class="field"><label>المرحلة</label><select name="stage"><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 full"><label>نص الإشعار</label><textarea name="body" required></textarea></div></div><br><button class="btn main" type="submit">إرسال الإشعار</button></form></div><div class="card"><h2>الإشعارات الحالية</h2><?php if($notifications && mysqli_num_rows($notifications)>0){ while($n=mysqli_fetch_assoc($notifications)){ ?><div class="notice-card"><div class="notice-head"><div><h3 style="margin:0;color:#0b2d4d"><?php echo h($n['title']); ?></h3><span class="badge"><?php echo h($n['target_type']); ?></span></div><span class="badge"><?php echo intval($n['is_active'])==1?'مفعل':'متوقف'; ?></span></div><p><?php echo nl2br(h($n['body'])); ?></p><small><?php echo h($n['created_at']); ?></small><div class="actions"><form method="POST"><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?php echo intval($n['id']); ?>"><input type="hidden" name="new_status" value="<?php echo intval($n['is_active'])==1?0:1; ?>"><button class="btn <?php echo intval($n['is_active'])==1?'red':'green'; ?>" type="submit"><?php echo intval($n['is_active'])==1?'إيقاف':'تفعيل'; ?></button></form><form method="POST" onsubmit="return confirm('هل تريد حذف الإشعار؟')"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?php echo intval($n['id']); ?>"><button class="btn red" type="submit">حذف</button></form></div></div><?php }} else { ?><div class="notice-card">لا توجد إشعارات حالياً.</div><?php } ?></div></div></body></html>
