2026-06-03 16:59:33 +03:30
|
|
|
import{_ as A,I as h}from"./index-108817e3.js";async function k(){const{data:e,error:r}=await h.from("matchs").select("*");if(r)throw r;return e}async function z(e,r){const{data:n,error:s}=await h.from("matchs").update(r).eq("id",e).select();if(s)throw s;return n}async function R(e){const{data:r,error:n}=await h.from("matchs").select("*").eq("id",e).single();if(n)throw n;const{data:s,error:o}=await h.from("questions").select("*").eq("match_id",e);if(o)throw o;return{...r,questions:s||[]}}async function P(e){console.log("addMatchWithQuestions payload:",e);const{data:r,error:n}=await h.from("matchs").insert([e.match]).select().single();if(console.log("Match insert result:",{matchData:r,matchError:n}),n)throw console.error("Match insert error:",n),n;for(const s of e.questions){const{data:o,error:f}=await h.from("questions").insert([{match_id:r.id,question:s.question,options:s.options,correct_answer:s.correct_answer}]).select().single();if(f)throw f;console.log("Question created:",o)}return r}async function V(e,r){const{error:n}=await h.from("matchs").update(r.match).eq("id",e);if(n)throw n;const{data:s}=await h.from("questions").select("id").eq("match_id",e);if(s)for(const o of s)await h.from("questions").delete().eq("id",o.id);for(const o of r.questions){const{data:f,error:g}=await h.from("questions").insert([{match_id:e,question:o.question,options:o.options,correct_answer:o.correct_answer}]).select().single();if(g)throw g;console.log("Question updated:",f)}return{id:e}}async function $(e){await h.from("questions").delete().eq("match_id",e);const{error:r}=await h.from("matchs").delete().eq("id",e);if(r)throw r}async function O(e){const{TableEngineRequest:r}=await A(()=>import("./buildSupabaseQuery-03e3555e.js"),["assets/buildSupabaseQuery-03e3555e.js","assets/index-108817e3.js","assets/index-6f54f748.css"]),n={...e};!n.sort&&!n.order&&(n.order="-id");const s=await r(n,"matchs");try{if(Array.isArray(s==null?void 0:s.rows)){const o=n.page||1,f=n.limit||200,g=(o-1)*f,q=s.rows.map((_,p)=>({id:_.id,title:_.title??"",description:_.description??"",created_at:_.created_at??"",index:g+p,..._}));return{...s,rows:q}}}catch(o){console.log("[getMatchTable] normalize error:",o)}return s}async function j(e){var r,n,s,o,f;try{const{limit:g,filters:q,page:_,sort:p,order:l,search:m}=e;let i=h.from("answers").select("*, matchs!match_id(id, title, description, minimum_score), users!user_id(id, first_name, last_name, phone, name, email, username)",{count:"exact"});q&&Array.isArray(q)&&q.forEach(t=>{const{field:a,op:c,value:d,hidden:u}=t;u||(a==="match_id"&&c==="eq"?i=i.eq("match_id",d):a==="match_id"&&c==="in"&&(i=i.in("match_id",Array.isArray(d)?d:[d])))});const{data:M,error:w,count:L}=await i;if(w)throw console.error("[getWinnersTable] Supabase query error:",w),w;if(!M||M.length===0)return{rows:[],total:0,query:{...e,total:0}};let y=M.map(t=>{const c=(Array.isArray(t.matchs)?t.matchs[0]:t.matchs)||{},u=(Array.isArray(t.users)?t.users[0]:t.users)||{},S=c.minimum_score?Number(c.minimum_score):0,T=t.score?Number(t.score):0;let b="نامشخص";return u&&Object.keys(u).length>0&&(u.name?b=u.name:u.first_name||u.last_name?b=`${u.first_name||""} ${u.last_name||""}`.trim():u.phone?b=u.phone:u.email?b=u.email:u.username&&(b=u.username)),{id:t.id,match_id:t.match_id,user_id:t.user_id,score:t.score,match_title:c.title||"",match_description:c.description||"",match_minimum_score:c.minimum_score||null,user_name:b,user_mobile:(u==null?void 0:u.phone)||"",created_at:t.created_at||"",_match:c,_user:u,_userScore:T,_minimumScore:S}}).filter(t=>t._userScore>=t._minimumScore).map(t=>{const{_match:a,_user:c,_userScore:d,_minimumScore:u,...S}=t;return S});if(e.search){const t=e.search.toLowerCase();y=y.filter(a=>(a.match_title||"").toLowerCase().includes(t)||(a.match_description||"").toLowerCase().includes(t)||(a.user_name||"").toLowerCase().includes(t)||(a.user_mobile||"").toLowerCase().includes(t)||String(a.score||"").includes(t))}e.filters&&e.filters.length>0&&(y=y.filter(t=>e.filters.every(a=>{if(!a.field||a.hidden)return!0;const c=t[a.field],d=a.value;if(d=
|