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