38244-vm/members.php
2026-02-07 11:39:06 +00:00

202 lines
10 KiB
PHP

<?php
declare(strict_types=1);
require_once 'db/config.php';
$current_branch_id = (int)($_GET['branch_id'] ?? 1);
$db = db();
// Handle Add Member
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
$name = $_POST['name'];
$code = $_POST['code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$stmt = $db->prepare("INSERT INTO members (code, name, phone, email) VALUES (?, ?, ?, ?)");
try {
$stmt->execute([$code, $name, $phone, $email]);
header("Location: members.php?branch_id=$current_branch_id&success=1");
exit;
} catch (Exception $e) {
$error = "Error adding member: " . $e->getMessage();
}
}
// Fetch Data
$branches = $db->query("SELECT * FROM branches")->fetchAll();
$current_branch = array_filter($branches, fn($b) => $b['id'] == $current_branch_id);
$current_branch = reset($current_branch);
$members = $db->query("SELECT * FROM members ORDER BY id DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Members - POS Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<!-- Sidebar -->
<nav id="sidebar" class="bg-dark text-white">
<div class="sidebar-header p-3">
<h4 class="mb-0">POS<span class="text-success">PRO</span></h4>
<small class="text-muted">Multi-Branch System</small>
</div>
<ul class="list-unstyled components p-2">
<li>
<a href="index.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-speedometer2 me-2"></i> Dashboard
</a>
</li>
<li>
<a href="pos.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-cart3 me-2"></i> Cashier (POS)
</a>
</li>
<li>
<a href="products.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-box-seam me-2"></i> Products
</a>
</li>
<li class="active">
<a href="members.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-people me-2"></i> Members
</a>
</li>
<li>
<a href="vouchers.php?branch_id=<?php echo $current_branch_id; ?>" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-ticket-perforated me-2"></i> Vouchers
</a>
</li>
<li>
<a href="#" class="nav-link text-white p-2 d-block rounded">
<i class="bi bi-graph-up me-2"></i> Reports
</a>
</li>
</ul>
</nav>
<!-- Page Content -->
<div id="content">
<nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom p-3">
<div class="container-fluid">
<button type="button" id="sidebarCollapse" class="btn btn-outline-dark me-3">
<i class="bi bi-list"></i>
</button>
<div class="ms-auto d-flex align-items-center">
<div class="dropdown me-3">
<button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="bi bi-geo-alt me-1"></i> <?php echo htmlspecialchars($current_branch['name']); ?>
</button>
<ul class="dropdown-menu shadow border-0">
<?php foreach ($branches as $branch): ?>
<li><a class="dropdown-item" href="?branch_id=<?php echo $branch['id']; ?>"><?php echo htmlspecialchars($branch['name']); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</nav>
<div class="container-fluid p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0">Member Management</h2>
<button class="btn btn-primary rounded-pill" data-bs-toggle="modal" data-bs-target="#addMemberModal">
<i class="bi bi-person-plus me-1"></i> Add Member
</button>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success alert-dismissible fade show rounded-4 border-0 shadow-sm" role="alert">
Member added successfully!
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="card border-0 shadow-sm rounded-4">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr class="text-muted small">
<th class="ps-4">CODE</th>
<th>NAME</th>
<th>PHONE</th>
<th>EMAIL</th>
<th>POINTS</th>
<th class="pe-4 text-end">ACTIONS</th>
</tr>
</thead>
<tbody>
<?php foreach ($members as $m): ?>
<tr>
<td class="ps-4 fw-bold"><?php echo htmlspecialchars($m['code']); ?></td>
<td><?php echo htmlspecialchars($m['name']); ?></td>
<td><?php echo htmlspecialchars($m['phone']); ?></td>
<td><?php echo htmlspecialchars($m['email']); ?></td>
<td><span class="badge bg-soft-info text-info"><?php echo number_format($m['points']); ?> Pts</span></td>
<td class="pe-4 text-end">
<button class="btn btn-sm btn-light rounded-circle"><i class="bi bi-pencil"></i></button>
<button class="btn btn-sm btn-light rounded-circle text-danger"><i class="bi bi-trash"></i></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Add Member Modal -->
<div class="modal fade" id="addMemberModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content border-0 shadow rounded-4">
<form method="POST">
<input type="hidden" name="action" value="add">
<div class="modal-header border-0 p-4">
<h5 class="modal-title">New Member</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body p-4 pt-0">
<div class="mb-3">
<label class="form-label">Member Code</label>
<input type="text" name="code" class="form-control" value="MEM-<?php echo strtoupper(bin2hex(random_bytes(2))); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Full Name</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Phone Number</label>
<input type="text" name="phone" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email" name="email" class="form-control">
</div>
</div>
<div class="modal-footer border-0 p-4 pt-0">
<button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary rounded-pill px-4">Save Member</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('sidebarCollapse').addEventListener('click', function() {
document.getElementById('sidebar').classList.toggle('active');
});
</script>
</body>
</html>