import{d as V,g as l,v as j,o as A,c as J,b as a,w as _,a as s,e as C,G as X,j as w,D as p,_ as q}from"./index-fe7e9f3a.js";import{_ as G}from"./BasicModal.vue_vue_type_style_index_0_lang-2c965652.js";import{_ as W}from"./BasicMainCard-9d8af607.js";import{_ as z,u as H}from"./useBaseTable-e8654654.js";import{_ as K}from"./button.vue_vue_type_style_index_0_lang-61c18c46.js";import{g as B,a as Q}from"./competition-8756ace7.js";import{t as Y,a as Z}from"./useDataTime-6319c12a.js";import{I as E}from"./Icon-b952e603.js";import{_ as ee}from"./TextPreview.vue_vue_type_script_setup_true_lang-375dd543.js";import{S as te}from"./fa_IR-23a1f71d.js";import"./index-c3b05d68.js";import"./shallowequal-84daf276.js";import"./vnode-4e2eb49f.js";import"./_plugin-vue_export-helper-84433aaa.js";import"./Col-4a3e6527.js";import"./index-4bebce71.js";import"./pickAttrs-c6e9cc38.js";import"./FormItemContext-b538849a.js";import"./index-a0d96113.js";import"./index-66219801.js";import"./useState-fbb2e402.js";import"./useRefs-f56804c8.js";import"./index-265527df.js";import"./isPlainObject-8df7c091.js";import"./lodash-7b4e0250.js";import"./index-6e5ff446.js";import"./index-63f18912.js";import"./toArray-1e9f5cfb.js";const oe=[{title:"ردیف",dataIndex:"id",key:"id",filterable:!1,width:70,align:"center",customRender:u=>u.record.index+1},{title:"نام مسابقه",dataIndex:"match_title",key:"match_title"},{title:"شرح مسابقه",dataIndex:"match_description",key:"match_description",slot:"match_description"},{title:"نام کاربر",dataIndex:"user_name",key:"user_name"},{title:"شماره همراه",dataIndex:"user_mobile",key:"user_mobile"},{title:"امتیاز کسب شده",dataIndex:"score",key:"score",align:"center",customRender:u=>u.record.score||0},{title:"زمان",dataIndex:"created_at",key:"created_at",customRender:u=>Y(u.record.created_at)}],ae={class:"flex justify-between items-center mb-4"},le=s("h2",null,"امتیازات و برندگان مسابقات",-1),se={class:"flex gap-2"},ne={class:"space-y-4"},ie=s("div",{class:"mb-1 text-sm text-gray-600"},"انتخاب مسابقه",-1),re={class:"space-y-4"},ce=s("div",{class:"mb-1 text-sm text-gray-600"},"انتخاب مسابقه",-1),Ve=V({__name:"index",setup(u){const I=te,$=l(null),[T,de]=H(),f=l(!1),g=l(!1),n=l(void 0),i=l(void 0),c=l([]),h=l(!1),b=l(!1),R=l(!1),F=l({columns:oe,api:B,defaultPageLimit:20});async function y(){try{h.value=!0;const t=await Q();c.value=(t||[]).map(e=>({label:e.title||`مسابقه ${e.id}`,value:e.id}))}catch(t){console.error("Error loading matchs:",t),p.error("خطا در بارگذاری لیست مسابقات")}finally{h.value=!1}}function N(){n.value=void 0,f.value=!0,c.value.length===0&&y()}function L(){f.value=!1,n.value=void 0}function P(){i.value=void 0,g.value=!0,c.value.length===0&&y()}function M(){g.value=!1,i.value=void 0}function S(){if(!i.value){p.warning("لطفاً مسابقه را انتخاب کنید");return}const t=c.value.find(v=>v.value===i.value),e=encodeURIComponent((t==null?void 0:t.label)||`مسابقه_${i.value}`),d=`/lottery?matchId=${i.value}&title=${e}`;window.open(d,"_blank","width=1200,height=800"),M()}async function U(){if(!n.value){p.warning("لطفاً مسابقه را انتخاب کنید");return}try{b.value=!0;const t={page:1,limit:1e4,filters:[{field:"match_id",op:"eq",value:n.value,cr:"and",hidden:!1,type:"number"}]},d=(await B(t)).rows||[];if(d.length===0){p.warning("برای این مسابقه برنده‌ای یافت نشد");return}const v=d.map((r,O)=>({ردیف:O+1,"نام مسابقه":r.match_title||"","شرح مسابقه":r.match_description||"","نام کاربر":r.user_name||"","شماره همراه":r.user_mobile||"","امتیاز کسب شده":r.score||0,تاریخ:Z(r.created_at||"")})),m=await q(()=>import("./xlsx-6ed613d4.js"),[]),x=m.utils.json_to_sheet(v),o=m.utils.book_new();m.utils.book_append_sheet(o,x,"برندگان");const k=c.value.find(r=>r.value===n.value),D=`برندگان_${(k==null?void 0:k.label)||`مسابقه_${n.value}`}_${new Date().getTime()}.xlsx`;m.writeFile(o,D),p.success("فایل اکسل با موفقیت دانلود شد"),L()}catch(t){console.error("Error exporting to Excel:",t),p.error("خطا در تولید فایل اکسل")}finally{b.value=!1}}return j(()=>{y()}),(t,e)=>{const d=K,v=z,m=W,x=G;return A(),J("div",null,[a(m,null,{default:_(()=>[s("div",ae,[le,s("div",se,[a(d,{onClick:P,style:{backgroundColor:"#FF6B35",borderColor:"#FF6B35",color:"#fff"},class:"lottery-btn"},{default:_(()=>[a(E,{icon:"vuesax-linear:gift",class:"mr-1"}),C(" قرعه کشی ")]),_:1}),a(d,{onClick:N,style:{backgroundColor:"#217346",borderColor:"#217346",color:"#fff"},class:"excel-export-btn"},{default:_(()=>[a(E,{icon:"vuesax-linear:document-download",class:"mr-1"}),C(" خروجی اکسل ")]),_:1})])]),a(v,X(F.value,{ref_key:"table",ref:$,onRegister:w(T)}),{match_description:_(({record:o})=>[a(ee,{text:o.match_description},null,8,["text"])]),_:1},16,["onRegister"])]),_:1}),a(x,{visible:f.value,"onUpdate:visible":e[1]||(e[1]=o=>f.value=o),title:"خروجی اکسل برندگان مسابقه",onOk:U,onCancel:L,confirmLoading:b.value},{default:_(()=>[s("div",ne,[s("div",null,[ie,a(w(I),{value:n.value,"onUpdate:value":e[0]||(e[0]=o=>n.value=o),options:c.value,placeholder:"لطفاً مسابقه را انتخاب کنید",loading:h.value,class:"w-full","allow-clear":""},null,8,["value","options","loading"])])])]),_:1},8,["visible","confirmLoading"]),a(x,{visible:g.value,"onUpdate:visible":e[3]||(e[3]=o=>g.value=o),title:"قرعه کشی برندگان مسابقه",onOk:S,onCancel:M,confirmLoading:R.value},{default:_(()=>[s("div",re,[s("div",null,[ce,a(w(I),{value:i.value,"onUpdate:value":e[2]||(e[2]=o=>i.value=o),options:c.value,placeholder:"لطفاً مسابقه را انتخاب کنید",loading:h.value,class:"w-full","allow-clear":""},null,8,["value","options","loading"])])])]),_:1},8,["visible","confirmLoading"])])}}});export{Ve as default};