1 line
34 KiB
JavaScript
1 line
34 KiB
JavaScript
import{n as e}from"./rolldown-runtime-Dl_DZoUm.js";import{s as t,tr as n,v as r}from"./index-CPt4N9y-.js";import{i,n as a,r as o,t as s}from"./controls-DCbTJv7w.js";import{i as c,n as l,r as u,t as d}from"./settingsModel-DuLjBEgM.js";import{i as f,t as p}from"./shared-Da74NgmJ.js";import{i as m,n as h,o as g,t as _}from"./BusinessControls-B2yydBuO.js";var v=e(n(),1),y=r();function b(e){let{name:t,amount:n,payer:r,enabled:i,byQuantity:a}=c(e);return{name:t,amount:n,payer:r,enabled:i===`true`,byQuantity:a===`true`}}function x({shop:e,client:t,members:n,team:r,busy:c,onBusy:l,dirty:g,onDirty:x,onSaved:S,notice:C}){let w=f.presetCatalog(e.costPresets),[T,E]=(0,v.useState)(``),[D,O]=(0,v.useState)(null),[k,A]=(0,v.useState)(!1),[j,M]=(0,v.useState)(``);function N(){k||g&&!window.confirm(`成本修改尚未保存,放弃修改?`)||(O(null),x(!1),M(``))}function P(n,r=``){if(c||t.busy||t.pending())return;let i=w[r]||{name:``,amount:`0`,payer:`order`,enabled:!0,byQuantity:!1};O({action:n,kind:r||`custom_`+crypto.randomUUID(),original:i,revision:e.revision}),x(!1),M(``)}async function F(e){if(e.preventDefault(),!D||k)return;let n=!1;try{if(c||t.busy||t.pending())throw Error(`请先确认当前操作,再保存成本`);let r=D.action===`delete`?null:b(e.currentTarget);if(r){if(r.name=r.name.trim(),!r.name||r.name.length>40)throw Error(`成本名称请填写1~40字`);if(p.cents(r.amount,`成本`)>1e8)throw Error(`成本最多1000000元`)}A(!0),l(!0),M(``);let i=await t.mutate(`/api/shop-settings`,{revision:D.revision,costPresets:{[D.kind]:r}});n=!0,O(null),x(!1),await S(i.shopSettings),C(D.action===`delete`?`成本项目已删除`:`成本项目已保存`)}catch(e){let t=n?`成本已经保存,请刷新页面核对`:d(e);M(t),C(t,!0)}finally{A(!1),l(!1)}}let I=Object.entries(w).filter(([,e])=>e.name.toLocaleLowerCase().includes(T.trim().toLocaleLowerCase()));return(0,y.jsxs)(`section`,{id:`shop-cost-manager`,className:`shop-cost-manager`,children:[(0,y.jsxs)(`div`,{className:`panel-title`,children:[(0,y.jsxs)(`div`,{children:[(0,y.jsx)(`h3`,{children:`勾选成本管理`}),(0,y.jsx)(`p`,{className:`small muted`,children:`自行添加需要的项目。删除后不再用于新单,历史订单成本保留。`})]}),(0,y.jsx)(s,{action:`shop-cost-add`,className:`primary`,onClick:()=>P(`add`),children:`+ 新增成本`})]}),(0,y.jsxs)(`label`,{className:`shop-cost-search`,children:[`搜索成本`,(0,y.jsx)(m,{type:`search`,id:`shop-cost-search`,placeholder:`输入成本名称`,autoComplete:`off`,value:T,onChange:e=>E(e.target.value)})]}),(0,y.jsx)(`div`,{id:`shop-cost-list`,children:I.length?(0,y.jsx)(`div`,{className:`table-wrap`,children:(0,y.jsx)(h,{className:`shop-cost-table`,columns:[{title:`成本名称`},{title:`金额/单价`},{title:`付款人`},{title:`计费方式`},{title:`状态`},{title:`操作`}],rows:I.map(([e,t])=>({key:e,attributes:{"data-cost-row":e},cells:[(0,y.jsx)(y.Fragment,{children:(0,y.jsx)(`strong`,{children:t.name})}),(0,y.jsxs)(y.Fragment,{children:[`¥`,t.amount]}),t.payer===`order`?`订单归属人`:n.find(e=>e.member===t.payer)?.label||`待确认`,t.byQuantity?`单价 × 数量`:`每单一次`,t.enabled?`启用`:`停用`,(0,y.jsx)(y.Fragment,{children:(0,y.jsxs)(`div`,{className:`actions`,children:[(0,y.jsx)(s,{action:`shop-cost-edit`,className:`quiet`,"data-kind":e,onClick:()=>P(`edit`,e),children:`编辑`}),(0,y.jsx)(s,{action:`shop-cost-delete`,className:`danger`,"data-kind":e,onClick:()=>P(`delete`,e),children:`删除`})]})})]}))})}):(0,y.jsx)(`p`,{className:`empty`,children:Object.keys(w).length?`没有匹配的成本项目。`:`还没有勾选成本,点击“新增成本”添加。`})}),D&&(0,y.jsxs)(o,{id:`shop-cost-dialog`,close:N,children:[(0,y.jsx)(s,{className:`dialog-close`,action:`shop-cost-close`,"aria-label":`关闭成本编辑`,onClick:N,children:`×`}),(0,y.jsx)(`h2`,{children:{add:`新增成本`,delete:`删除成本`,edit:`编辑成本`}[D.action]}),(0,y.jsxs)(`form`,{id:`shop-cost-form`,onSubmit:F,onChange:e=>{let t=b(e.currentTarget);x(Object.keys(t).some(e=>t[e]!==D.original[e]))},children:[D.action===`delete`?(0,y.jsxs)(y.Fragment,{children:[(0,y.jsxs)(`p`,{children:[`删除“`,(0,y.jsx)(`strong`,{children:D.original.name}),`”?`]}),(0,y.jsx)(`p`,{className:`muted`,children:`删除后不会再出现在新单勾选项里,已开订单、成本和流水保留。`})]}):(0,y.jsxs)(`div`,{className:`grid two`,children:[(0,y.jsx)(i,{label:`成本名称`,name:`name`,defaultValue:D.original.name,required:!0,maxLength:40,disabled:k}),(0,y.jsx)(i,{label:`金额/单价(元)`,name:`amount`,type:`number`,defaultValue:D.original.amount,required:!0,min:0,max:1e6,step:`0.01`,disabled:k}),(0,y.jsx)(a,{label:`付款人`,name:`payer`,defaultValue:D.original.payer,options:[[`order`,`订单归属人`],...u(n,D.original.payer,r)],disabled:k}),(0,y.jsx)(a,{label:`计费方式`,name:`byQuantity`,defaultValue:String(D.original.byQuantity),options:[[`false`,`每单一次`],[`true`,`单价 × 数量`]],disabled:k}),(0,y.jsx)(a,{label:`状态`,name:`enabled`,defaultValue:String(D.original.enabled),options:[[`true`,`启用`],[`false`,`停用`]],disabled:k})]}),(0,y.jsx)(`div`,{id:`shop-cost-error`,role:`alert`,className:j?`error`:void 0,children:j}),(0,y.jsxs)(`div`,{className:`form-actions`,children:[(0,y.jsx)(s,{action:`shop-cost-close`,className:`quiet`,onClick:N,disabled:k,children:`取消`}),(0,y.jsx)(_,{type:`submit`,className:D.action===`delete`?`danger`:`primary`,disabled:k,children:D.action===`delete`?`确认删除`:`保存成本`})]})]})]})]})}function S(e){let t=e=>[`owner`,`assistant`,`assistant2`].includes(e)||/^staff_[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.test(e);if(!e||!Number.isSafeInteger(e.revision)||e.revision<0||!Array.isArray(e.employees)||new Set(e.employees.map(e=>e?.member)).size!==e.employees.length||!e.employees.every(e=>e&&t(e.member)&&typeof e.label==`string`&&e.label&&typeof e.loginName==`string`&&[`owner`,`assistant`].includes(e.role)&&e.role===`owner`==(e.member===`owner`)&&[e.enabled,e.passwordSet,e.hasBusinessData,e.canDelete,e.canDeactivate].every(e=>typeof e==`boolean`)&&(!e.canDelete||e.member!==`owner`&&!e.hasBusinessData)&&(!e.canDeactivate||e.member!==`owner`)&&Array.isArray(e.businessUsage)&&e.businessUsage.every(e=>e&&typeof e.source==`string`&&Number.isSafeInteger(e.count)&&e.count>0)))throw Error(`员工资料未完整返回,请重新读取。`);return e}function C(e){return e.member===`owner`?`店主账号始终保留,不能停用或删除。`:e.hasBusinessData?`已有业务记录,不能删除。可停用账号,保留历史订单、付款人与统计。`:e.canDelete?`未关联业务记录,可以删除。`:`暂不能删除,请重新读取员工资料核对。`}var w={deactivate:`停用`,reactivate:`启用`,delete:`删除`},T={deactivate:`停用后立即退出登录,新订单和付款选择中不再出现;历史订单、付款人和统计仍保留。`,reactivate:`启用后可以重新登录,并重新出现在新订单和付款人选项中。`,delete:`此员工没有业务记录。删除后登录账号和员工资料将移除。`},E=/^1[3-9]\d{9}$/,D=new WeakMap,O=(0,v.forwardRef)(function({client:e,shopDirty:t,reloadKey:n,onSaved:r,onAccountChanged:l,notice:u},f){let[p,g]=(0,v.useState)(null),[b,x]=(0,v.useState)(!0),[O,k]=(0,v.useState)(``),[A,j]=(0,v.useState)(()=>D.get(e)||{search:``,status:`all`}),[M,N]=(0,v.useState)(null),[P,F]=(0,v.useState)(!1),[I,ee]=(0,v.useState)(!1),[L,R]=(0,v.useState)(null),[te,z]=(0,v.useState)(``),[B,V]=(0,v.useState)(!1),H=(0,v.useRef)(0),U=(0,v.useRef)(!1),W=I||e.busy||!!e.pending()||!!L,G=(0,v.useCallback)(async()=>{let t=++H.current;x(!0),k(``);try{let n=S(await e.api(`/api/employees?`+new URLSearchParams(A)));U.current&&t===H.current&&g(n)}catch(e){U.current&&t===H.current&&(g(null),k(d(e)))}finally{U.current&&t===H.current&&x(!1)}},[e,A]);(0,v.useEffect)(()=>(U.current=!0,()=>{U.current=!1}),[]),(0,v.useEffect)(()=>{G()},[G,n]);function K(e=!1){if(I||L)return u(`员工操作结果待确认,请先恢复本次员工操作。`,!0),!1;let t=e?`员工资料尚未保存,离开会丢弃填写内容。继续离开?`:`员工资料尚未保存,关闭会丢弃填写内容。继续关闭?`;return P&&!window.confirm(t)?!1:(N(null),F(!1),z(``),V(!1),!0)}(0,v.useImperativeHandle)(f,()=>({canLeave:()=>K(!0),beforeUnload:()=>I||!!L||P}));function q(e,n){W||(t?u(`请先保存店铺与打印设置,再修改员工资料。`,!0):p?K()&&N({kind:e,row:n,revision:p.revision}):u(`请先重新读取员工资料。`,!0))}function J(e,t=``){let n=t?d(e).split(t).join(`••••`):d(e);z(n),u(n,!0),e.status===409&&V(!0)}async function Y(t){ee(!0),z(``);let n=!1;try{let i=await e.employeeWrite(t.url,t.data,t.key,t.user);if(n=!0,R(null),S(i),!U.current||e.user!==t.user||(N(null),F(!1),await r(),!U.current||e.user!==t.user))return;await G(),u(`员工资料已保存`)}catch(e){if(!U.current)return;let r=e;if(n){if(r.status===401){u(`密码已修改,请重新登录。`),l();return}J(Error(`员工资料已经保存,请刷新页面核对。`))}else r.code===`ACCOUNT_CHANGED`?(R(null),F(!1),N(null),l()):r.completed||r.confirmed&&[400,401,403,404,409,413].includes(r.status??0)?(R(null),J(r,String(t.data.password||``))):(R(t),J(Error(`连接中断,员工变更结果待确认。请恢复本次员工操作,原操作号会保留。`)))}finally{U.current&&ee(!1)}}async function ne(n){if(n.preventDefault(),!M||W||B)return;let r=c(n.currentTarget),{kind:i,row:a,revision:o}=M;try{if(t)throw Error(`请先保存店铺与打印设置,再修改员工资料。`);if(o!==p?.revision)throw Error(`员工资料已有变动,请重新打开资料后核对。`);let n;if(i===`add`||i===`update`){let e=r.label.trim(),t=r.operatorName.trim(),s=r.loginName.trim(),c=r.password;if(t.length>30)throw Error(`经办人名称最多30字。`);if(!e||e.length>30)throw Error(`账号名称请填写1~30字。`);if(!s||s.length>60||/[\s<>]/.test(s))throw Error(`登录名请填写1~60个字符,不含空白及尖括号。`);if((i===`add`||s!==a?.loginName)&&!E.test(s))throw Error(`请填写11位有效手机号,用于登录。`);if((i===`add`||c!==``)&&(c.length<12||c.length>500))throw Error(`登录密码须为12~500个字符。`);n={revision:o,label:e,operatorName:t,...E.test(s)?{phone:s}:{loginName:s},...c?{password:c}:{}}}else{if(r.verified!==`on`)throw Error(`请核对员工并勾选确认。`);n={revision:o,verified:!0}}await Y({url:i===`add`?`/api/employees`:`/api/employees/`+encodeURIComponent(a.member)+`/`+i,data:n,key:crypto.randomUUID(),user:e.user})}catch(e){J(e,r.password)}}let X=M?.row,Z=M?.kind===`add`,Q=Z||M?.kind===`update`,$=M&&!Q?M.kind:null,re=(0,y.jsx)(`div`,{id:`employee-form-error`,"aria-live":`polite`,children:te&&(0,y.jsxs)(`div`,{className:`error`,children:[te,L?(0,y.jsx)(s,{action:`employee-retry`,className:`danger`,disabled:I||e.busy,onClick:()=>Y(L),children:`恢复本次员工操作`}):B?(0,y.jsx)(s,{action:`employee-reload`,onClick:G,children:`读取最新员工列表`}):null]})});return(0,y.jsxs)(`section`,{className:`panel employee-management`,id:`employee-management-panel`,children:[(0,y.jsxs)(`div`,{className:`panel-title`,children:[(0,y.jsxs)(`div`,{children:[(0,y.jsx)(`h2`,{children:`员工管理`}),(0,y.jsx)(`p`,{className:`small muted`,children:`新增员工、修改登录资料,或停用离职账号。员工只查看本人业务,店主查看全店。`})]}),(0,y.jsx)(s,{action:`employee-add`,className:`primary`,disabled:W,onClick:()=>q(`add`),children:`新增员工`})]}),(0,y.jsxs)(`form`,{id:`employee-search-form`,className:`employee-filters`,onSubmit:t=>{t.preventDefault();let n=c(t.currentTarget),r={search:n.search.trim(),status:n.status};D.set(e,r),j(r)},children:[(0,y.jsx)(i,{label:`搜索账号、经办人或手机号`,name:`search`,type:`search`,maxLength:80,autoComplete:`off`,defaultValue:A.search,disabled:W}),(0,y.jsx)(a,{label:`账号状态`,name:`status`,options:[[`all`,`全部状态`],[`active`,`已启用`],[`inactive`,`已停用`]],defaultValue:A.status,disabled:W}),(0,y.jsx)(_,{type:`submit`,disabled:W,children:`搜索`}),(0,y.jsx)(s,{action:`employee-reload`,className:`quiet`,disabled:W,onClick:G,children:`刷新`})]}),(0,y.jsx)(`div`,{id:`employee-list`,"aria-live":`polite`,"aria-busy":b,children:b?(0,y.jsx)(`p`,{className:`small muted`,children:`正在读取员工资料…`}):O?(0,y.jsxs)(`div`,{className:`error`,children:[O,` `,(0,y.jsx)(s,{action:`employee-reload`,disabled:W,onClick:G,children:`重新读取`})]}):p?.employees.length?(0,y.jsx)(`div`,{className:`table-wrap`,children:(0,y.jsx)(h,{className:`employee-table`,columns:[{title:`账号名称`},{title:`经办人名称(开单人)`},{title:`手机号/原登录名`},{title:`账号状态`},{title:`历史记录`},{title:`操作`}],rows:p.employees.map(e=>({key:e.member,attributes:{"data-employee":e.member},cells:[(0,y.jsxs)(y.Fragment,{children:[(0,y.jsx)(`strong`,{children:e.label}),(0,y.jsx)(`div`,{className:`small muted`,children:e.member===`owner`?`店主`:`员工`})]}),e.operatorName||e.label,(0,y.jsxs)(y.Fragment,{children:[e.loginName,(0,y.jsx)(`div`,{className:`small muted`,children:e.passwordSet?`已设置密码`:`尚未设置密码`})]}),(0,y.jsx)(y.Fragment,{children:(0,y.jsx)(`span`,{className:`badge `+(e.enabled?``:`warn`),children:e.enabled?`已启用`:`已停用`})}),(0,y.jsxs)(y.Fragment,{children:[(0,y.jsx)(`p`,{className:`small muted employee-delete-reason`,children:C(e)}),!!e.businessUsage.length&&(0,y.jsxs)(`details`,{children:[(0,y.jsx)(`summary`,{className:`small`,children:`关联记录`}),(0,y.jsx)(`ul`,{className:`small`,children:e.businessUsage.map(e=>(0,y.jsxs)(`li`,{children:[e.label||e.source,`:`,e.count,` 条`]},e.source))})]})]}),(0,y.jsx)(y.Fragment,{children:(0,y.jsxs)(`div`,{className:`actions`,children:[(0,y.jsx)(s,{action:`employee-edit`,"data-member":e.member,onClick:()=>q(`update`,e),disabled:W,children:`编辑`}),e.canDeactivate?(0,y.jsx)(s,{action:`employee-deactivate`,className:`quiet`,"data-member":e.member,disabled:W,onClick:()=>q(`deactivate`,e),children:`停用`}):e.member!==`owner`&&!e.enabled?(0,y.jsx)(s,{action:`employee-reactivate`,"data-member":e.member,disabled:W,onClick:()=>q(`reactivate`,e),children:`启用`}):null,e.member!==`owner`&&(0,y.jsx)(s,{action:`employee-delete`,className:`quiet`,"data-member":e.member,disabled:W||!e.canDelete,"aria-label":`删除`+e.label,title:C(e),onClick:()=>q(`delete`,e),children:`删除`})]})})]}))})}):(0,y.jsx)(`p`,{className:`empty`,children:`没有符合条件的员工。`})}),M&&(0,y.jsxs)(o,{id:`employee-dialog`,labelledBy:`employee-dialog-title`,close:()=>K(),children:[(0,y.jsx)(s,{action:`employee-close`,"aria-label":Q?`关闭员工资料`:`关闭员工操作`,className:`dialog-close`,disabled:I||!!L,onClick:()=>K(),children:`×`}),(0,y.jsx)(`h2`,{id:`employee-dialog-title`,children:Q?Z?`新增员工`:`编辑员工`:w[$]+`员工`}),Q?(0,y.jsxs)(y.Fragment,{children:[X?.member===`owner`&&(0,y.jsx)(`p`,{className:`note`,children:`店主账号始终启用,不能删除或停用。修改本人密码后需要重新登录。`}),(0,y.jsxs)(`form`,{id:`employee-form`,"data-revision":M.revision,"data-stale":B||void 0,onSubmit:ne,onChange:e=>{let t=c(e.currentTarget);F(!!t.password||t.label!==(X?.label||``)||t.operatorName!==(X?.operatorName||``)||t.loginName!==(X?.loginName||``))},children:[(0,y.jsxs)(`fieldset`,{className:`employee-form-fields`,disabled:W||B,children:[(0,y.jsxs)(`div`,{className:`grid two`,children:[(0,y.jsx)(i,{label:`账号名称`,name:`label`,required:!0,maxLength:30,autoComplete:`off`,defaultValue:X?.label||``}),(0,y.jsx)(i,{label:`经办人名称(开单人)`,name:`operatorName`,maxLength:30,autoComplete:`off`,placeholder:`例如:客服一;留空沿用账号名称`,defaultValue:X?.operatorName||``}),(0,y.jsx)(i,{label:Z||E.test(X?.loginName||``)?`手机号(登录账号)`:`原登录名(可改为手机号)`,name:`loginName`,required:!0,maxLength:60,autoComplete:`off`,spellCheck:!1,type:Z?`tel`:`text`,defaultValue:X?.loginName||``})]}),(0,y.jsx)(i,{label:Z?`登录密码(12~500个字符)`:`新密码(留空保留原密码)`,name:`password`,type:`password`,required:Z,minLength:12,maxLength:500,autoComplete:`new-password`}),(0,y.jsx)(`p`,{className:`small muted`,children:Z?`账号名称填写实际员工,如娜娜;经办人名称填写开单显示名,如客服一。留空沿用账号名称。手机号和密码用于登录,新增账号使用员工权限。`:`账号名称用于内部识别员工,经办人名称自动带入新单开单人;留空沿用账号名称。原登录名可保留,换登录账号请填写手机号。历史订单开单人、归属与收付款账户保留。重设密码后该账号须重新登录。`})]}),re,(0,y.jsxs)(`div`,{className:`form-actions`,children:[(0,y.jsx)(s,{action:`employee-close`,className:`quiet`,disabled:I||!!L,onClick:()=>K(),children:`取消`}),(0,y.jsx)(_,{type:`submit`,className:`primary`,disabled:W||B,children:Z?`添加员工`:`保存修改`})]})]})]}):$&&(0,y.jsxs)(y.Fragment,{children:[(0,y.jsxs)(`p`,{children:[(0,y.jsx)(`strong`,{children:X.label}),` · `,X.loginName]}),(0,y.jsx)(`p`,{className:`note`,children:T[$]}),(0,y.jsxs)(`form`,{id:`employee-status-form`,onSubmit:ne,"data-stale":B||void 0,children:[(0,y.jsxs)(`label`,{className:`check`,children:[(0,y.jsx)(m,{type:`checkbox`,name:`verified`,required:!0,disabled:W||B}),`已核对员工,确认`,w[$],`账号。`]}),re,(0,y.jsxs)(`div`,{className:`form-actions`,children:[(0,y.jsx)(s,{action:`employee-close`,className:`quiet`,disabled:I||!!L,onClick:()=>K(),children:`取消`}),(0,y.jsxs)(_,{type:`submit`,className:$===`delete`?`danger`:`primary`,disabled:W||B,children:[`确认`,w[$]]})]})]})]})]})]})}),k=[[`rows`,`表格最少行数`,3,12],[`offsetX`,`左右偏移(mm,正数向右)`,-30,30],[`offsetY`,`上下偏移(mm,正数向下)`,-30,30],[`contentTop`,`内容距顶部(mm)`,5,40],[`rowHeight`,`商品行高(mm)`,7,16]];function A({client:e,printing:t,revision:n,orderId:r,shopDirty:a,dirty:o,onDirty:l,onBusy:u,refresh:f,notice:p}){let[m,h]=(0,v.useState)(!1),[b,x]=(0,v.useState)(!1),[S,C]=(0,v.useState)(``),[w,T]=(0,v.useState)(0),E=(0,v.useRef)(null);async function D(t){t.preventDefault();let r=c(t.currentTarget),i=!1;try{if(a)throw Error(`店铺配置尚未保存,请先保存店铺配置,再保存打印设置。`);if(m)return;C(``),h(!0),u(!0),await e.mutate(`/api/printing`,{...r,shopSettingsRevision:n}),i=!0,await f(),T(e=>e+1),x(!0),l(!1),p(`打印设置已保存,预览和后续打印使用新设置`)}catch(e){let t=i?`打印设置已保存,但最新设置暂未读到。请刷新页面核对。`:d(e);C(t),p(t,!0)}finally{h(!1),u(!1)}}async function O(){try{if(a)throw Error(`店铺配置尚未保存,请先保存再打开预览。`);if(m)throw Error(`打印设置正在保存,请保存完成后再打开预览。`);if(o)throw E.current?.scrollIntoView({block:`center`}),Error(`打印设置已修改但尚未保存,请先点击“保存打印设置”,再打开预览。`);window.open(`/print/`+r+`?mode=full`,`_blank`,`noopener`),await e.mutate(`/api/orders/`+r+`/print`,{mode:`full`}),p(`客户打印页已打开;若未出现,请允许此网站打开新窗口`)}catch(e){p(d(e),!0)}}return(0,y.jsxs)(`div`,{className:`split`,children:[(0,y.jsxs)(`section`,{className:`panel`,children:[(0,y.jsxs)(`div`,{className:`panel-title print-settings-title`,children:[(0,y.jsxs)(`div`,{children:[(0,y.jsx)(`h2`,{children:`客户打印模板`}),(0,y.jsx)(`p`,{ref:E,id:`print-settings-status`,className:`small `+(o?`danger-text`:`muted`),"aria-live":`polite`,children:m?`正在保存打印设置…`:o?`修改后未保存,请点击“保存打印设置”。`:b?`已保存,打印使用当前设置。`:`当前生效设置;修改后请点击保存。`})]}),(0,y.jsx)(_,{type:`submit`,form:`print-settings`,className:`primary`,disabled:m||e.busy,children:`保存打印设置`})]}),(0,y.jsxs)(`form`,{id:`print-settings`,onSubmit:D,onChange:e=>{let n=c(e.currentTarget);l(k.some(([e])=>n[e].trim()===``||Number(n[e])!==t[e]))},children:[(0,y.jsx)(`p`,{className:`small muted`,children:`店名、单据标题与联系信息在店铺配置中保存。`}),(0,y.jsx)(`fieldset`,{className:`print-settings-fields`,disabled:m,children:(0,y.jsxs)(`div`,{className:`grid two`,children:[(0,y.jsx)(i,{label:`店名`,name:`store`,value:t.store,maxLength:80,required:!0,readOnly:!0}),(0,y.jsx)(i,{label:`单据标题`,name:`subtitle`,value:t.subtitle,maxLength:80,readOnly:!0}),(0,y.jsx)(i,{label:`客户联系信息(选填)`,name:`contact`,value:t.contact,maxLength:80,readOnly:!0}),k.map(([e,n,r,a])=>(0,y.jsx)(i,{label:n,name:e,type:`number`,min:r,max:a,step:e===`rows`?void 0:`0.1`,required:!0,defaultValue:t[e]},e))]})}),(0,y.jsx)(`div`,{className:`form-actions`,children:r&&(0,y.jsx)(s,{action:`print`,"data-id":r,onClick:O,disabled:e.busy,children:`打开最近一单预览`})}),(0,y.jsx)(`div`,{id:`print-settings-error`,"aria-live":`polite`,children:S&&(0,y.jsx)(`div`,{className:`error`,children:S})})]},w)]}),(0,y.jsxs)(`section`,{className:`panel print-settings-preview`,children:[(0,y.jsx)(`p`,{className:`small muted`,children:`填写效果示意;保存后用于实际打印。`}),(0,y.jsx)(`div`,{className:`print-preview-mini`,children:(0,y.jsxs)(`div`,{className:`paper-mini`,children:[(0,y.jsx)(`strong`,{id:`print-mini-store`,children:t.store}),(0,y.jsx)(`p`,{id:`print-mini-subtitle`,children:t.subtitle}),(0,y.jsx)(`p`,{id:`print-mini-contact`,hidden:!t.contact,children:t.contact}),(0,y.jsx)(g,{children:(0,y.jsxs)(`tbody`,{children:[(0,y.jsxs)(`tr`,{children:[(0,y.jsx)(`td`,{children:`商品名称`}),(0,y.jsx)(`td`,{children:`克重`}),(0,y.jsx)(`td`,{children:`成交金额`})]}),(0,y.jsxs)(`tr`,{children:[(0,y.jsx)(`td`,{children:`足金手链`}),(0,y.jsx)(`td`,{children:`1.200g`}),(0,y.jsx)(`td`,{children:`1,200.00`})]}),(0,y.jsxs)(`tr`,{children:[(0,y.jsx)(`td`,{children:` `}),(0,y.jsx)(`td`,{}),(0,y.jsx)(`td`,{})]})]})}),(0,y.jsxs)(`p`,{children:[`合计金额`,` `,`¥1,200.00`]}),(0,y.jsx)(`p`,{children:`大写:壹仟贰佰元整`}),(0,y.jsx)(`p`,{children:`注意事项 · 珠宝首饰保养须知`}),(0,y.jsx)(`p`,{children:`经办人 ______`})]})}),(0,y.jsx)(`div`,{className:`note`,children:`固定241×140mm横版,明细多时自动缩小到一页。打印窗口也选241×140mm,关闭页眉页脚。表格最少行数只用于补空行;套打按实物纸样校准偏移。`}),(0,y.jsx)(`p`,{className:`small muted`,children:`补打沿用同一订单号,不会重复出库或收入。客户预览接口不返回成本、垫资人、利润及内部操作日志。`})]})]})}function j(e,t){let n=t.slice(0,7),[r,i]=n.split(`-`).map(Number),a=(i===12?r+1:r)+`-`+String(i===12?1:i+1).padStart(2,`0`);return{storeName:e.storeName,receiptSubtitle:e.receiptSubtitle||``,contact:e.contact||``,creditCardRatePermille:String(e.creditCardRatePermille),profitSharePercent:String(e.profitSharePercent),profitShareEffectiveMonth:e.profitShareEffectiveMonth>=n?e.profitShareEffectiveMonth:a,...e.feeDefaults}}function M({client:e,shop:t,today:n,members:r,team:o,busy:s,dirty:l,onDirty:f,onBusy:m,onSaved:h,notice:g}){let b=j(t,n),[x,S]=(0,v.useState)(``),[C,w]=(0,v.useState)(!1),[T,E]=(0,v.useState)(!1),[D,O]=(0,v.useState)(0);async function k(i){i.preventDefault();let a=c(i.currentTarget),o=!1;try{if(s||e.busy||e.pending())throw Error(`请先等当前操作完成,再保存店铺配置。`);let i=(e,t,n)=>{if(!/^\d+$/.test(a[e])||Number(a[e])>t)throw Error(n+`请填写0~`+t+`之间的整数。`);return Number(a[e])};if(!a.storeName.trim()||a.storeName.trim().length>80)throw Error(`店名请填写1~80字。`);for(let e of[`certificate`,`packaging`,`shipping`])if(p.cents(a[e],`默认费用`)>1e8)throw Error(`默认费用最多1000000元。`);if(!r.some(e=>e.member===a.packagingPayer))throw Error(`请选择包装默认付款人。`);if(!/^\d{4}-(0[1-9]|1[0-2])$/.test(a.profitShareEffectiveMonth)||a.profitShareEffectiveMonth<n.slice(0,7))throw Error(`分成生效月份请选择本月或以后。`);let c={revision:t.revision,storeName:a.storeName.trim(),receiptSubtitle:a.receiptSubtitle.trim(),contact:a.contact.trim(),creditCardRatePermille:i(`creditCardRatePermille`,1e3,`信用卡费率`),...a.profitSharePercent!==b.profitSharePercent||a.profitShareEffectiveMonth!==b.profitShareEffectiveMonth?{profitSharePercent:i(`profitSharePercent`,100,`利润分成比例`),profitShareEffectiveMonth:a.profitShareEffectiveMonth}:{},feeDefaults:{certificate:a.certificate,packaging:a.packaging,shipping:a.shipping,packagingPayer:a.packagingPayer}};E(!0),m(!0),S(``);let l=await e.mutate(`/api/shop-settings`,c);o=!0,await h(l.shopSettings),f(!1),w(!0),O(e=>e+1),g(`店铺配置已保存`)}catch(e){let t=o?`店铺配置已保存,页面更新未完成,请刷新后核对。`:d(e);S(t+(e.status===409?` 已填写内容已保留,请刷新读取最新配置后再修改。`:``)),g(t,!0)}finally{E(!1),m(!1)}}let A={type:`number`,required:!0,min:0,max:1e6,step:`0.01`};return(0,y.jsxs)(y.Fragment,{children:[(0,y.jsx)(`div`,{className:`panel-title`,children:(0,y.jsxs)(`div`,{children:[(0,y.jsx)(`h2`,{children:`店铺配置`}),(0,y.jsx)(`p`,{id:`shop-settings-status`,"aria-live":`polite`,className:`small `+(l?`danger-text`:`muted`),children:T?`正在保存店铺配置…`:l?`修改尚未保存。`:C?`已保存,后续开单与登录使用当前配置。`:`当前生效配置;仅店主可以修改。`})]})}),(0,y.jsxs)(`form`,{id:`shop-settings-form`,"data-revision":t.revision,onSubmit:k,onChange:e=>{let t=c(e.currentTarget);f(Object.entries(b).some(([e,n])=>String(n??``)!==t[e]))},children:[(0,y.jsxs)(`fieldset`,{className:`shop-settings-fields`,disabled:T,children:[(0,y.jsxs)(`div`,{className:`grid three`,children:[(0,y.jsx)(i,{label:`店名`,name:`storeName`,defaultValue:b.storeName,required:!0,maxLength:80}),(0,y.jsx)(i,{label:`单据标题`,name:`receiptSubtitle`,defaultValue:b.receiptSubtitle,maxLength:80}),(0,y.jsx)(i,{label:`联系电话/客户联系信息(选填)`,name:`contact`,defaultValue:b.contact,maxLength:80})]}),(0,y.jsx)(`h3`,{children:`开单默认费用`}),(0,y.jsxs)(`div`,{className:`grid four`,children:[(0,y.jsx)(i,{label:`证书成本(元)`,name:`certificate`,defaultValue:b.certificate,...A}),(0,y.jsx)(i,{label:`包装成本(元)`,name:`packaging`,defaultValue:b.packaging,...A}),(0,y.jsx)(i,{label:`整单快递成本(元)`,name:`shipping`,defaultValue:b.shipping,...A}),(0,y.jsx)(a,{label:`包装默认付款人`,name:`packagingPayer`,defaultValue:b.packagingPayer,options:u(r,b.packagingPayer,o)})]}),(0,y.jsx)(`p`,{className:`small muted`,children:`默认费用只用于新增项目。已有订单、模板和草稿里已经填写的费用及付款人按原值保留,开单时仍可按实际修改。`}),(0,y.jsx)(`h3`,{children:`手续费与利润分成`}),(0,y.jsxs)(`div`,{className:`grid three`,children:[(0,y.jsx)(i,{label:`信用卡手续费率(‰)`,name:`creditCardRatePermille`,defaultValue:b.creditCardRatePermille,type:`number`,min:0,max:1e3,step:1,required:!0}),(0,y.jsx)(i,{label:`助理利润分成比例(%)`,name:`profitSharePercent`,defaultValue:b.profitSharePercent,type:`number`,min:0,max:100,step:1,required:!0}),(0,y.jsx)(i,{label:`分成生效月份`,name:`profitShareEffectiveMonth`,defaultValue:b.profitShareEffectiveMonth,type:`month`,min:n.slice(0,7),required:!0})]}),(0,y.jsx)(`p`,{className:`small muted`,children:`新建订单使用当前信用卡费率;修改旧单沿用原费率。分成按生效月份统计,历史月份保留原口径。`})]}),(0,y.jsx)(`div`,{id:`shop-settings-error`,className:x?`error`:void 0,"aria-live":`polite`,children:x}),(0,y.jsx)(`div`,{className:`form-actions`,children:(0,y.jsx)(_,{className:`primary`,type:`submit`,disabled:s||e.busy||!!e.pending(),children:`保存店铺配置`})})]},D)]})}function N({client:e,state:n,session:r,refreshState:a,onAccountChanged:o,registerLeaveGuard:u}){let f=n,p=f.user.role===`owner`,m=f.mode===`demo`,h=f.shopSettings,g=f.members?.length?f.members:h?.members?.length?h.members:l,b=!!(f.user.member||f.members?.some(e=>e.member===`assistant2`)),[S,C]=(0,v.useState)(!1),[w,T]=(0,v.useState)(!1),[E,D]=(0,v.useState)(!1),[k,j]=(0,v.useState)(!1),[N,P]=(0,v.useState)(!1),[F,I]=(0,v.useState)(0),[ee,L]=(0,v.useState)(0),[R,te]=(0,v.useState)({text:``,error:!1}),[z,B]=(0,v.useState)(!1),V=(0,v.useRef)(null),H=(0,v.useCallback)((e,t=!1)=>te({text:e,error:t}),[]);t(e);let U=(0,v.useCallback)(()=>V.current&&!V.current.canLeave()?!1:k||N?(H(`设置正在保存,请稍候。`,!0),!1):!(S||w||E)||window.confirm(`店铺或打印设置尚未保存,离开会丢弃这些修改。继续离开?`),[k,N,S,w,E,H]);(0,v.useEffect)(()=>u(U),[u,U]),(0,v.useEffect)(()=>{let e=e=>{(k||S||w||E||V.current?.beforeUnload())&&(e.preventDefault(),e.returnValue=``)};return window.addEventListener(`beforeunload`,e),()=>window.removeEventListener(`beforeunload`,e)},[k,S,w,E]);function W(){C(!1),T(!1),D(!1),L(e=>e+1)}async function G(t){t.preventDefault();let n=c(t.currentTarget);try{if(S||w)throw Error(`请先保存店铺与打印设置,再修改密码。`);await e.mutate(`/api/password`,n),o()}catch(e){H(d(e),!0)}}async function K(t){if(U())try{await e.api(`/api/demo-login`,{member:t}),o()}catch(e){H(d(e),!0)}}async function q(t){if(!z&&!(t===`backup`?V.current&&!V.current.canLeave():!U())){B(!0);try{if(t===`backup`){let t=await e.api(`/api/backup`,{}),n=document.createElement(`a`);n.href=`/api/backup/`+t.name,n.download=t.name,n.click(),H(`完整备份已生成`)}else{let t=await e.api(`/api/sync`,{});H(t.error||`同步重试完成,请查看状态`,!!t.error)}await a(),W()}catch(e){H(d(e),!0)}finally{B(!1)}}}let J=f.user.member||r.user?.member||(p?`owner`:`assistant`),Y=(0,y.jsxs)(`section`,{className:`panel`,children:[(0,y.jsx)(`h2`,{children:`更改当前账号密码`}),(0,y.jsxs)(`form`,{id:`password-form`,onSubmit:G,children:[(0,y.jsxs)(`div`,{className:`grid two`,children:[(0,y.jsx)(i,{label:`原密码`,name:`current`,type:`password`,required:!0,autoComplete:`current-password`}),(0,y.jsx)(i,{label:`新密码(至少12个字符)`,name:`password`,type:`password`,required:!0,minLength:12,autoComplete:`new-password`})]}),(0,y.jsx)(`div`,{className:`form-actions`,children:(0,y.jsx)(_,{disabled:e.busy,children:`修改并重新登录`})})]})]});return(0,y.jsxs)(`div`,{className:`settings-page`,onKeyDown:e=>{e.target instanceof HTMLInputElement&&e.target.type===`number`&&[`ArrowUp`,`ArrowDown`].includes(e.key)&&e.preventDefault()},onWheel:e=>{e.target instanceof HTMLInputElement&&e.target.type===`number`&&document.activeElement===e.target&&e.target.blur()},children:[p&&b&&(0,y.jsxs)(`section`,{className:`panel team-settings`,children:[(0,y.jsxs)(`div`,{className:`panel-title`,children:[(0,y.jsx)(`h2`,{children:`成员与账号`}),(0,y.jsxs)(`span`,{className:`small muted`,children:[`当前:`,g.find(e=>e.member===J)?.label||`待确认`]})]}),m&&(0,y.jsxs)(y.Fragment,{children:[(0,y.jsx)(`p`,{className:`note`,children:`三人功能预览·虚构数据,切换身份体验各自订单与账目。`}),(0,y.jsx)(`div`,{className:`actions`,children:g.filter(e=>e.enabled!==!1).map(e=>(0,y.jsxs)(s,{action:`demo-`+e.member,onClick:()=>K(e.member),children:[`切换`,e.label]},e.member))})]}),(0,y.jsx)(`p`,{className:`small muted`,children:`员工姓名、登录名、密码及启停状态在「员工管理」中维护。`})]}),(0,y.jsx)(`div`,{className:`page-heading`,children:(0,y.jsxs)(`div`,{children:[(0,y.jsx)(`div`,{className:`eyebrow`,children:p?`SHOP & PRINT SETTINGS`:`ACCOUNT`}),(0,y.jsx)(`h1`,{children:p?`店铺与打印设置`:`修改密码`}),(0,y.jsx)(`p`,{className:`muted`,children:p?`店铺身份、成员和默认值统一配置;打印布局按实际纸张校准。`:`更改本人登录密码,保存后重新登录。`})]})}),p?(0,y.jsxs)(y.Fragment,{children:[h&&(0,y.jsxs)(`section`,{id:`shop-settings-panel`,className:`panel shop-settings`,children:[(0,y.jsx)(M,{client:e,shop:h,today:f.today,members:g,team:b,busy:k||N,dirty:S,onDirty:C,onBusy:j,onSaved:async()=>{await a(),I(e=>e+1)},notice:H}),(0,y.jsx)(x,{client:e,shop:h,members:g,team:b,busy:k||N,dirty:E,onDirty:D,onBusy:j,onSaved:async()=>{await a()},notice:H})]}),(0,y.jsx)(O,{ref:V,client:e,shopDirty:S||w,reloadKey:F,onSaved:a,onAccountChanged:o,notice:H}),(0,y.jsx)(A,{client:e,printing:f.printing,revision:h?.revision,orderId:f.orders[0]?.id,shopDirty:S,dirty:w,onDirty:T,onBusy:P,refresh:a,notice:H}),(0,y.jsxs)(`div`,{className:`grid two`,children:[(0,y.jsxs)(`section`,{className:`panel`,children:[(0,y.jsx)(`h2`,{children:`数据库与备份`}),(0,y.jsx)(`p`,{children:f.mode===`production`?`主账保存在部署服务器的独立SQLite数据库。`:`现在运行在本机试用环境,云端正式数据库尚未部署。`}),(0,y.jsx)(`p`,{className:`small muted`,children:`启动时和运行期间每6小时生成完整数据库备份。本机同盘备份不能防电脑损坏,正式部署还需异地备份。`}),f.backup?.last&&(0,y.jsxs)(`p`,{className:`small`,children:[`本次运行最近备份:`,f.backup.last.time]}),f.backup?.error&&(0,y.jsx)(`p`,{className:`error`,children:f.backup.error}),(0,y.jsx)(s,{action:`backup`,disabled:z||e.busy,onClick:()=>q(`backup`),children:`生成并下载完整备份`})]}),(0,y.jsxs)(`section`,{className:`panel`,children:[(0,y.jsx)(`h2`,{children:`飞书查看副本`}),(0,y.jsx)(`p`,{children:(0,y.jsx)(`span`,{className:`badge `+(f.sync.enabled?``:`warn`),children:f.sync.enabled?`已配置`:`尚未连接`})}),(0,y.jsx)(`p`,{className:`small muted`,children:`使用新账专用表单向同步。修改和复核在本系统完成;同步失败不重开订单、不影响本地账务保存。`}),f.sync.enabled?(0,y.jsxs)(y.Fragment,{children:[(0,y.jsxs)(`p`,{children:[`待同步 `,f.sync.pending,` 项 · 失败 `,f.sync.failed,` 项`]}),f.sync.error&&(0,y.jsx)(`p`,{className:`error`,children:f.sync.error}),(0,y.jsx)(s,{action:`sync`,disabled:z||e.busy,onClick:()=>q(`sync`),children:`立即重试同步`})]}):(0,y.jsx)(`p`,{className:`note`,children:`已预留应用API同步通道。正式连接时再配置新表,未连接原销售表,未试写真实账务。`})]})]}),!m&&Y]}):m?(0,y.jsx)(`section`,{className:`panel`,children:(0,y.jsx)(`p`,{className:`muted`,children:`演示账号不提供密码修改。`})}):Y,R.text&&(0,y.jsx)(`div`,{className:`settings-notice `+(R.error?`error`:``),role:R.error?`alert`:`status`,children:R.text})]},ee)}export{N as SettingsPage,N as default}; |