2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
import{_ as ue}from"./BasicMainCard-f5011b39.js";import{_ as _e}from"./BasicModal.vue_vue_type_style_index_0_lang-a547157a.js";import{_ as pe}from"./BasicTextArea.vue_vue_type_style_index_0_lang-5cc0457f.js";import{_ as me,a as he,u as G}from"./useBaseTable-13b436e1.js";import{_ as fe}from"./BasicInput.vue_vue_type_style_index_0_lang-5aefde0f.js";import{_ as ge}from"./button.vue_vue_type_style_index_0_lang-851afdd8.js";import{I as S,_ as ve,d as xe,g as T,A as ye,o as q,c as A,b as l,w as v,j as I,a as s,e as we,G as X,t as j,k as W,z as be,F as ke,p as Se,D as b,H as Q,a8 as Ce,a9 as Ie}from"./index-82b07f49.js";import{t as H}from"./useDataTime-10f46d84.js";import{I as D}from"./Icon-04c02c0b.js";import{a as Y}from"./index-a4d2dbbb.js";import{M as Ue}from"./index-5dc3a2fa.js";import{S as Te}from"./index-1f61ecf5.js";import{T as V}from"./shallowequal-17b27246.js";import{U as Me}from"./index-0f0f5d8d.js";import{_ as $e}from"./_plugin-vue_export-helper-f81f33f1.js";import"./index-ad11eb53.js";import"./Col-5d52aed9.js";import"./vnode-007df887.js";import"./isPlainObject-f3a46277.js";import"./index-c6d6dccd.js";import"./FormItemContext-f4f190fd.js";import"./index-7e5faec0.js";import"./toArray-e3bbd719.js";import"./pickAttrs-b9acd388.js";import"./useState-e0a5bff5.js";import"./index-d634ff29.js";import"./usePrice-7afab779.js";import"./useRefs-e9385981.js";import"./index-2a5d6560.js";function Z(e,i){return i.map((n,c)=>({challenge_id:e,step_order:c+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 qe(e){const{data:i,error:n}=await S.from("step_challenges").select("*").eq("id",e).single();if(n)throw n;const{data:c,error:p}=await S.from("step_challenge_steps").select("*").eq("challenge_id",e).order("step_order",{ascending:!0});if(p)throw p;return{...i,steps:(c||[]).map(m=>({...m,duration_minutes:Math.round((m.duration_seconds||60)/60)}))}}async function Ae(e,i){const{data:n,error:c}=await S.from("step_challenges").insert([{title:e.title,character_image_url:e.character_image_url,show:e.show??!1,sort_order:e.sort_order??0,owner_id:i||null}]).select().single();if(c)throw c;const p=Z(n.id,e.steps),{error:m}=await S.from("step_challenge_steps").insert(p);if(m)throw m;return n}async function Le(e,i){const{error:n}=await S.from("step_challenges").update({title:i.title,character_image_url:i.character_image_url,show:i.show??!1,sort_order:i.sort_order??0,updated_at:new Date().toISOString()}).eq("id",e);if(n)throw n;const{error:c}=await S.from("step_challenge_steps").delete().eq("challenge_id",e);if(c)throw c;const p=Z(e,i.steps),{error:m}=await S.from("step_challenge_steps").insert(p);if(m)throw m;return{id:e}}async function Be(e){const{error:i}=await S.from("step_challenges").delete().eq("id",e);if(i)throw i}async function Re(e,i){const{data:n,error:c}=await S.from("step_challenges").update({...i,updated_at:new Date().toISOString()}).eq("id",e).select();if(c)throw c;return n}async function De(e){const{TableEngineRequest:i}=await ve(()=>import("./buildSupabaseQuery-4056c4b8.js"),["assets/buildSupabaseQuery-4056c4b8.js","assets/index-82b07f49.js","assets/index-f86b861c.css"]),n={...e};!n.sort&&!n.order&&(n.order="-id");const c=await i(n,"step_challenges");try{if(Array.isArray(c==null?void 0:c.rows)){const p=c.rows,m=p.map(r=>r.id).filter(Boolean);let C={};if(m.length>0){const{data:r}=await S.from("step_challenge_steps").select("id, challenge_id, step_order, duration_seconds").in("challenge_id",m);C=(r||[]).reduce((u,a)=>(u[a.challenge_id]||(u[a.challenge_id]=[]),u[a.challenge_id].push(a),u),{})}const P=n.page||1,k=n.limit||20,M=(P-1)*k,E=p.map((r,u)=>{const a=C[r.id]||[],U=a.reduce((d,z)=>d+Math.round((z.duration_seconds||0)/60),0);return{...r,index:M+u,steps_count:a.length,total_minutes:U}});return{...c,rows:E}}}catch(p){console.log("[getStepChallengesTable] normalize error:",p)}return c}async function Pe(e){const{limit:i=20,page:n=1,search:c}=e;let p=S.from("user_step_challenges").select("*, step_challenges(title), users(name, phone, first_name, last_name)",{count:"exact"}).order("created_at",{ascending:!1});const{data:m,error:C,count:P}=await p;if(C)throw C;let k=(m||[]).map((r,u)=>{const a=Array.isArray(r.users)?r.users[0]:r.users,U=Array.isArray(r.step_challenges)?r.step_challenges[0]:r.step_challenges;let d="نامشخص";return a!=null&&a.name?d=a.name:a!=null&&a.first_name||a!=null&&a.last_name?d=`${a.first_name||""} ${a.last_name||""}`.trim():a!=null&&a.phone&&(d=a.phone),{...r,index:u,challenge_title:(U==null?void 0:U.title)||"",user_name:d,user_phone:(a==null?void 0:a.phone)||""}});if(c){const r=c.toLowerCase();k=k.filter(u=>(u.challenge_title||"").toLowerCase().includes(r)||(u.user_name||"").toLowerCase().includes(r)||(u.user_phone||"").toLowerCase().includes(r)||(u.status||"").toLowerCase().includes(r))}const M=(n-1)*i;return{rows:k.slice(M,M+i).map((r,u)=>({...r,index:M+u})),total:k.length,query:{...e,total:k.length,page:n,limit:i}}}const Ee=[{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 i=e.record.steps_count||0,n=e.record.total_minutes||0;return i?`${i} مرحله / ${n} دقیقه`:"-"}},{title:"نمایش",dataIndex:"show",key:"show",customRender:e=>e.record.show?"فعال":"مخفی"},{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"}],ze={in_progress:"در حال انجام",completed:"تکمیل شده",abandoned:"رها شده"},Ne=[{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=>ze[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):"-"}],h=e=>(Ce("data-v-4dfbd8e7"),e=e(),Ie(),e),Fe={class:"flex justify-between items-center mb-4"},Oe=h(()=>s("h2",null,"چالشهای مرحلهای",-1)),je={class:"gap-2 flex justify-center"},We=h(()=>s("span",null,"حذف",-1)),Ve=h(()=>s("span",null,"ویرایش",-1)),He=h(()=>s("div",{class:"mb-4"},[s("h2",null,"پیشرفت کاربران در چالشها")],-1)),Ke={class:"space-y-4 relative"},Je={key:0,class:"absolute inset-0 bg-white/60 z-10 flex items-center justify-center"},Ge=h(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"عنوان چالش",-1)),Xe={class:"grid grid-cols-2 gap-4"},Qe=h(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"ترتیب نمایش",-1)),Ye=h(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"وضعیت نمایش",-1)),Ze={class:"flex items-center gap-2 mt-2"},et={class:"text-sm text-gray-600"},tt={class:"flex justify-center pb-2"},st=h(()=>s("div",{class:"mb-1 text-sm text-gray-600"},"عکس کاراکتر (کارت لیست)",-1)),at={key:0},ot=h(()=>s("div",null,"آپلود تصویر",-1)),nt={key:0,class:"mt-1"},rt={class:"h-2 rounded bg-gray-200 overflow-hidden"},lt=h(()=>s("div",{class:"section-divider"},null,-1)),it={class:"pt-2"},dt=h(()=>s("div",{class:"mb-2 text-sm text-gray-600 font-semibold"},"مراحل چالش",-1)),ct={class:"space-y-3"},ut={class:"font-semibold text-gray-700"},_t=h(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"توضیحات مرحله",-1)),pt={class:"grid grid-cols-2 gap-3"},mt=h(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"زمان حل (دقیقه)",-1)),ht={class:"flex items-end pb-1"},ft={class:"flex items-center gap-2"},gt=h(()=>s("span",{class:"text-sm text-gray-600"},"نیاز به بارگذاری عکس",-1)),vt=h(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"کد embed آپارات",-1)),xt=h(()=>s("div",{class:"mb-1 text-xs text-gray-500"},"توضیح مجازات",-1)),yt={class:"flex justify-end"},wt=h(()=>s("span",{class:"text-[#DC2626]"},"حذف مرحله",-1)),bt=h(()=>s("span",{class:"text-gray-500"},"+ افزودن مرحله",-1)),kt=xe({__name:"index",setup(e){const i=Y,n=Y.TabPane,c=T("challenges"),p=T(!1),m=T(!1),C=T(null),[P,k]=G(),[M]=G(),E=ye(),r=T([]),u=T(0),a=T(0),U=()=>({description:"",duration_minutes:5,aparat_embed_code:"",punishment_description:"",requires_photo_upload:!1}),d=T({title:"",character_image_url:null,show:!1,sort_order:0,steps:[U()]}),z={columns:Ee,api:De,defaultPageLimit:20},ee={columns:Ne,api:Pe,defaultPageLimit:20};function te(_){return{width:`${Math.max(0,Math.min(100,_))}%`}}function K(){d.value={title:"",character_image_url:null,show:!1,sort_order:0,steps:[U()]},r.value=[]}function se(){m.value=!1,C.value=null,K(),p.value=!0}function J(){p.value=!1,K()}function ae(){d.value.steps.push(U())}function oe(_){d.value.steps.length<=1||d.value.steps.splice(_,1)}function ne(_){return _.type.startsWith("image/")?_.size/1024/1024<5?!0:(b.error("حجم تصویر باید کمتر از 5MB باشد!"),!1):(b.error("فقط فایلهای تصویری قابل آپلود هستند!"),!1)}async function re(_){const{file:o,onSuccess:f,onError:w,onProgress:$}=_,L=new FormData;L.append("file",o);const B=`${Q}/upload/machartaa-img`;a.value=0,u.value++,await new Promise((N,R)=>{const t=new XMLHttpRequest;t.open("POST",B),t.setRequestHeader("Authorization",`Bearer ${localStorage.getItem("token")||""}`),t.upload.onprogress=x=>{if(x.lengthComputable){const g=Math.round(x.loaded/x.total*100);a.value=g,$==null||$({percent:g})}},t.onload=()=>{var x,g;if(a.value=100,t.status>=200&&t.status<300)try{const y=JSON.parse(t.responseText||"{}"),F=y.full_src||y.url||y.src||y.path||((x=y.data)==null?void 0:x.full_src)||((g=y.data)==null?void 0:g.url)||"",O=F.startsWith("http")?F:`${Q}${F}`;d.value.character_image_url=O,r.value=O?[{uid:`${Date.now()}`,name:"image.jpg",status:"done",url:O}]:[],f==null||f(y,t),b.success("تصویر با موفقیت آپلود شد!"),N()}catch(y){w==null||w(y,t),R(y)}else{const y=new Error("خطا در آپلود تصویر");w==null||w(y,t),R(y)}},t.onerror=()=>{w==null||w(new Error("خطا در آپلود تصویر!"),t),R(new Error("خطا در آپلود تصویر!"))},t.onloadend=()=>{u.value=Math.max(0,u.value-1),a.value=0},t.send(L)})}async function le(_){try{const o=await qe(_.id);m.value=!0,C.value=_.id,d.value={title:o.title,character_image_url:o.character_image_url,show:!!o.show,sort_order:o.sort_order||0,steps:(o.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}))},o.character_image_url&&(r.value=[{uid:`${Date.now()}`,name:"image.jpg",status:"done",url:o.character_image_url}]),p.value=!0}catch(o){b.error(o.message||"خطا در بارگذاری چالش")}}async function ie(_){Ue.confirm({title:"حذف چالش",content:"آیا از حذف این چالش مطمئن هستید؟",okText:"بله",cancelText:"خیر",onOk:async()=>{await Be(_.id),b.success("چالش حذف شد"),k()}})}async function de(_){await Re(_.id,{show:!_.show}),b.success(_.show?"چالش مخفی شد":"چالش نمایش داده میشود"),k()}async function ce(){var _;if(!d.value.title.trim()){b.error("عنوان چالش الزامی است");return}if(d.value.steps.some(o=>!o.description.trim())){b.error("توضیحات همه مراحل الزامی است");return}if(d.value.steps.some(o=>!o.punishment_description.trim())){b.error("توضیح مجازات همه مراحل الزامی است");return}try{const o={...d.value,sort_order:Number(d.value.sort_order)||0};m.value&&C.value?(await Le(C.value,o),b.success("چالش ویرایش شد")):(await Ae(o,(_=E.userInfo)==null?void 0:_.id),b.success("چالش اضافه شد")),J(),k()}catch(o){b.error(o.message||"خطا در ذخیره چالش")}}return(_,o)=>{const f=ge,w=me,$=fe,L=he,B=pe,N=_e,R=ue;return q(),A("div",null,[l(R,null,{default:v(()=>[l(I(i),{activeKey:c.value,"onUpdate:activeKey":o[0]||(o[0]=t=>c.value=t)},{default:v(()=>[l(I(n),{key:"challenges",tab:"مدیریت چالشها"},{default:v(()=>[s("div",Fe,[Oe,l(f,{type:"primary",onClick:se},{default:v(()=>[we("افزودن چالش")]),_:1})]),l(w,X(z,{ref:"table",onRegister:I(P)}),{action:v(({record:t})=>[s("div",je,[l(I(V),{class:"cursor-pointer"},{title:v(()=>[s("span",null,j(t.show?"مخفی کردن":"نمایش"),1)]),default:v(()=>[l(D,{icon:t.show?"vuesax-linear:eye-slash":"vuesax-linear:eye",color:t.show?"#F59E0B":"#16A34A",onClick:x=>de(t)},null,8,["icon","color","onClick"])]),_:2},1024),l(I(V),{class:"cursor-pointer"},{title:v(()=>[We]),default:v(()=>[l(D,{icon:"vuesax-linear:trash",color:"#DC2626",onClick:x=>ie(t)},null,8,["onClick"])]),_:2},1024),l(I(V),{class:"cursor-pointer"},{title:v(()=>[Ve]),default:v(()=>[l(D,{icon:"vuesax-linear:edit",color:"#1D4ED8",onClick:x=>le(t)},null,8,["onClick"])]),_:2},1024)])]),_:1},16,["onRegister"])]),_:1}),l(I(n),{key:"progress",tab:"پیشرفت کاربران"},{default:v(()=>[He,l(w,X(ee,{onRegister:I(M)}),null,16,["onRegister"])]),_:1})]),_:1},8,["activeKey"]),l(N,{visible:p.value,"onUpdate:visible":o[5]||(o[5]=t=>p.value=t),title:m.value?"ویرایش چالش":"افزودن چالش",onOk:ce,onCancel:J,width:"85%",confirmLoading:u.value>0},{default:v(()=>[s("div",Ke,[u.value>0?(q(),A("div",Je,[l(I(Te),{size:"large"})])):W("",!0),s("div",null,[Ge,l($,{value:d.value.title,"onUpdate:value":o[1]||(o[1]=t=>d.value.title=t),placeholder:"عنوان چالش"},null,8,["value"])]),s("div",Xe,[s("div",null,[Qe,l($,{value:d.value.sort_order,"onUpdate:value":o[2]||(o[2]=t=>d.value.sort_order=t),type:"number",placeholder:"0"},null,8,["value"])]),s("div",null,[Ye,s("div",Ze,[l(L,{value:d.value.show,"onUpdate:value":o[3]||(o[3]=t=>d.value.show=t)},null,8,["value"]),s("span",et,j(d.value.show?"نمایش":"مخفی"),1)])])]),s("div",tt,[s("div",null,[st,l(I(Me),{"file-list":r.value,"onUpdate:fileList":o[4]||(o[4]=t=>r.value=t),"before-upload":ne,"custom-request":re,accept:"image/*","max-count":1,"list-type":"picture-card"},{default:v(()=>[r.value.length<1?(q(),A("div",at,[l(D,{icon:"vuesax-linear:image",class:"text-2xl mb-2"}),ot])):W("",!0)]),_:1},8,["file-list"]),a.value>0&&a.value<100?(q(),A("div",nt,[s("div",rt,[s("div",{class:"h-2 bg-blue-500 transition-all",style:be(te(a.value))},null,4)])])):W("",!0)])]),lt,s("div",it,[dt,s("div",ct,[(q(!0),A(ke,null,Se(d.value.steps,(t,x)=>(q(),A("div",{key:x,class:"border rounded-lg p-4 space-y-3"},[s("div",ut,"مرحله "+j(x+1),1),s("div",null,[_t,l(B,{value:t.description,"onUpdate:value":g=>t.description=g,placeholder:"توضیحات مرحله",rows:2},null,8,["value","onUpdate:value"])]),s("div",pt,[s("div",null,[mt,l($,{value:t.duration_minutes,"onUpdate:value":g=>t.duration_minutes=g,type:"number",min:"1",placeholder:"5"},null,8,["value","onUpdate:value"])]),s("div",ht,[s("div",ft,[l(L,{value:t.requires_photo_upload,"onUpdate:value":g=>t.requires_photo_upload=g},null,8,["value","onUpdate:value"]),gt])])]),s("div",null,[vt,l(B,{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,[xt,l(B,{value:t.punishment_description,"onUpdate:value":g=>t.punishment_description=g,placeholder:"مجازات در صورت انجام ندادن",rows:2},null,8,["value","onUpdate:value"])]),s("div",yt,[l(f,{type:"text",danger:"",onClick:g=>oe(x),disabled:d.value.steps.length<=1},{default:v(()=>[l(D,{icon:"vuesax-linear:trash",color:"#DC2626"}),wt]),_:2},1032,["onClick","disabled"])])]))),128)),l(f,{type:"dashed",onClick:ae,class:"w-full"},{default:v(()=>[bt]),_:1})])])])]),_:1},8,["visible","title","confirmLoading"])]),_:1})])}}});const Zt=$e(kt,[["__scopeId","data-v-4dfbd8e7"]]);export{Zt as default};
|