227 lines
9.3 KiB
PHP
227 lines
9.3 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../includes/functions.php";
|
|
require_permission("outlets_view");
|
|
require_once __DIR__ . '/../db/config.php';
|
|
$pdo = db();
|
|
|
|
$message = '';
|
|
|
|
// Handle Add/Edit Outlet
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
$action = $_POST['action'];
|
|
$name = trim($_POST['name']);
|
|
$name_ar = trim($_POST['name_ar'] ?? '');
|
|
$address = trim($_POST['address']);
|
|
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
|
|
|
|
if (empty($name)) {
|
|
$message = '<div class="alert alert-danger">Outlet name is required.</div>';
|
|
} else {
|
|
try {
|
|
if ($action === 'edit_outlet' && $id) {
|
|
if (!has_permission('outlets_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE outlets SET name = ?, name_ar = ?, address = ? WHERE id = ?");
|
|
$stmt->execute([$name, $name_ar, $address, $id]);
|
|
$message = '<div class="alert alert-success">Outlet updated successfully!</div>';
|
|
}
|
|
} elseif ($action === 'add_outlet') {
|
|
if (!has_permission('outlets_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO outlets (name, name_ar, address) VALUES (?, ?, ?)");
|
|
$stmt->execute([$name, $name_ar, $address]);
|
|
$message = '<div class="alert alert-success">Outlet created successfully!</div>';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Delete
|
|
if (isset($_GET['delete'])) {
|
|
if (!has_permission('outlets_del')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to delete outlets.</div>';
|
|
} else {
|
|
$id = $_GET['delete'];
|
|
$pdo->prepare("DELETE FROM outlets WHERE id = ?")->execute([$id]);
|
|
header("Location: outlets.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$query = "SELECT * FROM outlets ORDER BY id DESC";
|
|
$outlets_pagination = paginate_query($pdo, $query);
|
|
$outlets = $outlets_pagination['data'];
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Outlets</h2>
|
|
<?php if (has_permission('outlets_add')): ?>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#outletModal" onclick="prepareAddForm()">
|
|
<i class="bi bi-plus-lg"></i> Add Outlet
|
|
</button>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?= $message ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body p-0">
|
|
<!-- Pagination Controls -->
|
|
<div class="p-3 border-bottom bg-light">
|
|
<?php render_pagination_controls($outlets_pagination); ?>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-4">ID</th>
|
|
<th>Name</th>
|
|
<th>Arabic Name</th>
|
|
<th>Address</th>
|
|
<th class="text-end pe-4">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($outlets as $outlet): ?>
|
|
<tr>
|
|
<td class="ps-4 fw-medium">#<?= $outlet['id'] ?></td>
|
|
<td class="fw-bold"><?= htmlspecialchars($outlet['name']) ?></td>
|
|
<td><?= htmlspecialchars($outlet['name_ar'] ?: '-') ?></td>
|
|
<td><small class="text-muted"><?= htmlspecialchars($outlet['address']) ?></small></td>
|
|
<td class="text-end pe-4">
|
|
<?php if (has_permission('outlets_add')): ?>
|
|
<button type="button" class="btn btn-sm btn-outline-primary me-1"
|
|
data-bs-toggle="modal" data-bs-target="#outletModal"
|
|
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($outlet), ENT_QUOTES, "UTF-8") ?>)'><i class="bi bi-pencil"></i></button>
|
|
<?php endif; ?>
|
|
|
|
<?php if (has_permission('outlets_del')): ?>
|
|
<a href="?delete=<?= $outlet['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this outlet?')"><i class="bi bi-trash"></i></a>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($outlets)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center py-4 text-muted">No outlets found.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- Bottom Pagination -->
|
|
<div class="p-3 border-top bg-light">
|
|
<?php render_pagination_controls($outlets_pagination); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Outlet Modal -->
|
|
<?php if (has_permission('outlets_add')): ?>
|
|
<div class="modal fade" id="outletModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title" id="outletModalTitle">Add New Outlet</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form method="POST" id="outletForm">
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" id="outletAction" value="add_outlet">
|
|
<input type="hidden" name="id" id="outletId">
|
|
<div class="mb-3">
|
|
<label class="form-label">Name <span class="text-danger">*</span></label>
|
|
<div class="input-group">
|
|
<input type="text" name="name" id="outletName" class="form-control" required>
|
|
<button class="btn btn-outline-secondary" type="button" id="btnTranslate">
|
|
<i class="bi bi-translate text-primary"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Arabic Name</label>
|
|
<input type="text" name="name_ar" id="outletNameAr" class="form-control" dir="rtl">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Address</label>
|
|
<textarea name="address" id="outletAddress" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Save Outlet</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function prepareAddForm() {
|
|
document.getElementById('outletModalTitle').innerText = 'Add New Outlet';
|
|
document.getElementById('outletAction').value = 'add_outlet';
|
|
document.getElementById('outletForm').reset();
|
|
document.getElementById('outletId').value = '';
|
|
}
|
|
|
|
function prepareEditForm(outlet) {
|
|
if (!outlet) return;
|
|
document.getElementById('outletModalTitle').innerText = 'Edit Outlet';
|
|
document.getElementById('outletAction').value = 'edit_outlet';
|
|
document.getElementById('outletId').value = outlet.id;
|
|
document.getElementById('outletName').value = outlet.name || '';
|
|
document.getElementById('outletNameAr').value = outlet.name_ar || '';
|
|
document.getElementById('outletAddress').value = outlet.address || '';
|
|
}
|
|
|
|
document.getElementById('btnTranslate').addEventListener('click', function() {
|
|
const text = document.getElementById('outletName').value;
|
|
if (!text) {
|
|
alert('Please enter an outlet name first.');
|
|
return;
|
|
}
|
|
|
|
const btn = this;
|
|
const originalHtml = btn.innerHTML;
|
|
btn.disabled = true;
|
|
btn.innerHTML = '<span class="spinner-border spinner-border-sm text-primary" role="status" aria-hidden="true"></span>';
|
|
|
|
fetch('../api/translate.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
text: text,
|
|
target_lang: 'Arabic'
|
|
}),
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
document.getElementById('outletNameAr').value = data.translated_text;
|
|
} else {
|
|
alert('Translation failed: ' + (data.error || 'Unknown error'));
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('An error occurred during translation.');
|
|
})
|
|
.finally(() => {
|
|
btn.disabled = false;
|
|
btn.innerHTML = originalHtml;
|
|
});
|
|
});
|
|
</script>
|
|
<?php endif; ?>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|