34894-vm/customer.php
Flatlogic Bot da0815949e finavo
2025-10-12 05:26:53 +00:00

228 lines
10 KiB
PHP

<?php
require_once 'includes/auth.php';
$active_page = 'customer';
require_once 'db/config.php';
// Handle delete request
if (isset($_GET['delete_id'])) {
$delete_id = $_GET['delete_id'];
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM customers WHERE id = ?");
$stmt->execute([$delete_id]);
header("Location: customer.php");
exit;
}
// Handle form submission for adding a new customer
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_customer'])) {
$companyName = $_POST['companyName'] ?? '';
$contactPerson = $_POST['contactPerson'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
if (!empty($companyName)) {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO customers (companyName, contactPerson, email, phone) VALUES (?, ?, ?, ?)");
$stmt->execute([$companyName, $contactPerson, $email, $phone]);
header("Location: customer.php");
exit;
}
}
// Handle form submission for updating a customer
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_customer'])) {
$id = $_POST['customer_id'];
$companyName = $_POST['companyName'] ?? '';
$contactPerson = $_POST['contactPerson'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
if (!empty($id) && !empty($companyName)) {
$pdo = db();
$stmt = $pdo->prepare("UPDATE customers SET companyName = ?, contactPerson = ?, email = ?, phone = ? WHERE id = ?");
$stmt->execute([$companyName, $contactPerson, $email, $phone, $id]);
header("Location: customer.php");
exit;
}
}
// Fetch all customers
$pdo = db();
$stmt = $pdo->query("SELECT * FROM customers ORDER BY createdAt DESC");
$customers = $stmt->fetchAll();
include 'includes/header.php';
?>
<div class="d-flex" id="wrapper">
<?php include 'includes/sidebar.php'; ?>
<!-- Page content wrapper-->
<div id="page-content-wrapper">
<!-- Top navigation-->
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container-fluid">
<button class="btn btn-primary" id="sidebarToggle"><i class="bi bi-list"></i></button>
<ul class="navbar-nav ms-auto mt-2 mt-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo htmlspecialchars($_SESSION["username"]); ?></a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#!">Profile</a>
<a class="dropdown-item" href="#!">Settings</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="logout.php">Logout</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- Page content-->
<main class="container-fluid p-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Customers</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addCustomerModal">
<i class="bi bi-plus-circle"></i>
Add Customer
</button>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Company Name</th>
<th scope="col">Contact Person</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($customers)): ?>
<tr>
<td colspan="6" class="text-center">No customers found.</td>
</tr>
<?php else: ?>
<?php foreach ($customers as $customer): ?>
<tr>
<td><?php echo htmlspecialchars($customer['id']); ?></td>
<td><?php echo htmlspecialchars($customer['companyName']); ?></td>
<td><?php echo htmlspecialchars($customer['contactPerson']); ?></td>
<td><?php echo htmlspecialchars($customer['email']); ?></td>
<td><?php echo htmlspecialchars($customer['phone']); ?></td>
<td>
<button class="btn btn-sm btn-outline-primary edit-customer-btn"
data-id="<?php echo $customer['id']; ?>"
data-company-name="<?php echo htmlspecialchars($customer['companyName']); ?>"
data-contact-person="<?php echo htmlspecialchars($customer['contactPerson']); ?>"
data-email="<?php echo htmlspecialchars($customer['email']); ?>"
data-phone="<?php echo htmlspecialchars($customer['phone']); ?>"
data-bs-toggle="modal" data-bs-target="#editCustomerModal">
<i class="bi bi-pencil-square"></i>
</button>
<a href="customer.php?delete_id=<?php echo $customer['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this customer?');"><i class="bi bi-trash"></i></a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</main>
</div>
</div>
<!-- Add Customer Modal -->
<div class="modal fade" id="addCustomerModal" tabindex="-1" aria-labelledby="addCustomerModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addCustomerModalLabel">Add New Customer</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" action="customer.php">
<input type="hidden" name="add_customer" value="1">
<div class="mb-3">
<label for="companyName" class="form-label">Company Name</label>
<input type="text" class="form-control" id="companyName" name="companyName" required>
</div>
<div class="mb-3">
<label for="contactPerson" class="form-label">Contact Person</label>
<input type="text" class="form-control" id="contactPerson" name="contactPerson">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="phone" name="phone">
</div>
<button type="submit" class="btn btn-primary">Save Customer</button>
</form>
</div>
</div>
</div>
</div>
<!-- Edit Customer Modal -->
<div class="modal fade" id="editCustomerModal" tabindex="-1" aria-labelledby="editCustomerModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editCustomerModalLabel">Edit Customer</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" action="customer.php">
<input type="hidden" name="update_customer" value="1">
<input type="hidden" name="customer_id" id="edit_customer_id">
<div class="mb-3">
<label for="edit_companyName" class="form-label">Company Name</label>
<input type="text" class="form-control" id="edit_companyName" name="companyName" required>
</div>
<div class="mb-3">
<label for="edit_contactPerson" class="form-label">Contact Person</label>
<input type="text" class="form-control" id="edit_contactPerson" name="contactPerson">
</div>
<div class="mb-3">
<label for="edit_email" class="form-label">Email</label>
<input type="email" class="form-control" id="edit_email" name="email">
</div>
<div class="mb-3">
<label for="edit_phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="edit_phone" name="phone">
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
var editCustomerModal = document.getElementById('editCustomerModal');
editCustomerModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var customerId = button.getAttribute('data-id');
var companyName = button.getAttribute('data-company-name');
var contactPerson = button.getAttribute('data-contact-person');
var email = button.getAttribute('data-email');
var phone = button.getAttribute('data-phone');
var modal = this;
modal.querySelector('#edit_customer_id').value = customerId;
modal.querySelector('#edit_companyName').value = companyName;
modal.querySelector('#edit_contactPerson').value = contactPerson;
modal.querySelector('#edit_email').value = email;
modal.querySelector('#edit_phone').value = phone;
});
});
</script>