import{A as e,C as t,D as n,E as r,M as i,O as a,T as o,_ as s,a as c,b as l,f as u,g as d,k as f,l as p,m,o as h,s as g,w as _,x as v,y}from"./index-vhvNh9F8.js";var b={class:`page`},x={class:`ex-user-pts`},S={class:`pts-num`},C={key:0,style:{"text-align":`center`,padding:`40px`,color:`#999`}},w={key:1,style:{"text-align":`center`,padding:`40px`,color:`#999`,"font-size":`14px`}},T={key:2,class:`exchange-grid`},E={class:`info`},D={class:`name`},O={class:`pts`},k={class:`btn-row`},A=[`onClick`],j={class:`exchange-dialog`},M={key:0,class:`item-info`},N={class:`meta`},P={class:`n`},F={class:`p`},I={style:{"font-size":`12px`,color:`#999`,"margin-top":`2px`}},L={class:`balance`},R={key:1,class:`balance`},z=g({__name:`Exchange`,setup(g){let z=p(),B=a(localStorage.getItem(`token`)||``),V=a([]),H=a(!1),U=a(0),W=a(!1),G=a(null),K=a(!1),q=n({name:``,phone:``,address:``,remark:``});async function J(){H.value=!0;try{let[e,t]=await Promise.all([c.get(`/exchange/products`),B.value?c.get(`/user/info`,{headers:{Authorization:`Bearer `+B.value}}):Promise.resolve({code:1})]);e.code===0&&(V.value=e.data||[]),t.code===0&&(U.value=t.data?.experience||0)}catch{}H.value=!1}function Y(e){if(!B.value){z.push(`/login`);return}G.value=e,q.name=``,q.phone=``,q.address=``,q.remark=``,W.value=!0}async function X(){if(G.value){if(!q.name||!q.phone||!q.address){alert(`请填写完整的收货信息`);return}if(!/^1\d{10}$/.test(q.phone)){alert(`请输入正确的手机号`);return}K.value=!0;try{let e=await c.post(`/exchange/order`,{product_id:G.value.id,quantity:1,recipient_name:q.name,recipient_phone:q.phone,recipient_address:q.address,remark:q.remark},{headers:{Authorization:`Bearer `+B.value}});if(e.code===0){alert(`兑换成功！`),W.value=!1,U.value-=G.value.points;let e=V.value.findIndex(e=>e.id===G.value.id);e>-1&&V.value[e].stock>0&&V.value[e].stock--}else alert(e.message||`兑换失败`)}catch{alert(`网络错误`)}K.value=!1}}return v(()=>{J()}),(n,a)=>{let c=o(`van-image`),p=o(`van-field`),g=o(`van-button`),v=o(`van-action-sheet`);return t(),s(`div`,b,[l(h,{title:`积分商城`,onBack:a[1]||=e=>f(z).push(`/`)},{right:r(()=>[m(`span`,{style:{"font-size":`13px`,cursor:`pointer`},onClick:a[0]||=e=>f(z).push(`/exchange-orders`)},`记录`)]),_:1}),m(`div`,x,[a[7]||=m(`span`,null,`我的积分`,-1),m(`span`,S,i(U.value),1)]),H.value?(t(),s(`div`,C,`加载中...`)):V.value.length===0?(t(),s(`div`,w,`暂无积分商品`)):(t(),s(`div`,T,[(t(!0),s(u,null,_(V.value,e=>(t(),s(`div`,{class:`exchange-card`,key:e.id},[l(c,{src:e.image,"lazy-load":``},{error:r(()=>[...a[8]||=[m(`div`,{class:`img-placeholder`,style:{height:`140px`}},`🎁`,-1)]]),_:1},8,[`src`]),m(`div`,E,[m(`div`,D,i(e.name),1),m(`div`,O,i(e.points)+` 积分`,1),m(`div`,k,[m(`div`,{class:`ex-btn primary`,onClick:t=>Y(e)},`立即兑换`,8,A)])])]))),128))])),l(v,{show:W.value,"onUpdate:show":a[6]||=e=>W.value=e,title:`确认兑换`},{default:r(()=>[m(`div`,j,[G.value?(t(),s(`div`,M,[l(c,{src:G.value.image,"lazy-load":``},{error:r(()=>[...a[9]||=[m(`div`,{class:`img-placeholder`,style:{width:`60px`,height:`60px`,"border-radius":`6px`,"font-size":`12px`}},`📷`,-1)]]),_:1},8,[`src`]),m(`div`,N,[m(`div`,P,i(G.value.name),1),m(`div`,F,i(G.value.points)+` 积分`,1),m(`div`,I,`库存: `+i(G.value.stock)+` 件`,1)])])):d(``,!0),m(`div`,L,[a[10]||=m(`span`,null,`当前积分`,-1),m(`span`,null,i(U.value)+` 分`,1)]),G.value?(t(),s(`div`,R,[a[11]||=m(`span`,null,`兑换消耗`,-1),m(`span`,{class:e(U.value>=G.value.points?`en`:`no`)},`- `+i(G.value.points)+` 分`,3)])):d(``,!0),l(p,{modelValue:q.name,"onUpdate:modelValue":a[2]||=e=>q.name=e,label:`收货人`,placeholder:`请输入收货人姓名`,required:``},null,8,[`modelValue`]),l(p,{modelValue:q.phone,"onUpdate:modelValue":a[3]||=e=>q.phone=e,label:`联系电话`,type:`tel`,placeholder:`请输入手机号`,required:``},null,8,[`modelValue`]),l(p,{modelValue:q.address,"onUpdate:modelValue":a[4]||=e=>q.address=e,type:`textarea`,label:`收货地址`,rows:`2`,placeholder:`请输入详细收货地址`,required:``},null,8,[`modelValue`]),l(p,{modelValue:q.remark,"onUpdate:modelValue":a[5]||=e=>q.remark=e,label:`备注`,placeholder:`选填`},null,8,[`modelValue`]),l(g,{round:``,block:``,class:`primary-btn`,style:{"margin-top":`16px`,height:`44px`},onClick:X,loading:K.value,disabled:U.value<(G.value?.points||0)},{default:r(()=>[...a[12]||=[y(`确认兑换`,-1)]]),_:1},8,[`loading`,`disabled`])])]),_:1},8,[`show`])])}}},[[`__scopeId`,`data-v-52d24f0e`]]);export{z as default};