owner and admin section

This commit is contained in:
2026-09-09 08:26:26 +02:00
parent de46a91aa8
commit 85702c3000
30 changed files with 5611 additions and 674 deletions
+363 -468
View File
@@ -1,233 +1,98 @@
<?php
// admin_subscriptions.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');
// DB
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// ---- Auth check ----
if (!isset($iduserlogin)) {
die("Access denied.");
header("Location: login.php");
exit;
}
if ((int)$kindofrole !== ROLE_ADMIN) {
header("Location: index.php");
exit;
}
// ---- Admin check (Vanguard usually uses role_id=1 for admin) ----
// Adjust role_id list if needed.
$stmt = $pdo->prepare("SELECT role_id, email FROM auth_users WHERE id = ? LIMIT 1");
$stmt->execute([$iduserlogin]);
$me = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$me || !in_array((int)$me['role_id'], [1])) {
die("Access denied: admin only.");
}
// ---- Handle POST actions ----
$success_message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
// Update school status (local app status)
if ($action === 'update_school_status') {
$school_id = (int)($_POST['school_id'] ?? 0);
$new_status = $_POST['status'] ?? 'active';
$allowed = ['active', 'inactive', 'suspended'];
if ($school_id <= 0 || !in_array($new_status, $allowed, true)) {
$error = "Invalid request.";
} else {
$stmt = $pdo->prepare("UPDATE schools SET status = ? WHERE id = ?");
if ($stmt->execute([$new_status, $school_id])) {
$success_message = "School status updated.";
} else {
$error = "Failed updating school status.";
}
}
}
// NOTE: Stripe actions (cancel, resume, change plan) should call Stripe API.
// Here we provide placeholders so UI is ready.
if ($action === 'flag_cancel_at_period_end') {
$sub_id = (int)($_POST['subscription_row_id'] ?? 0);
$flag = (int)($_POST['flag'] ?? 0);
$school_id = (int)($_POST['school_id'] ?? 0);
if ($sub_id <= 0 || $school_id <= 0 || !in_array($flag, [0, 1], true)) {
$error = "Invalid request.";
} else {
$stmt = $pdo->prepare("
UPDATE school_subscriptions
SET cancel_at_period_end = ?
WHERE id = ? AND school_id = ?
");
$ok = $stmt->execute([$flag, $sub_id, $school_id]);
if ($ok) {
$success_message = $flag ? "Marked cancel at period end (LOCAL)." : "Unmarked cancel at period end (LOCAL).";
} else {
$error = "Failed updating subscription flag.";
}
}
}
}
// ---- Filters (GET) ----
$q = trim($_GET['q'] ?? '');
$plan_id = (int)($_GET['plan_id'] ?? 0);
$sub_status = trim($_GET['sub_status'] ?? ''); // e.g. active, trialing, past_due, canceled, incomplete, unpaid
$school_status = trim($_GET['school_status'] ?? ''); // active, inactive, suspended
$has_sub = $_GET['has_sub'] ?? ''; // '1' or '0' or ''
$where = [];
$params = [];
// Search by school name/email/owner email
if ($q !== '') {
$where[] = "(s.name LIKE ? OR s.email LIKE ? OR ou.email LIKE ? OR CONCAT(ou.first_name,' ',ou.last_name) LIKE ?)";
$like = '%' . $q . '%';
$params[] = $like;
$params[] = $like;
$params[] = $like;
$params[] = $like;
}
if ($plan_id > 0) {
$where[] = "ss.plan_id = ?";
$params[] = $plan_id;
}
if ($sub_status !== '') {
$where[] = "ss.status = ?";
$params[] = $sub_status;
}
if ($school_status !== '') {
$where[] = "s.status = ?";
$params[] = $school_status;
}
if ($has_sub === '1') {
$where[] = "ss.id IS NOT NULL";
} elseif ($has_sub === '0') {
$where[] = "ss.id IS NULL";
}
$sqlWhere = '';
if (!empty($where)) {
$sqlWhere = "WHERE " . implode(" AND ", $where);
}
// ---- Load plans for filter dropdown ----
$stmt = $pdo->prepare("SELECT id, code, name, currency, unit_amount, `interval`, interval_count, is_active FROM billing_plans ORDER BY is_active DESC, unit_amount ASC, name ASC");
$stmt->execute();
$plans = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ---- Main query: schools + subscription + owner + plan ----
$stmt = $pdo->prepare("
SELECT
s.id AS school_id,
s.name AS school_name,
s.email AS school_email,
s.status AS school_status,
s.created_at AS school_created_at,
ou.id AS owner_id,
ou.first_name AS owner_first_name,
ou.last_name AS owner_last_name,
ou.email AS owner_email,
ss.id AS subscription_row_id,
ss.stripe_customer_id,
ss.stripe_subscription_id,
COALESCE(ss.status, 'none') AS subscription_status,
ss.current_period_start,
ss.current_period_end,
ss.trial_start,
ss.trial_end,
ss.cancel_at_period_end,
ss.updated_at AS subscription_updated_at,
bp.id AS plan_id,
bp.code AS plan_code,
bp.name AS plan_name,
bp.currency,
bp.unit_amount,
bp.`interval`,
bp.interval_count
FROM schools s
JOIN auth_users ou ON ou.id = s.owner_id
LEFT JOIN school_subscriptions ss ON ss.school_id = s.id
LEFT JOIN billing_plans bp ON bp.id = ss.plan_id
$sqlWhere
ORDER BY
-- Put schools without subscription at the bottom
CASE WHEN ss.id IS NULL THEN 1 ELSE 0 END ASC,
-- Show problematic subscriptions first
FIELD(ss.status, 'past_due','unpaid','incomplete','incomplete_expired','canceled','paused','trialing','active') ASC,
s.created_at DESC
");
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ---- Helpers ----
function h($v)
function e(?string $v): string
{
return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function moneyFmt($unit_amount, $currency)
function getFlash(): ?array
{
if ($unit_amount === null || $currency === null) return '—';
$amount = ((int)$unit_amount) / 100;
return number_format($amount, 2, ',', '.') . ' ' . strtoupper($currency);
if (!isset($_SESSION['flash'])) return null;
$f = $_SESSION['flash'];
unset($_SESSION['flash']);
return $f;
}
function dateFmt($dt)
// Etichette + badge per stato abbonamento
function subMeta(?string $status): array
{
if (!$dt) return '—';
// Accept both timestamp and datetime strings
$ts = is_numeric($dt) ? (int)$dt : strtotime($dt);
if (!$ts) return '—';
return date('d/m/Y', $ts);
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'];
}
}
function badgeClassForSub($status)
{
$map = [
'active' => 'bg-success',
'trialing' => 'bg-info',
'past_due' => 'bg-warning',
'unpaid' => 'bg-danger',
'incomplete' => 'bg-warning',
'incomplete_expired' => 'bg-danger',
'canceled' => 'bg-secondary',
'paused' => 'bg-secondary',
'none' => 'bg-secondary',
];
return $map[$status] ?? 'bg-dark';
}
// --- Filtri ---
$q_search = trim($_GET['q'] ?? '');
$q_status = trim($_GET['status'] ?? '');
function badgeClassForSchool($status)
{
$map = [
'active' => 'bg-success',
'inactive' => 'bg-secondary',
'suspended' => 'bg-danger',
];
return $map[$status] ?? 'bg-dark';
$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">
@@ -238,6 +103,59 @@ function badgeClassForSchool($status)
<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>
@@ -247,298 +165,275 @@ function badgeClassForSchool($status)
<div class="page-wrapper">
<div class="page-content">
<div class="card radius-10 mb-3">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between">
<div>
<h5 class="mb-0">Admin Subscriptions</h5>
<div class="text-muted small">Schools + Stripe subscription status overview</div>
</div>
<div class="d-flex gap-2">
<a href="admin_billing_plans.php" class="btn btn-outline-primary">
<i class="bx bx-list-ul"></i> Billing Plans
</a>
</div>
<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>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo h($success_message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo h($error); ?></div>
<?php endif; ?>
<!-- Filters -->
<div class="card radius-10 mb-4">
<div class="card-body">
<form method="GET" class="row g-2 align-items-end">
<div class="col-md-4">
<label class="form-label">Search</label>
<input type="text" class="form-control" name="q" value="<?php echo h($q); ?>" placeholder="School / email / owner">
<?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">
<label class="form-label">Plan</label>
<select class="form-control" name="plan_id">
<option value="0">All plans</option>
<?php foreach ($plans as $p): ?>
<option value="<?php echo (int)$p['id']; ?>" <?php echo ((int)$p['id'] === $plan_id) ? 'selected' : ''; ?>>
<?php
$label = $p['name'] . ' (' . $p['code'] . ') - ' . moneyFmt($p['unit_amount'], $p['currency']);
$label .= ' / ' . $p['interval_count'] . ' ' . $p['interval'];
if (!(int)$p['is_active']) $label .= ' [inactive]';
echo h($label);
?>
</option>
<?php endforeach; ?>
<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">
<label class="form-label">Subscription status</label>
<select class="form-control" name="sub_status">
<option value="">All</option>
<?php foreach (['none', 'active', 'trialing', 'past_due', 'unpaid', 'incomplete', 'incomplete_expired', 'canceled', 'paused'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($sub_status === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label">School status</label>
<select class="form-control" name="school_status">
<option value="">All</option>
<?php foreach (['active', 'inactive', 'suspended'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($school_status === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-1">
<label class="form-label">Has sub</label>
<select class="form-control" name="has_sub">
<option value="" <?php echo ($has_sub === '') ? 'selected' : ''; ?>>All</option>
<option value="1" <?php echo ($has_sub === '1') ? 'selected' : ''; ?>>Yes</option>
<option value="0" <?php echo ($has_sub === '0') ? 'selected' : ''; ?>>No</option>
</select>
</div>
<div class="col-md-12 d-flex gap-2 mt-2">
<button class="btn btn-primary" type="submit">
<i class="bx bx-filter"></i> Apply
</button>
<a class="btn btn-outline-secondary" href="admin_subscriptions.php">Reset</a>
<div class="col-md-2 d-grid">
<button class="btn btn-primary"><i class="bx bx-search"></i> Filtra</button>
</div>
</form>
</div>
</div>
<!-- Results -->
<div class="card radius-10">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between mb-3">
<div>
<h6 class="mb-0">Schools</h6>
<div class="text-muted small"><?php echo (int)count($rows); ?> rows</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-bordered align-middle mb-0">
<table class="table table-hover align-middle">
<thead>
<tr>
<th style="min-width:260px;">School</th>
<th style="min-width:220px;">Owner</th>
<th style="min-width:190px;">School status</th>
<th style="min-width:160px;">Subscription</th>
<th style="min-width:220px;">Plan</th>
<th style="min-width:200px;">Period</th>
<th style="min-width:200px;">Trial</th>
<th style="min-width:140px;">Cancel at period end</th>
<th style="min-width:260px;">Stripe IDs</th>
<th style="min-width:160px;">Updated</th>
<th style="min-width:180px;">Actions</th>
<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 foreach ($rows as $row): ?>
<?php
$hasSubscription = !empty($row['subscription_row_id']);
$subStatus = $row['subscription_status'] ?? 'none';
?>
<tr>
<td>
<div class="fw-bold"><?php echo h($row['school_name']); ?></div>
<div class="text-muted small">
ID: <?php echo (int)$row['school_id']; ?> · <?php echo h($row['school_email']); ?>
</div>
<div class="text-muted small">
Created: <?php echo h(dateFmt($row['school_created_at'])); ?>
</div>
</td>
<td>
<div class="fw-bold"><?php echo h(trim(($row['owner_first_name'] ?? '') . ' ' . ($row['owner_last_name'] ?? ''))); ?></div>
<div class="text-muted small"><?php echo h($row['owner_email']); ?></div>
</td>
<td>
<form method="POST" class="d-flex gap-2 align-items-center">
<input type="hidden" name="action" value="update_school_status">
<input type="hidden" name="school_id" value="<?php echo (int)$row['school_id']; ?>">
<select name="status" class="form-control form-control-sm" style="min-width:140px;">
<?php foreach (['active', 'inactive', 'suspended'] as $st): ?>
<option value="<?php echo h($st); ?>" <?php echo ($row['school_status'] === $st) ? 'selected' : ''; ?>>
<?php echo h($st); ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="btn btn-sm btn-outline-primary">
Save
</button>
</form>
<div class="mt-2">
<span class="badge <?php echo h(badgeClassForSchool($row['school_status'])); ?>">
<?php echo h($row['school_status']); ?>
</span>
</div>
</td>
<td>
<span class="badge <?php echo h(badgeClassForSub($subStatus)); ?>">
<?php echo h($subStatus); ?>
</span>
<?php if ($hasSubscription): ?>
<div class="text-muted small mt-2">
Qty: <?php echo (int)($row['quantity'] ?? 1); ?>
</div>
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription && !empty($row['plan_id'])): ?>
<div class="fw-bold"><?php echo h($row['plan_name']); ?></div>
<div class="text-muted small">
<?php echo h($row['plan_code']); ?> · <?php echo h(moneyFmt($row['unit_amount'], $row['currency'])); ?>
</div>
<div class="text-muted small">
Every <?php echo (int)$row['interval_count']; ?> <?php echo h($row['interval']); ?>
</div>
<?php else: ?>
—
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
Start: <?php echo h(dateFmt($row['current_period_start'])); ?>
</div>
<div class="text-muted small">
End: <?php echo h(dateFmt($row['current_period_end'])); ?>
</div>
<?php else: ?>
—
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
Start: <?php echo h(dateFmt($row['trial_start'])); ?>
</div>
<div class="text-muted small">
End: <?php echo h(dateFmt($row['trial_end'])); ?>
</div>
<?php else: ?>
—
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<?php if ((int)$row['cancel_at_period_end'] === 1): ?>
<span class="badge bg-warning">Yes</span>
<?php else: ?>
<span class="badge bg-secondary">No</span>
<?php endif; ?>
<?php else: ?>
—
<?php endif; ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<div class="text-muted small">
cust: <?php echo h($row['stripe_customer_id'] ?: '—'); ?>
</div>
<div class="text-muted small">
sub: <?php echo h($row['stripe_subscription_id'] ?: '—'); ?>
</div>
<?php else: ?>
—
<?php endif; ?>
</td>
<td>
<?php echo h(dateFmt($row['subscription_updated_at'])); ?>
</td>
<td>
<?php if ($hasSubscription): ?>
<form method="POST" class="d-inline">
<input type="hidden" name="action" value="flag_cancel_at_period_end">
<input type="hidden" name="subscription_row_id" value="<?php echo (int)$row['subscription_row_id']; ?>">
<input type="hidden" name="school_id" value="<?php echo (int)$row['school_id']; ?>">
<input type="hidden" name="flag" value="<?php echo ((int)$row['cancel_at_period_end'] === 1) ? 0 : 1; ?>">
<?php if ((int)$row['cancel_at_period_end'] === 1): ?>
<button type="submit" class="btn btn-sm btn-outline-success">
Unmark
</button>
<?php else: ?>
<button type="submit" class="btn btn-sm btn-outline-warning">
Mark
</button>
<?php endif; ?>
</form>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($rows)): ?>
<tr>
<td colspan="11" class="text-center text-muted py-4">No results</td>
<td colspan="6" class="text-center text-muted py-4">Nessun salone trovato.</td>
</tr>
<?php endif; ?>
<?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>
</div> <!-- /page-content -->
</div> <!-- /page-wrapper -->
<?php include('include/footer.php'); ?>
</div>
<?php if (file_exists('include/footer.php')) include('include/footer.php'); ?>
</div> <!-- /wrapper -->
<!-- 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>
<?php if (file_exists('jsinclude.php')) include('jsinclude.php'); ?>
<!-- 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>