Autosave: 20260315-102441

This commit is contained in:
Flatlogic Bot 2026-03-15 10:24:41 +00:00
parent 5f78c2abad
commit 4f1232c067
10 changed files with 3074 additions and 105 deletions

View File

@ -1,16 +1,146 @@
<?php
$query = "SELECT * FROM cities ORDER BY id DESC";
$stmt = $db->query($query);
$search_name = $_GET['name'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$where .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
// Count Total
$countQuery = "SELECT COUNT(*) FROM cities $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalCities = $stmt->fetchColumn();
$totalPages = ceil($totalCities / $limit);
// Fetch Data
$query = "SELECT * FROM cities $where ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$cities = $stmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($cities)):
?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-building display-4 d-block mb-3"></i>
<?php echo __('no_cities_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($cities as $city): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $city['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($city['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($city['name_ar']); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditCityModal(<?php echo htmlspecialchars(json_encode($city, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteCityModal(<?php echo $city['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalCities); ?> <?php echo __('of'); ?> <?php echo $totalCities; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('cities'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addCityModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addCityModal" onclick="resetCityModal()">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_city'); ?>
</button>
</div>
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form id="citiesSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-10">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" id="citySearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('search_by_name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-secondary w-100" onclick="fetchCities(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
@ -23,12 +153,12 @@ $cities = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="citiesTableBody">
<?php if (empty($cities)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-building display-4 d-block mb-3"></i>
No cities found.
<?php echo __('no_cities_found'); ?>
</td>
</tr>
<?php else: ?>
@ -57,5 +187,200 @@ $cities = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="citiesPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalCities); ?> <?php echo __('of'); ?> <?php echo $totalCities; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Add City Modal -->
<div class="modal fade" id="addCityModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="cityModalTitle"><?php echo __('add_city'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="cityAction" value="add_city">
<input type="hidden" name="id" id="cityId">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="cityNameEn" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="cityNameAr" required>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete City Modal -->
<div class="modal fade" id="deleteCityModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_city'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_city">
<input type="hidden" name="id" id="deleteCityId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('citySearchName');
const paginationContainer = document.getElementById('citiesPagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchCities(1), 300);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchCities(page);
}
});
}
});
function fetchCities(page) {
const name = document.getElementById('citySearchName').value;
const tableBody = document.getElementById('citiesTableBody');
const paginationContainer = document.getElementById('citiesPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
params.append('page', page);
params.append('ajax_search', '1');
fetch('cities.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching cities:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetCityModal() {
document.getElementById('cityModalTitle').textContent = '<?php echo __('add_city'); ?>';
document.getElementById('cityAction').value = 'add_city';
document.getElementById('cityId').value = '';
document.getElementById('cityNameEn').value = '';
document.getElementById('cityNameAr').value = '';
}
function showEditCityModal(city) {
document.getElementById('cityModalTitle').textContent = '<?php echo __('edit_city'); ?>';
document.getElementById('cityAction').value = 'edit_city';
document.getElementById('cityId').value = city.id;
document.getElementById('cityNameEn').value = city.name_en;
document.getElementById('cityNameAr').value = city.name_ar;
var modal = new bootstrap.Modal(document.getElementById('addCityModal'));
modal.show();
}
function showDeleteCityModal(id) {
document.getElementById('deleteCityId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteCityModal'));
modal.show();
}
</script>

View File

@ -1,16 +1,146 @@
<?php
$query = "SELECT * FROM departments ORDER BY id DESC";
$stmt = $db->query($query);
$search_name = $_GET['name'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$where .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
// Count Total
$countQuery = "SELECT COUNT(*) FROM departments $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalDepartments = $stmt->fetchColumn();
$totalPages = ceil($totalDepartments / $limit);
// Fetch Data
$query = "SELECT * FROM departments $where ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$departments = $stmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($departments)):
?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-diagram-3 display-4 d-block mb-3"></i>
<?php echo __('no_departments_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($departments as $dept): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $dept['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($dept['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($dept['name_ar']); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditDepartmentModal(<?php echo htmlspecialchars(json_encode($dept, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteDepartmentModal(<?php echo $dept['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalDepartments); ?> <?php echo __('of'); ?> <?php echo $totalDepartments; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('departments'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addDepartmentModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addDepartmentModal" onclick="resetDepartmentModal()">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_department'); ?>
</button>
</div>
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form id="departmentsSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-10">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" id="deptSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-secondary w-100" onclick="fetchDepartments(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
@ -23,12 +153,12 @@ $departments = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="departmentsTableBody">
<?php if (empty($departments)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-diagram-3 display-4 d-block mb-3"></i>
No departments found.
<?php echo __('no_departments_found'); ?>
</td>
</tr>
<?php else: ?>
@ -57,5 +187,201 @@ $departments = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="departmentsPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalDepartments); ?> <?php echo __('of'); ?> <?php echo $totalDepartments; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Add/Edit Department Modal -->
<div class="modal fade" id="addDepartmentModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="deptModalTitle"><?php echo __('add_department'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="deptAction" value="add_department">
<input type="hidden" name="id" id="deptId">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="deptNameEn" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="deptNameAr" required>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Department Modal -->
<div class="modal fade" id="deleteDepartmentModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_department'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_department">
<input type="hidden" name="id" id="deleteDeptId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('deptSearchName');
const paginationContainer = document.getElementById('departmentsPagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchDepartments(1), 300);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchDepartments(page);
}
});
}
});
function fetchDepartments(page) {
const name = document.getElementById('deptSearchName').value;
const tableBody = document.getElementById('departmentsTableBody');
const paginationContainer = document.getElementById('departmentsPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
params.append('page', page);
params.append('ajax_search', '1');
fetch('departments.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching departments:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetDepartmentModal() {
document.getElementById('deptModalTitle').textContent = '<?php echo __('add_department'); ?>';
document.getElementById('deptAction').value = 'add_department';
document.getElementById('deptId').value = '';
document.getElementById('deptNameEn').value = '';
document.getElementById('deptNameAr').value = '';
}
function showEditDepartmentModal(dept) {
document.getElementById('deptModalTitle').textContent = '<?php echo __('edit_department'); ?>';
document.getElementById('deptAction').value = 'edit_department';
document.getElementById('deptId').value = dept.id;
document.getElementById('deptNameEn').value = dept.name_en;
document.getElementById('deptNameAr').value = dept.name_ar;
var modal = new bootstrap.Modal(document.getElementById('addDepartmentModal'));
modal.show();
}
function showDeleteDepartmentModal(id) {
document.getElementById('deleteDeptId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteDepartmentModal'));
modal.show();
}
</script>

View File

@ -1,13 +1,122 @@
<?php
$query = "SELECT * FROM drugs_groups ORDER BY id DESC";
$stmt = $db->query($query);
// Pagination Logic
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where_clause = "";
$params = [];
if ($search) {
$where_clause = "WHERE name_en LIKE ? OR name_ar LIKE ?";
$params[] = "%$search%";
$params[] = "%$search%";
}
// Total count
$count_query = "SELECT COUNT(*) FROM drugs_groups $where_clause";
$stmt = $db->prepare($count_query);
$stmt->execute($params);
$total_records = $stmt->fetchColumn();
$total_pages = ceil($total_records / $limit);
// Fetch data
$query = "SELECT * FROM drugs_groups $where_clause ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$groups = $stmt->fetchAll();
// Handle AJAX Request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
ob_start();
?>
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
<th class="px-4 py-3">#</th>
<th class="py-3"><?php echo __('name_en'); ?></th>
<th class="py-3"><?php echo __('name_ar'); ?></th>
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($groups)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-collection display-4 d-block mb-3"></i>
<?php echo __('no_data_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($groups as $group): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $group['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($group['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($group['name_ar']); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditDrugGroupModal(<?php echo htmlspecialchars(json_encode($group, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteDrugGroupModal(<?php echo $group['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
<?php
$html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('drugs_groups'); ?></h3>
<div>
<button class="btn btn-outline-primary shadow-sm me-2" data-bs-toggle="modal" data-bs-target="#importDrugsGroupsModal">
<div class="d-flex gap-2">
<div class="input-group shadow-sm" style="width: 250px;">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
<input type="text" id="searchInput" class="form-control border-start-0 ps-0" placeholder="<?php echo __('search'); ?>..." onkeyup="debounceSearch()">
</div>
<button class="btn btn-outline-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#importDrugsGroupsModal">
<i class="bi bi-upload me-1"></i> <?php echo __('import_csv'); ?>
</button>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addDrugGroupModal" onclick="resetGroupModal()">
@ -18,7 +127,8 @@ $groups = $stmt->fetchAll();
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
<div class="table-responsive" id="tableContainer">
<!-- Initial Table Load -->
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
@ -61,6 +171,34 @@ $groups = $stmt->fetchAll();
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls (Initial) -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
@ -151,6 +289,31 @@ $groups = $stmt->fetchAll();
</div>
<script>
let searchTimeout;
function debounceSearch() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
loadPage(1);
}, 300);
}
function loadPage(page) {
const search = document.getElementById('searchInput').value;
const url = `drugs_groups.php?page=${page}&search=${encodeURIComponent(search)}`;
fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
document.getElementById('tableContainer').innerHTML = data.html;
})
.catch(error => console.error('Error:', error));
}
function resetGroupModal() {
document.getElementById('groupModalTitle').textContent = '<?php echo __('add_drug_group'); ?>';
document.getElementById('groupAction').value = 'add_drug_group';

View File

@ -1,34 +1,162 @@
<?php
// Pagination and Search
$search_name = $_GET['name'] ?? '';
$search_dept = $_GET['department_id'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$where .= " AND (e.name_en LIKE ? OR e.name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
if ($search_dept) {
$where .= " AND e.department_id = ?";
$params[] = $search_dept;
}
// Count Total
$countQuery = "SELECT COUNT(*) FROM employees e $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalEmployees = $stmt->fetchColumn();
$totalPages = ceil($totalEmployees / $limit);
// Fetch Data
$query = "
SELECT e.*, d.name_$lang as department_name, p.name_$lang as position_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id
LEFT JOIN positions p ON e.position_id = p.id
WHERE 1=1";
$params = [];
$where
ORDER BY e.id DESC
LIMIT $limit OFFSET $offset";
if ($search_name) {
$query .= " AND (e.name_en LIKE ? OR e.name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
if ($search_dept) {
$query .= " AND e.department_id = ?";
$params[] = $search_dept;
}
$query .= " ORDER BY e.id DESC";
$stmt = $db->prepare($query);
$stmt->execute($params);
$employees = $stmt->fetchAll();
// Fetch Departments for Dropdown (Needed for Search Bar and Modal)
$deptQuery = "SELECT * FROM departments ORDER BY name_$lang";
$deptStmt = $db->query($deptQuery);
$all_departments = $deptStmt->fetchAll();
// Fetch Positions for Dropdown (Needed for Modal)
$posQuery = "SELECT * FROM positions ORDER BY name_$lang";
$posStmt = $db->query($posQuery);
$all_positions = $posStmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($employees)):
?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-person-workspace display-4 d-block mb-3"></i>
<?php echo __('no_employees_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($employees as $emp): ?>
<tr>
<td class="px-4">
<div class="fw-semibold text-dark"><?php echo htmlspecialchars($emp['name_'.$lang]); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($emp['name_'.($lang == 'en' ? 'ar' : 'en')]); ?></small>
</td>
<td>
<span class="badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 px-2 py-1">
<?php echo htmlspecialchars($emp['department_name'] ?: '-'); ?>
</span>
<div class="small text-muted mt-1">
<i class="bi bi-diagram-2 me-1"></i> <?php echo htmlspecialchars($emp['position_name'] ?: '-'); ?>
</div>
</td>
<td>
<div class="small text-secondary"><i class="bi bi-telephone me-1"></i> <?php echo htmlspecialchars($emp['mobile'] ?: '-'); ?></div>
<div class="small text-secondary"><i class="bi bi-envelope me-1"></i> <?php echo htmlspecialchars($emp['email'] ?: '-'); ?></div>
</td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditEmployeeModal(<?php echo htmlspecialchars(json_encode($emp, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteEmployeeModal(<?php echo $emp['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalEmployees); ?> <?php echo __('of'); ?> <?php echo $totalEmployees; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('employees'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addEmployeeModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addEmployeeModal" onclick="resetEmployeeModal()">
<i class="bi bi-person-plus-fill me-1"></i> <?php echo __('add_employee'); ?>
</button>
</div>
@ -36,25 +164,25 @@ $employees = $stmt->fetchAll();
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="GET" action="" class="row g-3">
<form id="employeesSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-6">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
<input type="text" name="name" id="empSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-4">
<select name="department_id" class="form-select bg-light">
<select name="department_id" id="empSearchDept" class="form-select bg-light">
<option value=""><?php echo __('department'); ?> (<?php echo __('all'); ?>)</option>
<?php foreach ($all_departments as $dept): ?>
<option value="<?php echo $dept['id']; ?>" <?php echo $search_dept == $dept['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($dept['name']); ?>
<?php echo htmlspecialchars($dept['name_'.$lang]); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
<button type="button" class="btn btn-secondary w-100" onclick="fetchEmployees(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
@ -72,12 +200,12 @@ $employees = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="employeesTableBody">
<?php if (empty($employees)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-person-workspace display-4 d-block mb-3"></i>
No employees found.
<?php echo __('no_employees_found'); ?>
</td>
</tr>
<?php else: ?>
@ -119,5 +247,273 @@ $employees = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="employeesPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalEmployees); ?> <?php echo __('of'); ?> <?php echo $totalEmployees; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Employee Modal -->
<div class="modal fade" id="addEmployeeModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="empModalTitle"><?php echo __('add_employee'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="empAction" value="add_employee">
<input type="hidden" name="id" id="empId">
<div class="modal-body p-4">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="empNameEn" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="empNameAr" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('email'); ?></label>
<input type="email" class="form-control" name="email" id="empEmail">
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('mobile'); ?></label>
<input type="text" class="form-control" name="mobile" id="empMobile">
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('dob'); ?></label>
<div class="input-group">
<span class="input-group-text bg-light"><i class="bi bi-calendar3"></i></span>
<input type="text" class="form-control" name="dob" id="empDob" placeholder="YYYY-MM-DD" data-inputmask="'alias': 'datetime', 'inputFormat': 'yyyy-mm-dd'" inputmode="numeric">
</div>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('department'); ?> <span class="text-danger">*</span></label>
<select class="form-select" name="department_id" id="empDeptId" required>
<option value=""><?php echo __('select_department'); ?></option>
<?php foreach ($all_departments as $dept): ?>
<option value="<?php echo $dept['id']; ?>">
<?php echo htmlspecialchars($dept['name_' . $lang]); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('position'); ?> <span class="text-danger">*</span></label>
<select class="form-select" name="position_id" id="empPosId" required>
<option value=""><?php echo __('select_position'); ?></option>
<?php foreach ($all_positions as $pos): ?>
<option value="<?php echo $pos['id']; ?>">
<?php echo htmlspecialchars($pos['name_' . $lang]); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12">
<label class="form-label"><?php echo __('address'); ?></label>
<textarea class="form-control" name="address" id="empAddress" rows="2"></textarea>
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Employee Modal -->
<div class="modal fade" id="deleteEmployeeModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_employee'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_employee">
<input type="hidden" name="id" id="deleteEmpId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('empSearchName');
const searchDept = document.getElementById('empSearchDept');
const paginationContainer = document.getElementById('employeesPagination');
// Initialize Inputmask for DOB
if (document.getElementById('empDob')) {
Inputmask().mask(document.getElementById('empDob'));
}
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchEmployees(1), 300);
});
}
if (searchDept) {
searchDept.addEventListener('change', function() {
fetchEmployees(1);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchEmployees(page);
}
});
}
});
function fetchEmployees(page) {
const name = document.getElementById('empSearchName').value;
const dept = document.getElementById('empSearchDept').value;
const tableBody = document.getElementById('employeesTableBody');
const paginationContainer = document.getElementById('employeesPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
if (dept) params.append('department_id', dept);
params.append('page', page);
params.append('ajax_search', '1');
fetch('employees.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching employees:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetEmployeeModal() {
document.getElementById('empModalTitle').textContent = '<?php echo __('add_employee'); ?>';
document.getElementById('empAction').value = 'add_employee';
document.getElementById('empId').value = '';
document.getElementById('empNameEn').value = '';
document.getElementById('empNameAr').value = '';
document.getElementById('empEmail').value = '';
document.getElementById('empMobile').value = '';
document.getElementById('empDob').value = '';
document.getElementById('empDeptId').value = '';
document.getElementById('empPosId').value = '';
document.getElementById('empAddress').value = '';
}
function showEditEmployeeModal(emp) {
document.getElementById('empModalTitle').textContent = '<?php echo __('edit_employee'); ?>';
document.getElementById('empAction').value = 'edit_employee';
document.getElementById('empId').value = emp.id;
document.getElementById('empNameEn').value = emp.name_en;
document.getElementById('empNameAr').value = emp.name_ar;
document.getElementById('empEmail').value = emp.email;
document.getElementById('empMobile').value = emp.mobile;
document.getElementById('empDob').value = emp.dob;
document.getElementById('empDeptId').value = emp.department_id;
document.getElementById('empPosId').value = emp.position_id;
document.getElementById('empAddress').value = emp.address;
var modal = new bootstrap.Modal(document.getElementById('addEmployeeModal'));
modal.show();
}
function showDeleteEmployeeModal(id) {
document.getElementById('deleteEmpId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteEmployeeModal'));
modal.show();
}
</script>

View File

@ -1,24 +1,114 @@
<?php
$search_name = $_GET['name'] ?? '';
$search_phone = $_GET['phone'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM insurance_companies WHERE 1=1";
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$query .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$where .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
if ($search_phone) {
$query .= " AND phone LIKE ?";
$where .= " AND phone LIKE ?";
$params[] = "%$search_phone%";
}
$query .= " ORDER BY id DESC";
// Count Total
$countQuery = "SELECT COUNT(*) FROM insurance_companies $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalInsurance = $stmt->fetchColumn();
$totalPages = ceil($totalInsurance / $limit);
// Fetch Data
$query = "SELECT * FROM insurance_companies $where ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$insurance_companies = $stmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($insurance_companies)):
?>
<tr>
<td colspan="6" class="text-center py-5 text-muted">
<i class="bi bi-shield-check display-4 d-block mb-3"></i>
<?php echo __('no_insurance_companies_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($insurance_companies as $ic): ?>
<tr>
<td class="px-4 text-secondary">#<?php echo $ic['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($ic['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($ic['name_ar']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($ic['email'] ?: '-'); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($ic['phone'] ?: '-'); ?></td>
<td class="text-end px-4 text-muted"><?php echo date('Y-m-d', strtotime($ic['created_at'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalInsurance); ?> <?php echo __('of'); ?> <?php echo $totalInsurance; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
@ -31,21 +121,21 @@ $insurance_companies = $stmt->fetchAll();
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="GET" action="" class="row g-3">
<form id="insuranceSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-5">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
<input type="text" name="name" id="insSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-5">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-telephone"></i></span>
<input type="text" name="phone" class="form-control bg-light border-start-0" placeholder="<?php echo __('phone'); ?>" value="<?php echo htmlspecialchars($search_phone); ?>">
<input type="text" name="phone" id="insSearchPhone" class="form-control bg-light border-start-0" placeholder="<?php echo __('phone'); ?>" value="<?php echo htmlspecialchars($search_phone); ?>">
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
<button type="button" class="btn btn-secondary w-100" onclick="fetchInsurance(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
@ -65,12 +155,12 @@ $insurance_companies = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('date'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="insuranceTableBody">
<?php if (empty($insurance_companies)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-muted">
<i class="bi bi-shield-check display-4 d-block mb-3"></i>
No insurance companies found.
<?php echo __('no_insurance_companies_found'); ?>
</td>
</tr>
<?php else: ?>
@ -88,5 +178,165 @@ $insurance_companies = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="insurancePagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalInsurance); ?> <?php echo __('of'); ?> <?php echo $totalInsurance; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add Insurance Modal -->
<div class="modal fade" id="addInsuranceModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title"><?php echo __('add_insurance'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="add_insurance">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('email'); ?></label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('phone'); ?></label>
<input type="text" class="form-control" name="phone">
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('discount_percentage'); ?></label>
<div class="input-group">
<input type="number" class="form-control" name="discount_percentage" step="0.01" min="0" max="100">
<span class="input-group-text">%</span>
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('insSearchName');
const searchPhone = document.getElementById('insSearchPhone');
const paginationContainer = document.getElementById('insurancePagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchInsurance(1), 300);
});
}
if (searchPhone) {
searchPhone.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchInsurance(1), 300);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchInsurance(page);
}
});
}
});
function fetchInsurance(page) {
const name = document.getElementById('insSearchName').value;
const phone = document.getElementById('insSearchPhone').value;
const tableBody = document.getElementById('insuranceTableBody');
const paginationContainer = document.getElementById('insurancePagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
if (phone) params.append('phone', phone);
params.append('page', page);
params.append('ajax_search', '1');
fetch('insurance.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
})
.catch(error => {
console.error('Error fetching insurance:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
</script>

View File

@ -1,33 +1,147 @@
<?php
$search_name = $_GET['name'] ?? '';
$search_dept = $_GET['department_id'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$query = "
SELECT n.*, dept.name_$lang as department_name
FROM nurses n
LEFT JOIN departments dept ON n.department_id = dept.id
WHERE 1=1";
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$query .= " AND (n.name_en LIKE ? OR n.name_ar LIKE ?)";
$where .= " AND (n.name_en LIKE ? OR n.name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
if ($search_dept) {
$query .= " AND n.department_id = ?";
$where .= " AND n.department_id = ?";
$params[] = $search_dept;
}
$query .= " ORDER BY n.id DESC";
// Count Total
$countQuery = "SELECT COUNT(*) FROM nurses n $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalNurses = $stmt->fetchColumn();
$totalPages = ceil($totalNurses / $limit);
// Fetch Data
$query = "
SELECT n.*, dept.name_$lang as department_name
FROM nurses n
LEFT JOIN departments dept ON n.department_id = dept.id
$where
ORDER BY n.id DESC
LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$nurses = $stmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($nurses)):
?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-person-heart display-4 d-block mb-3"></i>
<?php echo __('no_nurses_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($nurses as $nurse): ?>
<tr>
<td class="px-4">
<div class="fw-semibold text-dark"><?php echo htmlspecialchars($nurse['name_'.$lang]); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($nurse['name_'.($lang == 'en' ? 'ar' : 'en')]); ?></small>
</td>
<td>
<span class="badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 px-2 py-1">
<?php echo htmlspecialchars($nurse['department_name'] ?: '-'); ?>
</span>
</td>
<td>
<div class="small text-secondary"><i class="bi bi-telephone me-1"></i> <?php echo htmlspecialchars($nurse['tel'] ?: '-'); ?></div>
<div class="small text-secondary"><i class="bi bi-envelope me-1"></i> <?php echo htmlspecialchars($nurse['email'] ?: '-'); ?></div>
</td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditNurseModal(<?php echo htmlspecialchars(json_encode($nurse, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteNurseModal(<?php echo $nurse['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalNurses); ?> <?php echo __('of'); ?> <?php echo $totalNurses; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('nurses'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addNurseModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addNurseModal" onclick="resetNurseModal()">
<i class="bi bi-person-plus-fill me-1"></i> <?php echo __('add_nurse'); ?>
</button>
</div>
@ -35,15 +149,15 @@ $nurses = $stmt->fetchAll();
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="GET" action="" class="row g-3">
<form id="nursesSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-6">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
<input type="text" name="name" id="nurseSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-4">
<select name="department_id" class="form-select bg-light">
<select name="department_id" id="nurseSearchDept" class="form-select bg-light">
<option value=""><?php echo __('department'); ?> (<?php echo __('all'); ?>)</option>
<?php foreach ($all_departments as $dept): ?>
<option value="<?php echo $dept['id']; ?>" <?php echo $search_dept == $dept['id'] ? 'selected' : ''; ?>>
@ -53,7 +167,7 @@ $nurses = $stmt->fetchAll();
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
<button type="button" class="btn btn-secondary w-100" onclick="fetchNurses(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
@ -71,12 +185,12 @@ $nurses = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="nursesTableBody">
<?php if (empty($nurses)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-person-heart display-4 d-block mb-3"></i>
No nurses found.
<?php echo __('no_nurses_found'); ?>
</td>
</tr>
<?php else: ?>
@ -115,5 +229,240 @@ $nurses = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="nursesPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalNurses); ?> <?php echo __('of'); ?> <?php echo $totalNurses; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Nurse Modal -->
<div class="modal fade" id="addNurseModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="nurseModalTitle"><?php echo __('add_nurse'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="nurseAction" value="add_nurse">
<input type="hidden" name="id" id="nurseId">
<div class="modal-body p-4">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="nurseNameEn" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="nurseNameAr" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('email'); ?></label>
<input type="email" class="form-control" name="email" id="nurseEmail">
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('tel'); ?></label>
<input type="text" class="form-control" name="tel" id="nurseTel">
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('department'); ?></label>
<select class="form-select" name="department_id" id="nurseDeptId">
<option value=""><?php echo __('select_department'); ?></option>
<?php foreach ($all_departments as $dept): ?>
<option value="<?php echo $dept['id']; ?>">
<?php echo htmlspecialchars($dept['name_' . $lang]); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Nurse Modal -->
<div class="modal fade" id="deleteNurseModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_nurse'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_nurse">
<input type="hidden" name="id" id="deleteNurseId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('nurseSearchName');
const searchDept = document.getElementById('nurseSearchDept');
const tableBody = document.getElementById('nursesTableBody');
const paginationContainer = document.getElementById('nursesPagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchNurses(1), 300);
});
}
if (searchDept) {
searchDept.addEventListener('change', function() {
fetchNurses(1);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchNurses(page);
}
});
}
});
function fetchNurses(page) {
const name = document.getElementById('nurseSearchName').value;
const dept = document.getElementById('nurseSearchDept').value;
const tableBody = document.getElementById('nursesTableBody');
const paginationContainer = document.getElementById('nursesPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
if (dept) params.append('department_id', dept);
params.append('page', page);
params.append('ajax_search', '1');
fetch('nurses.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching nurses:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetNurseModal() {
document.getElementById('nurseModalTitle').textContent = '<?php echo __('add_nurse'); ?>';
document.getElementById('nurseAction').value = 'add_nurse';
document.getElementById('nurseId').value = '';
document.getElementById('nurseNameEn').value = '';
document.getElementById('nurseNameAr').value = '';
document.getElementById('nurseEmail').value = '';
document.getElementById('nurseTel').value = '';
document.getElementById('nurseDeptId').value = '';
}
function showEditNurseModal(nurse) {
document.getElementById('nurseModalTitle').textContent = '<?php echo __('edit_nurse'); ?>';
document.getElementById('nurseAction').value = 'edit_nurse';
document.getElementById('nurseId').value = nurse.id;
document.getElementById('nurseNameEn').value = nurse.name_en;
document.getElementById('nurseNameAr').value = nurse.name_ar;
document.getElementById('nurseEmail').value = nurse.email;
document.getElementById('nurseTel').value = nurse.tel;
document.getElementById('nurseDeptId').value = nurse.department_id || '';
var modal = new bootstrap.Modal(document.getElementById('addNurseModal'));
modal.show();
}
function showDeleteNurseModal(id) {
document.getElementById('deleteNurseId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteNurseModal'));
modal.show();
}
</script>

View File

@ -1,24 +1,126 @@
<?php
$search_name = $_GET['name'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM positions WHERE 1=1";
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$query .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$where .= " AND (name_en LIKE ? OR name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
$query .= " ORDER BY id DESC";
// Count Total
$countQuery = "SELECT COUNT(*) FROM positions $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalPositions = $stmt->fetchColumn();
$totalPages = ceil($totalPositions / $limit);
// Fetch Data
$query = "SELECT * FROM positions $where ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$positions = $stmt->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($positions)):
?>
<tr>
<td colspan="5" class="text-center py-5 text-muted">
<i class="bi bi-person-badge display-4 d-block mb-3"></i>
<?php echo __('no_positions_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($positions as $position): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $position['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($position['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($position['name_ar']); ?></td>
<td><small class="text-truncate d-inline-block text-muted" style="max-width: 300px;"><?php echo htmlspecialchars($position['description_en']); ?></small></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditPositionModal(<?php echo htmlspecialchars(json_encode($position, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeletePositionModal(<?php echo $position['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalPositions); ?> <?php echo __('of'); ?> <?php echo $totalPositions; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('positions'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addPositionModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addPositionModal" onclick="resetPositionModal()">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_position'); ?>
</button>
</div>
@ -26,15 +128,15 @@ $positions = $stmt->fetchAll();
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="GET" action="" class="row g-3">
<form id="positionsSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-10">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
<input type="text" name="name" id="positionSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
<button type="button" class="btn btn-secondary w-100" onclick="fetchPositions(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
@ -53,12 +155,12 @@ $positions = $stmt->fetchAll();
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="positionsTableBody">
<?php if (empty($positions)): ?>
<tr>
<td colspan="5" class="text-center py-5 text-muted">
<i class="bi bi-person-badge display-4 d-block mb-3"></i>
No positions found.
<?php echo __('no_positions_found'); ?>
</td>
</tr>
<?php else: ?>
@ -88,5 +190,213 @@ $positions = $stmt->fetchAll();
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="positionsPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalPositions); ?> <?php echo __('of'); ?> <?php echo $totalPositions; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Position Modal -->
<div class="modal fade" id="addPositionModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="positionModalTitle"><?php echo __('add_position'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="positionAction" value="add_position">
<input type="hidden" name="id" id="positionId">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="positionNameEn" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="positionNameAr" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('description_en'); ?></label>
<textarea class="form-control" name="description_en" id="positionDescEn" rows="2"></textarea>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('description_ar'); ?></label>
<textarea class="form-control" name="description_ar" id="positionDescAr" rows="2"></textarea>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Position Modal -->
<div class="modal fade" id="deletePositionModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_position'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_position">
<input type="hidden" name="id" id="deletePositionId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('positionSearchName');
const paginationContainer = document.getElementById('positionsPagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchPositions(1), 300);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchPositions(page);
}
});
}
});
function fetchPositions(page) {
const name = document.getElementById('positionSearchName').value;
const tableBody = document.getElementById('positionsTableBody');
const paginationContainer = document.getElementById('positionsPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
params.append('page', page);
params.append('ajax_search', '1');
fetch('positions.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching positions:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetPositionModal() {
document.getElementById('positionModalTitle').textContent = '<?php echo __('add_position'); ?>';
document.getElementById('positionAction').value = 'add_position';
document.getElementById('positionId').value = '';
document.getElementById('positionNameEn').value = '';
document.getElementById('positionNameAr').value = '';
document.getElementById('positionDescEn').value = '';
document.getElementById('positionDescAr').value = '';
}
function showEditPositionModal(position) {
document.getElementById('positionModalTitle').textContent = '<?php echo __('edit_position'); ?>';
document.getElementById('positionAction').value = 'edit_position';
document.getElementById('positionId').value = position.id;
document.getElementById('positionNameEn').value = position.name_en;
document.getElementById('positionNameAr').value = position.name_ar;
document.getElementById('positionDescEn').value = position.description_en;
document.getElementById('positionDescAr').value = position.description_ar;
var modal = new bootstrap.Modal(document.getElementById('addPositionModal'));
modal.show();
}
function showDeletePositionModal(id) {
document.getElementById('deletePositionId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deletePositionModal'));
modal.show();
}
</script>

View File

@ -1,19 +1,131 @@
<?php
$query = "SELECT * FROM test_groups ORDER BY id DESC";
$stmt = $db->query($query);
// Pagination Logic
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where_clause = "";
$params = [];
if ($search) {
$where_clause = "WHERE name_en LIKE ? OR name_ar LIKE ?";
$params[] = "%$search%";
$params[] = "%$search%";
}
// Total count
$count_query = "SELECT COUNT(*) FROM test_groups $where_clause";
$stmt = $db->prepare($count_query);
$stmt->execute($params);
$total_records = $stmt->fetchColumn();
$total_pages = ceil($total_records / $limit);
// Fetch data
$query = "SELECT * FROM test_groups $where_clause ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$groups = $stmt->fetchAll();
// Handle AJAX Request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
ob_start();
?>
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
<th class="px-4 py-3">#</th>
<th class="py-3"><?php echo __('name_en'); ?></th>
<th class="py-3"><?php echo __('name_ar'); ?></th>
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($groups)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-collection display-4 d-block mb-3"></i>
<?php echo __('no_data_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($groups as $group): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $group['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($group['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($group['name_ar']); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditTestGroupModal(<?php echo htmlspecialchars(json_encode($group, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteTestGroupModal(<?php echo $group['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
<?php
$html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('test_groups'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addTestGroupModal">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_test_group'); ?>
</button>
<div class="d-flex gap-2">
<div class="input-group shadow-sm" style="width: 250px;">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
<input type="text" id="searchInput" class="form-control border-start-0 ps-0" placeholder="<?php echo __('search'); ?>..." onkeyup="debounceSearch()">
</div>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addTestGroupModal" onclick="resetTestGroupModal()">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_test_group'); ?>
</button>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
<div class="table-responsive" id="tableContainer">
<!-- Initial Table Load -->
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
@ -28,7 +140,7 @@ $groups = $stmt->fetchAll();
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-collection display-4 d-block mb-3"></i>
No groups found.
<?php echo __('no_data_found'); ?>
</td>
</tr>
<?php else: ?>
@ -56,6 +168,145 @@ $groups = $stmt->fetchAll();
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls (Initial) -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Test Group Modal -->
<div class="modal fade" id="addTestGroupModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="groupModalTitle"><?php echo __('add_test_group'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="groupAction" value="add_test_group">
<input type="hidden" name="id" id="groupId">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="groupNameEn" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="groupNameAr" required>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Test Group Modal -->
<div class="modal fade" id="deleteTestGroupModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_test_group'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_test_group">
<input type="hidden" name="id" id="deleteGroupId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
let searchTimeout;
function debounceSearch() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
loadPage(1);
}, 300);
}
function loadPage(page) {
const search = document.getElementById('searchInput').value;
const url = `test_groups.php?page=${page}&search=${encodeURIComponent(search)}`;
fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
document.getElementById('tableContainer').innerHTML = data.html;
})
.catch(error => console.error('Error:', error));
}
function resetTestGroupModal() {
document.getElementById('groupModalTitle').textContent = '<?php echo __('add_test_group'); ?>';
document.getElementById('groupAction').value = 'add_test_group';
document.getElementById('groupId').value = '';
document.getElementById('groupNameEn').value = '';
document.getElementById('groupNameAr').value = '';
}
function showEditTestGroupModal(group) {
document.getElementById('groupModalTitle').textContent = '<?php echo __('edit_test_group'); ?>';
document.getElementById('groupAction').value = 'edit_test_group';
document.getElementById('groupId').value = group.id;
document.getElementById('groupNameEn').value = group.name_en;
document.getElementById('groupNameAr').value = group.name_ar;
var modal = new bootstrap.Modal(document.getElementById('addTestGroupModal'));
modal.show();
}
function showDeleteTestGroupModal(id) {
document.getElementById('deleteGroupId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteTestGroupModal'));
modal.show();
}
</script>

View File

@ -1,19 +1,131 @@
<?php
$query = "SELECT * FROM xray_groups ORDER BY id DESC";
$stmt = $db->query($query);
// Pagination Logic
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where_clause = "";
$params = [];
if ($search) {
$where_clause = "WHERE name_en LIKE ? OR name_ar LIKE ?";
$params[] = "%$search%";
$params[] = "%$search%";
}
// Total count
$count_query = "SELECT COUNT(*) FROM xray_groups $where_clause";
$stmt = $db->prepare($count_query);
$stmt->execute($params);
$total_records = $stmt->fetchColumn();
$total_pages = ceil($total_records / $limit);
// Fetch data
$query = "SELECT * FROM xray_groups $where_clause ORDER BY id DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$groups = $stmt->fetchAll();
// Handle AJAX Request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
ob_start();
?>
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
<th class="px-4 py-3">#</th>
<th class="py-3"><?php echo __('name_en'); ?></th>
<th class="py-3"><?php echo __('name_ar'); ?></th>
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($groups)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-collection display-4 d-block mb-3"></i>
<?php echo __('no_data_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($groups as $group): ?>
<tr>
<td class="px-4 text-secondary"><?php echo $group['id']; ?></td>
<td class="fw-semibold text-dark"><?php echo htmlspecialchars($group['name_en']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($group['name_ar']); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditXrayGroupModal(<?php echo htmlspecialchars(json_encode($group, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteXrayGroupModal(<?php echo $group['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
<?php
$html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('xray_groups'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addXrayGroupModal">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_xray_group'); ?>
</button>
<div class="d-flex gap-2">
<div class="input-group shadow-sm" style="width: 250px;">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
<input type="text" id="searchInput" class="form-control border-start-0 ps-0" placeholder="<?php echo __('search'); ?>..." onkeyup="debounceSearch()">
</div>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addXrayGroupModal" onclick="resetXrayGroupModal()">
<i class="bi bi-plus-lg me-1"></i> <?php echo __('add_xray_group'); ?>
</button>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
<div class="table-responsive" id="tableContainer">
<!-- Initial Table Load -->
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-secondary">
<tr>
@ -28,7 +140,7 @@ $groups = $stmt->fetchAll();
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-collection display-4 d-block mb-3"></i>
No groups found.
<?php echo __('no_data_found'); ?>
</td>
</tr>
<?php else: ?>
@ -56,6 +168,145 @@ $groups = $stmt->fetchAll();
<?php endif; ?>
</tbody>
</table>
<!-- Pagination Controls (Initial) -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top bg-light">
<div class="small text-muted">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page - 1; ?>); return false;">
<i class="bi bi-chevron-left"></i>
</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $i; ?>); return false;"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" onclick="loadPage(<?php echo $page + 1; ?>); return false;">
<i class="bi bi-chevron-right"></i>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Xray Group Modal -->
<div class="modal fade" id="addXrayGroupModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="groupModalTitle"><?php echo __('add_xray_group'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="groupAction" value="add_xray_group">
<input type="hidden" name="id" id="groupId">
<div class="modal-body p-4">
<div class="mb-3">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="groupNameEn" required>
</div>
<div class="mb-3">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="groupNameAr" required>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Xray Group Modal -->
<div class="modal fade" id="deleteXrayGroupModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_xray_group'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_xray_group">
<input type="hidden" name="id" id="deleteGroupId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
let searchTimeout;
function debounceSearch() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
loadPage(1);
}, 300);
}
function loadPage(page) {
const search = document.getElementById('searchInput').value;
const url = `xray_groups.php?page=${page}&search=${encodeURIComponent(search)}`;
fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
document.getElementById('tableContainer').innerHTML = data.html;
})
.catch(error => console.error('Error:', error));
}
function resetXrayGroupModal() {
document.getElementById('groupModalTitle').textContent = '<?php echo __('add_xray_group'); ?>';
document.getElementById('groupAction').value = 'add_xray_group';
document.getElementById('groupId').value = '';
document.getElementById('groupNameEn').value = '';
document.getElementById('groupNameAr').value = '';
}
function showEditXrayGroupModal(group) {
document.getElementById('groupModalTitle').textContent = '<?php echo __('edit_xray_group'); ?>';
document.getElementById('groupAction').value = 'edit_xray_group';
document.getElementById('groupId').value = group.id;
document.getElementById('groupNameEn').value = group.name_en;
document.getElementById('groupNameAr').value = group.name_ar;
var modal = new bootstrap.Modal(document.getElementById('addXrayGroupModal'));
modal.show();
}
function showDeleteXrayGroupModal(id) {
document.getElementById('deleteGroupId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteXrayGroupModal'));
modal.show();
}
</script>

View File

@ -1,36 +1,155 @@
<?php
$search_name = $_GET['name'] ?? '';
$search_group = $_GET['group_id'] ?? '';
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$query = "
SELECT t.*, g.name_$lang as group_name
FROM xray_tests t
LEFT JOIN xray_groups g ON t.group_id = g.id
WHERE 1=1";
$where = "WHERE 1=1";
$params = [];
if ($search_name) {
$query .= " AND (t.name_en LIKE ? OR t.name_ar LIKE ?)";
$where .= " AND (t.name_en LIKE ? OR t.name_ar LIKE ?)";
$params[] = "%$search_name%";
$params[] = "%$search_name%";
}
if ($search_group) {
$query .= " AND t.group_id = ?";
$where .= " AND t.group_id = ?";
$params[] = $search_group;
}
$query .= " ORDER BY t.id DESC";
// Count Total
$countQuery = "SELECT COUNT(*) FROM xray_tests t $where";
$stmt = $db->prepare($countQuery);
$stmt->execute($params);
$totalTests = $stmt->fetchColumn();
$totalPages = ceil($totalTests / $limit);
// Fetch Data
$query = "
SELECT t.*, g.name_$lang as group_name
FROM xray_tests t
LEFT JOIN xray_groups g ON t.group_id = g.id
$where
ORDER BY t.id DESC
LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$tests = $stmt->fetchAll();
// Fetch all xray groups for the dropdown
$all_xray_groups_list = $db->query("SELECT id, name_$lang as name FROM xray_groups ORDER BY name_$lang ASC")->fetchAll();
// --- AJAX HANDLER ---
if (isset($_GET['ajax_search'])) {
ob_start();
if (empty($tests)):
?>
<tr>
<td colspan="5" class="text-center py-5 text-muted">
<i class="bi bi-prescription2 display-4 d-block mb-3"></i>
<?php echo __('no_xrays_found'); ?>
</td>
</tr>
<?php else: ?>
<?php foreach ($tests as $test): ?>
<tr>
<td class="px-4 fw-medium text-secondary"><?php echo $test['id']; ?></td>
<td>
<div class="d-flex align-items-center">
<div class="bg-primary bg-opacity-10 text-primary p-2 rounded-circle me-3">
<i class="bi bi-list-check fs-5"></i>
</div>
<div>
<div class="fw-semibold text-dark"><?php echo htmlspecialchars($test['name_'.$lang]); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($test['name_'.($lang == 'en' ? 'ar' : 'en')]); ?></small>
</div>
</div>
</td>
<td>
<span class="badge bg-info bg-opacity-10 text-info border border-info border-opacity-25 px-2 py-1">
<?php echo htmlspecialchars($test['group_name'] ?? '-'); ?>
</span>
</td>
<td class="text-secondary fw-bold"><?php echo number_format($test['price'], 2); ?></td>
<td class="text-end px-4">
<div class="btn-group shadow-sm border rounded bg-white">
<button class="btn btn-link text-primary py-1 px-2 border-end"
onclick="showEditXrayTestModal(<?php echo htmlspecialchars(json_encode($test, JSON_UNESCAPED_UNICODE)); ?>)"
data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
<i class="bi bi-pencil-square"></i>
</button>
<button class="btn btn-link text-danger py-1 px-2"
onclick="showDeleteXrayTestModal(<?php echo $test['id']; ?>)"
data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
<i class="bi bi-trash3"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif;
$table_html = ob_get_clean();
ob_start();
if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalTests); ?> <?php echo __('of'); ?> <?php echo $totalTests; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif;
$pagination_html = ob_get_clean();
header('Content-Type: application/json');
echo json_encode(['html' => $table_html, 'pagination' => $pagination_html]);
exit;
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold text-secondary"><?php echo __('xray_tests'); ?></h3>
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addXrayTestModal">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addXrayTestModal" onclick="resetXrayTestModal()">
<i class="bi bi-plus-circle me-1"></i> <?php echo __('add_xray_test'); ?>
</button>
</div>
@ -38,15 +157,15 @@ $all_xray_groups_list = $db->query("SELECT id, name_$lang as name FROM xray_grou
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="GET" action="" class="row g-3">
<form id="xrayTestsSearchForm" class="row g-3" onsubmit="return false;">
<div class="col-md-6">
<div class="input-group">
<span class="input-group-text bg-light border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="name" class="form-control bg-light border-start-0" placeholder="<?php echo __('xray_name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
<input type="text" name="name" id="xrayTestSearchName" class="form-control bg-light border-start-0" placeholder="<?php echo __('xray_name'); ?>" value="<?php echo htmlspecialchars($search_name); ?>">
</div>
</div>
<div class="col-md-4">
<select name="group_id" class="form-select bg-light">
<select name="group_id" id="xrayTestSearchGroup" class="form-select bg-light">
<option value=""><?php echo __('xray_group'); ?> (<?php echo __('all'); ?>)</option>
<?php foreach ($all_xray_groups_list as $group): ?>
<option value="<?php echo $group['id']; ?>" <?php echo $search_group == $group['id'] ? 'selected' : ''; ?>>
@ -56,7 +175,7 @@ $all_xray_groups_list = $db->query("SELECT id, name_$lang as name FROM xray_grou
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-secondary w-100"><?php echo __('search'); ?></button>
<button type="button" class="btn btn-secondary w-100" onclick="fetchXrayTests(1)"><?php echo __('search'); ?></button>
</div>
</form>
</div>
@ -75,7 +194,7 @@ $all_xray_groups_list = $db->query("SELECT id, name_$lang as name FROM xray_grou
<th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="xrayTestsTableBody">
<?php if (empty($tests)): ?>
<tr>
<td colspan="5" class="text-center py-5 text-muted">
@ -124,5 +243,234 @@ $all_xray_groups_list = $db->query("SELECT id, name_$lang as name FROM xray_grou
</tbody>
</table>
</div>
<!-- Pagination -->
<div id="xrayTestsPagination">
<?php if ($totalPages > 1): ?>
<div class="d-flex justify-content-between align-items-center p-3 border-top">
<div class="text-muted small">
<?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalTests); ?> <?php echo __('of'); ?> <?php echo $totalTests; ?>
</div>
<nav aria-label="Page navigation">
<ul class="pagination pagination-sm mb-0">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page - 1; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php
$range = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
if ($totalPages > 1) { $pages_to_show[] = $totalPages; }
for ($i = $page - $range; $i <= $page + $range; $i++) {
if ($i > 1 && $i < $totalPages) { $pages_to_show[] = $i; }
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_page = 0;
foreach ($pages_to_show as $p):
if ($prev_page > 0 && $p > $prev_page + 1): ?>
<li class="page-item disabled"><span class="page-link">...</span></li>
<?php endif; ?>
<li class="page-item <?php echo $page == $p ? 'active' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $p; ?>">
<?php echo $p; ?>
</a>
</li>
<?php
$prev_page = $p;
endforeach;
?>
<li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
<a class="page-link" href="#" data-page="<?php echo $page + 1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Add/Edit Xray Test Modal -->
<div class="modal fade" id="addXrayTestModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="xrayTestModalTitle"><?php echo __('add_xray_test'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" id="xrayTestAction" value="add_xray_test">
<input type="hidden" name="id" id="xrayTestId">
<div class="modal-body p-4">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label"><?php echo __('name_en'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_en" id="xrayTestNameEn" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('name_ar'); ?> <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name_ar" id="xrayTestNameAr" required>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('xray_group'); ?></label>
<select class="form-select" name="group_id" id="xrayTestGroupId">
<option value=""><?php echo __('select_group'); ?></option>
<?php foreach ($all_xray_groups_list as $group): ?>
<option value="<?php echo $group['id']; ?>">
<?php echo htmlspecialchars($group['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label"><?php echo __('price'); ?></label>
<div class="input-group">
<span class="input-group-text">$</span>
<input type="number" step="0.01" class="form-control" name="price" id="xrayTestPrice">
</div>
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Xray Test Modal -->
<div class="modal fade" id="deleteXrayTestModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<div class="modal-header bg-danger text-white">
<h5 class="modal-title"><?php echo __('delete_xray_test'); ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="">
<input type="hidden" name="action" value="delete_xray_test">
<input type="hidden" name="id" id="deleteXrayTestId">
<div class="modal-body p-4 text-center">
<div class="mb-3 text-danger">
<i class="bi bi-exclamation-triangle display-1"></i>
</div>
<p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
<p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
</div>
<div class="modal-footer bg-light justify-content-center">
<button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
<button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchName = document.getElementById('xrayTestSearchName');
const searchGroup = document.getElementById('xrayTestSearchGroup');
const paginationContainer = document.getElementById('xrayTestsPagination');
let timeout = null;
if (searchName) {
searchName.addEventListener('input', function() {
clearTimeout(timeout);
timeout = setTimeout(() => fetchXrayTests(1), 300);
});
}
if (searchGroup) {
searchGroup.addEventListener('change', function() {
fetchXrayTests(1);
});
}
if (paginationContainer) {
paginationContainer.addEventListener('click', function(e) {
e.preventDefault();
const link = e.target.closest('.page-link');
if (link && !link.parentElement.classList.contains('disabled')) {
const page = link.getAttribute('data-page');
if (page) fetchXrayTests(page);
}
});
}
});
function fetchXrayTests(page) {
const name = document.getElementById('xrayTestSearchName').value;
const group = document.getElementById('xrayTestSearchGroup').value;
const tableBody = document.getElementById('xrayTestsTableBody');
const paginationContainer = document.getElementById('xrayTestsPagination');
if (tableBody) tableBody.style.opacity = '0.5';
const params = new URLSearchParams();
if (name) params.append('name', name);
if (group) params.append('group_id', group);
params.append('page', page);
params.append('ajax_search', '1');
fetch('xray_tests.php?' + params.toString())
.then(response => response.json())
.then(data => {
if (tableBody) {
tableBody.innerHTML = data.html;
tableBody.style.opacity = '1';
}
if (paginationContainer) {
paginationContainer.innerHTML = data.pagination;
}
// Re-initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
})
.catch(error => {
console.error('Error fetching xray tests:', error);
if (tableBody) tableBody.style.opacity = '1';
});
}
function resetXrayTestModal() {
document.getElementById('xrayTestModalTitle').textContent = '<?php echo __('add_xray_test'); ?>';
document.getElementById('xrayTestAction').value = 'add_xray_test';
document.getElementById('xrayTestId').value = '';
document.getElementById('xrayTestNameEn').value = '';
document.getElementById('xrayTestNameAr').value = '';
document.getElementById('xrayTestGroupId').value = '';
document.getElementById('xrayTestPrice').value = '';
}
function showEditXrayTestModal(test) {
document.getElementById('xrayTestModalTitle').textContent = '<?php echo __('edit_xray_test'); ?>';
document.getElementById('xrayTestAction').value = 'edit_xray_test';
document.getElementById('xrayTestId').value = test.id;
document.getElementById('xrayTestNameEn').value = test.name_en;
document.getElementById('xrayTestNameAr').value = test.name_ar;
document.getElementById('xrayTestGroupId').value = test.group_id || '';
document.getElementById('xrayTestPrice').value = test.price;
var modal = new bootstrap.Modal(document.getElementById('addXrayTestModal'));
modal.show();
}
function showDeleteXrayTestModal(id) {
document.getElementById('deleteXrayTestId').value = id;
var modal = new bootstrap.Modal(document.getElementById('deleteXrayTestModal'));
modal.show();
}
</script>