38929-vm/branches.php
2026-03-03 05:22:06 +00:00

256 lines
13 KiB
PHP

<?php
// ACTION HANDLING FIRST
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/lang.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
// Initial view check
if (!has_permission('view')) {
header('Location: admin.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
if ($action === 'add_branch' && !has_permission('add')) {
header('Location: branches.php?error=no_permission');
exit;
}
if ($action === 'edit_branch' && !has_permission('edit')) {
header('Location: branches.php?error=no_permission');
exit;
}
if ($action === 'add_branch') {
$name_en = $_POST['name_en'];
$name_ar = $_POST['name_ar'];
$company_id = $_POST['company_id'];
$phone = $_POST['phone'];
$prefix = strtoupper(substr($_POST['prefix'] ?? '', 0, 3));
$stmt = db()->prepare("INSERT INTO branches (name_en, name_ar, company_id, phone, prefix) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name_en, $name_ar, $company_id, $phone, $prefix]);
header('Location: branches.php?success=branch_added');
exit;
}
if ($action === 'edit_branch') {
$id = $_POST['id'];
$name_en = $_POST['name_en'];
$name_ar = $_POST['name_ar'];
$company_id = $_POST['company_id'];
$phone = $_POST['phone'];
$prefix = strtoupper(substr($_POST['prefix'] ?? '', 0, 3));
$stmt = db()->prepare("UPDATE branches SET name_en = ?, name_ar = ?, company_id = ?, phone = ?, prefix = ? WHERE id = ?");
$stmt->execute([$name_en, $name_ar, $company_id, $phone, $prefix, $id]);
header('Location: branches.php?success=branch_updated');
exit;
}
}
if (isset($_GET['delete'])) {
if (!has_permission('delete')) {
header('Location: branches.php?error=no_permission');
exit;
}
$id = $_GET['delete'];
$stmt = db()->prepare("DELETE FROM branches WHERE id = ?");
$stmt->execute([$id]);
header('Location: branches.php?success=branch_deleted');
exit;
}
// NOW Include header
$title = 'branches';
require_once __DIR__ . '/includes/header.php';
$branches = db()->query("SELECT b.*, c.name_en as company_name_en FROM branches b JOIN companies c ON b.company_id = c.id")->fetchAll();
$companies = db()->query("SELECT * FROM companies")->fetchAll();
?>
<?php if(isset($_GET['success'])): ?>
<div class="alert alert-success alert-dismissible fade show rounded-4 shadow-sm mb-4 border-0" role="alert">
<i class="bi bi-check-circle-fill me-2"></i>
<?= __($_GET['success']) ?? 'Action completed successfully' ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if(isset($_GET['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show rounded-4 shadow-sm mb-4 border-0" role="alert">
<i class="bi bi-exclamation-triangle-fill me-2"></i>
<?= __($_GET['error']) ?? 'Permission Denied' ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="row">
<div class="col-md-4">
<div class="card p-4 shadow-sm border-0 mb-4" style="border-radius: 20px;">
<?php if (has_permission('add')): ?>
<h5 class="fw-bold mb-4"><?= __('add_new_branch') ?? 'Add New Branch' ?></h5>
<form method="POST">
<input type="hidden" name="action" value="add_branch">
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('company') ?? 'Company' ?></label>
<select name="company_id" class="form-select" style="border-radius: 12px;">
<?php foreach($companies as $c): ?>
<option value="<?= $c['id'] ?>"><?= $lang === 'ar' ? $c['name_ar'] : $c['name_en'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('name_en') ?></label>
<input type="text" name="name_en" class="form-control" required style="border-radius: 12px;">
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('name_ar') ?></label>
<input type="text" name="name_ar" class="form-control" style="border-radius: 12px;">
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('initial_letters') ?></label>
<input type="text" name="prefix" class="form-control" maxlength="3" minlength="3" required style="border-radius: 12px;" placeholder="e.g. MCT">
<div class="form-text small"><?= __('exactly_3_letters') ?? 'Exactly 3 letters' ?></div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('phone') ?></label>
<input type="text" name="phone" class="form-control" style="border-radius: 12px;">
</div>
<button type="submit" class="btn btn-primary w-100 py-3 fw-bold shadow-sm" style="border-radius: 15px;"><?= __('add_branch') ?></button>
</form>
<?php else: ?>
<div class="text-center py-4">
<i class="bi bi-lock fs-1 text-muted opacity-25"></i>
<p class="text-muted mt-2 small">You don't have permission to add branches.</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="col-md-8">
<div class="card p-0 shadow-sm border-0" style="border-radius: 20px; overflow: hidden;">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr class="py-3">
<th class="ps-4 py-3">#</th>
<th class="py-3"><?= __('name') ?></th>
<th class="py-3"><?= __('initial_letters') ?></th>
<th class="py-3"><?= __('company') ?></th>
<th class="py-3"><?= __('phone') ?></th>
<th class="pe-4 py-3 text-end"><?= __('actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach($branches as $b): ?>
<tr>
<td class="ps-4"><?= $b['id'] ?></td>
<td class="fw-bold"><?= $lang === 'ar' ? ($b['name_ar'] ?: $b['name_en']) : $b['name_en'] ?></td>
<td class="text-primary fw-bold"><?= $b['prefix'] ?></td>
<td><?= $b['company_name_en'] ?></td>
<td><?= $b['phone'] ?></td>
<td class="pe-4 text-end">
<?php if (has_permission('edit')): ?>
<button class="btn btn-sm btn-outline-primary border-0 edit-branch"
data-id="<?= $b['id'] ?>"
data-name_en="<?= htmlspecialchars($b['name_en']) ?>"
data-name_ar="<?= htmlspecialchars($b['name_ar']) ?>"
data-company_id="<?= $b['company_id'] ?>"
data-phone="<?= htmlspecialchars($b['phone']) ?>"
data-prefix="<?= htmlspecialchars($b['prefix']) ?>"
style="border-radius: 8px;">
<i class="bi bi-pencil"></i>
</button>
<?php endif; ?>
<?php if (has_permission('delete')): ?>
<a href="?delete=<?= $b['id'] ?>" class="btn btn-sm btn-outline-danger border-0"
onclick="return confirm('<?= __('are_you_sure') ?>')"
style="border-radius: 8px;">
<i class="bi bi-trash"></i>
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($branches)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-muted">
<i class="bi bi-shop fs-1 d-block mb-3 opacity-25"></i>
No branches found.
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Edit Branch Modal -->
<?php if (has_permission('edit')): ?>
<div class="modal fade" id="editBranchModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content border-0 shadow-lg" style="border-radius: 20px;">
<div class="modal-header border-0 pb-0">
<h5 class="fw-bold"><?= __('edit') ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit_branch">
<input type="hidden" name="id" id="edit_id">
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('company') ?? 'Company' ?></label>
<select name="company_id" id="edit_company_id" class="form-select" style="border-radius: 12px;">
<?php foreach($companies as $c): ?>
<option value="<?= $c['id'] ?>"><?= $lang === 'ar' ? $c['name_ar'] : $c['name_en'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('name_en') ?></label>
<input type="text" name="name_en" id="edit_name_en" class="form-control" required style="border-radius: 12px;">
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('name_ar') ?></label>
<input type="text" name="name_ar" id="edit_name_ar" class="form-control" style="border-radius: 12px;">
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('initial_letters') ?></label>
<input type="text" name="prefix" id="edit_prefix" class="form-control" maxlength="3" minlength="3" required style="border-radius: 12px;">
<div class="form-text small"><?= __('exactly_3_letters') ?? 'Exactly 3 letters' ?></div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold"><?= __('phone') ?></label>
<input type="text" name="phone" id="edit_phone" class="form-control" style="border-radius: 12px;">
</div>
</div>
<div class="modal-footer border-0 pt-0">
<button type="button" class="btn btn-light fw-bold" data-bs-dismiss="modal" style="border-radius: 12px;"><?= __('cancel') ?></button>
<button type="submit" class="btn btn-primary fw-bold shadow-sm" style="border-radius: 12px;"><?= __('save_changes') ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
document.querySelectorAll('.edit-branch').forEach(btn => {
btn.addEventListener('click', function() {
const d = this.dataset;
document.getElementById('edit_id').value = d.id;
document.getElementById('edit_name_en').value = d.name_en;
document.getElementById('edit_name_ar').value = d.name_ar;
document.getElementById('edit_company_id').value = d.company_id;
document.getElementById('edit_phone').value = d.phone;
document.getElementById('edit_prefix').value = d.prefix;
new bootstrap.Modal(document.getElementById('editBranchModal')).show();
});
});
</script>
<?php endif; ?>
<?php require_once __DIR__ . '/includes/footer.php'; ?>