<script lang="ts">
	import { api } from '$lib/api';
	import { goto } from '$app/navigation';
	import { page } from '$app/stores';
	import { onMount } from 'svelte';
	import type { Mitglied, Gruppe } from '$lib/types';

	const id = $derived($page.params.id as string);

	let gruppen    = $state<Gruppe[]>([]);
	let loading    = $state(true);
	let saving     = $state(false);
	let error      = $state('');
	let editMode   = $state(false);
	let showDelete = $state(false);

	// Felder
	let vorname          = $state('');
	let nachname         = $state('');
	let email            = $state('');
	let telefon          = $state('');
	let geburtsdatum     = $state('');
	let status           = $state('aktiv');
	let eintrittsdatum   = $state('');
	let austrittsdatum   = $state('');
	let strasse          = $state('');
	let plz              = $state('');
	let ort              = $state('');
	let iban             = $state('');
	let bic              = $state('');
	let mandatsreferenz  = $state('');
	let mandatsdatum     = $state('');
	let notizen          = $state('');
	let gruppe_ids       = $state<string[]>([]);

	function pbDateToInput(val: string | undefined): string {
		// PocketBase gibt "2026-05-20 00:00:00.000Z" zurück, input[type=date] braucht "2026-05-20"
		if (!val) return '';
		return val.slice(0, 10);
	}

	function loadRecord(m: Mitglied) {
		vorname         = m.vorname;
		nachname        = m.nachname;
		email           = m.email           ?? '';
		telefon         = m.telefon         ?? '';
		geburtsdatum    = pbDateToInput(m.geburtsdatum);
		status          = m.status;
		eintrittsdatum  = pbDateToInput(m.eintrittsdatum);
		austrittsdatum  = pbDateToInput(m.austrittsdatum);
		strasse         = m.strasse         ?? '';
		plz             = m.plz             ?? '';
		ort             = m.ort             ?? '';
		iban            = m.iban            ?? '';
		bic             = m.bic             ?? '';
		mandatsreferenz = m.mandatsreferenz ?? '';
		mandatsdatum    = pbDateToInput(m.mandatsdatum);
		notizen         = m.notizen         ?? '';
		gruppe_ids      = m.gruppe_ids      ?? [];
	}

	onMount(async () => {
		const [m, g] = await Promise.all([
			api.get<Mitglied>('/mitglieder/' + id),
			api.get<Gruppe[]>('/gruppen', { sort: 'name' }),
		]);
		loadRecord(m);
		gruppen = g;
		loading = false;
	});

	function toggleGruppe(gid: string) {
		gruppe_ids = gruppe_ids.includes(gid)
			? gruppe_ids.filter(g => g !== gid)
			: [...gruppe_ids, gid];
	}

	async function speichern() {
		error = ''; saving = true;
		try {
			await api.put('/mitglieder/' + id, {
				vorname:         vorname.trim(),
				nachname:        nachname.trim(),
				email:           email.trim()           || null,
				telefon:         telefon.trim()         || null,
				geburtsdatum:    geburtsdatum           || null,
				status,
				eintrittsdatum:  eintrittsdatum         || null,
				austrittsdatum:  austrittsdatum         || null,
				strasse:         strasse.trim()         || null,
				plz:             plz.trim()             || null,
				ort:             ort.trim()             || null,
				iban:            iban.trim()            || null,
				bic:             bic.trim()             || null,
				mandatsreferenz: mandatsreferenz.trim() || null,
				mandatsdatum:    mandatsdatum           || null,
				notizen:         notizen.trim()         || null,
				gruppe_ids,
			});
			editMode = false;
		} catch (e: unknown) {
			error = e instanceof Error ? e.message : 'Fehler beim Speichern.';
		} finally {
			saving = false;
		}
	}

	async function loeschen() {
		try {
			await api.del('/mitglieder/' + id);
			goto('/mitglieder');
		} catch (e: unknown) {
			error = e instanceof Error ? e.message : 'Fehler beim Löschen.';
			showDelete = false;
		}
	}

	function gruppenName(ids: string[]): string {
		return (ids ?? [])
			.map(gid => gruppen.find(g => g.id === gid)?.name)
			.filter(Boolean)
			.join(', ');
	}

	function formatDatum(iso: string): string {
		if (!iso) return '—';
		return new Date(iso).toLocaleDateString('de-DE');
	}

	const statusFarbe: Record<string, string> = {
		aktiv: '#16a34a', passiv: '#f59e0b', ausgetreten: '#94a3b8',
	};
</script>

<svelte:head><title>{vorname} {nachname} — vereins.haus</title></svelte:head>

<div class="top">
	<a class="back" href="/mitglieder">← Mitglieder</a>
	{#if !loading}
		<button class="edit-btn" onclick={() => { editMode = !editMode; error = ''; }}>
			{editMode ? 'Abbrechen' : 'Bearbeiten'}
		</button>
	{/if}
</div>

{#if loading}
	<p class="hint">Laden…</p>

{:else if !editMode}
	<!-- Detailansicht -->
	<div class="hero">
		<div class="avatar-lg">{vorname[0]}{nachname[0]}</div>
		<h1>{vorname} {nachname}</h1>
		<span class="status-badge" style="color:{statusFarbe[status] ?? '#94a3b8'}">{status}</span>
	</div>

	<div class="detail-block">
		<h2>Kontakt</h2>
		{#if email}
			<div class="row-detail">
				<span class="dl">E-Mail</span>
				<a href="mailto:{email}" class="dv link">{email}</a>
			</div>
		{/if}
		{#if telefon}
			<div class="row-detail">
				<span class="dl">Telefon</span>
				<a href="tel:{telefon}" class="dv link">{telefon}</a>
			</div>
		{/if}
		{#if strasse || plz || ort}
			<div class="row-detail">
				<span class="dl">Adresse</span>
				<span class="dv">{[strasse, [plz, ort].filter(Boolean).join(' ')].filter(Boolean).join(', ')}</span>
			</div>
		{/if}
		{#if !email && !telefon && !strasse}
			<p class="leer">Keine Kontaktdaten hinterlegt.</p>
		{/if}
	</div>

	<div class="detail-block">
		<h2>Mitgliedschaft</h2>
		{#if eintrittsdatum}
			<div class="row-detail">
				<span class="dl">Eintritt</span>
				<span class="dv">{formatDatum(eintrittsdatum)}</span>
			</div>
		{/if}
		{#if geburtsdatum}
			<div class="row-detail">
				<span class="dl">Geburtsdatum</span>
				<span class="dv">{formatDatum(geburtsdatum)}</span>
			</div>
		{/if}
		{#if austrittsdatum}
			<div class="row-detail">
				<span class="dl">Austritt</span>
				<span class="dv">{formatDatum(austrittsdatum)}</span>
			</div>
		{/if}
		{#if gruppe_ids?.length}
			<div class="row-detail">
				<span class="dl">Gruppen</span>
				<span class="dv">{gruppenName(gruppe_ids)}</span>
			</div>
		{/if}
	</div>

	{#if iban || bic || mandatsreferenz}
		<div class="detail-block">
			<h2>SEPA-Lastschrift</h2>
			{#if iban}
				<div class="row-detail">
					<span class="dl">IBAN</span>
					<span class="dv mono">{iban}</span>
				</div>
			{/if}
			{#if bic}
				<div class="row-detail">
					<span class="dl">BIC</span>
					<span class="dv mono">{bic}</span>
				</div>
			{/if}
			{#if mandatsreferenz}
				<div class="row-detail">
					<span class="dl">Mandat</span>
					<span class="dv mono">{mandatsreferenz}{mandatsdatum ? ' · ' + formatDatum(mandatsdatum) : ''}</span>
				</div>
			{/if}
		</div>
	{/if}

	{#if notizen}
		<div class="detail-block">
			<h2>Notizen</h2>
			<p class="notiz-text">{notizen}</p>
		</div>
	{/if}

	<button class="btn-delete" onclick={() => showDelete = true}>Mitglied löschen</button>

{:else}
	<!-- Bearbeitungsformular -->
	<form onsubmit={(e) => { e.preventDefault(); speichern(); }}>

		<section>
			<h2>Stammdaten</h2>
			<div class="row">
				<div class="field">
					<label for="vorname">Vorname *</label>
					<input id="vorname" type="text" bind:value={vorname} required />
				</div>
				<div class="field">
					<label for="nachname">Nachname *</label>
					<input id="nachname" type="text" bind:value={nachname} required />
				</div>
			</div>
			<div class="row">
				<div class="field">
					<label for="status">Status</label>
					<select id="status" bind:value={status}>
						<option value="aktiv">Aktiv</option>
						<option value="passiv">Passiv</option>
						<option value="ausgetreten">Ausgetreten</option>
					</select>
				</div>
				<div class="field">
					<label for="geburtsdatum">Geburtsdatum</label>
					<input id="geburtsdatum" type="date" bind:value={geburtsdatum} />
				</div>
			</div>
			<div class="row">
				<div class="field">
					<label for="eintrittsdatum">Eintrittsdatum</label>
					<input id="eintrittsdatum" type="date" bind:value={eintrittsdatum} />
				</div>
				<div class="field">
					<label for="austrittsdatum">Austrittsdatum</label>
					<input id="austrittsdatum" type="date" bind:value={austrittsdatum} />
				</div>
			</div>
		</section>

		<section>
			<h2>Kontakt</h2>
			<div class="field">
				<label for="email">E-Mail</label>
				<input id="email" type="email" bind:value={email} />
			</div>
			<div class="field">
				<label for="telefon">Telefon</label>
				<input id="telefon" type="tel" bind:value={telefon} />
			</div>
			<div class="field">
				<label for="strasse">Straße & Hausnummer</label>
				<input id="strasse" type="text" bind:value={strasse} />
			</div>
			<div class="row">
				<div class="field" style="flex: 0 0 5rem">
					<label for="plz">PLZ</label>
					<input id="plz" type="text" inputmode="numeric" bind:value={plz} />
				</div>
				<div class="field">
					<label for="ort">Ort</label>
					<input id="ort" type="text" bind:value={ort} />
				</div>
			</div>
		</section>

		<section>
			<h2>SEPA-Lastschrift</h2>
			<div class="field">
				<label for="iban">IBAN</label>
				<input id="iban" type="text" bind:value={iban} placeholder="DE12 3456 7890 …" />
			</div>
			<div class="field">
				<label for="bic">BIC</label>
				<input id="bic" type="text" bind:value={bic} placeholder="COBADEFFXXX" />
			</div>
			<div class="row">
				<div class="field">
					<label for="mandatsreferenz">Mandatsreferenz</label>
					<input id="mandatsreferenz" type="text" bind:value={mandatsreferenz} placeholder="automatisch" />
				</div>
				<div class="field">
					<label for="mandatsdatum">Mandatsdatum</label>
					<input id="mandatsdatum" type="date" bind:value={mandatsdatum} />
				</div>
			</div>
		</section>

		{#if gruppen.length > 0}
			<section>
				<h2>Gruppen</h2>
				<div class="checkboxes">
					{#each gruppen as g (g.id)}
						<label class="check-label" class:active={gruppe_ids.includes(g.id)}>
							<input type="checkbox" checked={gruppe_ids.includes(g.id)} onchange={() => toggleGruppe(g.id)} />
							{g.name}
						</label>
					{/each}
				</div>
			</section>
		{/if}

		<section>
			<h2>Notizen</h2>
			<div class="field">
				<label for="notizen">Interne Notizen</label>
				<textarea id="notizen" bind:value={notizen} rows="3"></textarea>
			</div>
		</section>

		{#if error}
			<p class="error">{error}</p>
		{/if}

		<div class="actions" style="margin-bottom:5rem">
			<button type="submit" class="btn-primary" disabled={saving || !vorname || !nachname}>
				{saving ? 'Speichern…' : 'Änderungen speichern'}
			</button>
		</div>
	</form>
{/if}

<!-- Lösch-Dialog -->
{#if showDelete}
	<div class="overlay" role="dialog" aria-modal="true">
		<div class="dialog">
			<p><strong>{vorname} {nachname}</strong> wirklich löschen?</p>
			<p class="dialog-sub">Diese Aktion kann nicht rückgängig gemacht werden.</p>
			<div class="dialog-actions">
				<button class="btn-ghost" onclick={() => showDelete = false}>Abbrechen</button>
				<button class="btn-danger" onclick={loeschen}>Löschen</button>
			</div>
		</div>
	</div>
{/if}

<style>
	.top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem; }
	.back { font-size: 0.9rem; color: #1e40af; text-decoration: none; }
	.edit-btn {
		background: none; border: 1.5px solid #e2e8f0; border-radius: 8px;
		padding: 0.4rem 0.85rem; font-size: 0.875rem; color: #475569; cursor: pointer;
	}
	h1 { font-size: 1.3rem; font-weight: 700; color: #1e293b; }
	.hint { color: #94a3b8; text-align: center; margin-top: 3rem; }

	.hero {
		display: flex; flex-direction: column; align-items: center; gap: 0.4rem;
		padding: 1.5rem 1rem; background: #fff;
		border: 1px solid #e2e8f0; border-radius: 12px; margin-bottom: 1rem;
	}
	.avatar-lg {
		width: 4rem; height: 4rem; border-radius: 50%;
		background: #e0e7ff; color: #1e40af;
		display: flex; align-items: center; justify-content: center;
		font-weight: 700; font-size: 1.3rem; text-transform: uppercase;
	}
	.status-badge { font-size: 0.8rem; font-weight: 600; text-transform: capitalize; }

	.detail-block {
		background: #fff; border: 1px solid #e2e8f0; border-radius: 12px;
		overflow: hidden; margin-bottom: 0.75rem; padding: 0.75rem 1rem;
	}
	.detail-block h2 {
		font-size: 0.72rem; font-weight: 700; color: #94a3b8;
		text-transform: uppercase; letter-spacing: 0.06em;
		margin-bottom: 0.6rem;
	}
	.row-detail {
		display: flex; justify-content: space-between; align-items: baseline;
		gap: 1rem; padding: 0.45rem 0;
		border-bottom: 1px solid #f1f5f9;
	}
	.row-detail:last-child { border-bottom: none; }
	.dl { font-size: 0.82rem; color: #94a3b8; flex-shrink: 0; }
	.dv { font-size: 0.88rem; color: #1e293b; text-align: right; }
	.dv.link { color: #1e40af; text-decoration: none; }
	.dv.mono { font-family: monospace; font-size: 0.82rem; }
	.leer { font-size: 0.85rem; color: #94a3b8; }
	.notiz-text { font-size: 0.875rem; color: #475569; white-space: pre-wrap; }

	.btn-delete {
		width: 100%; padding: 0.75rem; background: none;
		border: 1.5px solid #fca5a5; border-radius: 8px;
		color: #dc2626; font-size: 0.9rem; cursor: pointer;
		transition: background 0.15s; margin-bottom: 5rem;
	}
	.btn-delete:hover { background: #fef2f2; }

	/* Formular */
	section {
		margin-bottom: 1.5rem; padding-bottom: 1.5rem;
		border-bottom: 1px solid #f1f5f9;
	}
	section:last-of-type { border-bottom: none; }
	section h2 {
		font-size: 0.72rem; font-weight: 700; color: #94a3b8;
		text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 0.85rem;
	}
	.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
	.field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.85rem; }
	label { font-size: 0.875rem; font-weight: 500; color: #475569; }

	input, select, textarea {
		padding: 0.65rem 0.85rem;
		border: 1.5px solid #e2e8f0; border-radius: 8px;
		font-size: 1rem; background: #fff; width: 100%;
		box-sizing: border-box; font-family: inherit; resize: vertical;
		transition: border-color 0.15s;
	}
	input:focus, select:focus, textarea:focus { outline: none; border-color: #1e40af; }

	.checkboxes { display: flex; flex-wrap: wrap; gap: 0.5rem; }
	.check-label {
		display: flex; align-items: center; gap: 0.4rem;
		padding: 0.4rem 0.75rem;
		border: 1.5px solid #e2e8f0; border-radius: 20px;
		font-size: 0.875rem; cursor: pointer;
		transition: border-color 0.15s, background 0.15s;
	}
	.check-label.active { border-color: #1e40af; background: #e0e7ff; color: #1e40af; }
	.check-label input { display: none; }

	.error { color: #dc2626; font-size: 0.875rem; margin-bottom: 0.75rem; }
	.actions { display: flex; gap: 0.75rem; margin-top: 1rem; }

	.btn-primary {
		flex: 1; padding: 0.75rem; background: #1e40af; color: #fff;
		border: none; border-radius: 8px; font-size: 1rem; font-weight: 600;
		cursor: pointer; transition: background 0.15s;
	}
	.btn-primary:hover:not(:disabled) { background: #1d3a9e; }
	.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }
	.btn-ghost {
		padding: 0.75rem 1rem; background: none;
		border: 1.5px solid #e2e8f0; border-radius: 8px;
		font-size: 1rem; color: #64748b; cursor: pointer;
	}

	.overlay {
		position: fixed; inset: 0; background: rgba(0,0,0,0.4);
		display: flex; align-items: flex-end; justify-content: center;
		z-index: 100; padding: 1rem;
		padding-bottom: calc(1rem + env(safe-area-inset-bottom));
	}
	.dialog {
		background: #fff; border-radius: 16px; padding: 1.5rem;
		width: 100%; max-width: 400px;
	}
	.dialog p { font-size: 1rem; color: #1e293b; margin-bottom: 0.5rem; }
	.dialog-sub { font-size: 0.875rem !important; color: #94a3b8; }
	.dialog-actions { display: flex; gap: 0.75rem; margin-top: 1.25rem; }
	.btn-danger {
		flex: 1; padding: 0.75rem; background: #dc2626; color: #fff;
		border: none; border-radius: 8px; font-size: 0.95rem; font-weight: 600;
		cursor: pointer; transition: background 0.15s;
	}
	.btn-danger:hover { background: #b91c1c; }
</style>
