Files
comelifacciamo/public/userarea/admin_shops.php
T
2026-09-09 08:26:26 +02:00

367 lines
18 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (session_status() === PHP_SESSION_NONE) session_start();
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
if ((int)$kindofrole !== ROLE_ADMIN) {
header("Location: index.php");
exit;
}
function e(?string $v): string
{
return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function getFlash(): ?array
{
if (!isset($_SESSION['flash'])) return null;
$f = $_SESSION['flash'];
unset($_SESSION['flash']);
return $f;
}
// --- Filtri ---
$q_search = trim($_GET['q'] ?? '');
$q_active = $_GET['active'] ?? ''; // '', '1', '0'
$where = ["1=1"];
$params = [];
if ($q_search !== '') {
$where[] = "(sh.name LIKE ? OR sh.city LIKE ? OR sh.email LIKE ?)";
$like = '%' . $q_search . '%';
array_push($params, $like, $like, $like);
}
if ($q_active === '1' || $q_active === '0') {
$where[] = "sh.active = ?";
$params[] = (int)$q_active;
}
$whereSql = implode(' AND ', $where);
$sql = "
SELECT sh.id, sh.name, sh.city, sh.province, sh.address, sh.zip_code,
sh.phone, sh.email, sh.logo, sh.active, sh.created_at,
CONCAT(u.first_name, ' ', u.last_name) AS owner_name, u.email AS owner_email,
(SELECT COUNT(*) FROM staff st WHERE st.shop_id = sh.id) AS staff_count,
(SELECT COUNT(*) FROM appointments a WHERE a.shop_id = sh.id) AS appt_count
FROM shops sh
LEFT JOIN auth_users u ON sh.owner_id = u.id
WHERE $whereSql
ORDER BY sh.created_at DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$shops = $stmt->fetchAll(PDO::FETCH_ASSOC);
$flash = getFlash();
$ret = http_build_query(array_filter(['q' => $q_search, 'active' => $q_active], fn($v) => $v !== '' && $v !== null));
?>
<!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>Gestione Saloni</title>
<style>
:root {
--soft-border: #e9edf3;
--muted: #6b7280;
--bg: #f6f7fb;
}
body {
background: var(--bg);
}
.card-shell {
border-radius: 14px;
border: 1px solid var(--soft-border);
background: #fff;
overflow: hidden;
}
.card-shell .card-header {
background: linear-gradient(180deg, #fff, #fbfcff);
border-bottom: 1px solid var(--soft-border);
}
.page-title {
font-weight: 900;
letter-spacing: -0.02em;
}
.shop-logo {
width: 44px;
height: 44px;
border-radius: 10px;
object-fit: cover;
background: #eef2f7;
display: inline-flex;
align-items: center;
justify-content: center;
font-weight: 800;
color: #64748b;
}
.table> :not(caption)>*>* {
padding: .85rem .75rem;
vertical-align: middle;
}
.muted-sm {
color: var(--muted);
font-size: .85rem;
}
.dot-status {
width: 9px;
height: 9px;
border-radius: 50%;
display: inline-block;
margin-right: 5px;
}
.st-on {
background: #16a34a;
}
.st-off {
background: #9ca3af;
}
</style>
</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-shell">
<div class="card-header p-3 p-md-4">
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2">
<div class="page-title h5 mb-0"><i class="bx bx-store me-1"></i> Gestione Saloni</div>
<span class="text-muted small"><?= count($shops) ?> saloni</span>
</div>
</div>
<div class="card-body">
<?php if ($flash): ?>
<div class="alert alert-<?= e($flash['type']) ?> alert-dismissible fade show" role="alert">
<?= e($flash['text']) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<form method="GET" class="row g-2 mb-3">
<div class="col-md-7">
<input type="text" name="q" class="form-control" placeholder="Cerca nome, città o email…" value="<?= e($q_search) ?>">
</div>
<div class="col-md-3">
<select name="active" class="form-select">
<option value="">Tutti</option>
<option value="1" <?= $q_active === '1' ? 'selected' : '' ?>>Attivi</option>
<option value="0" <?= $q_active === '0' ? 'selected' : '' ?>>Disattivati</option>
</select>
</div>
<div class="col-md-2 d-grid">
<button class="btn btn-primary"><i class="bx bx-search"></i> Filtra</button>
</div>
</form>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead>
<tr>
<th>Salone</th>
<th>Titolare</th>
<th>Città</th>
<th>Staff</th>
<th>Appunt.</th>
<th>Stato</th>
<th class="text-end">Azioni</th>
</tr>
</thead>
<tbody>
<?php if (empty($shops)): ?>
<tr>
<td colspan="7" class="text-center text-muted py-4">Nessun salone trovato.</td>
</tr>
<?php else: foreach ($shops as $sh): ?>
<?php
$initial = strtoupper(mb_substr($sh['name'], 0, 1));
$isOn = (int)$sh['active'] === 1;
$payload = e(json_encode([
'id' => (int)$sh['id'],
'name' => $sh['name'],
'address' => $sh['address'],
'city' => $sh['city'],
'province' => $sh['province'],
'zip_code' => $sh['zip_code'],
'phone' => $sh['phone'],
'email' => $sh['email'],
], JSON_UNESCAPED_UNICODE));
?>
<tr>
<td>
<div class="d-flex align-items-center gap-2">
<?php if (!empty($sh['logo'])): ?>
<img src="../<?= e($sh['logo']) ?>" class="shop-logo" alt="">
<?php else: ?>
<span class="shop-logo"><?= e($initial) ?></span>
<?php endif; ?>
<div>
<div class="fw-bold"><?= e($sh['name']) ?></div>
<div class="muted-sm"><?= e($sh['email'] ?: '—') ?></div>
</div>
</div>
</td>
<td>
<div><?= e($sh['owner_name'] ?: '—') ?></div>
<div class="muted-sm"><?= e($sh['owner_email'] ?: '') ?></div>
</td>
<td><?= e($sh['city']) ?><?= $sh['province'] ? ' (' . e($sh['province']) . ')' : '' ?></td>
<td><?= (int)$sh['staff_count'] ?></td>
<td><?= (int)$sh['appt_count'] ?></td>
<td>
<span class="dot-status <?= $isOn ? 'st-on' : 'st-off' ?>"></span>
<?= $isOn ? 'Attivo' : 'Disattivato' ?>
</td>
<td class="text-end">
<div class="dropdown">
<button class="btn btn-sm btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">Gestisci</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><button class="dropdown-item js-edit-shop" data-bs-toggle="modal" data-bs-target="#shopModal" data-shop='<?= $payload ?>'><i class="bx bx-edit me-1"></i> Modifica</button></li>
<li>
<form method="POST" action="admin_shop_action.php">
<input type="hidden" name="action" value="set_active">
<input type="hidden" name="shop_id" value="<?= (int)$sh['id'] ?>">
<input type="hidden" name="active" value="<?= $isOn ? 0 : 1 ?>">
<input type="hidden" name="ret" value="<?= e($ret) ?>">
<button class="dropdown-item">
<?php if ($isOn): ?><i class="bx bx-block me-1"></i> Disattiva<?php else: ?><i class="bx bx-check-circle me-1"></i> Attiva<?php endif; ?>
</button>
</form>
</li>
<?php /* "Vedi pagina" disattivato finché lo slug non è configurato — vedi TODO */ ?>
<li><span class="dropdown-item disabled text-muted" title="Disponibile quando lo slug sarà configurato"><i class="bx bx-link-external me-1"></i> Vedi pagina (presto)</span></li>
<li>
<hr class="dropdown-divider">
</li>
<li>
<form method="POST" action="admin_shop_action.php" onsubmit="return confirm('Eliminare questo salone e TUTTI i suoi dati (staff, servizi, appuntamenti)? L\'azione è irreversibile.');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="shop_id" value="<?= (int)$sh['id'] ?>">
<input type="hidden" name="ret" value="<?= e($ret) ?>">
<button class="dropdown-item text-danger"><i class="bx bx-trash me-1"></i> Elimina</button>
</form>
</li>
</ul>
</div>
</td>
</tr>
<?php endforeach;
endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- Modale modifica salone -->
<div class="modal fade" id="shopModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<form method="POST" action="admin_shop_action.php" class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modifica salone</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="shop_id" id="msId">
<input type="hidden" name="ret" value="<?= e($ret) ?>">
<div class="mb-2">
<label class="form-label">Nome *</label>
<input type="text" name="name" id="msName" class="form-control" required>
</div>
<div class="mb-2">
<label class="form-label">Indirizzo *</label>
<input type="text" name="address" id="msAddress" class="form-control" required>
</div>
<div class="row g-2">
<div class="col-6 mb-2">
<label class="form-label">Città *</label>
<input type="text" name="city" id="msCity" class="form-control" required>
</div>
<div class="col-3 mb-2">
<label class="form-label">Prov.</label>
<input type="text" name="province" id="msProvince" class="form-control" maxlength="50">
</div>
<div class="col-3 mb-2">
<label class="form-label">CAP *</label>
<input type="text" name="zip_code" id="msZip" class="form-control" required maxlength="10">
</div>
</div>
<div class="row g-2">
<div class="col-6 mb-2">
<label class="form-label">Telefono</label>
<input type="text" name="phone" id="msPhone" class="form-control">
</div>
<div class="col-6 mb-2">
<label class="form-label">Email</label>
<input type="email" name="email" id="msEmail" class="form-control">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button class="btn btn-primary">Salva</button>
</div>
</form>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
var shopModal = document.getElementById('shopModal');
if (shopModal) shopModal.addEventListener('show.bs.modal', function(ev) {
var b = ev.relatedTarget;
if (!b || !b.classList.contains('js-edit-shop')) return;
var d;
try {
d = JSON.parse(b.getAttribute('data-shop'));
} catch (e) {
return;
}
document.getElementById('msId').value = d.id;
document.getElementById('msName').value = d.name || '';
document.getElementById('msAddress').value = d.address || '';
document.getElementById('msCity').value = d.city || '';
document.getElementById('msProvince').value = d.province || '';
document.getElementById('msZip').value = d.zip_code || '';
document.getElementById('msPhone').value = d.phone || '';
document.getElementById('msEmail').value = d.email || '';
});
</script>
</body>
</html>