/* global React, KPI, fmtN */ /* Doluluk Fırsatları — the seats and rooms the desk must fill (low-LF flights, unsold allotment before release) and the guests who will fill them. Each card reads left to right as the revenue loop: empty seat / unsold room → the right guests → a campaign. Data: crm.json opportunities (synthetic guests). Reuses the CRM kit from page-crm.jsx (window.CrmKit): guest card drawer, badges, campaign handoff. */ (() => { const { useState, useEffect } = React; const SPAN = 70; // timeline strip: next 10 weeks const store = { kind: 'all', open: null }; // Compact Gantt, one row per opportunity: the bar is the selling time left (today → release for allotment, // today → departure for flights); for allotment the check-in day follows the release, dotted. const Timeline = ({ K, items, asOf, onPick }) => { const h = K.h, T = K.T; const x = (d) => Math.max(0, Math.min(100, K.dayDiff(asOf, d) / SPAN * 100)); const ticks = []; for (let i = 0; i <= SPAN / 7; i++) ticks.push(i); return h('div', { className: 'panel fill-tl' }, h('div', { className: 'panel-head' }, h('div', null, h('div', { className: 'panel-head-title' }, T('Önümüzdeki 10 hafta')), h('div', { className: 'panel-head-sub' }, T('Çubuk = satış için kalan süre: odada release gününe, uçuşta kalkışa kadar · satıra tıklayın'))), h('div', { className: 'fill-tl-legend' }, h('span', null, h('i', { className: 'fill-tl-key al' }), T('Oda kontenjanı')), h('span', null, h('i', { className: 'fill-tl-key fl' }), T('Uçuş')), h('span', null, h('i', { className: 'fill-dm' }), T('Release')), h('span', null, h('i', { className: 'fill-sq' }), T('Giriş günü')), h('span', null, h('i', { className: 'fill-dot' }), T('Kalkış günü')))), h('div', { className: 'fill-tl-body' }, h('div', { className: 'fill-tl-axis' }, h('div', { className: 'fill-tl-lab' }), h('div', { className: 'fill-tl-track' }, ticks.map(i => h('span', { key: i, className: 'fill-tl-tick' + (i % 3 ? '' : ' t3'), style: { left: (i * 7 / SPAN * 100) + '%' } }, i === 0 ? T('Bugün') : K.fmtDay(K.addDays(asOf, i * 7)))))), items.map(it => { const o = it.o; const lab = it.flight ? [o.flight_no, o.route].filter(Boolean).join(' · ') : (o.hotel || o.route || o.id); const end = x(it.flight ? o.date : (o.release_date || o.date)); const stay = x(o.date); const hot = it.days <= 7 ? ' hot' : ''; return h('button', { key: o.id, type: 'button', className: 'fill-tl-row' + (it.flight ? ' fl' : ' al'), title: K.L(o.title), onClick: () => onPick(o.id) }, h('span', { className: 'fill-tl-lab' }, lab), h('span', { className: 'fill-tl-track' }, ticks.map(i => h('i', { key: i, className: 'fill-tl-grid', style: { left: (i * 7 / SPAN * 100) + '%' } })), h('i', { className: 'fill-tl-win' + hot, style: { width: end + '%' } }), !it.flight && stay > end ? h('i', { className: 'fill-tl-after', style: { left: end + '%', width: (stay - end) + '%' } }) : null, !it.flight ? h('i', { className: 'fill-dm' + hot, style: { left: end + '%' } }) : null, h('i', { className: it.flight ? 'fill-dot' : 'fill-sq', style: { left: stay + '%' } }))); }))); }; const OppCard = ({ K, crm, it, onOpen, flash }) => { const h = K.h, T = K.T, tx = K.tx, o = it.o; const ev = (g) => (g.offer && g.offer.expected_rev_eur) || 0; const guests = (o.guest_ids || []).map(id => crm.byId[id]).filter(Boolean).sort((x, y) => ev(y) - ev(x)); const nDirect = guests.filter(g => g.owner === 'direct').length; const urg = it.days <= 7 ? 'hi' : it.days <= 21 ? 'mid' : 'lo'; const soldPct = it.flight ? (o.lf || 0) : (o.allotment ? Math.max(0, (o.allotment - (o.unsold_rooms || 0)) / o.allotment * 100) : null); const need = it.flight ? [h('div', { key: 't', className: 'fill-title' }, o.route || '—'), h('div', { key: 's', className: 'fill-sub' }, [o.flight_no, o.date ? tx('Kalkış {d}', { d: K.fmtDay(o.date) }) : null].filter(Boolean).join(' · ')), h('div', { key: 'm', className: 'fill-meter' }, h('div', { className: 'fill-bar', 'aria-hidden': true }, h('span', { style: { width: Math.min(100, soldPct) + '%' } })), h('strong', { className: 'fill-meter-l' }, 'LF ' + K.pct(o.lf) + (o.empty_seats != null ? ' · ' + tx('{n} boş koltuk', { n: o.empty_seats }) : '')))] : [h('div', { key: 't', className: 'fill-title' }, h(K.HotelName, { name: o.hotel })), h('div', { key: 's', className: 'fill-sub' }, [window.crmRoom(o.room), o.date ? tx('Giriş {d}', { d: K.fmtDay(o.date) }) : null, o.release_date ? tx('Release {d}', { d: K.fmtDay(o.release_date) }) : null, o.route].filter(Boolean).join(' · ')), h('div', { key: 'm', className: 'fill-meter' }, soldPct != null ? h('div', { className: 'fill-bar', 'aria-hidden': true }, h('span', { style: { width: soldPct + '%' } })) : null, o.unsold_rooms != null ? h('strong', { className: 'fill-meter-l' }, o.allotment ? tx('{u}/{a} boş', { u: o.unsold_rooms, a: o.allotment }) : tx('{n} oda satılmadı', { n: o.unsold_rooms })) : null)]; return h('article', { className: 'panel fill-card' + (flash ? ' flash' : ''), id: 'fill-' + o.id }, h('div', { className: 'fill-z fill-need' }, h('div', { className: 'fill-kick' }, h('span', { className: 'fill-kind ' + (it.flight ? 'fl' : 'al') }, T(it.flight ? 'Boş koltuk' : 'Satılmamış oda')), it.days < 999 ? h('span', { className: 'fill-urg ' + urg }, it.flight ? tx('Kalkışa {n} gün', { n: it.days }) : tx('Release\'e {n} gün', { n: it.days })) : null), ...need, (o.why || []).length ? h('ul', { className: 'crm-why crm-why-col' }, o.why.map((w, i) => h('li', { key: i }, K.L(w)))) : null), h('div', { className: 'fill-arrow', 'aria-hidden': true }, '→'), h('div', { className: 'fill-z fill-who' }, h('div', { className: 'fill-zk' }, tx('Eşleşen {n} misafir', { n: guests.length }), o.matched_pax ? h('span', null, ' · ' + tx('{n} kişi', { n: o.matched_pax })) : null), guests.length ? h('ul', { className: 'fill-guests' + (guests.length > 5 ? ' two' : '') }, guests.map(g => h('li', { key: g.id }, h('button', { type: 'button', className: 'fill-g', onClick: () => onOpen(g.id) }, h('span', { className: 'fill-g-n' }, K.gName(g), h(K.NpsBadge, { g })), h('span', { className: 'fill-g-m' }, h(K.SegChip, { crm, id: g.segment }), h('span', { className: 'fill-g-o ' + (g.owner === 'agency' ? 'ag' : 'dr') }, g.owner === 'agency' ? tx('Acente: {a}', { a: g.agency || '—' }) : T('Doğrudan'))), h('span', { className: 'fill-g-e mono' }, K.eur(g.offer && g.offer.expected_rev_eur)))))) : h('div', { className: 'crm-empty' }, T('Eşleşen misafir yok.'))), h('div', { className: 'fill-arrow', 'aria-hidden': true }, '→'), h('div', { className: 'fill-z fill-act' }, h('div', { className: 'fill-zk' }, T('Beklenen gelir')), h('div', { className: 'fill-exp' }, K.eur(o.expected_rev_eur)), guests.length ? h('div', { className: 'fill-mix' }, tx('{d} doğrudan · {a} acente üzerinden', { d: nDirect, a: guests.length - nDirect })) : null, h('button', { type: 'button', className: 'btn btn-accent fill-cta', disabled: !guests.length, onClick: () => K.toCampaign('opportunity', o.id, (o.guest_ids || []).slice(), K.L(o.title) || o.route || o.id) }, T('Kampanya oluştur') + ' →'))); }; const FillPage = () => { const K = window.CrmKit; if (!K) return React.createElement('div', { className: 'crm-page' }, window.t('Misafir verisi yüklenemedi.')); const h = K.h, T = K.T, tx = K.tx; const [{ crm, err }, reload] = K.useCrm(); const [kind, setKindState] = useState(store.kind); const setKind = (k) => { store.kind = k; setKindState(k); }; const [openId, setOpenId] = K.useDrawer(store); const [focus, setFocus] = useState(() => { const f = window.__fillFocus || null; window.__fillFocus = null; return f; }); useEffect(() => { if (!crm || !focus) return; const el = document.getElementById('fill-' + focus); if (el) el.scrollIntoView({ block: 'center' }); const t = setTimeout(() => setFocus(null), 1600); return () => clearTimeout(t); }, [crm, focus]); const pick = (id) => { setOpenId(null); if (kind !== 'all' && !(crm.opportunities || []).some(o => o.id === id && (o.kind === 'flight') === (kind === 'flight'))) setKind('all'); setFocus(null); setTimeout(() => setFocus(id), 0); }; const head = h(K.PageHead, { crm, eyebrow: T('Doluluk Fırsatları · Gelir masası × CRM'), title: T('Boş koltuk ve odalar, '), em: T('doğru misafirlerle.'), lede: T('Masanın doldurması gereken koltuk ve odalar — ve onları dolduracak misafirler.') }); if (err) return h('div', { className: 'crm-page fill-page' }, head, h(K.LoadError, { onRetry: reload })); if (!crm) return h('div', { className: 'crm-page fill-page' }, head, h(K.Loading)); const asOf = crm.as_of; const all = (crm.opportunities || []).map(o => { const flight = o.kind === 'flight'; const deadline = flight ? o.date : (o.release_date || o.date); return { o, flight, days: deadline ? Math.max(0, K.dayDiff(asOf, deadline)) : 999 }; }).sort((a, b) => a.days - b.days || (b.o.expected_rev_eur || 0) - (a.o.expected_rev_eur || 0)); const items = all.filter(it => kind === 'all' || (kind === 'flight') === it.flight); const flights = all.filter(it => it.flight), rooms = all.filter(it => !it.flight); const sum = (arr, f) => arr.reduce((s, it) => s + (f(it.o) || 0), 0); const uniq = new Set(); all.forEach(it => (it.o.guest_ids || []).forEach(id => uniq.add(id))); const nextRelease = rooms.length ? rooms[0] : null; return h('div', { className: 'crm-page fill-page' }, head, h('div', { className: 'grid grid-5 gap-3 crm-kpis' }, h(KPI, { label: T('Açık fırsat'), value: fmtN(all.length), sub: tx('{f} uçuş · {r} oda kontenjanı', { f: flights.length, r: rooms.length }) }), h(KPI, { label: T('Boş koltuk'), value: fmtN(sum(flights, o => o.empty_seats)), sub: tx('{n} düşük LF\'li uçuşta', { n: flights.length }) }), h(KPI, { label: T('Satılmamış oda'), value: fmtN(sum(rooms, o => o.unsold_rooms)), sub: nextRelease && nextRelease.days < 999 ? tx('ilk release {d} · {n} gün', { d: K.fmtDay(nextRelease.o.release_date || nextRelease.o.date), n: nextRelease.days }) : null }), h(KPI, { label: T('Eşleşen misafir'), value: fmtN(uniq.size), sub: tx('{n} kişi', { n: sum(all, o => o.matched_pax) }) }), h(KPI, { label: T('Beklenen gelir'), value: K.eurK(sum(all, o => o.expected_rev_eur)), sub: T('eşleşen misafirlerden, kampanya ile') })), all.length ? h(Timeline, { K, items: all, asOf, onPick: pick }) : null, h('div', { className: 'fill-bar-row' }, h('div', { className: 'fill-chips' }, [['all', T('Tümü'), all.length], ['flight', T('Uçuş'), flights.length], ['allotment', T('Oda kontenjanı'), rooms.length]].map(c => h('button', { key: c[0], type: 'button', className: 'filter-chip' + (kind === c[0] ? ' active' : ''), 'aria-pressed': kind === c[0], onClick: () => setKind(c[0]) }, c[1], h('span', { className: 'crm-chip-n' }, fmtN(c[2]))))), h('div', { className: 'fill-sort' }, T('Aciliyete göre sıralı: release ve kalkışa kalan gün'))), items.length ? h('div', { className: 'fill-list' }, items.map(it => h(OppCard, { key: it.o.id, K, crm, it, onOpen: setOpenId, flash: focus === it.o.id }))) : h('div', { className: 'panel crm-empty crm-empty-pad' }, T('Şu an doldurulacak açık fırsat yok.')), h(K.SelectionBar), openId ? h(K.GuestCard, { id: openId, onClose: () => setOpenId(null), onOpp: pick }) : null); }; window.FillPage = FillPage; })();