Files
imf-sms-dashboard/web/index.html
T

395 lines
17 KiB
HTML

<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Home — IMFestival SMS Dashboard</title>
<link href="assets/vendor/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet"
crossorigin="anonymous">
<link href="assets/css/dashboard.css" rel="stylesheet">
</head>
<body>
<!-- ─── Mobile top bar ─── -->
<div class="topbar">
<span style="font-weight:600;font-size:0.95rem;">SMS Dashboard</span>
<span class="nav-username ms-auto" style="font-size:0.82rem;color:var(--text-muted);">User</span>
</div>
<!-- ─── Desktop sidebar ─── -->
<div class="sidebar">
<div class="brand">
<span style="margin: 0 auto;">IMFestival SMS Dashboard</span>
</div>
<nav class="d-flex flex-column gap-1 mt-2">
<a href="index.html" class="nav-link-custom active">
<i class="fa-solid fa-house fa-fw"></i>
Home
</a>
<a href="attendees.html" class="nav-link-custom">
<i class="fa-solid fa-users fa-fw"></i>
Attendees
</a>
<a href="templates.html" class="nav-link-custom">
<i class="fa-solid fa-comment-sms fa-fw"></i>
SMS Templates
</a>
<a href="schedule.html" class="nav-link-custom">
<i class="fa-solid fa-calendar-days fa-fw"></i>
Schedule
</a>
<a href="delivery-log.html" class="nav-link-custom">
<i class="fa-solid fa-list-check fa-fw"></i>
Delivery Log
</a>
</nav>
<div class="mt-auto pt-3" style="border-top:1px solid var(--border);">
<div class="nav-link-custom" style="cursor:default;">
<i class="fa-solid fa-circle-user fa-fw"></i>
<span class="nav-username" style="font-size:0.82rem;">User</span>
</div>
</div>
</div>
<!-- ─── Main content ─── -->
<div class="main">
<div class="main-inner">
<div id="sandboxBanner" class="alert alert-warning"
style="display:none;font-size:0.85rem;font-weight:600;text-align:center;">
<i class="fa-solid fa-flask me-1"></i> Sandbox mode is active — texts are tagged as a test and excluded from
cost calculations.
</div>
<div class="page-header">
<h1>Overview</h1>
<p>Welcome back. Here's what's happening today.</p>
</div>
<!-- Stats -->
<div class="row g-3 mb-4">
<div class="col-6 col-sm-3">
<div class="stat-card border-0">
<div class="stat-label">Attendees</div>
<div class="stat-value" id="stat-attendees"></div>
</div>
</div>
<div class="col-6 col-sm-3">
<div class="stat-card border-0">
<div class="stat-label">Total Sent</div>
<div class="stat-value" id="stat-sent"></div>
</div>
</div>
<div class="col-6 col-sm-3">
<div class="stat-card border-0">
<div class="stat-label">Success Rate</div>
<div class="stat-value" id="stat-rate"></div>
</div>
</div>
<div class="col-6 col-sm-3">
<div class="stat-card border-0" id="spendCard" role="button" tabindex="0" style="cursor:pointer;">
<div class="stat-label" id="stat-spend-label">Balance</div>
<div class="stat-value" id="stat-spend"></div>
</div>
</div>
</div>
<!-- Quick links -->
<div class="row g-3">
<!-- Send SMS Now — primary action card, full width -->
<div class="col-12">
<a href="send-now.html" class="card p-4 text-decoration-none d-flex flex-row align-items-center gap-3"
style="border-color:var(--accent);background:linear-gradient(135deg,#1a2540 0%,var(--bg-card) 100%);">
<i class="fa-solid fa-paper-plane fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Send SMS Now</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Compose and send a one-off message instantly</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<div class="col-12 col-sm-6">
<a href="attendees.html"
class="card p-4 border-0 text-decoration-none d-flex flex-row align-items-center gap-3">
<i class="fa-solid fa-users fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Attendees</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Upload and manage participant CSV</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<div class="col-12 col-sm-6">
<a href="templates.html"
class="card p-4 border-0 text-decoration-none d-flex flex-row align-items-center gap-3">
<i class="fa-solid fa-comment-sms fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">SMS Templates</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Create and preview message templates</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<div class="col-12 col-sm-6">
<a href="schedule.html"
class="card p-4 border-0 text-decoration-none d-flex flex-row align-items-center gap-3">
<i class="fa-solid fa-calendar-days fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Schedule</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Set send rules and timing</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<div class="col-12 col-sm-6">
<a href="delivery-log.html"
class="card p-4 border-0 text-decoration-none d-flex flex-row align-items-center gap-3">
<i class="fa-solid fa-list-check fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Delivery Log</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Track sent, delivered and failed messages</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<!-- Start Admin Settings -->
<div id="adminSection" class="d-none col-12">
<hr class="mt-4 mb-3">
<div class="page-header mt-0 mb-4">
<p><em>You're seeing these options because you're an admin. Proceed with caution!</em></p>
</div>
<div class="row g-3">
<div class="col-12 col-sm-6">
<a href="admin.html" class="card p-4 border-0 text-decoration-none d-flex flex-row align-items-center gap-3">
<i class="fa-solid fa-user-shield fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Admin</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Sandbox mode and data management controls</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</a>
</div>
<div class="col-12 col-sm-6">
<button type="button" onclick="openApiKeyModal()" data-bs-toggle="modal" data-bs-target="#apiKeyModal"
class="card p-4 text-decoration-none d-flex flex-row align-items-center gap-3 w-100 border-0 text-start"
style="cursor:pointer;">
<i class="fa-solid fa-cog fa-lg" style="color:var(--accent);"></i>
<div>
<div style="font-weight:600;color:var(--text-primary);">Clickatell Settings</div>
<div style="font-size:0.82rem;color:var(--text-muted);">Change Clickatell API key and cost per SMS</div>
</div>
<i class="fa-solid fa-chevron-right ms-auto" style="color:var(--text-muted);font-size:0.8rem;"></i>
</button>
</div>
</div><!-- /.row (admin cards) -->
</div><!-- /#adminSection -->
</div>
</div>
</div>
<!-- ─── Clickatell API Key modal ─── -->
<div class="modal fade" id="apiKeyModal" tabindex="-1" aria-labelledby="apiKeyModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="apiKeyModalLabel">
<i class="fa-solid fa-gear fa-fw me-2"></i>Clickatell Settings
</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p id="apiKeyStatus" class="text-muted mb-3" style="font-size:0.85rem;">Checking…</p>
<div class="mb-3">
<label for="apiKeyInput" class="form-label">API Key</label>
<input type="password" class="form-control" id="apiKeyInput" placeholder="Enter new API key"
autocomplete="off">
</div>
<div class="mb-1">
<label for="costPerSmsInput" class="form-label">Cost per SMS (£)</label>
<input type="number" class="form-control" id="costPerSmsInput" placeholder="e.g. 0.04849" step="0.00001"
min="0">
</div>
<div id="apiKeyError" class="text-danger mt-2" style="font-size:0.82rem;"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="apiKeySaveBtn" onclick="saveApiKey()">
<i class="fa-solid fa-floppy-disk fa-fw"></i> Save
</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
crossorigin="anonymous"></script>
<script src="assets/js/nav.js"></script>
<script>
function esc(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
async function openApiKeyModal() {
document.getElementById('apiKeyInput').value = '';
document.getElementById('apiKeyError').textContent = '';
const statusEl = document.getElementById('apiKeyStatus');
statusEl.textContent = 'Checking…';
try {
const [keyData, costData] = await Promise.all([
fetch('/api/settings.php?key=clickatell_api_key').then(r => r.json()),
fetch('/api/settings.php?key=clickatell_cost_per_sms').then(r => r.json()),
]);
statusEl.innerHTML = keyData.set
? `API key set: <code>${esc(keyData.hint)}</code> — enter a new key to replace it.`
: '<span class="text-warning">No API key configured.</span> Enter one below to enable SMS sending.';
document.getElementById('costPerSmsInput').value = costData.value ?? '0.04849';
} catch {
statusEl.textContent = 'Could not load current settings.';
}
}
async function saveApiKey() {
const key = document.getElementById('apiKeyInput').value.trim();
const cost = document.getElementById('costPerSmsInput').value.trim();
const errEl = document.getElementById('apiKeyError');
const btn = document.getElementById('apiKeySaveBtn');
errEl.textContent = '';
if (!key && !cost) { errEl.textContent = 'Please enter at least one value.'; return; }
const costNum = parseFloat(cost);
if (cost && (isNaN(costNum) || costNum < 0)) { errEl.textContent = 'Cost must be a positive number.'; return; }
btn.disabled = true;
try {
const saves = [];
if (key) saves.push(fetch('/api/settings.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ key: 'clickatell_api_key', value: key }) }));
if (cost) saves.push(fetch('/api/settings.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ key: 'clickatell_cost_per_sms', value: cost }) }));
const results = await Promise.all(saves);
for (const res of results) { if (!res.ok) throw new Error((await res.json()).error ?? 'Save failed'); }
bootstrap.Modal.getInstance(document.getElementById('apiKeyModal')).hide();
loadStats();
} catch (e) {
errEl.textContent = e.message;
} finally {
btn.disabled = false;
}
}
let spendView = 'balance';
let spendData = { balance: null, currency: null, estimatedCost: null };
function formatCurrency(amount, currency) {
if (amount == null || Number.isNaN(Number(amount))) return '—';
const sym = currency === 'GBP' ? '£' : currency === 'USD' ? '$' : currency === 'EUR' ? '€' : (currency ?? '');
return sym + Number(amount).toFixed(2);
}
function renderSpendCard() {
const labelEl = document.getElementById('stat-spend-label');
const valueEl = document.getElementById('stat-spend');
if (!labelEl || !valueEl) return;
if (spendView === 'cost') {
labelEl.textContent = 'EST. COST';
valueEl.textContent = formatCurrency(spendData.estimatedCost, spendData.currency);
return;
}
labelEl.textContent = 'Balance';
valueEl.textContent = formatCurrency(spendData.balance, spendData.currency);
}
function toggleSpendCard() {
spendView = spendView === 'balance' ? 'cost' : 'balance';
renderSpendCard();
}
async function loadStats() {
try {
const [att, log, bal, cost, admin] = await Promise.all([
fetch('/api/attendees.php').then(r => r.json()),
fetch('/api/delivery-log.php').then(r => r.json()),
fetch('/api/balance.php').then(r => r.json()),
fetch('/api/settings.php?key=clickatell_cost_per_sms').then(r => r.json()),
fetch('/api/admin.php').then(r => r.json()),
]);
document.getElementById('stat-attendees').textContent = att.total ?? '—';
document.getElementById('stat-sent').textContent = log.stats?.total_sent ?? '—';
const rate = log.stats?.success_rate;
document.getElementById('stat-rate').textContent = rate != null ? rate + '%' : '—';
spendData.balance = bal.balance != null ? Number(bal.balance) : null;
spendData.currency = bal.currency ?? 'GBP';
const realSent = Number(admin.delivery_log_real_sent);
const costPerSms = Number(cost.value);
spendData.estimatedCost = Number.isFinite(realSent) && Number.isFinite(costPerSms)
? realSent * costPerSms
: null;
renderSpendCard();
} catch { /* DB unavailable — stats stay as dashes */ }
}
document.getElementById('spendCard')?.addEventListener('click', toggleSpendCard);
document.getElementById('spendCard')?.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleSpendCard();
}
});
loadStats();
setInterval(() => loadStats(), 10_000);
</script>
<script>
async function checkAuth() {
try {
const r = await fetch('/api/admin.php', { redirect: 'manual' });
if (r.type === 'opaqueredirect' || !r.ok) { window.location.reload(); return; }
const d = await r.json();
document.getElementById('sandboxBanner').style.display = d.sandbox_mode ? '' : 'none';
document.querySelectorAll('.admin-nav-link').forEach(el => el.classList.toggle('d-none', !d.is_admin));
const adminSection = document.getElementById('adminSection');
if (adminSection) adminSection.classList.toggle('d-none', !d.is_admin);
const name = d.name || d.username;
if (name) {
document.querySelectorAll('.nav-username').forEach(el => { el.textContent = name; });
}
} catch { }
}
checkAuth();
document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') checkAuth(); });
</script>
<nav class="bottom-nav" aria-label="Navigation">
<a href="index.html" class="bottom-nav-item active">
<i class="fa-solid fa-house"></i>
<span>Home</span>
</a>
<a href="attendees.html" class="bottom-nav-item">
<i class="fa-solid fa-users"></i>
<span>Attendees</span>
</a>
<a href="templates.html" class="bottom-nav-item">
<i class="fa-solid fa-comment-sms"></i>
<span>Templates</span>
</a>
<a href="schedule.html" class="bottom-nav-item">
<i class="fa-solid fa-calendar-days"></i>
<span>Schedule</span>
</a>
<a href="delivery-log.html" class="bottom-nav-item">
<i class="fa-solid fa-list-check"></i>
<span>Log</span>
</a>
<a href="admin.html" class="bottom-nav-item admin-nav-link d-none">
<i class="fa-solid fa-user-shield"></i>
<span>Admin</span>
</a>
</nav>
</body>
</html>