<?php
include 'auth.php';
require_permission('news_view');
include 'includes/header.php';
?>
<?php
// Fetch all news articles
$result = $conn->query("SELECT * FROM news ORDER BY created_at DESC");
$news_articles = $result->fetch_all(MYSQLI_ASSOC);
?>
<div class="row">
<div class="col-12">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fs-3">إدارة الأخبار</h2>
<a href="news_form.php" class="btn btn-primary">
<i class="fas fa-plus me-2"></i>إضافة خبر جديد
</a>
</div>
<div class="glass-card p-4">
<?php if (count($news_articles) > 0): ?>
<div class="table-responsive">
<table class="table table-dark table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">العنوان</th>
<th scope="col">تاريخ الإنشاء</th>
<th scope="col">الإجراءات</th>
</tr>
</thead>
<tbody>
<?php foreach ($news_articles as $article): ?>
<tr>
<td><?php echo $article['id']; ?></td>
<td>
<?php echo htmlspecialchars($article['title']); ?>
<?php if (!empty($article['video_type']) && $article['video_type'] !== 'none'): ?>
<span class="badge bg-primary ms-2" title="يحتوي على فيديو">
<i class="fas fa-video"></i>
</span>
<?php endif; ?>
</td>
<td><?php echo date('d/m/Y H:i', strtotime($article['created_at'])); ?></td>
<td>
<a href="news_form.php?id=<?php echo $article['id']; ?>" class="btn btn-sm btn-warning me-2">
<i class="fas fa-edit"></i> تعديل
</a>
<a href="news_delete.php?id=<?php echo $article['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('هل أنت متأكد من حذف هذا الخبر؟')">
<i class="fas fa-trash"></i> حذف
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="text-center py-5">
<i class="fas fa-newspaper fa-3x text-muted mb-3"></i>
<h4 class="text-muted">لا توجد أخبار حالياً</h4>
<p class="text-muted">ابدأ بإضافة أول خبر</p>
<a href="news_form.php" class="btn btn-primary">
<i class="fas fa-plus me-2"></i>إضافة خبر جديد
</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>