<?php include 'includes/header.php'; ?>
<?php
$title = '';
$content = '';
$image = '';
$video_type = 'none';
$video_url = '';
$video_file = '';
$id = null;
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM news WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$article = $result->fetch_assoc();
if ($article) {
$title = $article['title'];
$content = $article['content'];
$image = $article['image'];
$video_type = $article['video_type'] ?? 'none';
$video_url = $article['video_url'] ?? '';
$video_file = $article['video_file'] ?? '';
}
}
?>
<div class="row">
<div class="col-12">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fs-3"><?php echo $id ? 'تعديل الخبر' : 'إضافة خبر جديد'; ?></h2>
<a href="news.php" class="btn btn-secondary">
<i class="fas fa-arrow-right me-2"></i>العودة للقائمة
</a>
</div>
<div class="glass-card p-4">
<form action="news_process.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="mb-3">
<label for="title" class="form-label">عنوان الخبر</label>
<input type="text" name="title" id="title" class="form-control" value="<?php echo htmlspecialchars($title); ?>" required>
</div>
<div class="mb-3">
<label for="content" class="form-label">محتوى الخبر</label>
<textarea name="content" id="content" class="form-control" rows="10" required><?php echo htmlspecialchars($content); ?></textarea>
</div>
<div class="mb-3">
<label for="image" class="form-label">صورة الخبر</label>
<input type="file" name="image" id="image" class="form-control" accept="image/*">
<?php if ($image): ?>
<div class="mt-3">
<p class="text-muted mb-2">الصورة الحالية:</p>
<img src="../uploads/news/<?php echo htmlspecialchars($image); ?>" alt="<?php echo htmlspecialchars($title); ?>" class="img-thumbnail" style="max-width: 200px;">
</div>
<?php endif; ?>
</div>
<div class="mb-3">
<label class="form-label">نوع الفيديو المضمن</label>
<div class="d-flex gap-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="video_type" id="video_type_none" value="none" <?php echo ($video_type == 'none') ? 'checked' : ''; ?>>
<label class="form-check-label" for="video_type_none">بدون فيديو</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="video_type" id="video_type_url" value="url" <?php echo ($video_type == 'url') ? 'checked' : ''; ?>>
<label class="form-check-label" for="video_type_url">رابط خارجي (YouTube)</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="video_type" id="video_type_file" value="file" <?php echo ($video_type == 'file') ? 'checked' : ''; ?>>
<label class="form-check-label" for="video_type_file">ملف فيديو محلي</label>
</div>
</div>
</div>
<div class="mb-3" id="url_section" style="<?php echo ($video_type != 'url') ? 'display:none;' : ''; ?>">
<label for="video_url" class="form-label">رابط الفيديو (YouTube)</label>
<input type="url" name="video_url" id="video_url" class="form-control" value="<?php echo htmlspecialchars($video_url); ?>" placeholder="https://www.youtube.com/watch?v=...">
</div>
<div class="mb-3" id="file_section" style="<?php echo ($video_type != 'file') ? 'display:none;' : ''; ?>">
<label for="video_file" class="form-label">ملف الفيديو</label>
<input type="file" name="video_file" id="video_file" class="form-control" accept="video/*">
<?php if ($video_file): ?>
<div class="mt-2">
<small class="text-muted">الملف الحالي: <?php echo htmlspecialchars($video_file); ?></small>
<input type="hidden" name="current_video_file" value="<?php echo htmlspecialchars($video_file); ?>">
</div>
<?php endif; ?>
</div>
<div class="d-flex gap-2">
<button type="submit" class="btn btn-primary">
<i class="fas fa-save me-2"></i><?php echo $id ? 'حفظ التعديلات' : 'إضافة الخبر'; ?>
</button>
<a href="news.php" class="btn btn-secondary">
<i class="fas fa-times me-2"></i>إلغاء
</a>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const typeNone = document.getElementById('video_type_none');
const typeUrl = document.getElementById('video_type_url');
const typeFile = document.getElementById('video_type_file');
const urlSection = document.getElementById('url_section');
const fileSection = document.getElementById('file_section');
function toggleSections() {
if (typeUrl.checked) {
urlSection.style.display = 'block';
fileSection.style.display = 'none';
} else if (typeFile.checked) {
urlSection.style.display = 'none';
fileSection.style.display = 'block';
} else {
urlSection.style.display = 'none';
fileSection.style.display = 'none';
}
}
typeNone.addEventListener('change', toggleSections);
typeUrl.addEventListener('change', toggleSections);
typeFile.addEventListener('change', toggleSections);
});
</script>
<?php include 'includes/footer.php'; ?>