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

300 lines
14 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;
}
// Piani + quanti saloni li usano (per decidere se eliminabili)
$plans = $pdo->query("
SELECT p.id, p.name, p.description, p.price, p.billing_period, p.is_active,
(SELECT COUNT(*) FROM shop_subscriptions s WHERE s.plan_id = p.id) AS in_use
FROM subscription_plans p
ORDER BY p.price ASC
")->fetchAll(PDO::FETCH_ASSOC);
$flash = getFlash();
?>
<!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>Piani Abbonamento</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;
}
.plan-price {
font-weight: 900;
font-size: 1.05rem;
}
.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-package me-1"></i> Piani Abbonamento</div>
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#planModal" id="btnNewPlan">
<i class="bx bx-plus"></i> Nuovo piano
</button>
</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; ?>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead>
<tr>
<th>Piano</th>
<th>Prezzo</th>
<th>Fatturazione</th>
<th>In uso</th>
<th>Stato</th>
<th class="text-end">Azioni</th>
</tr>
</thead>
<tbody>
<?php if (empty($plans)): ?>
<tr>
<td colspan="6" class="text-center text-muted py-4">Nessun piano. Creane uno con "Nuovo piano".</td>
</tr>
<?php else: foreach ($plans as $p): ?>
<?php
$isOn = (int)$p['is_active'] === 1;
$inUse = (int)$p['in_use'];
$payload = e(json_encode([
'id' => (int)$p['id'],
'name' => $p['name'],
'description' => $p['description'],
'price' => $p['price'],
'billing_period' => $p['billing_period'],
'is_active' => (int)$p['is_active'],
], JSON_UNESCAPED_UNICODE));
?>
<tr>
<td>
<div class="fw-bold"><?= e($p['name']) ?></div>
<?php if ($p['description']): ?><div class="muted-sm"><?= e($p['description']) ?></div><?php endif; ?>
</td>
<td class="plan-price">€<?= number_format((float)$p['price'], 2, ',', '.') ?></td>
<td><?= $p['billing_period'] === 'monthly' ? 'Mensile' : 'Annuale' ?></td>
<td><?= $inUse > 0 ? $inUse . ' salon' . ($inUse === 1 ? 'e' : 'i') : '<span class="text-muted">—</span>' ?></td>
<td>
<span class="dot-status <?= $isOn ? 'st-on' : 'st-off' ?>"></span>
<?= $isOn ? 'Attivo' : 'Disattivato' ?>
</td>
<td class="text-end">
<button class="btn btn-sm btn-outline-secondary js-edit-plan"
data-bs-toggle="modal" data-bs-target="#planModal" data-plan='<?= $payload ?>'>
<i class="bx bx-edit"></i>
</button>
<form method="POST" action="admin_plan_action.php" class="d-inline">
<input type="hidden" name="action" value="toggle_active">
<input type="hidden" name="plan_id" value="<?= (int)$p['id'] ?>">
<input type="hidden" name="is_active" value="<?= $isOn ? 0 : 1 ?>">
<button class="btn btn-sm btn-outline-secondary" title="<?= $isOn ? 'Disattiva' : 'Attiva' ?>">
<i class="bx <?= $isOn ? 'bx-toggle-right' : 'bx-toggle-left' ?>"></i>
</button>
</form>
<form method="POST" action="admin_plan_action.php" class="d-inline"
onsubmit="return confirm('Eliminare questo piano?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="plan_id" value="<?= (int)$p['id'] ?>">
<button class="btn btn-sm btn-outline-danger" <?= $inUse > 0 ? 'disabled title="In uso: disattivalo invece di eliminarlo"' : '' ?>>
<i class="bx bx-trash"></i>
</button>
</form>
</td>
</tr>
<?php endforeach;
endif; ?>
</tbody>
</table>
</div>
<p class="muted-sm mt-2"><i class="bx bx-info-circle me-1"></i>Un piano <strong>in uso</strong> non è eliminabile: disattivalo per non proporlo più ai nuovi saloni senza toccare gli abbonamenti esistenti.</p>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<!-- Modale crea/modifica piano -->
<div class="modal fade" id="planModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<form method="POST" action="admin_plan_action.php" class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="pmTitle">Nuovo piano</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="plan_id" id="pmId" value="0">
<div class="mb-2">
<label class="form-label">Nome *</label>
<input type="text" name="name" id="pmName" class="form-control" required placeholder="Es. Base, Pro, Premium">
</div>
<div class="mb-2">
<label class="form-label">Descrizione</label>
<textarea name="description" id="pmDesc" class="form-control" rows="2" placeholder="Cosa include il piano"></textarea>
</div>
<div class="row g-2">
<div class="col-6 mb-2">
<label class="form-label">Prezzo (€) *</label>
<input type="number" name="price" id="pmPrice" class="form-control" step="0.01" min="0" required value="0.00">
</div>
<div class="col-6 mb-2">
<label class="form-label">Fatturazione</label>
<select name="billing_period" id="pmPeriod" class="form-select">
<option value="monthly">Mensile</option>
<option value="yearly">Annuale</option>
</select>
</div>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="is_active" id="pmActive" value="1" checked>
<label class="form-check-label" for="pmActive">Piano attivo (proponibile ai saloni)</label>
</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 planModal = document.getElementById('planModal');
// Nuovo piano: reset campi
var btnNew = document.getElementById('btnNewPlan');
if (btnNew) btnNew.addEventListener('click', function() {
document.getElementById('pmTitle').textContent = 'Nuovo piano';
document.getElementById('pmId').value = 0;
document.getElementById('pmName').value = '';
document.getElementById('pmDesc').value = '';
document.getElementById('pmPrice').value = '0.00';
document.getElementById('pmPeriod').value = 'monthly';
document.getElementById('pmActive').checked = true;
});
// Modifica: popola dal data-plan
if (planModal) planModal.addEventListener('show.bs.modal', function(ev) {
var b = ev.relatedTarget;
if (!b || !b.classList.contains('js-edit-plan')) return;
var d;
try {
d = JSON.parse(b.getAttribute('data-plan'));
} catch (e) {
return;
}
document.getElementById('pmTitle').textContent = 'Modifica piano';
document.getElementById('pmId').value = d.id;
document.getElementById('pmName').value = d.name || '';
document.getElementById('pmDesc').value = d.description || '';
document.getElementById('pmPrice').value = parseFloat(d.price).toFixed(2);
document.getElementById('pmPeriod').value = d.billing_period || 'monthly';
document.getElementById('pmActive').checked = (parseInt(d.is_active, 10) === 1);
});
</script>
</body>
</html>