import{C as e,E as t,M as n,O as r,T as i,_ as a,a as o,b as s,f as c,h as l,k as u,l as d,m as f,o as p,p as m,s as h,t as g,w as _,x as v,y}from"./index-vhvNh9F8.js";var b={class:`page`},x={key:0},S={class:`card`,style:{margin:`6px 12px`}},C={class:`card-body`,style:{display:`flex`,gap:`10px`,"align-items":`center`}},w={style:{flex:`1`,"min-width":`0`}},T={style:{"font-size":`13px`,overflow:`hidden`,"text-overflow":`ellipsis`,"white-space":`nowrap`}},E={style:{color:`var(--danger)`,"font-weight":`700`,"margin-top":`4px`}},D={class:`cart-bottom-bar`},O={style:{display:`flex`,"align-items":`center`,gap:`10px`}},k={style:{color:`var(--danger)`}},A=h({__name:`Cart`,setup(h){let A=d(),j=r([]),M=r(!1),N=r(localStorage.getItem(`token`)||``),P=m(()=>j.value.filter(e=>e.selected).reduce((e,t)=>e+(+t.price||0)*(t.quantity||1),0)),F=m(()=>j.value.filter(e=>e.selected).length);function I(){M.value=j.value.length>0&&j.value.every(e=>e.selected)}function L(){j.value.forEach(e=>e.selected=M.value)}async function R(){if(N.value)try{let e=await o.get(`/cart/list`,{headers:{Authorization:`Bearer `+N.value}});e.code===0&&(j.value=(e.data||[]).map(e=>({...e,selected:!0})),I())}catch{}}async function z(){N.value&&(j.value=[],await o.delete(`/cart/clear`,{headers:{Authorization:`Bearer `+N.value}}).catch(()=>{}))}async function B(e){let t=j.value[e];if(t){try{await o.post(`/cart/remove`,{cart_id:t.id||t.cart_id},{headers:{Authorization:`Bearer `+N.value}})}catch{}j.value.splice(e,1),I()}}async function V(){let e=j.value.filter(e=>e.selected);if(!e.length){alert(`请选择商品`);return}let t=JSON.stringify(e.map(e=>({id:e.product_id||e.id,name:e.name,image:e.image,price:e.price,quantity:e.quantity||1})));A.push(`/checkout?items=`+encodeURIComponent(t)+`&total=`+P.value)}return v(()=>{R()}),(r,o)=>{let d=i(`van-checkbox`),m=i(`van-image`),h=i(`van-stepper`),v=i(`van-button`),N=i(`van-swipe-cell`),R=i(`van-empty`);return e(),a(`div`,b,[s(p,{title:`购物车`,onBack:o[0]||=e=>u(A).push(`/`)},{right:t(()=>[f(`span`,{style:{"font-size":`13px`,cursor:`pointer`},onClick:z},`清空`)]),_:1}),j.value.length?(e(),a(`div`,x,[(e(!0),a(c,null,_(j.value,(r,i)=>(e(),l(N,{key:r.id||i},{right:t(()=>[s(v,{square:``,type:`danger`,text:`删除`,style:{height:`100%`,"border-radius":`0`},onClick:e=>B(i)},null,8,[`onClick`])]),default:t(()=>[f(`div`,S,[f(`div`,C,[s(d,{modelValue:r.selected,"onUpdate:modelValue":e=>r.selected=e,onChange:I,shape:`square`,"checked-color":`#6C5CE7`},null,8,[`modelValue`,`onUpdate:modelValue`]),s(m,{src:r.image,style:{width:`64px`,height:`64px`,"border-radius":`8px`,"object-fit":`cover`,"flex-shrink":`0`},"lazy-load":``},{error:t(()=>[...o[3]||=[f(`div`,{class:`img-placeholder`,style:{width:`64px`,height:`64px`,"border-radius":`8px`,"font-size":`14px`}},`📷`,-1)]]),_:1},8,[`src`]),f(`div`,w,[f(`div`,T,n(r.name),1),f(`div`,E,`¥`+n(u(g)(r.price)),1)]),s(h,{modelValue:r.quantity,"onUpdate:modelValue":e=>r.quantity=e,min:`1`,max:r.stock||99,size:`26`,theme:`round`},null,8,[`modelValue`,`onUpdate:modelValue`,`max`])])])]),_:2},1024))),128)),f(`div`,D,[f(`div`,null,[s(d,{modelValue:M.value,"onUpdate:modelValue":o[1]||=e=>M.value=e,onChange:L,shape:`square`,"checked-color":`#6C5CE7`},{default:t(()=>[...o[4]||=[y(`全选`,-1)]]),_:1},8,[`modelValue`])]),f(`div`,O,[f(`span`,null,[o[5]||=y(`合计: `,-1),f(`b`,k,`¥`+n(u(g)(P.value)),1)]),s(v,{round:``,class:`primary-btn`,size:`small`,style:{padding:`0 20px`},onClick:V},{default:t(()=>[y(`结算(`+n(F.value)+`)`,1)]),_:1})])])])):(e(),l(R,{key:1,description:`购物车是空的`},{default:t(()=>[s(v,{plain:``,round:``,size:`small`,class:`primary-btn--plain`,style:{"margin-top":`12px`},onClick:o[2]||=e=>u(A).push(`/`)},{default:t(()=>[...o[6]||=[y(`去逛逛`,-1)]]),_:1})]),_:1}))])}}},[[`__scopeId`,`data-v-1c78c057`]]);export{A as default};