439 lines
22 KiB
PHP
439 lines
22 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;
|
|
}
|
|
|
|
// Etichette + badge per stato abbonamento
|
|
function subMeta(?string $status): array
|
|
{
|
|
switch ($status) {
|
|
case 'active':
|
|
return ['badge' => 'bg-success', 'label' => 'Attivo'];
|
|
case 'trialing':
|
|
return ['badge' => 'bg-info text-dark', 'label' => 'In prova'];
|
|
case 'expired':
|
|
return ['badge' => 'bg-warning text-dark', 'label' => 'Scaduto'];
|
|
case 'blocked':
|
|
return ['badge' => 'bg-danger', 'label' => 'Bloccato'];
|
|
case 'pending':
|
|
return ['badge' => 'bg-secondary', 'label' => 'In attesa'];
|
|
default:
|
|
return ['badge' => 'bg-light text-dark', 'label' => 'Nessuno'];
|
|
}
|
|
}
|
|
|
|
// --- Filtri ---
|
|
$q_search = trim($_GET['q'] ?? '');
|
|
$q_status = trim($_GET['status'] ?? '');
|
|
|
|
$where = ["1=1"];
|
|
$params = [];
|
|
if ($q_search !== '') {
|
|
$where[] = "(sh.name LIKE ? OR sh.city LIKE ?)";
|
|
$like = '%' . $q_search . '%';
|
|
array_push($params, $like, $like);
|
|
}
|
|
if ($q_status !== '') {
|
|
if ($q_status === 'none') {
|
|
$where[] = "sub.id IS NULL";
|
|
} else {
|
|
$where[] = "sub.status = ?";
|
|
$params[] = $q_status;
|
|
}
|
|
}
|
|
$whereSql = implode(' AND ', $where);
|
|
|
|
// Tutti i saloni + eventuale abbonamento (LEFT JOIN: mostra anche chi non ne ha)
|
|
$sql = "
|
|
SELECT sh.id AS shop_id, sh.name AS shop_name, sh.city, sh.active AS shop_active,
|
|
sub.id AS sub_id, sub.plan_id, sub.status, sub.started_at, sub.expires_at, sub.block_reason,
|
|
p.name AS plan_name,
|
|
CONCAT(u.first_name, ' ', u.last_name) AS owner_name
|
|
FROM shops sh
|
|
LEFT JOIN shop_subscriptions sub ON sub.shop_id = sh.id
|
|
LEFT JOIN subscription_plans p ON sub.plan_id = p.id
|
|
LEFT JOIN auth_users u ON sh.owner_id = u.id
|
|
WHERE $whereSql
|
|
ORDER BY sh.name ASC
|
|
";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Piani per le select
|
|
$plans = $pdo->query("SELECT id, name, price, billing_period FROM subscription_plans WHERE is_active = 1 ORDER BY price ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$flash = getFlash();
|
|
$ret = http_build_query(array_filter(['q' => $q_search, 'status' => $q_status]));
|
|
$today = date('Y-m-d');
|
|
?>
|
|
<!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>Abbonamenti 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;
|
|
}
|
|
|
|
.table> :not(caption)>*>* {
|
|
padding: .85rem .75rem;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.muted-sm {
|
|
color: var(--muted);
|
|
font-size: .85rem;
|
|
}
|
|
|
|
.exp-soon {
|
|
color: #b45309;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.exp-past {
|
|
color: #dc2626;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.shop-off {
|
|
opacity: .6;
|
|
}
|
|
</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-credit-card me-1"></i> Abbonamenti Saloni</div>
|
|
<span class="text-muted small"><?= count($rows) ?> 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 salone o città…" value="<?= e($q_search) ?>">
|
|
</div>
|
|
<div class="col-md-3">
|
|
<select name="status" class="form-select">
|
|
<option value="">Tutti gli stati</option>
|
|
<option value="active" <?= $q_status === 'active' ? 'selected' : '' ?>>Attivi</option>
|
|
<option value="trialing" <?= $q_status === 'trialing' ? 'selected' : '' ?>>In prova</option>
|
|
<option value="expired" <?= $q_status === 'expired' ? 'selected' : '' ?>>Scaduti</option>
|
|
<option value="blocked" <?= $q_status === 'blocked' ? 'selected' : '' ?>>Bloccati</option>
|
|
<option value="pending" <?= $q_status === 'pending' ? 'selected' : '' ?>>In attesa</option>
|
|
<option value="none" <?= $q_status === 'none' ? 'selected' : '' ?>>Senza abbonamento</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>Piano</th>
|
|
<th>Stato</th>
|
|
<th>Scadenza</th>
|
|
<th>Salone attivo?</th>
|
|
<th class="text-end">Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($rows)): ?>
|
|
<tr>
|
|
<td colspan="6" class="text-center text-muted py-4">Nessun salone trovato.</td>
|
|
</tr>
|
|
<?php else: foreach ($rows as $r): ?>
|
|
<?php
|
|
$meta = subMeta($r['status']);
|
|
$expires = $r['expires_at'];
|
|
$expClass = '';
|
|
if ($expires) {
|
|
if ($expires < $today) $expClass = 'exp-past';
|
|
elseif ($expires <= date('Y-m-d', strtotime('+7 days'))) $expClass = 'exp-soon';
|
|
}
|
|
$payload = e(json_encode([
|
|
'shop_id' => (int)$r['shop_id'],
|
|
'shop_name' => $r['shop_name'],
|
|
'plan_id' => $r['plan_id'] ? (int)$r['plan_id'] : 0,
|
|
'status' => $r['status'] ?: 'active',
|
|
'started_at' => $r['started_at'] ?: '',
|
|
'expires_at' => $r['expires_at'] ?: '',
|
|
'has_sub' => $r['sub_id'] ? 1 : 0,
|
|
'block_reason' => $r['block_reason'] ?: '',
|
|
], JSON_UNESCAPED_UNICODE));
|
|
?>
|
|
<tr class="<?= (int)$r['shop_active'] === 0 ? 'shop-off' : '' ?>">
|
|
<td>
|
|
<div class="fw-bold"><?= e($r['shop_name']) ?></div>
|
|
<div class="muted-sm"><?= e($r['city']) ?> · <?= e($r['owner_name'] ?: '—') ?></div>
|
|
</td>
|
|
<td><?= $r['plan_name'] ? e($r['plan_name']) : '<span class="text-muted">—</span>' ?></td>
|
|
<td>
|
|
<span class="badge <?= $meta['badge'] ?>"><?= $meta['label'] ?></span>
|
|
<?php if ($r['status'] === 'blocked' && $r['block_reason']): ?>
|
|
<i class="bx bx-info-circle text-danger" title="<?= e($r['block_reason']) ?>"></i>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<?php if ($expires): ?>
|
|
<span class="<?= $expClass ?>"><?= date('d/m/Y', strtotime($expires)) ?></span>
|
|
<?php else: ?>
|
|
<span class="text-muted">—</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?= (int)$r['shop_active'] === 1 ? '<span class="text-success fw-bold">Sì</span>' : '<span class="text-muted">No</span>' ?></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-manage" data-bs-toggle="modal" data-bs-target="#subModal" data-sub='<?= $payload ?>'>
|
|
<i class="bx bx-edit me-1"></i> <?= $r['sub_id'] ? 'Modifica abbonamento' : 'Assegna abbonamento' ?>
|
|
</button></li>
|
|
<?php if ($r['sub_id']): ?>
|
|
<li><button class="dropdown-item js-gift" data-bs-toggle="modal" data-bs-target="#giftModal" data-shop="<?= (int)$r['shop_id'] ?>" data-name="<?= e($r['shop_name']) ?>">
|
|
<i class="bx bx-gift me-1"></i> Regala mesi
|
|
</button></li>
|
|
<?php if ($r['status'] === 'blocked'): ?>
|
|
<li>
|
|
<form method="POST" action="admin_subscription_action.php">
|
|
<input type="hidden" name="action" value="unblock">
|
|
<input type="hidden" name="shop_id" value="<?= (int)$r['shop_id'] ?>">
|
|
<input type="hidden" name="ret" value="<?= e($ret) ?>">
|
|
<button class="dropdown-item text-success"><i class="bx bx-check-shield me-1"></i> Sblocca</button>
|
|
</form>
|
|
</li>
|
|
<?php else: ?>
|
|
<li><button class="dropdown-item text-danger js-block" data-bs-toggle="modal" data-bs-target="#blockModal" data-shop="<?= (int)$r['shop_id'] ?>" data-name="<?= e($r['shop_name']) ?>">
|
|
<i class="bx bx-block me-1"></i> Blocca (violazione)
|
|
</button></li>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</ul>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach;
|
|
endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p class="muted-sm mt-2"><i class="bx bx-info-circle me-1"></i>I saloni scaduti vengono spenti automaticamente dallo script giornaliero. Qui puoi intervenire manualmente in qualsiasi momento.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
|
|
<!-- Modale assegna/modifica abbonamento -->
|
|
<div class="modal fade" id="subModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<form method="POST" action="admin_subscription_action.php" class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Abbonamento — <span id="smShopName"></span></h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" value="save">
|
|
<input type="hidden" name="shop_id" id="smShopId">
|
|
<input type="hidden" name="ret" value="<?= e($ret) ?>">
|
|
<div class="mb-2">
|
|
<label class="form-label">Piano</label>
|
|
<select name="plan_id" id="smPlan" class="form-select">
|
|
<option value="0">Nessuno (omaggio)</option>
|
|
<?php foreach ($plans as $p): ?>
|
|
<option value="<?= (int)$p['id'] ?>"><?= e($p['name']) ?> — €<?= number_format($p['price'], 2, ',', '.') ?>/<?= $p['billing_period'] === 'monthly' ? 'mese' : 'anno' ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-2">
|
|
<label class="form-label">Stato</label>
|
|
<select name="status" id="smStatus" class="form-select">
|
|
<option value="active">Attivo</option>
|
|
<option value="trialing">In prova</option>
|
|
<option value="pending">In attesa</option>
|
|
<option value="expired">Scaduto</option>
|
|
</select>
|
|
</div>
|
|
<div class="row g-2">
|
|
<div class="col-6 mb-2">
|
|
<label class="form-label">Inizio</label>
|
|
<input type="date" name="started_at" id="smStarted" class="form-control">
|
|
</div>
|
|
<div class="col-6 mb-2">
|
|
<label class="form-label">Scadenza</label>
|
|
<input type="date" name="expires_at" id="smExpires" class="form-control">
|
|
</div>
|
|
</div>
|
|
<p class="muted-sm mb-0"><i class="bx bx-info-circle me-1"></i>Salvando, il salone viene acceso o spento in base allo stato e alla scadenza.</p>
|
|
</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>
|
|
|
|
<!-- Modale regala mesi -->
|
|
<div class="modal fade" id="giftModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<form method="POST" action="admin_subscription_action.php" class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Regala mesi</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" value="gift_months">
|
|
<input type="hidden" name="shop_id" id="gmShopId">
|
|
<input type="hidden" name="ret" value="<?= e($ret) ?>">
|
|
<p class="text-muted">Estende la scadenza di <strong id="gmShopName"></strong> e riattiva l'abbonamento.</p>
|
|
<label class="form-label">Mesi da regalare</label>
|
|
<input type="number" name="months" class="form-control" min="1" max="24" value="1" required>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
|
|
<button class="btn btn-primary">Regala</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modale blocca -->
|
|
<div class="modal fade" id="blockModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<form method="POST" action="admin_subscription_action.php" class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Blocca salone</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" value="block">
|
|
<input type="hidden" name="shop_id" id="bmShopId">
|
|
<input type="hidden" name="ret" value="<?= e($ret) ?>">
|
|
<p class="text-muted">Il salone <strong id="bmShopName"></strong> verrà spento e reso invisibile finché non lo sblocchi.</p>
|
|
<label class="form-label">Motivo (violazione)</label>
|
|
<input type="text" name="block_reason" class="form-control" placeholder="Es. Contenuti non conformi" maxlength="255">
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
|
|
<button class="btn btn-danger">Blocca</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include('jsinclude.php'); ?>
|
|
<script>
|
|
var subModal = document.getElementById('subModal');
|
|
if (subModal) subModal.addEventListener('show.bs.modal', function(ev) {
|
|
var b = ev.relatedTarget;
|
|
if (!b || !b.classList.contains('js-manage')) return;
|
|
var d;
|
|
try {
|
|
d = JSON.parse(b.getAttribute('data-sub'));
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
document.getElementById('smShopId').value = d.shop_id;
|
|
document.getElementById('smShopName').textContent = d.shop_name;
|
|
document.getElementById('smPlan').value = d.plan_id || 0;
|
|
document.getElementById('smStatus').value = d.status || 'active';
|
|
document.getElementById('smStarted').value = d.started_at || '';
|
|
document.getElementById('smExpires').value = d.expires_at || '';
|
|
});
|
|
|
|
var giftModal = document.getElementById('giftModal');
|
|
if (giftModal) giftModal.addEventListener('show.bs.modal', function(ev) {
|
|
var b = ev.relatedTarget;
|
|
document.getElementById('gmShopId').value = b.getAttribute('data-shop');
|
|
document.getElementById('gmShopName').textContent = b.getAttribute('data-name');
|
|
});
|
|
|
|
var blockModal = document.getElementById('blockModal');
|
|
if (blockModal) blockModal.addEventListener('show.bs.modal', function(ev) {
|
|
var b = ev.relatedTarget;
|
|
document.getElementById('bmShopId').value = b.getAttribute('data-shop');
|
|
document.getElementById('bmShopName').textContent = b.getAttribute('data-name');
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|