2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
import{_ as re}from"./BasicMainCard-6d707328.js";import{M as ue,_ as ce}from"./BasicModal.vue_vue_type_style_index_0_lang-a11eae64.js";import{_ as de}from"./BasicTextArea.vue_vue_type_style_index_0_lang-4ee7776d.js";import{_ as pe}from"./BasicInput.vue_vue_type_style_index_0_lang-392b5b97.js";import{_ as me}from"./index-ccdd9f59.js";import{_ as _e}from"./button.vue_vue_type_style_index_0_lang-ad1ddd30.js";import{a3 as ve,a5 as X,d as fe,r as b,Y as he,i as ge,o as M,c as S,b as c,w,a as t,e as xe,a2 as ye,L as T,N as B,X as H,O as W,F as be,S as we,a0 as D,a4 as P}from"./index-89ffa310.js";import{t as ke}from"./useDataTime-8307199e.js";import{I as R}from"./Icon-71136976.js";import{u as Ce}from"./useBaseTable-3aeb2499.js";import{_ as $e}from"./TextPreview.vue_vue_type_script_setup_true_lang-ca174b43.js";import{S as Ue}from"./index-d95ef8ac.js";import{T as G}from"./styleChecker-47862663.js";import{U as J}from"./index-816ff657.js";import"./_plugin-vue_export-helper-0eb14515.js";import"./index-956b313f.js";import"./useState-33268b88.js";import"./vnode-75acd593.js";import"./Col-fc0de6a1.js";import"./index-7d158bfa.js";import"./usePrice-7afab779.js";import"./index-a33d0202.js";async function Me(m){console.log("API addChallenge payload:",m);const d={title:m.title,description:m.description,owner_id:m.owner_id,minute:m.minute,score:m.score,image_url:m.image_url||null,clip_url:m.clip_url||null,steps:m.steps||[]};console.log("Supabase insert data:",d);const{data:f,error:_}=await X.from("challenge").insert([d]);if(_)throw console.error("Supabase insert error:",_),_;return console.log("Supabase insert success:",f),f}async function Se(m,d){const{data:f,error:_}=await X.from("challenge").update({title:d.title,description:d.description,owner_id:d.owner_id,minute:d.minute,score:d.score,image_url:d.image_url||null,clip_url:d.clip_url||null,steps:d.steps||[]}).eq("id",m).select();if(_)throw _;return f}async function Ie(m){const{error:d}=await X.from("challenge").delete().eq("id",m);if(d)throw d}async function Le(m){const{TableEngineRequest:d}=await ve(()=>import("./buildSupabaseQuery-64b2d9ac.js"),["assets/buildSupabaseQuery-64b2d9ac.js","assets/index-89ffa310.js","assets/index-c73797be.css"]),f={...m};!f.sort&&!f.order&&(f.order="-id");const _=await d(f,"challenge");try{if(Array.isArray(_==null?void 0:_.rows)){const C=f.page||1,k=f.limit||200,I=(C-1)*k,x=_.rows.map((h,U)=>({id:h.id,title:h.title??"",description:h.description??"",steps:h.steps??[],created_at:h.created_at??"",index:I+U,...h}));return{..._,rows:x}}}catch(C){console.log("[getChallengeTable] normalize error:",C)}return _}const De=[{title:"ردیف",dataIndex:"id",key:"id",filterable:!1,width:70,align:"center",customRender:m=>m.record.index+1},{title:"عنوان",dataIndex:"title",key:"title"},{title:"شرح",dataIndex:"description",key:"description",slot:"description"},{title:"مراحل",dataIndex:"steps",key:"steps",customRender:m=>{const d=m.record.steps||[];if(d.length===0)return"-";const f=d.reduce((C,k)=>C+(Number(k.minute)||0),0),_=d.reduce((C,k)=>C+(Number(k.score)||0),0);return`${d.length} مرحله / ${f} دقیقه / ${_} امتیاز`}},{title:"زمان ایجاد",dataIndex:"created_at",key:"created_at",customRender:m=>ke(m.record.created_at)},{title:"عملیات",dataIndex:"action",key:"action",slot:"action",filterable:!1,align:"center"}],Te={class:"flex justify-between items-center mb-4"},Be=t("h2",null,"چالش ها",-1),Pe={class:"gap-2 flex justify-center"},Re=t("span",null,"حذف",-1),qe=t("span",null,"ویرایش",-1),Ae={class:"space-y-4 relative"},Ne={key:0,class:"absolute inset-0 bg-white/60 z-10 flex items-center justify-center"},Ve=t("div",{class:"mb-1 text-sm text-gray-600"},"عنوان چالش",-1),ze=t("div",{class:"mb-1 text-sm text-gray-600"},"شرح چالش",-1),Oe={class:"flex justify-center gap-10 pb-4"},Fe=t("div",{class:"mb-1 text-sm text-gray-600"},"آپلود تصویر",-1),He={key:0},We=t("div",null,"آپلود تصویر",-1),Je={key:0,class:"mt-1"},Xe={class:"h-2 rounded bg-gray-200 overflow-hidden"},je={class:"text-xs text-gray-700 font-semibold text-center mt-1"},Ye=t("div",{class:"mb-1 text-sm text-gray-600"},"آپلود ویدئو",-1),Ge={key:0},Ke=t("div",null,"آپلود ویدئو",-1),Qe={key:0,class:"mt-1"},Ze={class:"h-2 rounded bg-gray-200 overflow-hidden"},Ee={class:"text-xs text-gray-700 font-semibold text-center mt-1"},et=t("div",{class:"section-divider"},null,-1),tt={class:"pt-4"},st=t("div",{class:"mb-1 text-sm text-gray-600"},"مراحل چالش",-1),lt={class:"space-y-3"},at={class:"grid grid-flow-col grid-rows-2 gap-4"},it={class:"col-span-2"},ot=t("div",{class:"mb-1 text-xs text-gray-500"},"عنوان مرحله",-1),nt={class:"col-span-2 flex gap-4"},rt={class:"flex-1"},ut=t("div",{class:"mb-1 text-xs text-gray-500"},"زمان (دقیقه)",-1),ct={class:"flex-1"},dt=t("div",{class:"mb-1 text-xs text-gray-500"},"امتیاز",-1),pt={class:"row-span-3 justify-self-center"},mt=t("div",{class:"mb-1 text-xs text-gray-500"},"آپلود ویدئوی مرحله",-1),_t={key:0},vt=t("div",{class:"text-xs"},"آپلود ویدئو",-1),ft={key:0,class:"text-xs text-gray-500 mt-1"},ht={class:"h-2 rounded bg-gray-200 overflow-hidden"},gt={class:"text-xs text-gray-700 font-semibold text-center mt-1"},xt=t("div",{class:"mb-1 text-xs text-gray-500"},"توضیحات مرحله",-1),yt={class:"flex justify-end"},bt=t("span",{class:"text-[#DC2626]"},"حذف مرحله",-1),wt=t("span",{class:"text-gray-500"},"+ افزودن مرحله",-1),Jt=fe({__name:"index",setup(m){b([]),b(!1);const d=b(null),[f,_]=Ce(),C=he(),k=b([]),I=b([]),x=b(0),h=b(0),U=b(0),L=b({});function V(s){return L.value[s]??0}function z(s){return{width:`${Math.max(0,Math.min(100,s))}%`}}function j(s){return s.type.startsWith("video/")?s.size/1024/1024<60?!0:(D.error("حجم فایل باید کمتر از 60MB باشد!"),!1):(D.error("فقط فایلهای ویدئو قابل آپلود هستند!"),!1)}async function K(s){const{file:i,onSuccess:p,onError:n,onProgress:u}=s,g=new FormData;g.append("file",i);const $=`${P}/upload/video`;U.value=0,x.value++,await new Promise((y,a)=>{const l=new XMLHttpRequest;l.open("POST",$),l.setRequestHeader("Authorization",`Bearer ${localStorage.getItem("token")||""}`),l.upload.onprogress=e=>{if(e.lengthComputable){const o=Math.round(e.loaded/e.total*100);U.value=o,u==null||u({percent:o})}},l.onload=()=>{if(U.value=100,l.status>=200&&l.status<300)try{const e=JSON.parse(l.responseText||"{}"),o=e&&(e.full_src||e.url||e.src||e.path||e.data&&(e.data.full_src||e.data.url||e.data.src||e.data.path))||"",v=o.startsWith("http")?o:`${P}${o}`;r.value.clip_url=v,k.value=v?[{uid:`${Date.now()}`,name:"video.mp4",status:"done",url:v,response:{path:v}}]:[],p==null||p(e,l),D.success("ویدئو با موفقیت آپلود شد!"),y()}catch(e){n==null||n(e,l),a(e)}else{const e=new Error("خطا در آپلود فایل");n==null||n(e,l),a(e)}},l.onerror=()=>{const e=new Error("خطا در آپلود ویدئو!");n==null||n(e,l),a(e)},l.onloadend=()=>{x.value=Math.max(0,x.value-1),U.value=0},l.send(g)})}function Q(s){return s.type.startsWith("image/")?s.size/1024/1024<5?!0:(D.error("حجم تصویر باید کمتر از 5MB باشد!"),!1):(D.error("فقط فایلهای تصویری قابل آپلود هستند!"),!1)}async function Z(s){const{file:i,onSuccess:p,onError:n,onProgress:u}=s,g=new FormData;g.append("file",i);const $=`${P}/upload/machartaa-img`;h.value=0,x.value++,await new Promise((y,a)=>{const l=new XMLHttpRequest;l.open("POST",$),l.setRequestHeader("Authorization",`Bearer ${localStorage.getItem("token")||""}`),l.upload.onprogress=e=>{if(e.lengthComputable){const o=Math.round(e.loaded/e.total*100);h.value=o,u==null||u({percent:o})}},l.onload=()=>{if(h.value=100,l.status>=200&&l.status<300)try{const e=JSON.parse(l.responseText||"{}"),o=e&&(e.full_src||e.url||e.src||e.path||e.data&&(e.data.full_src||e.data.url||e.data.src||e.data.path))||"",v=o.startsWith("http")?o:`${P}${o}`;r.value.image_url=v,I.value=v?[{uid:`${Date.now()}`,name:"image.jpg",status:"done",url:v,response:{path:v}}]:[],p==null||p(e,l),D.success("تصویر با موفقیت آپلود شد!"),y()}catch(e){n==null||n(e,l),a(e)}else{const e=new Error("خطا در آپلود تصویر");n==null||n(e,l),a(e)}},l.onerror=()=>{const e=new Error("خطا در آپلود تصویر!");n==null||n(e,l),a(e)},l.onloadend=()=>{x.value=Math.max(0,x.value-1),h.value=0},l.send(g)})}async function E(s,i){const{file:p,onSuccess:n,onError:u,onProgress:g}=s,$=new FormData;$.append("file",p);const y=`${P}/upload/video`;L.value={...L.value,[i]:0},x.value++,await new Promise((a,l)=>{const e=new XMLHttpRequest;e.open("POST",y),e.setRequestHeader("Authorization",`Bearer ${localStorage.getItem("token")||""}`),e.upload.onprogress=o=>{if(o.lengthComputable){const v=Math.round(o.loaded/o.total*100);L.value={...L.value,[i]:v},g==null||g({percent:v})}},e.onload=()=>{if(L.value={...L.value,[i]:100},e.status>=200&&e.status<300)try{const o=JSON.parse(e.responseText||"{}"),v=o.full_src||o.src||o.path||o.url,O=v.startsWith("http")?v:`${P}${v}`,F=r.value.steps[i];F&&(F.clip_url=O||"",F.fileList=[{uid:`${Date.now()}`,name:"video.mp4",status:"done",url:O,response:{path:O}}]),n==null||n(o,e),D.success("ویدئوی مرحله با موفقیت آپلود شد!"),a()}catch(o){u==null||u(o,e),l(o)}else{const o=new Error("خطا در آپلود فایل");u==null||u(o,e),l(o)}},e.onerror=()=>{const o=new Error("خطا در آپلود ویدئوی مرحله!");u==null||u(o,e),l(o)},e.onloadend=()=>{x.value=Math.max(0,x.value-1),L.value={...L.value,[i]:0}},e.send($)})}const ee=b({columns:De,api:Le,defaultPageLimit:20}),q=b(!1),A=b(!1),N=b(null),r=b({title:"",description:"",owner_id:"",minute:0,score:0,image_url:"",clip_url:"",steps:[{id:1,title:"",description:"",clip_url:"",minute:0,score:0}]});async function te(){}async function se(){var i;await C.getMyInfo();const s=((i=C.userInfo)==null?void 0:i.id)||"0";console.log("Current user info:",C.userInfo),console.log("Current user ID:",s),k.value=[],I.value=[],r.value={title:"",description:"",owner_id:s,minute:0,score:0,image_url:"",clip_url:"",steps:[{id:1,title:"",description:"",clip_url:"",minute:0,score:0}]},A.value=!1,N.value=null,q.value=!0}function Y(){q.value=!1,A.value=!1,N.value=null}async function le(){var n,u,g,$;if(!((n=r.value.title)!=null&&n.trim()))return;const s=r.value.steps.reduce((y,a)=>y+(Number(a.minute)||0),0),i=r.value.steps.reduce((y,a)=>y+(Number(a.score)||0),0),p={title:r.value.title.trim(),description:((u=r.value.description)==null?void 0:u.trim())||"",owner_id:r.value.owner_id,minute:s,score:i,image_url:((g=r.value.image_url)==null?void 0:g.trim())||null,clip_url:(($=r.value.clip_url)==null?void 0:$.trim())||null,steps:r.value.steps.filter(y=>y.title.trim()||y.description.trim())};if(x.value>0){D.warning("لطفاً تا پایان آپلود ویدئوها صبر کنید.");return}console.log("Challenge payload:",p),A.value&&N.value?await Se(N.value,p):await Me(p),Y(),await _()}function ae(s){s.clip_url?k.value=[{uid:"-1",name:"video.mp4",status:"done",url:s.clip_url,response:{path:s.clip_url}}]:k.value=[],s.image_url?I.value=[{uid:"-2",name:"image.jpg",status:"done",url:s.image_url,response:{path:s.image_url}}]:I.value=[],r.value={title:s.title||"",description:s.description||"",owner_id:s.owner_id||"",minute:s.minute||0,score:s.score||0,image_url:s.image_url||"",clip_url:s.clip_url||"",steps:s.steps&&s.steps.length>0?s.steps.map((i,p)=>({id:p+1,title:i.title||"",description:i.description||"",clip_url:i.clip_url||"",minute:i.minute||0,score:i.score||0})):[{id:1,title:"",description:"",clip_url:"",minute:0,score:0}]},A.value=!0,N.value=s.id,q.value=!0}function ie(s){ue.confirm({title:"حذف چالش",content:`آیا مطمئن هستید که میخواهید چالش "${s.title}" را حذف کنید؟`,okText:"بله، حذف کن",cancelText:"انصراف",okType:"danger",centered:!0,onOk:async()=>{await Ie(s.id),await _()}})}function oe(){r.value.steps.push({id:r.value.steps.length+1,title:"",description:"",clip_url:"",minute:0,score:0})}function ne(s){r.value.steps.length>1&&(r.value.steps.splice(s,1),r.value.steps.forEach((i,p)=>{i.id=p+1}))}return ge(te),(s,i)=>{const p=_e,n=me,u=pe,g=de,$=ce,y=re;return M(),S("div",null,[c(y,null,{default:w(()=>[t("div",Te,[Be,c(p,{type:"primary",onClick:se},{default:w(()=>[xe("افزودن چالش")]),_:1})]),c(n,ye(ee.value,{ref_key:"table",ref:d,onRegister:T(f)}),{description:w(({record:a})=>[c($e,{text:a.description},null,8,["text"])]),action:w(({record:a})=>[t("div",Pe,[c(T(G),{class:"cursor-pointer"},{title:w(()=>[Re]),default:w(()=>[c(R,{icon:"vuesax-linear:trash",color:"#DC2626",onClick:l=>ie(a)},null,8,["onClick"])]),_:2},1024),c(T(G),{class:"cursor-pointer"},{title:w(()=>[qe]),default:w(()=>[c(R,{icon:"vuesax-linear:edit",color:"#1D4ED8",onClick:l=>ae(a)},null,8,["onClick"])]),_:2},1024)])]),_:1},16,["onRegister"]),c($,{visible:q.value,"onUpdate:visible":i[4]||(i[4]=a=>q.value=a),title:A.value?"ویرایش چالش":"افزودن چالش",onOk:le,onCancel:Y,width:"80%",confirmLoading:x.value>0},{default:w(()=>[t("div",Ae,[x.value>0?(M(),S("div",Ne,[c(T(Ue),{size:"large"})])):B("",!0),t("div",null,[Ve,c(u,{value:r.value.title,"onUpdate:value":i[0]||(i[0]=a=>r.value.title=a),placeholder:"عنوان چالش"},null,8,["value"])]),t("div",null,[ze,c(g,{value:r.value.description,"onUpdate:value":i[1]||(i[1]=a=>r.value.description=a),placeholder:"شرح چالش"},null,8,["value"])]),t("div",Oe,[t("div",null,[Fe,c(T(J),{"file-list":I.value,"onUpdate:fileList":i[2]||(i[2]=a=>I.value=a),"before-upload":Q,"custom-request":Z,accept:"image/*","max-count":1,"list-type":"picture-card"},{default:w(()=>[I.value.length<1?(M(),S("div",He,[c(R,{icon:"vuesax-linear:image",class:"text-2xl mb-2"}),We])):B("",!0)]),_:1},8,["file-list"]),h.value>0&&h.value<100?(M(),S("div",Je,[t("div",Xe,[t("div",{class:"h-2 bg-blue-500 transition-all",style:H(z(h.value))},null,4)]),t("div",je,W(h.value)+"%",1)])):B("",!0)]),t("div",null,[Ye,c(T(J),{"file-list":k.value,"onUpdate:fileList":i[3]||(i[3]=a=>k.value=a),"before-upload":j,"custom-request":K,accept:"video/*","max-count":1,"list-type":"picture-card"},{default:w(()=>[k.value.length<1?(M(),S("div",Ge,[c(R,{icon:"vuesax-linear:video",class:"text-2xl mb-2"}),Ke])):B("",!0)]),_:1},8,["file-list"]),U.value>0&&U.value<100?(M(),S("div",Qe,[t("div",Ze,[t("div",{class:"h-2 bg-blue-500 transition-all",style:H(z(U.value))},null,4)]),t("div",Ee,W(U.value)+"%",1)])):B("",!0)])]),et,t("div",tt,[st,t("div",lt,[(M(!0),S(be,null,we(r.value.steps,(a,l)=>(M(),S("div",{key:l,class:"border rounded-lg p-3 space-y-3"},[t("div",at,[t("div",it,[ot,c(u,{value:a.title,"onUpdate:value":e=>a.title=e,placeholder:`مرحله ${l+1}`},null,8,["value","onUpdate:value","placeholder"])]),t("div",nt,[t("div",rt,[ut,c(u,{value:a.minute,"onUpdate:value":e=>a.minute=e,type:"number",placeholder:"0"},null,8,["value","onUpdate:value"])]),t("div",ct,[dt,c(u,{value:a.score,"onUpdate:value":e=>a.score=e,type:"number",placeholder:"0"},null,8,["value","onUpdate:value"])])]),t("div",pt,[mt,c(T(J),{"file-list":a.fileList||[],"before-upload":j,"custom-request":e=>E(e,l),accept:"video/*","max-count":1,"list-type":"picture-card"},{default:w(()=>[a.fileList&&a.fileList.length?B("",!0):(M(),S("div",_t,[c(R,{icon:"vuesax-linear:video",class:"text-xl mb-1"}),vt]))]),_:2},1032,["file-list","custom-request"]),V(l)>0&&V(l)<100?(M(),S("div",ft,[t("div",ht,[t("div",{class:"h-2 bg-blue-500 transition-all",style:H(z(V(l)))},null,4)]),t("div",gt,"در حال آپلود ویدئو... "+W(V(l))+"%",1)])):B("",!0)])]),xt,c(g,{value:a.description,"onUpdate:value":e=>a.description=e,placeholder:"توضیحات مرحله",rows:2},null,8,["value","onUpdate:value"]),t("div",yt,[c(p,{type:"text",danger:"",onClick:e=>ne(l),disabled:r.value.steps.length<=1},{default:w(()=>[c(R,{icon:"vuesax-linear:trash",color:"#DC2626"}),bt]),_:2},1032,["onClick","disabled"])])]))),128)),c(p,{type:"dashed",onClick:oe,class:"w-full"},{default:w(()=>[wt]),_:1})])])])]),_:1},8,["visible","title","confirmLoading"])]),_:1})])}}});export{Jt as default};
|