1import{o as ae,l as x,a3 as T,a4 as F,K as re,a as se,n as ne,m as ie,r as m,s as p,j as e,g as le,p as i,I as h,t as q,v as V,O as k,w as E,y as j,B as b,D as oe,h as de,_ as ce,i as me,a0 as ue,a1 as xe,q as u}from"./index-DnLpPdJu.js";import{T as pe}from"./textarea-BGHgl9MJ.js";import{S as H}from"./switch-D7hwodgj.js";import{e as G,h as J}from"./tournaments-BXkCUYTe.js";import{T as $}from"./trash-2-9vLPNF96.js";const ge=ae({title:x().trim().min(3).max(120),category:T(["P25","P50","P100","P250","P500"]),gender:T(["hommes","femmes","mixte"]),age_limit:T(["normal","plus45"]).optional(),tournament_date:x().min(1),start_time:x().optional(),end_time:x().optional(),referee_name:x().trim().max(120).optional(),max_pairs:F.number().int().min(2).max(64),description:x().max(2e3).optional(),twitch_channel_court1:x().trim().max(50).optional(),twitch_channel_court2:x().trim().max(50).optional(),price_euros:F.number().min(0).max(1e3).optional()}),ve=()=>{var L,M;const{id:o}=re(),d=!o||o==="nouveau",{user:s,isReferee:z,isAdmin:v}=se(),y=ne(),[K]=ie(),C=K.get("date")??"",[a,g]=m.useState(null),[A,f]=m.useState(!1),[S,O]=m.useState(null),[Q,I]=m.useState(16),[_,P]=m.useState(""),[U,W]=m.useState(0),[X,N]=m.useState(!1),[w,R]=m.useState(!1);if(m.useEffect(()=>{if(d){(async()=>{let t="";if(s!=null&&s.id){const{data:c}=await p.from("profiles").select("first_name").eq("id",s.id).maybeSingle();t=(c==null?void 0:c.first_name)??""}const{data:l}=await p.rpc("get_public_app_settings"),n=Array.isArray(l)?l[0]:l,r=n?!!n.payments_required_default:!0;g({category:"P100",gender:"hommes",age_limit:"normal",max_pairs:16,status:"open",referee_name:t,start_time:"08:30",end_time:"16:00",tournament_date:C||void 0,payment_enabled:r,onsite_payment_allowed:!0,price_cents:null}),C&&P(C)})();return}p.from("tournaments").select("*").eq("id",o).maybeSingle().then(({data:t})=>{g(t),t&&(I(t.max_pairs??16),P(t.tournament_date??""))}),p.from("registrations").select("id",{count:"exact",head:!0}).eq("tournament_id",o).then(({count:t})=>{W(t??0)})},[o,d,s==null?void 0:s.id]),m.useEffect(()=>{if(!_){O(null);return}let t=p.from("tournaments").select("max_pairs,id").eq("tournament_date",_).neq("status","cancelled");!d&&o&&(t=t.neq("id",o)),t.then(({data:l})=>{const n=(l??[]).reduce((r,c)=>r+(c.max_pairs??0),0);O({count:(l==null?void 0:l.length)??0,pairs:n})})},[_,o,d]),!z)return e.jsx("div",{className:"container py-20 text-center text-muted-foreground",children:"Réservé aux juges-arbitres."});if(!a)return e.jsx("div",{className:"container py-20 text-center text-muted-foreground",children:"Chargementâ¦"});const Y=async t=>{t.preventDefault();const l=new FormData(t.currentTarget),n=Object.fromEntries(l);n.category=a.category,n.gender=a.gender,n.referee_name=a.referee_name??"",n.age_limit=a.age_limit??"normal",n.title=G(a.category,a.gender,a.age_limit);const r=ge.safeParse(n);if(!r.success)return u.error(r.error.errors[0].message);f(!0);const c={title:r.data.title,category:r.data.category,gender:r.data.gender,age_limit:r.data.age_limit??"normal",tournament_date:r.data.tournament_date,max_pairs:r.data.max_pairs,start_time:r.data.start_time||null,end_time:r.data.end_time||null,referee_name:r.data.referee_name||null,description:r.data.description||null,twitch_channel_court1:r.data.twitch_channel_court1||null,twitch_channel_court2:r.data.twitch_channel_court2||null,payment_enabled:!!a.payment_enabled,onsite_payment_allowed:a.onsite_payment_allowed!==!1,price_cents:a.payment_enabled?Math.round((r.data.price_euros??0)*100):null,referee_id:s.id};if(a.payment_enabled&&(!r.data.price_euros||r.data.price_euros<=0))return f(!1),u.error("Indiquez un prix par paire supérieur à 0 â¬.");if(d&&J(s==null?void 0:s.id,v,r.data.tournament_date))return f(!1),u.error("Tu n'es pas autorisé à créer un tournoi à partir du 1er octobre 2026. Contacte un administrateur.");if(d){const{data:D,error:B}=await p.from("tournaments").insert({...c,status:"draft",created_by:s.id}).select().single();if(f(!1),B)return u.error(B.message);u.success("Tournoi enregistré en brouillon â mets-le en ligne quand le planning est prêt."),y("/ja")}
1else{const{error:D}=await p.from("tournaments").update(c).eq("id",o);if(f(!1),D)return u.error(D.message);u.success("Tournoi mis à jour"),y(`/ja/tournoi/${o}`)}},Z=!!s&&(a.referee_id===s.id||a.created_by===s.id),ee=!d&&a.status==="draft"&&U===0&&(v||Z),te=async()=>{R(!0);const{error:t}=await p.from("tournaments").delete().eq("id",o);if(R(!1),N(!1),t)return u.error(t.message);u.success("Brouillon supprimé."),y("/ja")};return e.jsxs("div",{className:"container py-10 max-w-3xl",children:[e.jsx("h1",{className:"font-display text-4xl mb-6",children:d?"Nouveau tournoi":"Modifier le tournoi"}),e.jsx(le,{className:"p-6",children:e.jsxs("form",{onSubmit:Y,className:"space-y-4",children:[(()=>{const t=G(a.category,a.gender,a.age_limit);return e.jsxs("div",{children:[e.jsx(i,{children:"Titre"}),e.jsx(h,{name:"title",value:t,readOnly:!0,disabled:!0,className:"bg-muted cursor-not-allowed"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Généré automatiquement à partir de la catégorie, du genre et de la limite d'âge."})]})})(),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsxs("div",{children:[e.jsx(i,{children:"Catégorie *"}),e.jsxs(q,{value:a.category,onValueChange:t=>g({...a,category:t}),children:[e.jsx(V,{children:e.jsx(k,{})}),e.jsx(E,{children:["P25","P50","P100","P250","P500"].map(t=>e.jsx(j,{value:t,children:t},t))})]})]}),e.jsxs("div",{children:[e.jsx(i,{children:"Genre *"}),e.jsxs(q,{value:a.gender,onValueChange:t=>g({...a,gender:t}),children:[e.jsx(V,{children:e.jsx(k,{})}),e.jsxs(E,{children:[e.jsx(j,{value:"hommes",children:"Hommes"}),e.jsx(j,{value:"femmes",children:"Femmes"}),e.jsx(j,{value:"mixte",children:"Mixte"})]})]})]}),e.jsxs("div",{children:[e.jsx(i,{children:"Paires max *"}),e.jsx(h,{type:"number",name:"max_pairs",defaultValue:a.max_pairs??16,min:2,required:!0,onChange:t=>I(Number(t.target.value)||0)})]})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsxs("div",{children:[e.jsx(i,{children:"Date *"}),e.jsx(h,{type:"date",name:"tournament_date",defaultValue:a.tournament_date??"",required:!0,onChange:t=>P(t.target.value)})]}),e.jsxs("div",{children:[e.jsx(i,{children:"Heure début"}),e.jsx(h,{type:"time",name:"start_time",defaultValue:((L=a.start_time)==null?void 0:L.slice(0,5))??""})]}),e.jsxs("div",{children:[e.jsx(i,{children:"Heure fin"}),e.jsx(h,{type:"time",name:"end_time",defaultValue:((M=a.end_time)==null?void 0:M.slice(0,5))??""})]})]}),e.jsx("div",{className:"grid gap-4 md:grid-cols-3",children:e.jsxs("div",{children:[e.jsx(i,{children:"Limite d'âge"}),e.jsxs(q,{value:a.age_limit??"normal",onValueChange:t=>g({...a,age_limit:t}),children:[e.jsx(V,{children:e.jsx(k,{})}),e.jsxs(E,{children:[e.jsx(j,{value:"normal",children:"Normal"}),e.jsx(j,{value:"plus45",children:"+45 ans"})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"« +45 ans » ajoute la mention au titre du tournoi."})]})}),d&&J(s==null?void 0:s.id,v,_)&&e.jsxs("div",{className:"rounded-lg border border-destructive/30 bg-destructive/10 p-3 text-sm text-destructive",children:["Création de tournoi non autorisée à partir du ",e.jsx("b",{children:"1er octobre 2026"})," pour ton compte. Contacte un administrateur."]}),S&&(()=>{const t=S.count+1,l=S.pairs+(Q||0),n=t>3,r=l>70,c=!n&&!r;return e.jsxs("div",{className:`rounded-lg border p-3 text-sm ${c?"bg-emerald-50 border-emerald-200 text-emerald-900":"bg-amber-50 border-amber-200 text-amber-900"}`,children:[e.jsxs("div",{className:"font-bold mb-1",children:["Capacité de la journée (",_,")"]}),e.jsxs("div",{children:["Tournois : ",e.jsxs("b",{children:[t,"/3"]})," ",n&&"â au-delà de la cible"]}),e.jsxs("div",{children:["Paires totales : ",e.jsxs("b",{children:[l,"/70"]})," ",r&&"â au-delà de la cible (140 joueurs)"]}),e.jsx("div",{className:"mt-1 text-xs opacity-80",children:"Indicatif : le JA peut enregistrer même au-delà si nécessaire."})]})})(),e.jsxs("div",{children:[e.jsx(i,{children:"Juge-arbitre"}),e.jsx(h,{name:"referee_name",defaultValue:a.referee_name??"",readOnly:!0,disabled:!0,className:"bg-muted cursor-not-allowed"})]}),e.jsxs("div",{children:[e.jsx(i,{children:"Description / règles"}),e.jsx(pe,{name:"description",defaultValue:a.description??"",rows:4})]}),e.jsxs("div",{className:"rounded-lg border p-4 space-y-4",children:[v?e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx(i,{className:"text-base",children:"Paiement en ligne"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Réservé à l'admin : active ou désactive le paiement pour ce tournoi."})]}),e.jsx(H,{checked:!!a.payment_enabled,onCheckedChange:t=>g({...a,payment_enabled:t})})]}):e.jsxs("div",{children:[e.jsxs(i,{className:"text-base",children:["Paiement en ligne ",a.payment_enabled?"(obligatoire)":"(désactivé)"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:a.payment_enabled?"Ce tournoi exige une carte à l'inscription (prépaiement ou empreinte).":"Le paiement est désactivé pour ce tournoi par l'administrateur."})]}),a.payment_enabled&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(i,{children:"Prix par paire (â¬) *"}),e.jsx(h,{type:"number",name:"price_euros",min:0,step:"0.01",defaultValue:a.price_cents!=null?(a.price_cents/100).toString():"",placeholder:"Ex : 30"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Montant réglé par la paire (payé par l'inscrivant)."})]}),e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx(i,{children:"Autoriser le paiement sur place"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Le joueur peut payer au club, mais sa carte est enregistrée en garantie."})]}),e.jsx(H,{checked:a.onsite_payment_allowed!==!1,onCheckedChange:t=>g({...a,onsite_payment_allowed:t})})]})]})]}),d&&e.jsxs("div",{className:"rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm text-amber-900",children:[e.jsx("b",{children:"Brouillon :"})," ce tournoi sera enregistré en programmation et restera invisible des joueurs. Tu pourras le mettre en ligne depuis l'espace juge-arbitre une fois le planning prêt."]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsx(b,{type:"submit",className:"bg-court",disabled:A,children:A?"Sauvegarde...":d?"Enregistrer en brouillon":"Enregistrer"}),e.jsx(b,{type:"button",variant:"outline",onClick:()=>y(-1),children:"Annuler"}),ee&&e.jsxs(b,{type:"button",variant:"outline",className:"text-destructive hover:text-destructive hover:bg-destructive/10 border-destructive/30 sm:ml-auto",onClick:()=>N(!0),children:[e.jsx($,{className:"h-4 w-4 mr-2"}),"Supprimer le tournoi"]})]})]})}),e.jsx(oe,{open:X,onOpenChange:t=>{w||N(t)},children:e.jsxs(de,{className:"sm:max-w-md",children:[e.jsxs(ce,{children:[e.jsx(me,{children:"Supprimer ce brou
1illon ?"}),e.jsxs(ue,{children:["Le tournoi ",e.jsx("span",{className:"font-semibold text-foreground",children:a.title})," sera définitivement supprimé. Cette action est irréversible."]})]}),e.jsxs(xe,{className:"gap-2 sm:gap-2",children:[e.jsx(b,{variant:"ghost",disabled:w,onClick:()=>N(!1),children:"Annuler"}),e.jsxs(b,{variant:"destructive",disabled:w,onClick:te,children:[e.jsx($,{className:"h-4 w-4 mr-2"}),w?"Suppressionâ¦":"Supprimer définitivement"]})]})]})})]})};export{ve as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.