2 lines
17 KiB
JavaScript
2 lines
17 KiB
JavaScript
import{_ as ge}from"./BasicMainCard-d587bd1b.js";import{_ as ve}from"./BasicModal.vue_vue_type_style_index_0_lang-b6f776a2.js";import{_ as ye}from"./BasicTextArea.vue_vue_type_style_index_0_lang-89f52309.js";import{_ as xe,a as we,u as Q}from"./useBaseTable-b3483437.js";import{_ as be}from"./BasicInput.vue_vue_type_style_index_0_lang-3f67ac52.js";import{_ as ke}from"./button.vue_vue_type_style_index_0_lang-670ebdf7.js";import{I as C,_ as Se,d as Ie,g as A,A as Ce,o as P,c as B,b as o,w as p,j as b,a as s,e as j,G as Y,t as V,k as W,y as Ue,F as $e,n as Te,D as S,J as Me,K as Ae,H as Re}from"./index-ec71f3f7.js";import{d as qe,e as Pe,f as Be}from"./supabaseTableQuery-48420152.js";import{t as H}from"./useDataTime-c77a5d27.js";import{I as z}from"./Icon-7e71129c.js";import{e as Z,g as ee}from"./convert-67c23be8.js";import{T as te}from"./index-1856af53.js";import{M as De}from"./index-8dd0eae2.js";import{S as Ee}from"./index-415c9c8e.js";import{S as se}from"./fa_IR-c2785588.js";import{T as K}from"./shallowequal-d184109e.js";import{U as Le}from"./index-abbfc6d2.js";import{_ as Oe}from"./_plugin-vue_export-helper-93085e13.js";import"./index-6d78e32d.js";import"./index-7e8c40b1.js";import"./Col-137c655a.js";import"./vnode-abb83d2f.js";import"./isPlainObject-fa667e03.js";import"./index-87a6a3b3.js";import"./FormItemContext-343cf89e.js";import"./useState-f67c0555.js";import"./lodash-6639d8f5.js";import"./index-8157723e.js";import"./pickAttrs-0202eba3.js";import"./toArray-eb32ddb8.js";import"./usePrice-bc935b83.js";import"./useRefs-a516dbdb.js";import"./index-055a3fbb.js";function ae(e,r){return r.map((n,l)=>({challenge_id:e,step_order:l+1,description:n.description,duration_seconds:Math.max(1,Math.round(Number(n.duration_minutes)*60)),aparat_embed_code:n.aparat_embed_code||null,punishment_description:n.punishment_description,requires_photo_upload:!!n.requires_photo_upload}))}async function ze(e){const{data:r,error:n}=await C.from("step_challenges").select("*").eq("id",e).single();if(n)throw n;const{data:l,error:c}=await C.from("step_challenge_steps").select("*").eq("challenge_id",e).order("step_order",{ascending:!0});if(c)throw c;return{...r,steps:(l||[]).map(h=>({...h,duration_minutes:Math.round((h.duration_seconds||60)/60)}))}}async function Fe(e,r){const{data:n,error:l}=await C.from("step_challenges").insert([{title:e.title,character_image_url:e.character_image_url,show:e.show??!1,access_type:e.access_type??"free",sort_order:e.sort_order??0,owner_id:r||null}]).select().single();if(l)throw l;const c=ae(n.id,e.steps),{error:h}=await C.from("step_challenge_steps").insert(c);if(h)throw h;return n}async function Ne(e,r){const{error:n}=await C.from("step_challenges").update({title:r.title,character_image_url:r.character_image_url,show:r.show??!1,access_type:r.access_type??"free",sort_order:r.sort_order??0,updated_at:new Date().toISOString()}).eq("id",e);if(n)throw n;const{error:l}=await C.from("step_challenge_steps").delete().eq("challenge_id",e);if(l)throw l;const c=ae(e,r.steps),{error:h}=await C.from("step_challenge_steps").insert(c);if(h)throw h;return{id:e}}async function je(e){const{error:r}=await C.from("step_challenges").delete().eq("id",e);if(r)throw r}async function Ve(e,r){const{data:n,error:l}=await C.from("step_challenges").update({...r,updated_at:new Date().toISOString()}).eq("id",e).select();if(l)throw l;return n}async function We(e){const{TableEngineRequest:r}=await Se(()=>import("./buildSupabaseQuery-d2c8ff03.js"),["assets/buildSupabaseQuery-d2c8ff03.js","assets/index-ec71f3f7.js","assets/index-f5b104f4.css"]),n={...e};!n.sort&&!n.order&&(n.order="-id");const l=await r(n,"step_challenges");try{if(Array.isArray(l==null?void 0:l.rows)){const c=l.rows,h=c.map(y=>y.id).filter(Boolean);let I={};if(h.length>0){const{data:y}=await C.from("step_challenge_steps").select("id, challenge_id, step_order, duration_seconds").in("challenge_id",h);I=(y||[]).reduce((M,v)=>(M[v.challenge_id]||(M[v.challenge_id]=[]),M[v.challenge_id].push(v),M),{})}const T=n.page||1,u=n.limit||20,i=(T-1)*u,U=c.map((y,M)=>{const v=I[y.id]||[],R=v.reduce(($,D)=>$+Math.round((D.duration_seconds||0)/60),0);return{...y,index:i+M,steps_count:v.length,total_minutes:R}});return{...l,rows:U}}}catch(c){console.log("[getStepChallengesTable] normalize error:",c)}return l}async function Ke(e){const{limit:r=20,page:n=1,search:l}=e;let c=C.from("user_step_challenges").select("*, step_challenges(title), users(name, phone, first_name, last_name)",{count:"exact"});if(l!=null&&l.trim()){const u=`%${l.trim()}%`;c=c.or(`status.ilike.${u},step_challenges.title.ilike.${u},users.name.ilike.${u},users.phone.ilike.${u},users.first_name.ilike.${u},users.last_name.ilike.${u}`)}c=qe(c,e,"-created_at",["created_at","status"]),c=Pe(c,n,r);const{data:h,error:I,count:T}=await c;if(I)throw I;return Be(h,T,e,u=>{const i=Array.isArray(u.users)?u.users[0]:u.users,U=Array.isArray(u.step_challenges)?u.step_challenges[0]:u.step_challenges;let y="نامشخص";return i!=null&&i.name?y=i.name:i!=null&&i.first_name||i!=null&&i.last_name?y=`${i.first_name||""} ${i.last_name||""}`.trim():i!=null&&i.phone&&(y=i.phone),{...u,challenge_title:(U==null?void 0:U.title)||"",user_name:y,user_phone:(i==null?void 0:i.phone)||""}})}const He=[{title:"ردیف",dataIndex:"id",key:"id",filterable:!1,width:70,align:"center",customRender:e=>e.record.index+1},{title:"عنوان",dataIndex:"title",key:"title"},{title:"مراحل",dataIndex:"steps_count",key:"steps_count",customRender:e=>{const r=e.record.steps_count||0,n=e.record.total_minutes||0;return r?`${r} مرحله / ${n} دقیقه`:"-"}},{title:"نمایش",dataIndex:"show",key:"show",customRender:e=>e.record.show?"فعال":"مخفی"},{title:"نوع",dataIndex:"access_type",key:"access_type",width:100,align:"center",customRender:e=>e.record.access_type==="subscription"?"اشتراکی":"رایگان"},{title:"ترتیب",dataIndex:"sort_order",key:"sort_order",width:80,align:"center"},{title:"زمان ایجاد",dataIndex:"created_at",key:"created_at",customRender:e=>H(e.record.created_at)},{title:"عملیات",dataIndex:"action",key:"action",slot:"action",filterable:!1,align:"center"}],Je={in_progress:"در حال انجام",completed:"تکمیل شده",abandoned:"رها شده"},Ge=[{title:"ردیف",dataIndex:"id",key:"id",filterable:!1,width:70,align:"center",customRender:e=>e.record.index+1},{title:"چالش",dataIndex:"challenge_title",key:"challenge_title"},{title:"کاربر",dataIndex:"user_name",key:"user_name"},{title:"موبایل",dataIndex:"user_phone",key:"user_phone"},{title:"وضعیت",dataIndex:"status",key:"status",customRender:e=>Je[e.record.status]||e.record.status},{title:"مرحله فعلی",dataIndex:"current_step_order",key:"current_step_order",align:"center"},{title:"شروع",dataIndex:"started_at",key:"started_at",customRender:e=>H(e.record.started_at)},{title:"پایان",dataIndex:"completed_at",key:"completed_at",customRender:e=>e.record.completed_at?H(e.record.completed_at):"-"}],m=e=>(Me("data-v-a2451c8c"),e=e(),Ae(),e),Xe={class:"flex justify-between items-center mb-4"},Qe=m(()=>s("h2",null,"چالشهای مرحلهای",-1)),Ye={class:"gap-2 flex justify-center"},Ze=m(()=>s("span",null,"حذف",-1)),et=m(()=>s("span",null,"ویرایش",-1)),tt=m(()=>s("div",{class:"mb-4"},[s("h2",null,"پیشرفت کاربران در چالشها")],-1)),st={class:"space-y-4 relative"},at={key:0,class:"absolute inset-0 bg-white/60 z-10 flex items-center justify-center"},ot=m(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"عنوان چالش",-1)),nt={class:"grid grid-cols-1 sm:grid-cols-3 gap-4"},rt=m(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"ترتیب نمایش",-1)),lt=m(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"وضعیت نمایش",-1)),it={class:"flex items-center gap-2 mt-2"},ct={class:"text-sm text-gray-600"},dt=m(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"نوع دسترسی",-1)),ut={class:"flex justify-center pb-2"},_t=m(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"عکس کاراکتر (کارت لیست)",-1)),pt={key:0},mt=m(()=>s("div",null,"آپلود تصویر",-1)),ht={key:0,class:"mt-1"},ft={class:"h-2 rounded bg-gray-200 overflow-hidden"},gt=m(()=>s("div",{class:"section-divider"},null,-1)),vt={class:"pt-2"},yt=m(()=>s("div",{class:"mb-2 text-sm text-gray-600 font-semibold"},"مراحل چالش",-1)),xt={class:"space-y-3"},wt={class:"font-semibold text-gray-700"},bt=m(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"توضیحات مرحله",-1)),kt={class:"grid grid-cols-2 gap-3"},St=m(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"زمان حل (دقیقه)",-1)),It={class:"flex items-end pb-1"},Ct={class:"flex items-center gap-2"},Ut=m(()=>s("span",{class:"text-sm text-gray-600"},"نیاز به بارگذاری عکس",-1)),$t=m(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"کد embed آپارات",-1)),Tt=m(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"توضیح مجازات",-1)),Mt={class:"flex justify-end"},At=m(()=>s("span",{class:"text-[#DC2626]"},"حذف مرحله",-1)),Rt=m(()=>s("span",{class:"text-gray-500"},"+ افزودن مرحله",-1)),qt=Ie({__name:"index",setup(e){const r=te,n=te.TabPane,l=se,c=se.Option,h=A("challenges"),I=A(!1),T=A(!1),u=A(null),[i,U]=Q(),[y]=Q(),M=Ce(),v=A([]),R=A(0),$=A(0),D=()=>({description:"",duration_minutes:5,aparat_embed_code:"",punishment_description:"",requires_photo_upload:!1}),d=A({title:"",character_image_url:null,show:!1,access_type:"free",sort_order:0,steps:[D()]}),oe={columns:He,api:We,defaultPageLimit:20},ne={columns:Ge,api:Ke,defaultPageLimit:20};function re(_){return{width:`${Math.max(0,Math.min(100,_))}%`}}function J(){d.value={title:"",character_image_url:null,show:!1,access_type:"free",sort_order:0,steps:[D()]},v.value=[]}function le(){T.value=!1,u.value=null,J(),I.value=!0}function G(){I.value=!1,J()}function ie(){d.value.steps.push(D())}function ce(_){d.value.steps.length<=1||d.value.steps.splice(_,1)}function de(_){return _.type.startsWith("image/")?_.size/1024/1024<5?!0:(S.error("حجم تصویر باید کمتر از 5MB باشد!"),!1):(S.error("فقط فایلهای تصویری قابل آپلود هستند!"),!1)}async function ue(_){const{file:a,onSuccess:f,onError:k,onProgress:q}=_,E=new FormData;E.append("file",a);const L=`${Re}/upload/machartaa-img`;$.value=0,R.value++,await new Promise((F,O)=>{const t=new XMLHttpRequest;t.open("POST",L),t.setRequestHeader("Authorization",`Bearer ${localStorage.getItem("token")||""}`),t.upload.onprogress=x=>{if(x.lengthComputable){const g=Math.round(x.loaded/x.total*100);$.value=g,q==null||q({percent:g})}},t.onload=()=>{var x,g;if($.value=100,t.status>=200&&t.status<300)try{const w=JSON.parse(t.responseText||"{}"),fe=w.full_src||w.url||w.src||w.path||((x=w.data)==null?void 0:x.full_src)||((g=w.data)==null?void 0:g.url)||"",N=Z(fe),X=ee(N);d.value.character_image_url=N,v.value=X?[{uid:`${Date.now()}`,name:"image.jpg",status:"done",url:X,response:{path:N}}]:[],f==null||f(w,t),S.success("تصویر با موفقیت آپلود شد!"),F()}catch(w){k==null||k(w,t),O(w)}else{const w=new Error("خطا در آپلود تصویر");k==null||k(w,t),O(w)}},t.onerror=()=>{k==null||k(new Error("خطا در آپلود تصویر!"),t),O(new Error("خطا در آپلود تصویر!"))},t.onloadend=()=>{R.value=Math.max(0,R.value-1),$.value=0},t.send(E)})}async function _e(_){try{const a=await ze(_.id);T.value=!0,u.value=_.id,d.value={title:a.title,character_image_url:Z(a.character_image_url),show:!!a.show,access_type:a.access_type==="subscription"?"subscription":"free",sort_order:a.sort_order||0,steps:(a.steps||[]).map(f=>({id:f.id,description:f.description,duration_minutes:f.duration_minutes||Math.round((f.duration_seconds||60)/60),aparat_embed_code:f.aparat_embed_code||"",punishment_description:f.punishment_description||"",requires_photo_upload:!!f.requires_photo_upload}))},a.character_image_url&&(v.value=[{uid:`${Date.now()}`,name:"image.jpg",status:"done",url:ee(a.character_image_url)}]),I.value=!0}catch(a){S.error(a.message||"خطا در بارگذاری چالش")}}async function pe(_){De.confirm({title:"حذف چالش",content:"آیا از حذف این چالش مطمئن هستید؟",okText:"بله",cancelText:"خیر",onOk:async()=>{await je(_.id),S.success("چالش حذف شد"),U()}})}async function me(_){await Ve(_.id,{show:!_.show}),S.success(_.show?"چالش مخفی شد":"چالش نمایش داده میشود"),U()}async function he(){var _;if(!d.value.title.trim()){S.error("عنوان چالش الزامی است");return}if(d.value.steps.some(a=>!a.description.trim())){S.error("توضیحات همه مراحل الزامی است");return}if(d.value.steps.some(a=>!a.punishment_description.trim())){S.error("توضیح مجازات همه مراحل الزامی است");return}try{const a={...d.value,sort_order:Number(d.value.sort_order)||0};T.value&&u.value?(await Ne(u.value,a),S.success("چالش ویرایش شد")):(await Fe(a,(_=M.userInfo)==null?void 0:_.id),S.success("چالش اضافه شد")),G(),U()}catch(a){S.error(a.message||"خطا در ذخیره چالش")}}return(_,a)=>{const f=ke,k=xe,q=be,E=we,L=ye,F=ve,O=ge;return P(),B("div",null,[o(O,null,{default:p(()=>[o(b(r),{activeKey:h.value,"onUpdate:activeKey":a[0]||(a[0]=t=>h.value=t)},{default:p(()=>[o(b(n),{key:"challenges",tab:"مدیریت چالشها"},{default:p(()=>[s("div",Xe,[Qe,o(f,{type:"primary",onClick:le},{default:p(()=>[j("افزودن چالش")]),_:1})]),o(k,Y(oe,{ref:"table",onRegister:b(i)}),{action:p(({record:t})=>[s("div",Ye,[o(b(K),{class:"cursor-pointer"},{title:p(()=>[s("span",null,V(t.show?"مخفی کردن":"نمایش"),1)]),default:p(()=>[o(z,{icon:t.show?"vuesax-linear:eye-slash":"vuesax-linear:eye",color:t.show?"#F59E0B":"#16A34A",onClick:x=>me(t)},null,8,["icon","color","onClick"])]),_:2},1024),o(b(K),{class:"cursor-pointer"},{title:p(()=>[Ze]),default:p(()=>[o(z,{icon:"vuesax-linear:trash",color:"#DC2626",onClick:x=>pe(t)},null,8,["onClick"])]),_:2},1024),o(b(K),{class:"cursor-pointer"},{title:p(()=>[et]),default:p(()=>[o(z,{icon:"vuesax-linear:edit",color:"#1D4ED8",onClick:x=>_e(t)},null,8,["onClick"])]),_:2},1024)])]),_:1},16,["onRegister"])]),_:1}),o(b(n),{key:"progress",tab:"پیشرفت کاربران"},{default:p(()=>[tt,o(k,Y(ne,{onRegister:b(y)}),null,16,["onRegister"])]),_:1})]),_:1},8,["activeKey"]),o(F,{visible:I.value,"onUpdate:visible":a[6]||(a[6]=t=>I.value=t),title:T.value?"ویرایش چالش":"افزودن چالش",onOk:he,onCancel:G,width:"85%",confirmLoading:R.value>0},{default:p(()=>[s("div",st,[R.value>0?(P(),B("div",at,[o(b(Ee),{size:"large"})])):W("",!0),s("div",null,[ot,o(q,{value:d.value.title,"onUpdate:value":a[1]||(a[1]=t=>d.value.title=t),placeholder:"عنوان چالش"},null,8,["value"])]),s("div",nt,[s("div",null,[rt,o(q,{value:d.value.sort_order,"onUpdate:value":a[2]||(a[2]=t=>d.value.sort_order=t),type:"number",placeholder:"0"},null,8,["value"])]),s("div",null,[lt,s("div",it,[o(E,{value:d.value.show,"onUpdate:value":a[3]||(a[3]=t=>d.value.show=t)},null,8,["value"]),s("span",ct,V(d.value.show?"نمایش":"مخفی"),1)])]),s("div",null,[dt,o(b(l),{value:d.value.access_type,"onUpdate:value":a[4]||(a[4]=t=>d.value.access_type=t),class:"w-full"},{default:p(()=>[o(b(c),{value:"free"},{default:p(()=>[j("رایگان (همه کاربران)")]),_:1}),o(b(c),{value:"subscription"},{default:p(()=>[j("اشتراکی (فقط اشتراک فعال)")]),_:1})]),_:1},8,["value"])])]),s("div",ut,[s("div",null,[_t,o(b(Le),{"file-list":v.value,"onUpdate:fileList":a[5]||(a[5]=t=>v.value=t),"before-upload":de,"custom-request":ue,accept:"image/*","max-count":1,"list-type":"picture-card"},{default:p(()=>[v.value.length<1?(P(),B("div",pt,[o(z,{icon:"vuesax-linear:image",class:"text-2xl mb-2"}),mt])):W("",!0)]),_:1},8,["file-list"]),$.value>0&&$.value<100?(P(),B("div",ht,[s("div",ft,[s("div",{class:"h-2 bg-blue-500 transition-all",style:Ue(re($.value))},null,4)])])):W("",!0)])]),gt,s("div",vt,[yt,s("div",xt,[(P(!0),B($e,null,Te(d.value.steps,(t,x)=>(P(),B("div",{key:x,class:"border rounded-lg p-4 space-y-3"},[s("div",wt,"مرحله "+V(x+1),1),s("div",null,[bt,o(L,{value:t.description,"onUpdate:value":g=>t.description=g,placeholder:"توضیحات مرحله",rows:2},null,8,["value","onUpdate:value"])]),s("div",kt,[s("div",null,[St,o(q,{value:t.duration_minutes,"onUpdate:value":g=>t.duration_minutes=g,type:"number",min:"1",placeholder:"5"},null,8,["value","onUpdate:value"])]),s("div",It,[s("div",Ct,[o(E,{value:t.requires_photo_upload,"onUpdate:value":g=>t.requires_photo_upload=g},null,8,["value","onUpdate:value"]),Ut])])]),s("div",null,[$t,o(L,{value:t.aparat_embed_code,"onUpdate:value":g=>t.aparat_embed_code=g,placeholder:"کد embed ویدیو آپارات",rows:3},null,8,["value","onUpdate:value"])]),s("div",null,[Tt,o(L,{value:t.punishment_description,"onUpdate:value":g=>t.punishment_description=g,placeholder:"مجازات در صورت انجام ندادن",rows:2},null,8,["value","onUpdate:value"])]),s("div",Mt,[o(f,{type:"text",danger:"",onClick:g=>ce(x),disabled:d.value.steps.length<=1},{default:p(()=>[o(z,{icon:"vuesax-linear:trash",color:"#DC2626"}),At]),_:2},1032,["onClick","disabled"])])]))),128)),o(f,{type:"dashed",onClick:ie,class:"w-full"},{default:p(()=>[Rt]),_:1})])])])]),_:1},8,["visible","title","confirmLoading"])]),_:1})])}}});const ps=Oe(qt,[["__scopeId","data-v-a2451c8c"]]);export{ps as default};
|