domaino/app-admin/src/routes/items/+page.svelte
agent a22d9f2348 Admin slots: places, blocked/full states, edits limited once booked
Calendar, day panel and list show available / in a basket / booked /
full / blocked (with what blocks it) and places taken. Services, Generate
and single slots take places (default from the service); Copy keeps
them. Once any place is taken only places (not below taken) and notes
can change. All reads go through the shop hook's slot details.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaXE9U7AmW3dfD2w6cbLfk
2026-10-24 08:26:37 +00:00

219 lines
8.5 KiB
Svelte

<script lang="ts">
import { uploadMedia } from '$lib/pb';
import { confirmSubmit } from '$lib/admin/confirm';
import Modal from '$lib/admin/Modal.svelte';
import type { ItemRecord, ItemType } from '$lib/types';
let { data, form } = $props();
let items = $derived(data.items || []);
let showEditor = $state(false);
let uploading = $state(false);
let uploadError = $state('');
let fileInput: HTMLInputElement | undefined = $state();
type ItemDraft = {
id: string;
name: string;
description: string;
price: number;
image: string;
imageUrl: string;
type: ItemType;
stock: string;
duration: string;
bufferMinutes: string;
capacity: string;
active: boolean;
};
let draft = $state<ItemDraft>(emptyDraft());
let action = $derived(draft.id ? '?/update' : '?/create');
let money = $derived(new Intl.NumberFormat('en-GB', { style: 'currency', currency: data.currency }));
function emptyDraft(): ItemDraft {
return { id: '', name: '', description: '', price: 0, image: '', imageUrl: '', type: 'item', stock: '', duration: '', bufferMinutes: '', capacity: '', active: true };
}
function createItem() {
draft = emptyDraft();
uploadError = '';
showEditor = true;
}
function editItem(item: ItemRecord) {
draft = {
id: item.id,
name: item.name,
description: item.description,
price: item.price,
image: item.image,
imageUrl: item.imageUrl,
type: item.type,
stock: item.stock == null ? '' : String(item.stock),
duration: item.duration == null ? '' : String(item.duration),
bufferMinutes: item.bufferMinutes == null ? '' : String(item.bufferMinutes),
capacity: item.capacity == null ? '' : String(item.capacity),
active: item.active
};
uploadError = '';
showEditor = true;
}
async function onImageSelected(event: Event) {
const input = event.currentTarget as HTMLInputElement;
const file = input.files?.[0];
if (!file) return;
uploading = true;
uploadError = '';
try {
const result = await uploadMedia(file);
draft = { ...draft, image: result.record.id, imageUrl: result.url };
} catch (err) {
uploadError = err instanceof Error ? err.message : 'Upload failed.';
} finally {
uploading = false;
input.value = '';
}
}
</script>
<svelte:head><title>Services & products | CMS</title></svelte:head>
<div class="space-y-6">
<div class="flex flex-col justify-between gap-4 md:flex-row md:items-end">
<div>
<h1 class="text-3xl font-bold">Services & products</h1>
<p class="text-base-content/60">Bookable services and products, shown in Shop blocks.</p>
</div>
<button class="btn btn-primary" type="button" onclick={() => createItem()}>Add service or product</button>
</div>
{#if form?.message}<div class="alert alert-error" role="alert">{form.message}</div>{/if}
<div class="rounded-box bg-base-100 p-5 shadow">
<div class="overflow-x-auto">
<table class="table">
<thead><tr><th>Name</th><th>Type</th><th>Price</th><th>Stock</th><th>Duration</th><th>Status</th><th></th></tr></thead>
<tbody>
{#each items as item (item.id)}
<tr>
<td>
<div class="flex items-center gap-3">
{#if item.imageUrl}
<img class="size-10 rounded-box object-cover" src={item.imageUrl} alt="" />
{:else}
<div class="size-10 rounded-box bg-base-200"></div>
{/if}
<span class="font-semibold">{item.name}</span>
</div>
</td>
<td><span class="badge">{item.type === 'item_with_time' ? 'Service' : 'Product'}</span></td>
<td>{money.format(item.price)}</td>
<td>{item.type === 'item' ? (item.stock ?? '∞') : '—'}</td>
<td>{item.type === 'item_with_time' ? `${item.duration ?? 0} min${item.bufferMinutes ? ` + ${item.bufferMinutes} get-ready` : ''}${(item.capacity ?? 1) > 1 ? ` · ${item.capacity} places` : ''}` : '—'}</td>
<td><span class="badge" class:badge-success={item.active}>{item.active ? 'Active' : 'Inactive'}</span></td>
<td class="text-right"><button class="btn btn-sm" type="button" onclick={() => editItem(item)}>Edit</button></td>
</tr>
{:else}
<tr><td colspan="7" class="py-10 text-center text-base-content/60">No items yet — create a service or product.</td></tr>
{/each}
</tbody>
</table>
</div>
</div>
</div>
<Modal bind:open={showEditor} title={draft.id ? `Edit “${draft.name}”` : 'New service or product'} wide>
<form method="POST" action={action} class="space-y-1">
<input type="hidden" name="id" value={draft.id} />
<input type="hidden" name="active" value={draft.active ? 'true' : 'false'} />
<input type="hidden" name="image" value={draft.image} />
<label class="fieldset">
<span class="fieldset-legend">Name</span>
<input class="input w-full" name="name" required bind:value={draft.name} />
</label>
<label class="fieldset">
<span class="fieldset-legend">Description</span>
<textarea class="textarea min-h-28 w-full" name="description" bind:value={draft.description}></textarea>
</label>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Price ({data.currency})</span>
<input class="input w-full" type="number" step="0.01" min="0" name="price" required bind:value={draft.price} />
</label>
<label class="fieldset">
<span class="fieldset-legend">Type</span>
<select class="select w-full" name="type" bind:value={draft.type}>
<option value="item">Product</option>
<option value="item_with_time">Service (bookable)</option>
</select>
</label>
</div>
{#if draft.type === 'item'}
<label class="fieldset">
<span class="fieldset-legend">Stock</span>
<input class="input w-full" type="number" min="0" step="1" name="stock" placeholder="Unlimited when empty" bind:value={draft.stock} />
</label>
{:else}
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Duration (minutes)</span>
<input class="input w-full" type="number" min="1" step="1" name="duration" placeholder="60" required bind:value={draft.duration} />
</label>
<label class="fieldset">
<span class="fieldset-legend">Get-ready time (minutes)</span>
<input class="input w-full" type="number" min="0" step="1" name="bufferMinutes" placeholder="0" bind:value={draft.bufferMinutes} />
<span class="label">Gap after each appointment — between generated slots, and before another service can start</span>
</label>
<label class="fieldset">
<span class="fieldset-legend">Places per slot</span>
<input class="input w-full" type="number" min="1" max="500" step="1" name="capacity" placeholder="1" bind:value={draft.capacity} />
<span class="label">1 for one-to-one; more for a group session. New slots start with this.</span>
</label>
</div>
{/if}
<div class="fieldset">
<span class="fieldset-legend">Image</span>
<div class="flex items-center gap-4">
{#if draft.imageUrl}
<img class="size-20 rounded-box object-cover shadow" src={draft.imageUrl} alt="" />
{:else}
<div class="grid size-20 place-items-center rounded-box border-2 border-dashed border-base-300 text-xs text-base-content/40">No image</div>
{/if}
<div class="flex flex-wrap gap-2">
<button class="btn btn-sm" type="button" disabled={uploading} onclick={() => fileInput?.click()}>
{#if uploading}<span class="loading loading-spinner loading-xs"></span> Uploading…{:else}{draft.imageUrl ? 'Replace image' : 'Upload image'}{/if}
</button>
{#if draft.image}
<button class="btn btn-ghost btn-sm text-error" type="button" onclick={() => (draft = { ...draft, image: '', imageUrl: '' })}>Remove</button>
{/if}
</div>
<input class="hidden" type="file" accept="image/*" bind:this={fileInput} onchange={onImageSelected} />
</div>
{#if uploadError}<span class="label text-error">{uploadError}</span>{/if}
</div>
<label class="label mt-2 cursor-pointer gap-3 text-base-content">
<input class="toggle toggle-success" type="checkbox" bind:checked={draft.active} />
Active <span class="text-base-content/60">— inactive items are hidden from the shop</span>
</label>
<div class="modal-action justify-between">
{#if draft.id}
<button class="btn btn-error btn-outline" type="submit" formaction="?/delete" formnovalidate onclick={confirmSubmit(`Delete "${draft.name}"?`)}>Delete</button>
{:else}
<span></span>
{/if}
<div class="flex gap-2">
<button class="btn" type="button" onclick={() => (showEditor = false)}>Cancel</button>
<button class="btn btn-primary" type="submit" disabled={uploading}>Save</button>
</div>
</div>
</form>
</Modal>