39496-vm/admin_students.php
2026-04-06 12:35:47 +00:00

327 lines
19 KiB
PHP

<?php
// admin_students.php
require_once __DIR__ . '/includes/app.php';
$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$post_action = $_POST['action'] ?? $action;
$post_id = (int)($_POST['id'] ?? $id);
if ($post_action === 'delete' && $post_id > 0) {
$stmt = db()->prepare("DELETE FROM student_subscriptions WHERE id = ?");
$stmt->execute([$post_id]);
header('Location: ' . app_url('admin.php', ['page' => 'students']));
exit;
}
if ($post_action === 'edit' || $post_action === 'add') {
$full_name = $_POST['full_name'] ?? '';
$email = $_POST['email'] ?? '';
$whatsapp = $_POST['whatsapp'] ?? '';
$plan_key = $_POST['plan_key'] ?? '';
$payment_status = $_POST['payment_status'] ?? 'active';
$status = $_POST['status'] ?? 'active';
$civil_id = $_POST['civil_id'] ?? '';
$picture = null;
if ($post_action === 'edit' && $post_id > 0) {
$stmt = db()->prepare("SELECT picture FROM student_subscriptions WHERE id = ?");
$stmt->execute([$post_id]);
$picture = $stmt->fetchColumn();
}
if (isset($_FILES['picture']) && $_FILES['picture']['error'] === UPLOAD_ERR_OK) {
$upload_dir = __DIR__ . '/assets/images/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
$filename = time() . '_' . basename($_FILES['picture']['name']);
$target_file = $upload_dir . $filename;
if (move_uploaded_file($_FILES['picture']['tmp_name'], $target_file)) {
$picture = 'assets/images/uploads/' . $filename;
}
}
if ($post_action === 'edit' && $post_id > 0) {
$stmt = db()->prepare("UPDATE student_subscriptions SET full_name=?, email=?, whatsapp=?, plan_key=?, payment_status=?, status=?, civil_id=?, picture=? WHERE id=?");
$stmt->execute([$full_name, $email, $whatsapp, $plan_key, $payment_status, $status, $civil_id, $picture, $post_id]);
} else {
$stmt = db()->prepare("INSERT INTO student_subscriptions (full_name, email, whatsapp, plan_key, payment_status, status, civil_id, picture) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$full_name, $email, $whatsapp, $plan_key, $payment_status, $status, $civil_id, $picture]);
}
header('Location: ' . app_url('admin.php', ['page' => 'students']));
exit;
}
}
// list view
$search = $_GET['search'] ?? '';
$page_num = max(1, (int)($_GET['p'] ?? 1));
$limit = 10;
$offset = ($page_num - 1) * $limit;
$where = "";
$params = [];
if ($search) {
$where = "WHERE full_name LIKE ? OR email LIKE ? OR whatsapp LIKE ?";
$params = ["%$search%", "%$search%", "%$search%"];
}
$count_stmt = db()->prepare("SELECT COUNT(*) FROM student_subscriptions $where");
$count_stmt->execute($params);
$total_items = $count_stmt->fetchColumn();
$total_pages = ceil($total_items / $limit);
$stmt = db()->prepare("SELECT * FROM student_subscriptions $where ORDER BY id DESC LIMIT $limit OFFSET $offset");
$stmt->execute($params);
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="section-header mb-4 d-flex justify-content-between align-items-center">
<div>
<span class="eyebrow"><?= h(t('Admin', 'الإدارة')) ?></span>
<h1 class="section-title mb-0"><?= h(t('Students', 'الطلاب')) ?></h1>
</div>
<button type="button" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);" data-bs-toggle="modal" data-bs-target="#addStudentModal">
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16" class="me-1"><path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/></svg>
<?= h(t('Add Student', 'إضافة طالب')) ?>
</button>
</div>
<div class="panel-card mb-4">
<form method="get" class="d-flex gap-2 align-items-center">
<input type="hidden" name="page" value="students">
<input type="text" name="search" class="form-control w-auto" placeholder="<?= h(t('Search...', 'بحث...')) ?>" value="<?= h($search) ?>">
<button type="submit" class="btn btn-outline-secondary"><?= h(t('Filter', 'تصفية')) ?></button>
<?php if ($search): ?>
<a href="<?= h(app_url('admin.php', ['page'=>'students'])) ?>" class="btn btn-link text-secondary text-decoration-none"><?= h(t('Clear', 'مسح')) ?></a>
<?php endif; ?>
</form>
</div>
<div class="panel-card">
<div class="table-responsive">
<table class="table align-middle dashboard-table mb-0">
<thead>
<tr>
<th><?= h(t('Name', 'الاسم')) ?></th>
<th><?= h(t('Status', 'الحالة')) ?></th>
<th><?= h(t('Email', 'البريد الإلكتروني')) ?></th>
<th><?= h(t('WhatsApp', 'واتساب')) ?></th>
<th><?= h(t('Plan', 'الخطة')) ?></th>
<th><?= h(t('Payment', 'الدفع')) ?></th>
<th><?= h(t('Actions', 'إجراءات')) ?></th>
</tr>
</thead>
<tbody>
<?php foreach($items as $row): ?>
<tr>
<td>
<div class="fw-semibold"><?= h($row['full_name']) ?></div>
</td>
<td>
<?php if (($row['status'] ?? 'active') === 'active'): ?>
<span class="badge bg-success"><?= h(t('Active', 'نشط')) ?></span>
<?php else: ?>
<span class="badge bg-secondary"><?= h(t('Inactive', 'غير نشط')) ?></span>
<?php endif; ?>
</td>
<td><?= h($row['email']) ?></td>
<td><?= h($row['whatsapp']) ?></td>
<td><?= h($row['plan_key']) ?></td>
<td>
<?php if (($row['payment_status'] ?? '') === 'active'): ?>
<span class="badge bg-success"><?= h(t('Active', 'نشط')) ?></span>
<?php else: ?>
<span class="badge bg-warning text-dark"><?= h($row['payment_status']) ?></span>
<?php endif; ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editStudentModal<?= $row['id'] ?>" title="<?= h(t('Edit', 'تعديل')) ?>">
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/></svg>
</button>
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'students', 'action'=>'delete', 'id'=>$row['id']])) ?>" class="d-inline" onsubmit="return confirm('<?= h(t('Are you sure?', 'هل أنت متأكد؟')) ?>');">
<button type="submit" class="btn btn-sm btn-outline-danger" title="<?= h(t('Delete', 'حذف')) ?>">
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/></svg>
</button>
</form>
</td>
</tr>
<!-- Edit Modal -->
<div class="modal fade" id="editStudentModal<?= $row['id'] ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content border-0 shadow">
<div class="modal-header border-bottom-0 pb-3" style="background-color: var(--bs-light);">
<div class="d-flex align-items-center gap-3">
<?php if (!empty($row['picture'])): ?>
<img src="<?= h($row['picture']) ?>" alt="Picture" class="rounded-circle border" style="width: 48px; height: 48px; object-fit: cover;">
<?php else: ?>
<div class="rounded-circle bg-secondary text-white d-flex align-items-center justify-content-center" style="width: 48px; height: 48px;">
<svg width="24" height="24" fill="currentColor" viewBox="0 0 16 16"><path d="M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z"/><path fill-rule="evenodd" d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z"/></svg>
</div>
<?php endif; ?>
<div>
<h5 class="modal-title fw-bold mb-0"><?= h(t('Edit Student', 'تعديل الطالب')) ?></h5>
<div class="text-muted small fw-semibold mt-1"><?= h($row['full_name']) ?></div>
</div>
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'students'])) ?>" enctype="multipart/form-data">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="<?= $row['id'] ?>">
<!-- Name Bar with Background -->
<div class="mb-3 p-3 bg-light rounded border">
<label class="form-label fw-bold"><?= h(t('Name', 'الاسم')) ?></label>
<input type="text" name="full_name" class="form-control" value="<?= h($row['full_name']) ?>" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Civil ID', 'الرقم المدني')) ?></label>
<input type="text" name="civil_id" class="form-control" value="<?= h($row['civil_id'] ?? '') ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'الصورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
<input type="email" name="email" class="form-control" value="<?= h($row['email']) ?>" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('WhatsApp', 'واتساب')) ?></label>
<input type="text" name="whatsapp" class="form-control" value="<?= h($row['whatsapp']) ?>">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Plan Key', 'الخطة')) ?></label>
<input type="text" name="plan_key" class="form-control" value="<?= h($row['plan_key']) ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Payment Status', 'حالة الدفع')) ?></label>
<input type="text" name="payment_status" class="form-control" value="<?= h($row['payment_status']) ?>">
</div>
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active" <?= ($row['status'] ?? 'active') === 'active' ? 'selected' : '' ?>><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive" <?= ($row['status'] ?? 'active') === 'inactive' ? 'selected' : '' ?>><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
<div class="d-flex justify-content-end gap-2 mt-4">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save Changes', 'حفظ التغييرات')) ?></button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php if(empty($items)): ?>
<tr>
<td colspan="7" class="text-center py-4 text-secondary">
<?= h(t('No students found.', 'لم يتم العثور على طلاب.')) ?>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php if($total_pages > 1): ?>
<nav class="mt-4">
<ul class="pagination justify-content-center">
<?php for($i=1; $i<=$total_pages; $i++): ?>
<li class="page-item <?= $i === $page_num ? 'active' : '' ?>">
<a class="page-link" href="<?= h(app_url('admin.php', ['page'=>'students', 'p'=>$i, 'search'=>$search])) ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php endif; ?>
<!-- Add Modal -->
<div class="modal fade" id="addStudentModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content border-0 shadow">
<div class="modal-header border-bottom-0 pb-3" style="background-color: var(--bs-light);">
<h5 class="modal-title fw-bold"><?= h(t('Add Student', 'إضافة طالب')) ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'students'])) ?>" enctype="multipart/form-data">
<input type="hidden" name="action" value="add">
<!-- Name Bar with Background -->
<div class="mb-3 p-3 bg-light rounded border">
<label class="form-label fw-bold"><?= h(t('Name', 'الاسم')) ?></label>
<input type="text" name="full_name" class="form-control" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Civil ID', 'الرقم المدني')) ?></label>
<input type="text" name="civil_id" class="form-control">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'الصورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('WhatsApp', 'واتساب')) ?></label>
<input type="text" name="whatsapp" class="form-control">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Plan Key', 'الخطة')) ?></label>
<input type="text" name="plan_key" class="form-control">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Payment Status', 'حالة الدفع')) ?></label>
<input type="text" name="payment_status" class="form-control" value="active">
</div>
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active"><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive"><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
<div class="d-flex justify-content-end gap-2 mt-4">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save', 'حفظ')) ?></button>
</div>
</form>
</div>
</div>
</div>
</div>