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