35324-vm/manage_services.php
Flatlogic Bot 9584826cb1 v3
2025-10-29 10:27:50 +00:00

149 lines
5.9 KiB
PHP

<?php
require_once 'db/config.php';
require_once '_header.php';
// Ensure user is a provider
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'provider') {
header("Location: index.php");
exit;
}
$provider_id = $_SESSION['user_id'];
$error = null;
$success = null;
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_service'])) {
$name = trim($_POST['name'] ?? '');
$description = trim($_POST['description'] ?? '');
$category_id = $_POST['category_id'] ?? null;
$price = $_POST['price'] ?? '';
if (empty($name) || empty($category_id) || empty($price)) {
$error = "Nome, categoria e prezzo sono obbligatori.";
} elseif (!is_numeric($price) || $price < 0) {
$error = "Il prezzo non è valido.";
} else {
try {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO services (provider_id, category_id, name, description, price) VALUES (?, ?, ?, ?, ?)"
);
if ($stmt->execute([$provider_id, $category_id, $name, $description, $price])) {
$success = "Servizio aggiunto con successo!";
} else {
$error = "Errore durante l'aggiunta del servizio.";
}
} catch (PDOException $e) {
$error = "Errore del database: " . $e->getMessage();
}
}
}
// Fetch data for the page
try {
$pdo = db();
// Fetch categories
$categories_stmt = $pdo->query("SELECT * FROM service_categories ORDER BY name");
$categories = $categories_stmt->fetchAll();
// Fetch provider's services
$services_stmt = $pdo->prepare(
"SELECT s.*, sc.name as category_name FROM services s JOIN service_categories sc ON s.category_id = sc.id WHERE s.provider_id = ? ORDER BY s.created_at DESC"
);
$services_stmt->execute([$provider_id]);
$services = $services_stmt->fetchAll();
} catch (PDOException $e) {
// Die on critical database error
die("Errore di connessione al database: " . $e->getMessage());
}
$pageTitle = "Gestisci Servizi";
?>
<main class="container my-5">
<h1 class="mb-4"><?= htmlspecialchars($pageTitle) ?></h1>
<?php if ($success): ?>
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<!-- Add Service Form -->
<div class="card mb-4">
<div class="card-header">
<h2 class="h5 mb-0">Aggiungi un nuovo servizio</h2>
</div>
<div class="card-body">
<form action="manage_services.php" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="name" class="form-label">Nome Servizio</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-6 mb-3">
<label for="category_id" class="form-label">Categoria</label>
<select class="form-select" id="category_id" name="category_id" required>
<option value="">Seleziona una categoria</option>
<?php foreach ($categories as $category): ?>
<option value="<?= $category['id'] ?>"><?= htmlspecialchars($category['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="mb-3">
<label for="description" class="form-label">Descrizione</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="price" class="form-label">Prezzo (€)</label>
<input type="number" step="0.01" class="form-control" id="price" name="price" required>
</div>
<button type="submit" name="add_service" class="btn btn-primary">Aggiungi Servizio</button>
</form>
</div>
</div>
<!-- List of Services -->
<div class="card">
<div class="card-header">
<h2 class="h5 mb-0">I tuoi servizi</h2>
</div>
<div class="card-body">
<?php if (empty($services)): ?>
<p>Non hai ancora aggiunto nessun servizio.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Prezzo</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($services as $service): ?>
<tr>
<td><?= htmlspecialchars($service['name']) ?></td>
<td><?= htmlspecialchars($service['category_name']) ?></td>
<td>€ <?= htmlspecialchars(number_format($service['price'], 2, ',', '.')) ?></td>
<td>
<a href="#" class="btn btn-sm btn-outline-secondary disabled">Modifica</a>
<a href="#" class="btn btn-sm btn-outline-danger disabled">Elimina</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</main>
<?php
require_once '_footer.php';
?>