import{d as U,r as c,v as W,o as $,g as v,J as q,c as _,k as u,K as H,z as w,w as l,H as I,e as r,t as d,l as p,C as k,F as J,y as K,Q as V,b as C,E as o,D as L,_ as Q}from"./index-CUnFTKV2.js";import{l as j,m as G,k as O}from"./config-J21_Zans.js";import X from"./MiniprogramForm-C_qHhOxK.js";const Y={class:"page-container"},Z={class:"card-header"},ee={class:"header-actions"},ae={key:1,class:"miniprogram-list"},te={key:2,class:"footer-actions"},ne=U({__name:"miniprogram",setup(re){const h=c(!1),m=c(!1),t=c([]),D=c([]),f=c([]),B=W(()=>t.value.map(n=>n.order_prefix||"")),y=()=>({name:"",appid:"",appsecret:"",order_prefix:"",is_default:t.value.length===0?1:0,merchants:[]}),F=async()=>{var n,e;try{const a=await O();(e=(n=a.data)==null?void 0:n.value)!=null&&e.merchants&&(D.value=a.data.value.merchants)}catch(a){console.error("加载微信支付商户列表失败:",a)}},P=async()=>{h.value=!0;try{await Promise.all([S(),F()])}finally{h.value=!1}},S=async()=>{var n,e;try{const a=await j();(e=(n=a.data)==null?void 0:n.value)!=null&&e.miniprograms&&a.data.value.miniprograms.length>0?t.value=a.data.value.miniprograms.map(s=>({name:s.name||"",appid:s.appid||"",appsecret:s.appsecret||"",order_prefix:s.order_prefix||"",is_default:s.is_default??0,merchants:s.merchants||[]})):t.value=[y()]}catch(a){o.error("加载配置失败"),console.error("加载配置失败:",a),t.value=[y()]}},x=()=>{t.value.push(y()),o.success("已添加新小程序，请填写配置信息")},E=async n=>{if(t.value.length<=1){o.warning("至少保留一个小程序配置");return}const e=t.value[n].is_default===1;try{await L.confirm(`确定要删除小程序"${t.value[n].name||"未命名"}"吗？${e?"（该小程序为默认小程序，删除后请设置新的默认小程序）":""}`,"删除确认",{confirmButtonText:"确定",cancelButtonText:"取消",type:"warning"}),t.value.splice(n,1),e&&t.value.length>0&&(t.value[0].is_default=1,o.info("已将第一个小程序设为默认")),o.success("已删除小程序")}catch{}},b=()=>{},N=n=>{t.value.forEach((e,a)=>{a!==n&&(e.is_default=0)})},T=async()=>!f.value||f.value.length===0?!0:(await Promise.all(f.value.map(e=>{var a;return((a=e==null?void 0:e.validate)==null?void 0:a.call(e))??Promise.resolve(!0)}))).every(e=>e===!0),M=async()=>{if(!await T()){o.warning("请检查表单填写是否正确");return}if(t.value.length===0){o.warning("至少需要配置一个小程序");return}if(!t.value.some(i=>i.is_default===1)){o.warning("必须设置一个默认小程序");return}const a=t.value.map(i=>i.order_prefix).filter(i=>i),s=new Set(a);if(a.length!==s.size){o.warning("存在重复的订单前缀，请检查");return}m.value=!0;try{const i={miniprograms:t.value};await G(i),o.success("保存成功")}catch(i){o.error("保存失败"),console.error("保存配置失败:",i)}finally{m.value=!1}};return $(()=>{P()}),(n,e)=>{const a=v("el-icon"),s=v("el-button"),i=v("el-empty"),z=v("el-card"),A=q("loading");return u(),_("div",Y,[H((u(),w(z,null,{header:l(()=>[C("div",Z,[e[2]||(e[2]=C("span",null,"小程序配置",-1)),C("div",ee,[r(s,{type:"primary",onClick:x},{default:l(()=>[r(a,null,{default:l(()=>[r(p(k))]),_:1}),e[0]||(e[0]=d("添加小程序 ",-1))]),_:1}),r(s,{type:"success",loading:m.value,onClick:M},{default:l(()=>[r(a,null,{default:l(()=>[r(p(V))]),_:1}),e[1]||(e[1]=d("保存配置 ",-1))]),_:1},8,["loading"])])])]),default:l(()=>[t.value.length===0?(u(),w(i,{key:0,description:"暂无小程序配置，请点击添加小程序"},{default:l(()=>[r(s,{type:"primary",onClick:x},{default:l(()=>[r(a,null,{default:l(()=>[r(p(k))]),_:1}),e[3]||(e[3]=d("添加小程序 ",-1))]),_:1})]),_:1})):(u(),_("div",ae,[(u(!0),_(J,null,K(t.value,(se,g)=>(u(),w(X,{key:g,ref_for:!0,ref_key:"miniprogramFormRefs",ref:f,modelValue:t.value[g],"onUpdate:modelValue":R=>t.value[g]=R,index:g,"can-delete":t.value.length>1,"existing-prefixes":B.value,"weixin-merchants":D.value,onDelete:E,onChange:b,onDefaultChange:N},null,8,["modelValue","onUpdate:modelValue","index","can-delete","existing-prefixes","weixin-merchants"]))),128))])),t.value.length>0?(u(),_("div",te,[r(s,{type:"primary",onClick:x},{default:l(()=>[r(a,null,{default:l(()=>[r(p(k))]),_:1}),e[4]||(e[4]=d("添加小程序 ",-1))]),_:1}),r(s,{type:"success",loading:m.value,onClick:M},{default:l(()=>[r(a,null,{default:l(()=>[r(p(V))]),_:1}),e[5]||(e[5]=d("保存配置 ",-1))]),_:1},8,["loading"])])):I("",!0)]),_:1})),[[A,h.value]])])}}}),ue=Q(ne,[["__scopeId","data-v-895ed96a"]]);export{ue as default};
