34777-vm/packages.php
2025-10-08 06:10:35 +00:00

174 lines
7.7 KiB
PHP

<?php
require_once 'auth.php';
require_once 'db/config.php';
// Restrict access to Administrators
if ($_SESSION['user']['role'] !== 'Administrator') {
header('Location: index.php');
exit;
}
$pdo = db();
$feedback = [];
$edit_package = null;
// Handle Edit Request
if (isset($_GET['edit_id'])) {
$stmt = $pdo->prepare("SELECT * FROM packages WHERE id = ?");
$stmt->execute([$_GET['edit_id']]);
$edit_package = $stmt->fetch();
}
// Handle Delete Request
if (isset($_POST['delete_id'])) {
try {
$stmt = $pdo->prepare("DELETE FROM packages WHERE id = ?");
$stmt->execute([$_POST['delete_id']]);
$feedback = ['type' => 'success', 'message' => 'Paket berhasil dihapus.'];
} catch (PDOException $e) {
$feedback = ['type' => 'danger', 'message' => 'Gagal menghapus paket: ' . $e->getMessage()];
}
}
// Handle Add/Update Request
if (isset($_POST['save_package'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$duration_days = $_POST['duration_days'];
$description = $_POST['description'];
$id = $_POST['id'];
// Basic validation
if (empty($name) || !is_numeric($price) || !is_numeric($duration_days)) {
$feedback = ['type' => 'danger', 'message' => 'Nama, Harga, dan Durasi harus diisi dengan benar.'];
} else {
try {
if (empty($id)) { // Add new
$stmt = $pdo->prepare("INSERT INTO packages (name, price, duration_days, description) VALUES (?, ?, ?, ?)");
$stmt->execute([$name, $price, $duration_days, $description]);
$feedback = ['type' => 'success', 'message' => 'Paket baru berhasil ditambahkan.'];
} else { // Update existing
$stmt = $pdo->prepare("UPDATE packages SET name = ?, price = ?, duration_days = ?, description = ? WHERE id = ?");
$stmt->execute([$name, $price, $duration_days, $description, $id]);
$feedback = ['type' => 'success', 'message' => 'Paket berhasil diperbarui.'];
// Redirect to clear edit state
header("Location: packages.php");
exit;
}
} catch (PDOException $e) {
$feedback = ['type' => 'danger', 'message' => 'Operasi gagal: ' . $e->getMessage()];
}
}
}
// Fetch all packages for display
$packages = $pdo->query("SELECT * FROM packages ORDER BY name ASC")->fetchAll();
$page_title = 'Paket Layanan';
require_once 'partials/header.php';
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0 text-gray-800"><?php echo htmlspecialchars($page_title); ?></h1>
<a href="index.php" class="btn btn-secondary">
<i data-feather="arrow-left" class="mr-2"></i>Kembali ke Dashboard
</a>
</div>
<?php if (!empty($feedback)): ?>
<div class="alert alert-<?php echo htmlspecialchars($feedback['type']); ?>">
<?php echo htmlspecialchars($feedback['message']); ?>
</div>
<?php endif; ?>
<!-- Add/Edit Form Card -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"><?php echo $edit_package ? 'Edit Paket' : 'Tambah Paket Baru'; ?></h6>
</div>
<div class="card-body">
<form action="packages.php" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_package['id'] ?? ''); ?>">
<div class="form-row">
<div class="form-group col-md-4">
<label for="name">Nama Paket</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_package['name'] ?? ''); ?>" required>
</div>
<div class="form-group col-md-2">
<label for="price">Harga (Rp)</label>
<input type="number" class="form-control" id="price" name="price" value="<?php echo htmlspecialchars($edit_package['price'] ?? ''); ?>" required>
</div>
<div class="form-group col-md-2">
<label for="duration_days">Durasi (Hari)</label>
<input type="number" class="form-control" id="duration_days" name="duration_days" value="<?php echo htmlspecialchars($edit_package['duration_days'] ?? ''); ?>" required>
</div>
</div>
<div class="form-group">
<label for="description">Deskripsi</label>
<textarea class="form-control" id="description" name="description" rows="2"><?php echo htmlspecialchars($edit_package['description'] ?? ''); ?></textarea>
</div>
<button type="submit" name="save_package" class="btn btn-primary">
<i data-feather="save" class="mr-2"></i>Simpan
</button>
<?php if ($edit_package): ?>
<a href="packages.php" class="btn btn-secondary">Batal</a>
<?php endif; ?>
</form>
</div>
</div>
<!-- Packages List Card -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Daftar Paket</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Nama</th>
<th>Harga</th>
<th>Durasi</th>
<th>Deskripsi</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php if (empty($packages)): ?>
<tr>
<td colspan="5" class="text-center">Belum ada paket yang ditambahkan.</td>
</tr>
<?php else: ?>
<?php foreach ($packages as $pkg): ?>
<tr>
<td><?php echo htmlspecialchars($pkg['name']); ?></td>
<td>Rp <?php echo number_format($pkg['price'], 0, ',', '.'); ?></td>
<td><?php echo htmlspecialchars($pkg['duration_days']); ?> hari</td>
<td><?php echo htmlspecialchars($pkg['description']); ?></td>
<td>
<a href="packages.php?edit_id=<?php echo $pkg['id']; ?>" class="btn btn-sm btn-warning">
<i data-feather="edit-2"></i>
</a>
<form action="packages.php" method="POST" onsubmit="return confirm('Yakin ingin menghapus paket ini?');" class="d-inline">
<input type="hidden" name="delete_id" value="<?php echo $pkg['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger">
<i data-feather="trash-2"></i>
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php require_once 'partials/footer.php'; ?>