function AppVersionsView() {
  const [doc,setDoc]=React.useState(null),[error,setError]=React.useState(''),[busy,setBusy]=React.useState(false),[saved,setSaved]=React.useState(false);
  const load=()=>api('/platform/app-config').then(setDoc).catch(e=>setError(e.message));
  React.useEffect(()=>{void load();},[]);
  const change=(platform,key,value)=>{setSaved(false);setDoc(old=>({...old,[platform]:{...old[platform],[key]:value||null}}));};
  const save=async e=>{e.preventDefault();if(busy)return;setBusy(true);setError('');setSaved(false);try{setDoc(await api('/platform/app-config',{method:'PUT',body:JSON.stringify({ios:doc.ios,android:doc.android,version:doc.version})}));setSaved(true);}catch(e){setError(e.message);}finally{setBusy(false);}};
  return <div className="page"><h1>{tx('Application versions')}</h1><p>{tx('Minimum version requires an update; recommended version suggests one. Set 0.0.0 to disable version requirements.')}</p>{error&&<p role="alert">{error} <button className="btn btn-outline" onClick={load}>{tx('Reload')}</button></p>}{saved&&<p role="status">{tx('Saved')}</p>}{doc&&<form onSubmit={save}>{['ios','android'].map(platform=><section className="card card-pad" key={platform}><h2>{platform==='ios'?'iOS':'Android'}</h2>{[['minVersion','Minimum version'],['recommendedVersion','Recommended version'],['storeUrl','Store URL']].map(([key,label])=><label className="field" key={key}><span>{tx(label)}</span><input className="input" aria-label={`${platform} ${tx(label)}`} value={doc[platform][key]||''} required={key!=='storeUrl'} disabled={busy} onChange={e=>change(platform,key,e.target.value)} /></label>)}</section>)}<button className="btn btn-primary" disabled={busy}>{tx(busy?'Saving...':'Save')}</button></form>}</div>;
}
function PlaceLinksView() {
  const [items,setItems]=React.useState([]),[cursor,setCursor]=React.useState(''),[error,setError]=React.useState(''),[busy,setBusy]=React.useState(false),[drafts,setDrafts]=React.useState({});
  const load=async(after='')=>{setError('');try{const result=await api(`/platform/tenant-place-links?missingOnly=true&after=${encodeURIComponent(after)}`);setItems(old=>after?[...old,...result.items]:result.items);setCursor(result.nextCursor||'');}catch(e){setError(e.message);}};
  React.useEffect(()=>{void load();},[]);
  const save=async item=>{if(busy||!drafts[item.id]?.trim())return;setBusy(true);setError('');try{await api(`/platform/tenants/${item.id}/place-link`,{method:'PUT',body:JSON.stringify({googlePlaceId:drafts[item.id].trim(),expectedPlaceId:item.googlePlaceId})});setItems(old=>old.filter(row=>row.id!==item.id));}catch(e){setError(e.message);}finally{setBusy(false);}};
  return <div className="page"><h1>{tx('Google Place links')}</h1><p>{tx('Select the exact Google Place ID for each workshop. Linking an old workshop does not create a referral reward.')}</p><button className="btn btn-outline" disabled={busy} onClick={()=>void load()}>{tx('Reload')}</button>{error&&<p role="alert">{error}</p>}{items.map(item=><section className="card card-pad" key={item.id}><h2>{item.name}</h2><p>{item.address}, {item.city}</p><a href={`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(`${item.name} ${item.address} ${item.city}`)}`} target="_blank" rel="noreferrer">{tx('Find on Google Maps')}</a><label className="field"><span>{tx('Google Place ID')}</span><input className="input" aria-label={`${item.name} Google Place ID`} disabled={busy} value={drafts[item.id]||''} onChange={e=>setDrafts(old=>({...old,[item.id]:e.target.value}))}/></label><button className="btn btn-primary" disabled={busy||!drafts[item.id]?.trim()} onClick={()=>void save(item)}>{tx('Link workshop')}</button></section>)}{!items.length&&!error&&<p>{tx('No workshops need a place link')}</p>}{cursor&&<button className="btn btn-outline" onClick={()=>void load(cursor)}>{tx('Load more')}</button>}</div>;
}
