import{$ as e,B as t,F as n,I as r,J as i,K as a,N as o,P as s,Q as c,R as l,S as u,St as d,V as f,W as p,X as m,Y as h,Z as g,_t as _,a as v,at as y,bt as b,ct as x,et as S,g as C,gt as w,h as T,ht as E,i as D,it as O,k,lt as A,mt as j,n as M,nt as N,o as P,ot as F,pt as I,q as L,rt as R,st as z,tt as B,ut as V,vt as H,x as U,xt as W,y as G,yt as K,z as q}from"./index-BHqDDh7X.js";import{t as J}from"./AdminSelect-BjB6PHdr.js";import{r as Y}from"./adminLabels-Cz9ccm1Z.js";import{c as X,i as Z,n as Q,o as ee,s as te,t as ne}from"./orderStatus-CYVblRJB.js";import{t as re}from"./StatusBadge-BMzuiwOg.js";function ie(){let e=n(),t=r(),i=q(),a=H([]),s=H([]),c=new Set(Q.map(e=>e.key));function l(){let t=typeof e.query.tab==`string`?e.query.tab:``;return c.has(t)?t:`all`}let u=H([]),d=H(l()),f=H(``),p=H(``),m=H(!1),g=H(!1),v=H(!1),y=H(``),b=H(!1),x=()=>({customerId:``,customerName:``,customerPhone:``,items:[]}),S=_(x()),w=h(()=>S.customerId.length>0&&S.items.length>0),E=h(()=>{let e=[...s.value];return e.sort((e,t)=>Number(t.isActive)-Number(e.isActive)||e.name.localeCompare(t.name,`fa`)),e});function D(e){let t=s.value.find(t=>t.id===e);S.customerId=e,S.customerName=t?.name??``,S.customerPhone=t?.phone??``}async function O(){try{s.value=await C.list()}catch(e){i.error(e instanceof o?e.message:`بارگذاری مشتریان انجام نشد.`)}}let k=h(()=>{let e=u.value;if(d.value!==`all`&&(e=e.filter(e=>e.status===d.value)),p.value.trim()){let t=p.value.trim();e=e.filter(e=>e.orderNumber.includes(t)||e.customerName.includes(t))}return e}),A=h(()=>u.value.find(e=>e.id===f.value)??null);function j(e){let t=e.trim();if(t)return u.value.find(e=>e.id===t||e.orderNumber.toUpperCase()===t.toUpperCase())}async function M(){let n=typeof e.query.order==`string`?e.query.order:``;if(!n||!u.value.length)return;let r=j(n);if(!r){p.value=n;return}let i=typeof e.query.focus==`string`?e.query.focus:``;if(i===`pricing`){await t.replace(`/orders/${r.id}/pricing`);return}if(i===`checks`){await t.replace(`/orders/${r.id}/checks`);return}L(r),p.value=r.orderNumber}async function N(){try{let[e,t,n]=await Promise.all([U.list(),T.list(),C.list()]);u.value=e.map(e=>G(e)),a.value=t,s.value=n,await M()}catch(e){i.error(e instanceof o?e.message:`بارگذاری سفارشها انجام نشد.`)}}F(()=>{N()}),I(()=>[e.query.order,e.query.focus],()=>{M()}),I(()=>e.query.tab,()=>{let e=l();d.value!==e&&(d.value=e)}),I(d,n=>{let r=typeof e.query.tab==`string`?e.query.tab:`all`,i=n===`all`?void 0:n;(r===`all`?void 0:r)!==i&&t.replace({query:{...e.query,tab:i}})});function P(e,t=!1){f.value=e.id,t&&(g.value=!0)}function L(e){f.value=e.id,v.value=!0}function R(){g.value=!1}function z(){v.value=!1}function B(e){return e?e===`check`?`چکی`:`نقدی`:`انتخابنشده`}let V=H(!1);async function W(e){if(V.value)return null;V.value=!0;try{let t=await U.updateStatus(e.orderId,{status:e.status,note:e.note,reason:e.reason,force:e.force}),n=G(t),r=u.value.findIndex(t=>t.id===e.orderId);return r>=0&&(u.value[r]={...u.value[r],...n}),i.success(`وضعیت سفارش به «${X(e.status)}» تغییر کرد.`),n}catch(e){return i.error(e instanceof o?e.message:`تغییر وضعیت انجام نشد.`),null}finally{V.value=!1}}function K(e){t.push(`/orders/${e.id}/pricing`)}function J(e){t.push(`/orders/${e.id}/checks`)}function Y(e){t.push({path:`/cash-payments`,query:{order:e.orderNumber}})}function Z(){Object.assign(S,x()),y.value=``,m.value=!0,O()}function ee(){m.value=!1,y.value=``}function te(e){return S.items.some(t=>t.productId===e.productId)?(y.value=`این محصول قبلاً به سفارش اضافه شده است.`,!1):(S.items.push(e),y.value=``,!0)}function ne(e){S.items=S.items.filter(t=>t.productId!==e)}async function re(){if(!w.value){y.value=`انتخاب مشتری و حداقل یک محصول الزامی است.`;return}b.value=!0;try{let e=await U.create({customerId:S.customerId||void 0,userId:S.customerId||void 0,customerName:S.customerName.trim(),customerPhone:S.customerPhone.trim()||void 0,items:S.items.map(e=>({productId:e.productId,meterage:e.meterage}))}),t=G(e);u.value.unshift(t),f.value=t.id,i.success(`سفارش جدید ثبت شد.`),ee()}catch(e){i.error(e instanceof o?e.message:`ثبت سفارش انجام نشد.`)}finally{b.value=!1}}return{companies:a,customers:s,activeCustomers:E,applyCustomer:D,loadCustomers:O,orderList:u,activeTab:d,selectedOrderId:f,searchQuery:p,showAddModal:m,showDetailPanel:g,showDetailModal:v,formError:y,form:S,addFormItem:te,removeFormItem:ne,isFormValid:w,filteredOrders:k,selectedOrder:A,selectOrder:P,openOrderDetail:L,closeDetailPanel:R,closeDetailModal:z,settlementLabel:B,changeStatus:W,statusSaving:V,goPricing:K,goChecks:J,goCashReceipt:Y,openAddModal:Z,closeAddModal:ee,submitAddOrder:re,saving:b}}var ae=Symbol(`orders-page`);function oe(){let e=ie();return A(ae,e),e}function se(){let e=O(ae);if(!e)throw Error(`Orders page context is missing`);return e}var ce={class:`order-detail__hero`},$={class:`order-detail__hero-text`},le={class:`order-detail__id`},ue={key:0,class:`order-detail__raw-status`},de={class:`order-detail__actions no-print`},fe=[`disabled`],pe={class:`order-detail__panels`},me={class:`order-detail__section`},he={class:`order-detail__info`},ge={class:`order-detail__info-row`},_e={class:`order-detail__info-value`},ve={class:`order-detail__info-row`},ye={class:`order-detail__info-value order-detail__info-value--phone`,dir:`ltr`},be={class:`order-detail__info-row`},xe={class:`order-detail__info-value`},Se={class:`order-detail__info-row`},Ce={class:`order-detail__info-value`},we={class:`order-detail__section`},Te={class:`order-detail__info`},Ee={class:`order-detail__info-row`},De={class:`order-detail__info-value`},Oe={class:`order-detail__info-row`},ke={class:`order-detail__info-value`},Ae={class:`order-detail__info-row`},je={class:`order-detail__info-value`},Me={class:`order-detail__info-row`},Ne={class:`order-detail__info-value`},Pe={key:0,class:`order-detail__section`},Fe={key:0,class:`admin-table-wrap order-detail__table`},Ie={class:`admin-table admin-table--compact`},Le={key:1,class:`order-detail__items`},Re={class:`order-detail__item-head`},ze={class:`order-detail__item-meta`},Be={key:0},Ve={key:1},He={key:2,class:`order-detail__item-total`},Ue={class:`order-detail__section order-detail__status no-print`},We={class:`order-detail__status-head`},Ge={class:`order-detail__manual`},Ke={key:0,class:`order-detail__status-hint`},qe={key:1,class:`order-detail__status-hint`},Je={key:2,class:`order-detail__status-form`},Ye={class:`admin-form__field`},Xe={class:`admin-form__field`},Ze={for:`status-note`},Qe=[`disabled`],$e=t(R({__name:`OrderDetailContent`,props:{order:{},settlementLabel:{},layout:{default:`compact`},statusSaving:{type:Boolean}},emits:[`pricing`,`checks`,`cashReceipt`,`finalApprove`,`status`],setup(t,{emit:n}){let r=t,i=n,a=H(!1),o=H(``),l=H(``),u=Object.keys(Z),g=h(()=>r.order.allowedTransitions??[]),_=h(()=>a.value?u.filter(e=>e!==r.order.rawStatus):[...g.value]),y=h(()=>[{value:``,label:`انتخاب کنید…`},..._.value.map(e=>({value:e,label:X(e)}))]),S=h(()=>a.value),C=h(()=>!!o.value&&(!S.value||l.value.trim().length>0)),w=h(()=>r.layout===`desktop`),T=h(()=>ne[r.order.status]?.label??r.order.status),D=h(()=>{let e=X(r.order.rawStatus);return!e||e===T.value?``:e}),O=h(()=>{let e=r.order.customerType;return e?Y[e]??r.order.customerType??`—`:`—`}),k=h(()=>te(r.order)),A=h(()=>ee(r.order)),j=h(()=>r.order.settlementMethod===`check`&&!k.value),F=h(()=>r.order.settlementMethod===`cash`&&!k.value&&r.order.rawStatus!==`cash_confirmed`),R=h(()=>k.value?`pricing`:A.value?`final`:j.value?`checks`:F.value?`cash`:null);I([()=>r.order.id,a],()=>{o.value=``,l.value=``});function z(){if(!o.value||!C.value)return;let e=l.value.trim()||void 0;i(`status`,{orderId:r.order.id,status:o.value,note:e,reason:a.value?e:void 0,force:a.value||void 0}),o.value=``,l.value=``}return(n,r)=>(x(),e(`div`,{class:b([`order-detail`,`order-detail--${t.layout}`])},[m(`header`,ce,[m(`div`,$,[m(`div`,le,[m(`strong`,null,d(t.order.orderNumber),1),N(re,{status:t.order.status},null,8,[`status`])]),D.value?(x(),e(`p`,ue,d(D.value),1)):c(``,!0)]),m(`div`,de,[k.value?(x(),e(`button`,{key:0,type:`button`,class:b([`admin-btn admin-btn--sm`,R.value===`pricing`?`admin-btn--primary`:`admin-btn--outline`]),onClick:r[0]||=e=>i(`pricing`)},` قیمتگذاری سفارش `,2)):c(``,!0),j.value?(x(),e(`button`,{key:1,type:`button`,class:b([`admin-btn admin-btn--sm`,R.value===`checks`?`admin-btn--primary`:`admin-btn--outline`]),onClick:r[1]||=e=>i(`checks`)},` مدیریت چکها `,2)):c(``,!0),F.value?(x(),e(`button`,{key:2,type:`button`,class:b([`admin-btn admin-btn--sm`,R.value===`cash`?`admin-btn--primary`:`admin-btn--outline`]),onClick:r[2]||=e=>i(`cashReceipt`)},` رسید پرداخت `,2)):c(``,!0),A.value?(x(),e(`button`,{key:3,type:`button`,class:b([`admin-btn admin-btn--sm`,R.value===`final`?`admin-btn--primary`:`admin-btn--outline`]),disabled:t.statusSaving,onClick:r[3]||=e=>i(`finalApprove`)},` تأیید نهایی `,10,fe)):c(``,!0)])]),m(`div`,pe,[m(`section`,me,[r[11]||=m(`h3`,{class:`order-detail__section-title`},`اطلاعات مشتری`,-1),m(`div`,he,[m(`div`,ge,[N(s,{name:`building`,size:16}),r[7]||=m(`span`,{class:`order-detail__info-label`},`مشتری`,-1),m(`span`,_e,d(t.order.customerName),1)]),m(`div`,ve,[N(s,{name:`phone`,size:16}),r[8]||=m(`span`,{class:`order-detail__info-label`},`تلفن`,-1),m(`span`,ye,d(t.order.customerPhone??`—`),1)]),m(`div`,be,[N(s,{name:`id-card`,size:16}),r[9]||=m(`span`,{class:`order-detail__info-label`},`شناسه ملی`,-1),m(`span`,xe,d(t.order.customerNationalId??`—`),1)]),m(`div`,Se,[N(s,{name:`user`,size:16}),r[10]||=m(`span`,{class:`order-detail__info-label`},`نوع مشتری`,-1),m(`span`,Ce,d(O.value),1)])])]),m(`section`,we,[r[16]||=m(`h3`,{class:`order-detail__section-title`},`اطلاعات سفارش`,-1),m(`div`,Te,[m(`div`,Ee,[r[12]||=m(`span`,{class:`order-detail__info-label`},`شرکت`,-1),m(`span`,De,d(t.order.companyName),1)]),m(`div`,Oe,[r[13]||=m(`span`,{class:`order-detail__info-label`},`روش تسویه`,-1),m(`span`,ke,d(t.settlementLabel(t.order.settlementMethod)),1)]),m(`div`,Ae,[r[14]||=m(`span`,{class:`order-detail__info-label`},`متراژ کل`,-1),m(`span`,je,d(K(v)(t.order.meterage)),1)]),m(`div`,Me,[r[15]||=m(`span`,{class:`order-detail__info-label`},`مبلغ کل`,-1),m(`span`,Ne,d(t.order.totalAmount==null?`پس از قیمتگذاری`:K(M)(t.order.totalAmount)),1)])])])]),t.order.items?.length?(x(),e(`section`,Pe,[r[18]||=m(`h3`,{class:`order-detail__section-title`},`محصولات سفارش`,-1),w.value?(x(),e(`div`,Fe,[m(`table`,Ie,[r[17]||=m(`thead`,null,[m(`tr`,null,[m(`th`,null,`شرکت`),m(`th`,null,`کالیته`),m(`th`,null,`محصول`),m(`th`,null,`رنگ`),m(`th`,null,`متراژ`),m(`th`,null,`قیمت`),m(`th`,null,`مبلغ`)])],-1),m(`tbody`,null,[(x(!0),e(L,null,V(t.order.items,n=>(x(),e(`tr`,{key:n.id},[m(`td`,null,d(n.companyName||t.order.companyName),1),m(`td`,null,d(n.catalogCode),1),m(`td`,null,d(n.productName),1),m(`td`,null,d(n.color),1),m(`td`,null,d(K(P)(n.confirmedMeterage??n.requestedMeterage)),1),m(`td`,null,d(n.supplyPrice==null?`—`:K(M)(n.supplyPrice)),1),m(`td`,null,d(n.totalAmount==null?`—`:K(M)(n.totalAmount)),1)]))),128))])])])):(x(),e(`ul`,Le,[(x(!0),e(L,null,V(t.order.items,t=>(x(),e(`li`,{key:t.id,class:`order-detail__item`},[m(`div`,Re,[m(`strong`,null,d(t.productName),1),m(`span`,null,d(t.catalogCode),1)]),m(`div`,ze,[t.companyName?(x(),e(`span`,Be,d(t.companyName),1)):c(``,!0),m(`span`,null,d(t.color),1),m(`span`,null,d(K(v)(t.confirmedMeterage??t.requestedMeterage)),1),t.supplyPrice==null?c(``,!0):(x(),e(`span`,Ve,d(K(M)(t.supplyPrice)),1)),t.totalAmount==null?c(``,!0):(x(),e(`span`,He,d(K(M)(t.totalAmount)),1))])]))),128))]))])):c(``,!0),m(`section`,Ue,[m(`div`,We,[r[20]||=m(`h3`,{class:`order-detail__section-title`},`تغییر وضعیت`,-1),m(`label`,Ge,[E(m(`input`,{"onUpdate:modelValue":r[4]||=e=>a.value=e,type:`checkbox`},null,512),[[f,a.value]]),r[19]||=m(`span`,null,`تغییر دستی`,-1)])]),a.value?(x(),e(`p`,Ke,` این گذر خارج از روال استاندارد است؛ ثبت یادداشت اجباری است. `)):_.value.length?c(``,!0):(x(),e(`p`,qe,` در این وضعیت گذر خودکاری وجود ندارد. برای تغییر، حالت دستی را فعال کنید. `)),_.value.length?(x(),e(`div`,Je,[m(`div`,Ye,[r[21]||=m(`label`,null,`وضعیت بعدی`,-1),N(J,{modelValue:o.value,"onUpdate:modelValue":r[5]||=e=>o.value=e,block:``,"aria-label":`وضعیت بعدی`,placeholder:`انتخاب کنید…`,options:y.value},null,8,[`modelValue`,`options`])]),m(`div`,Xe,[m(`label`,Ze,` یادداشت`+d(S.value?` (اجباری)`:` (اختیاری)`),1),E(m(`input`,{id:`status-note`,"onUpdate:modelValue":r[6]||=e=>l.value=e,type:`text`,class:`admin-input`,placeholder:`دلیل یا توضیح این تغییر`},null,512),[[p,l.value]])]),m(`button`,{type:`button`,class:`admin-btn admin-btn--primary admin-btn--sm`,disabled:!C.value||t.statusSaving,onClick:z},d(t.statusSaving?`در حال ثبت…`:`ثبت وضعیت`),9,Qe)])):c(``,!0)])],2))}}),[[`__scopeId`,`data-v-bfe77a62`]]),et={class:`orders-page`},tt={class:`orders-page__list-panel admin-card`},nt={class:`orders-page__list-header`},rt={class:`orders-page__list-actions`},it={type:`button`,class:`admin-btn admin-btn--outline admin-btn--sm`},at={class:`orders-page__search`},ot={class:`admin-tabs`},st=[`onClick`],ct={class:`admin-table-wrap`},lt={class:`admin-table admin-table--compact`},ut=[`onClick`],dt={class:`orders-page__actions-cell`},ft=[`aria-expanded`,`onClick`],pt={type:`button`,class:`admin-btn admin-btn--outline admin-btn--sm`},mt=t(R({__name:`DesktopOrdersPage`,setup(t){let n=k(`(min-width: 1280px)`),{activeTab:r,selectedOrderId:o,searchQuery:u,filteredOrders:f,selectedOrder:h,showDetailModal:_,openOrderDetail:C,closeDetailModal:T,settlementLabel:O,goPricing:A,goChecks:P,goCashReceipt:I,changeStatus:R,statusSaving:U,openAddModal:G}=se(),q=H(null),J=H({});function Y(){q.value=null}async function X(e,t){if(t.stopPropagation(),q.value?.id===e.id){Y();return}let n=t.currentTarget.getBoundingClientRect();q.value=e,await y();let r=Math.min(Math.max(8,n.right-168),window.innerWidth-168-8);J.value={position:`fixed`,top:`${n.bottom+4}px`,left:`${r}px`,width:`168px`}}function Z(){Y()}F(()=>{document.addEventListener(`click`,Z),window.addEventListener(`scroll`,Y,!0),window.addEventListener(`resize`,Y)}),z(()=>{document.removeEventListener(`click`,Z),window.removeEventListener(`scroll`,Y,!0),window.removeEventListener(`resize`,Y)});function ne(){h.value&&(T(),A(h.value))}function ie(){h.value&&(T(),P(h.value))}function ae(){h.value&&(T(),I(h.value))}function oe(){h.value&&R({orderId:h.value.id,status:`final_approved`,note:`تأیید نهایی سفارش`})}function ce(){if(!h.value)return;document.documentElement.classList.add(`printing-order-detail`);let e=()=>{document.documentElement.classList.remove(`printing-order-detail`),window.removeEventListener(`afterprint`,e)};window.addEventListener(`afterprint`,e),window.setTimeout(e,6e4),window.print()}function $(e){let t=q.value;if(t){if(Y(),e===`detail`){C(t);return}if(e===`pricing`){A(t);return}if(e===`checks`){P(t);return}if(e===`final`){R({orderId:t.id,status:`final_approved`,note:`تأیید نهایی سفارش`});return}I(t)}}return(t,y)=>(x(),e(`div`,et,[m(`div`,tt,[m(`div`,nt,[y[9]||=m(`div`,null,[m(`h2`,{class:`orders-page__list-title`},`سفارشها`)],-1),m(`div`,rt,[m(`button`,it,[N(s,{name:`filter`,size:16}),y[8]||=B(` فیلتر `,-1)]),m(`button`,{type:`button`,class:`admin-btn admin-btn--primary admin-btn--sm`,onClick:y[0]||=(...e)=>K(G)&&K(G)(...e)},` سفارش جدید `)])]),m(`div`,at,[N(s,{name:`search`,size:18}),E(m(`input`,{"onUpdate:modelValue":y[1]||=e=>w(u)?u.value=e:null,type:`search`,placeholder:`جستجو در شماره سفارش، مشتری...`,class:`orders-page__search-input`},null,512),[[p,K(u)]])]),m(`div`,ot,[(x(!0),e(L,null,V(K(Q),t=>(x(),e(`button`,{key:t.key,type:`button`,class:b([`admin-tab`,{active:K(r)===t.key}]),onClick:e=>r.value=t.key},[B(d(t.label)+` `,1),`count`in t&&t.count?(x(),e(L,{key:0},[B(` (`+d(t.count)+`)`,1)],64)):c(``,!0)],10,st))),128))]),m(`div`,ct,[m(`table`,lt,[y[10]||=m(`thead`,null,[m(`tr`,null,[m(`th`,null,`شماره سفارش`),m(`th`,null,`مشتری`),m(`th`,null,`روش تسویه`),m(`th`,null,`مبلغ کل`),m(`th`,null,`متراژ`),m(`th`,null,`وضعیت`),m(`th`,null,`تاریخ ثبت`),m(`th`,null,`عملیات`)])],-1),m(`tbody`,null,[(x(!0),e(L,null,V(K(f),t=>(x(),e(`tr`,{key:t.id,class:b({"is-selected":K(o)===t.id}),onClick:e=>K(C)(t)},[m(`td`,null,d(t.orderNumber),1),m(`td`,null,d(t.customerName),1),m(`td`,null,d(K(O)(t.settlementMethod)),1),m(`td`,null,d(t.totalAmount?K(M)(t.totalAmount):`—`),1),m(`td`,null,d(K(v)(t.meterage)),1),m(`td`,null,[N(re,{status:t.status},null,8,[`status`])]),m(`td`,null,d(K(D)(t.createdAt)),1),m(`td`,dt,[m(`button`,{type:`button`,class:`orders-page__more-btn`,"aria-expanded":q.value?.id===t.id,"aria-haspopup":`menu`,"aria-label":`عملیات سفارش`,onClick:e=>X(t,e)},[N(s,{name:`more`,size:18})],8,ft)])],10,ut))),128))])])]),y[11]||=S(`