2026-01-25 21:22:35 +01:00

305 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Forza la visualizzazione degli errori (solo dev)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
// Connessione DB
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Verifica utente loggato
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
// Controlla se esiste almeno un salone
$stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
if ($stmt->fetchColumn() === 0) {
header("Location: onboarding_salon.php");
exit;
}
// Prendi il primo salone (o quello attivo puoi aggiungere switcher dopo)
$stmt = $pdo->prepare("
SELECT id, name
FROM shops
WHERE owner_id = ?
ORDER BY created_at ASC
LIMIT 1
");
$stmt->execute([$iduserlogin]);
$shop = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$shop) {
die("Errore: salone non trovato.");
}
$shop_id = $shop['id'];
$shop_name = $shop['name'];
// =========================================================================
// Gestione POST (add / edit / delete)
// =========================================================================
$success_message = '';
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
if ($action === 'add' || $action === 'edit') {
$start_date = trim($_POST['start_date'] ?? '');
$end_date = trim($_POST['end_date'] ?? '');
$description = trim($_POST['description'] ?? '');
$id = ($action === 'edit') ? (int)($_POST['id'] ?? 0) : 0;
// Validazioni
if (empty($start_date) || empty($end_date)) {
$error_message = "Le date di inizio e fine sono obbligatorie.";
} elseif (strtotime($end_date) < strtotime($start_date)) {
$error_message = "La data di fine non può essere precedente alla data di inizio.";
} else {
if ($action === 'add') {
$stmt = $pdo->prepare("
INSERT INTO shop_day_off (shop_id, date, title, description, is_recurring, created_at)
VALUES (?, ?, ?, ?, 0, NOW())
");
$ok = $stmt->execute([$shop_id, $start_date, $description ?: 'Chiusura', $description]);
$success_message = $ok ? "Giorno di chiusura aggiunto!" : "Errore durante l'aggiunta.";
} else { // edit
$stmt = $pdo->prepare("
UPDATE shop_day_off
SET date = ?, title = ?, description = ?, updated_at = NOW()
WHERE id = ? AND shop_id = ?
");
$ok = $stmt->execute([$start_date, $description ?: 'Chiusura', $description, $id, $shop_id]);
$success_message = $ok ? "Giorno di chiusura aggiornato!" : "Errore durante l'aggiornamento.";
}
}
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id > 0) {
$stmt = $pdo->prepare("DELETE FROM shop_day_off WHERE id = ? AND shop_id = ?");
$ok = $stmt->execute([$id, $shop_id]);
$success_message = $ok ? "Giorno di chiusura eliminato!" : "Errore durante l'eliminazione.";
}
}
// Evita doppio submit
if ($success_message || $error_message) {
header("Location: day_off.php" . ($success_message ? "?msg=success" : "?msg=error"));
exit;
}
}
// Recupera tutti i giorni di chiusura
$stmt = $pdo->prepare("
SELECT id, date, title, description, is_recurring
FROM shop_day_off
WHERE shop_id = ?
ORDER BY date ASC
");
$stmt->execute([$shop_id]);
$days_off = $stmt->fetchAll();
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<title>Giorni di Chiusura - <?= htmlspecialchars($shop_name) ?></title>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card radius-10">
<div class="card-header bg-light d-flex align-items-center justify-content-between">
<h6 class="mb-0">Giorni di Chiusura - <?= htmlspecialchars($shop_name) ?></h6>
<div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addDayOffModal">
<i class="bx bx-plus me-1"></i> Aggiungi Chiusura
</button>
<a href="salon_dashboard.php" class="btn btn-outline-secondary ms-2">
<i class="bx bx-arrow-back me-1"></i> Dashboard
</a>
</div>
</div>
<div class="card-body">
<?php if (isset($_GET['msg'])): ?>
<?php if ($_GET['msg'] === 'success'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?= htmlspecialchars($success_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php elseif ($_GET['msg'] === 'error'): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?= htmlspecialchars($error_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php endif; ?>
<?php if (empty($days_off)): ?>
<div class="alert alert-info text-center py-4">
Non hai ancora impostato giorni di chiusura.<br>
Aggiungine uno per bloccare le prenotazioni in quei giorni.
</div>
<?php else: ?>
<div class="table-responsive">
<table id="daysOffTable" class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Data</th>
<th>Titolo / Descrizione</th>
<th>Ricorrente</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($days_off as $day): ?>
<tr>
<td><?= htmlspecialchars($day['date']) ?></td>
<td><?= htmlspecialchars($day['title'] ?: $day['description'] ?: 'Chiusura') ?></td>
<td>
<?php if ($day['is_recurring']): ?>
<span class="badge bg-success">Sì (ogni anno)</span>
<?php else: ?>
<span class="badge bg-secondary">No</span>
<?php endif; ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-warning me-1"
data-bs-toggle="modal" data-bs-target="#editDayOffModal"
onclick='fillEditModal(<?= json_encode([
"id" => $day['id'],
"date" => $day['date'],
"title" => htmlspecialchars($day['title'] ?? '', ENT_QUOTES),
"description" => htmlspecialchars($day['description'] ?? '', ENT_QUOTES)
]) ?>)'>
<i class="bx bx-edit"></i> Modifica
</button>
<form action="" method="POST" style="display:inline;"
onsubmit="return confirm('Confermi l\'eliminazione?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= $day['id'] ?>">
<button type="submit" class="btn btn-sm btn-danger">
<i class="bx bx-trash"></i> Elimina
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- Modal Aggiungi -->
<div class="modal fade" id="addDayOffModal" tabindex="-1" aria-labelledby="addDayOffModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="addDayOffModalLabel">Aggiungi Giorno di Chiusura</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label class="form-label fw-bold">Data <span class="text-danger">*</span></label>
<input type="date" class="form-control" name="start_date" required>
</div>
<div class="mb-3">
<label class="form-label fw-bold">Descrizione / Motivo</label>
<input type="text" class="form-control" name="description"
placeholder="Es: Ferragosto, Ferie estive, Corso formazione">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-primary">Aggiungi</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal Modifica -->
<div class="modal fade" id="editDayOffModal" tabindex="-1" aria-labelledby="editDayOffModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-warning text-dark">
<h5 class="modal-title" id="editDayOffModalLabel">Modifica Giorno di Chiusura</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" id="edit_id">
<div class="mb-3">
<label class="form-label fw-bold">Data <span class="text-danger">*</span></label>
<input type="date" class="form-control" name="start_date" id="edit_date" required>
</div>
<div class="mb-3">
<label class="form-label fw-bold">Descrizione / Motivo</label>
<input type="text" class="form-control" name="description" id="edit_description"
placeholder="Es: Ferie natalizie">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-warning">Salva Modifiche</button>
</div>
</form>
</div>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
$(document).ready(function() {
$('#daysOffTable').DataTable({
language: {
url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
},
order: [
[0, 'asc']
]
});
});
function fillEditModal(data) {
document.getElementById('edit_id').value = data.id;
document.getElementById('edit_date').value = data.date;
document.getElementById('edit_description').value = data.description || '';
}
</script>
</body>
</html>