Files
vitfit-r2/V2/app/public/create-workout.html
T
2026-06-30 10:15:18 -04:00

334 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Workout — VitFit</title>
<link rel="manifest" href="/manifest.json">
<link rel="stylesheet" href="/styles/base.css">
<link rel="stylesheet" href="/styles/layout.css">
<link rel="stylesheet" href="/styles/components.css">
<style>
.preview-grid { display:grid; gap:3px; background:#000; width:100%; height:100%; }
.preview-grid.panes-1 { grid-template-columns:1fr; grid-template-rows:1fr; }
.preview-grid.panes-2 { grid-template-columns:1fr 1fr; grid-template-rows:1fr; }
.preview-grid.panes-3,
.preview-grid.panes-4 { grid-template-columns:1fr 1fr; grid-template-rows:1fr 1fr; }
.preview-pane { position:relative; background:#111; overflow:hidden; min-height:0; }
.preview-pane video { width:100%; height:100%; object-fit:cover; display:block; }
.preview-pane-missing { width:100%; height:100%; display:flex; align-items:center; justify-content:center; color:rgba(255,255,255,0.3); font-size:14px; }
.preview-pane-corner { position:absolute; bottom:8px; right:12px; font-size:clamp(24px,5vw,72px); font-weight:900; color:#fff; text-shadow:0 2px 12px rgba(0,0,0,0.9); line-height:1; }
.preview-empty { display:flex; align-items:center; justify-content:center; height:100%; color:rgba(255,255,255,0.3); font-size:14px; }
</style>
</head>
<body>
<header class="banner">
<button class="banner-hamburger" id="nav-hamburger" aria-label="Menu">&#9776;</button>
<img id="banner-logo" alt="Logo" style="display:none;height:36px;">
<span id="app-name" class="banner-title">VitFit</span>
</header>
<div class="drawer-scrim" id="drawer-scrim"></div>
<nav class="nav-drawer" id="nav-drawer"><ul class="nav-list" id="nav-list"></ul></nav>
<main class="main-content">
<div class="page-header" style="flex-direction:column;align-items:center;text-align:center;">
<h1 class="page-title" style="margin:0;">Create Workout</h1>
<div class="flex gap-3" style="margin-top:var(--space-3);">
<a href="/workouts.html" class="btn btn-ghost">Back</a>
<button class="btn btn-primary" id="save-btn">Save Workout</button>
</div>
</div>
<!-- Meta -->
<div class="card" style="margin-bottom:var(--space-6);">
<div class="card-body" style="display:flex;flex-direction:column;gap:var(--space-4);">
<div class="form-group" style="margin:0;">
<label class="form-label">Workout Name <span class="required">*</span></label>
<input class="form-input" type="text" id="name" maxlength="120" required>
</div>
<div class="form-group" style="margin:0;">
<label class="form-label">Date</label>
<input class="form-input" type="date" id="workout-date">
</div>
<div class="form-group" style="margin:0;">
<label class="form-label">Notes</label>
<textarea class="form-input" id="notes" rows="2" maxlength="2000"></textarea>
</div>
</div>
</div>
<!-- Exercises -->
<div class="card" style="margin-bottom:var(--space-6);">
<div class="card-header">
<h2 class="card-title">Exercises</h2>
<button class="btn btn-secondary btn-sm" id="add-slot-btn">+ Add Exercise</button>
</div>
<div id="exercise-list">
<div class="empty-state"><p class="empty-state-text">No exercises yet. Add up to 4.</p></div>
</div>
<div id="form-error" class="form-error" style="display:none;margin-top:var(--space-4);"></div>
</div>
<!-- TV Preview -->
<div class="card">
<h2 class="card-title" style="margin-bottom:var(--space-4);">TV Preview</h2>
<div style="position:relative;width:100%;padding-top:56.25%;background:#000;border-radius:var(--radius-md);overflow:hidden;">
<div id="preview-grid" class="preview-grid" style="position:absolute;inset:0;">
<div class="preview-empty">Add exercises to see preview</div>
</div>
</div>
</div>
</main>
<!-- Video picker modal -->
<div class="modal-overlay" id="video-modal" style="display:none;">
<div class="modal modal-wide">
<div class="modal-header">
<h2 class="modal-title">Pick a Video</h2>
<button class="modal-close" id="modal-close">&times;</button>
</div>
<div style="padding:var(--space-4) var(--space-6) var(--space-3);display:flex;gap:var(--space-3);">
<input class="form-input" type="search" id="modal-search" placeholder="Search videos…" style="flex:1;">
<select class="form-select" id="modal-category" style="width:180px;">
<option value="">All Categories</option>
</select>
</div>
<div id="modal-video-list" class="modal-scroll modal-video-grid" style="padding:0 var(--space-6) var(--space-6);"></div>
</div>
</div>
<div id="session-expired-overlay" class="session-expired-overlay">
<div class="session-expired-box"><p>Your session has expired.</p><a href="/" class="btn btn-primary">Sign In Again</a></div>
</div>
<script src="/js/core.js"></script>
<script>
const MAX_SLOTS = 4;
let _slots = []; // [{video: {id, description, streamUrl, fileExists}, reps: null}]
let _pickingIdx = null;
function esc(s) { return String(s ?? '').replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;'); }
function renderSlots() {
const el = document.getElementById('exercise-list');
document.getElementById('add-slot-btn').disabled = _slots.length >= MAX_SLOTS;
if (!_slots.length) {
el.innerHTML = '<div class="empty-state"><p class="empty-state-text">No exercises yet. Add up to 4.</p></div>';
renderPreview();
return;
}
el.innerHTML = `<table class="table"><thead><tr><th>#</th><th>Exercise</th><th>Overlay #</th><th></th></tr></thead><tbody>
${_slots.map((s, i) => `<tr>
<td>${i + 1}</td>
<td>
<button class="btn btn-ghost btn-sm" data-action="pick" data-idx="${i}">
${s.video ? esc(s.video.description || 'Untitled') : 'Pick Video'}
</button>
</td>
<td>
<input class="form-input" type="number" min="0" placeholder="—" data-action="reps" data-idx="${i}" value="${s.reps ?? ''}" style="width:72px;">
</td>
<td class="table-actions">
${_slots.length > 1
? `<button class="btn btn-ghost btn-sm" data-action="remove" data-idx="${i}" style="color:var(--color-danger);">✕</button>`
: ''}
</td>
</tr>`).join('')}
</tbody></table>`;
renderPreview();
}
function renderPreview() {
const gridEl = document.getElementById('preview-grid');
const filled = _slots.filter(s => s.video);
const count = Math.min(filled.length, 4);
if (!count) {
gridEl.className = 'preview-grid';
gridEl.innerHTML = '<div class="preview-empty">Add exercises to see preview</div>';
return;
}
const jwt = Auth.getJwt();
const selectedOrg = Auth.getSelectedOrg();
const orgParam = selectedOrg ? `&orgId=${selectedOrg.id}` : '';
gridEl.className = `preview-grid panes-${count}`;
gridEl.innerHTML = filled.slice(0, 4).map(s => {
const src = s.video.streamUrl
? `${s.video.streamUrl}?token=${encodeURIComponent(jwt)}${orgParam}`
: '';
return `<div class="preview-pane">
${src
? `<video src="${src}" preload="none" muted playsinline style="width:100%;height:100%;object-fit:cover;display:block;"></video>`
: `<div class="preview-pane-missing">Missing Video</div>`}
${s.reps != null ? `<div class="preview-pane-corner">${s.reps}</div>` : ''}
</div>`;
}).join('');
}
function openPicker(idx) {
_pickingIdx = idx;
document.getElementById('modal-search').value = '';
document.getElementById('modal-category').value = '';
document.getElementById('video-modal').style.display = 'flex';
loadModalVideos();
}
async function loadModalVideos() {
const el = document.getElementById('modal-video-list');
const search = document.getElementById('modal-search').value.trim();
const category = document.getElementById('modal-category').value;
el.innerHTML = '<p class="text-secondary" style="grid-column:1/-1;padding:var(--space-4);">Loading…</p>';
try {
const jwt = Auth.getJwt();
const selectedOrg = Auth.getSelectedOrg();
const orgParam = selectedOrg ? `&orgId=${selectedOrg.id}` : '';
const params = { limit: 60 };
if (search) params.search = search;
if (category) params.category = category;
const res = await Api.get('/api/v1/videos', params);
if (!res.ok) throw new Error();
const { data } = await res.json();
if (!data.length) {
el.innerHTML = '<p class="text-secondary" style="grid-column:1/-1;padding:var(--space-4);">No videos found.</p>';
return;
}
el.innerHTML = data.map(v => `
<div class="modal-video-card" data-video-id="${v.id}" data-video-desc="${esc(v.description || '')}" data-stream-url="${esc(v.streamUrl || '')}" data-file-exists="${v.fileExists}">
<div class="modal-video-thumb">
${v.fileExists
? `<video preload="metadata" src="${v.streamUrl}?token=${encodeURIComponent(jwt)}${orgParam}" muted></video>`
: `<div class="video-missing-placeholder"><span>Missing</span></div>`}
</div>
<div class="modal-video-info">
<p class="modal-video-name">${esc(v.description || 'Untitled')}</p>
${v.category ? `<span class="badge" style="font-size:10px;">${esc(v.category)}</span>` : ''}
</div>
</div>`).join('');
el.querySelectorAll('video').forEach(vid => {
vid.addEventListener('error', () => {
vid.closest('.modal-video-thumb').innerHTML = '<div class="video-missing-placeholder"><span>Missing</span></div>';
});
});
} catch {
el.innerHTML = '<p class="text-secondary" style="grid-column:1/-1;padding:var(--space-4);">Failed to load videos.</p>';
}
}
document.addEventListener('DOMContentLoaded', async () => {
await Page.init(['Owner', 'Coach']);
document.getElementById('workout-date').value = new Date().toLocaleDateString('en-CA');
// Load categories
try {
const res = await Api.get('/api/v1/categories');
if (res.ok) {
const { data } = await res.json();
const sel = document.getElementById('modal-category');
data.forEach(c => {
const opt = document.createElement('option');
opt.value = c.name; opt.textContent = c.name;
sel.appendChild(opt);
});
}
} catch {}
// Add slot
document.getElementById('add-slot-btn').addEventListener('click', () => {
if (_slots.length >= MAX_SLOTS) return;
_slots.push({ video: null, reps: null });
renderSlots();
openPicker(_slots.length - 1);
});
// Exercise list delegation
document.getElementById('exercise-list').addEventListener('click', (e) => {
const btn = e.target.closest('button[data-action]');
if (!btn) return;
const idx = parseInt(btn.dataset.idx, 10);
if (btn.dataset.action === 'pick') openPicker(idx);
if (btn.dataset.action === 'remove') { _slots.splice(idx, 1); renderSlots(); }
});
document.getElementById('exercise-list').addEventListener('input', (e) => {
const input = e.target.closest('input[data-action="reps"]');
if (!input) return;
const idx = parseInt(input.dataset.idx, 10);
_slots[idx].reps = input.value !== '' ? parseInt(input.value, 10) : null;
renderPreview();
});
// Modal pick
document.getElementById('modal-video-list').addEventListener('click', (e) => {
const card = e.target.closest('[data-video-id]');
if (!card || _pickingIdx === null) return;
_slots[_pickingIdx].video = {
id: parseInt(card.dataset.videoId, 10),
description: card.dataset.videoDesc,
streamUrl: card.dataset.streamUrl,
fileExists: card.dataset.fileExists === 'true',
};
document.getElementById('video-modal').style.display = 'none';
renderSlots();
});
document.getElementById('modal-close').addEventListener('click', () => {
// If the new slot has no video picked, remove it
if (_pickingIdx !== null && !_slots[_pickingIdx]?.video) {
_slots.splice(_pickingIdx, 1);
renderSlots();
}
_pickingIdx = null;
document.getElementById('video-modal').style.display = 'none';
});
let debounce;
document.getElementById('modal-search').addEventListener('input', () => {
clearTimeout(debounce); debounce = setTimeout(loadModalVideos, 300);
});
document.getElementById('modal-category').addEventListener('change', loadModalVideos);
// Save
document.getElementById('save-btn').addEventListener('click', async () => {
const errEl = document.getElementById('form-error');
errEl.style.display = 'none';
const btn = document.getElementById('save-btn');
const name = document.getElementById('name').value.trim();
if (!name) { errEl.textContent = 'Workout name is required.'; errEl.style.display = 'block'; return; }
const payload = {
name,
workoutDate: document.getElementById('workout-date').value || null,
notes: document.getElementById('notes').value.trim() || null,
};
_slots.filter(s => s.video).slice(0, MAX_SLOTS).forEach((s, i) => {
payload[`video${i + 1}`] = s.video.id;
if (s.reps != null) payload[`number${i + 1}`] = s.reps;
});
btn.disabled = true; btn.textContent = 'Saving…';
try {
const res = await Api.post('/api/v1/workouts', payload);
if (res.ok) {
const { data } = await res.json();
toast('Workout created!', 'success');
setTimeout(() => { window.location.href = `/workout-view.html?id=${data.id}`; }, 800);
} else {
const b = await res.json().catch(() => ({}));
errEl.textContent = b.message || 'Failed to save.'; errEl.style.display = 'block';
}
} catch { errEl.textContent = 'Network error.'; errEl.style.display = 'block'; }
finally { btn.disabled = false; btn.textContent = 'Save Workout'; }
});
});
</script>
</body>
</html>