import{s as y,j as e}from"./index-3808da28.js";import{F as o,d as fe,a6 as ve,r as d,e as ke,m as h,C as Z,y as we,B as m,a7 as Se,I as v,z as E,ae as Ce,K as ee,E as Te,Q as _e,V as te,af as se,P as T,X as qe,a8 as Ve,l as Ae}from"./vendor-61f0f29a.js";const{Title:re,Text:O}=Ve,Ee=()=>{const[l]=o.useForm(),k=fe(),{id:i}=ve(),[L,w]=d.useState(!1),g=ke().search.includes("edit=true"),[_,q]=d.useState(null),[M,ae]=d.useState([]),[u,p]=d.useState({sections:[{items:[{}]}],currency:"CNY"}),[ne,S]=d.useState(!1),[U,ce]=d.useState([]),[V,P]=d.useState([]);d.useEffect(()=>{i&&ie(),Y(),le(),I()},[i]);const ie=async()=>{try{w(!0);const{data:t,error:s}=await y.from("resources").select("*").eq("id",i).single();if(s)throw s;const a={templateName:t.attributes.templateName,description:t.attributes.description,category:t.attributes.category.map(n=>n.id),sections:t.attributes.sections};l.setFieldsValue(a),p(a)}catch(t){console.error("获取服务模版失败:",t),h.error("获取服务模版失败")}finally{w(!1)}},Y=async()=>{try{const{data:t,error:s}=await y.from("resources").select("*").eq("type","sections").order("created_at",{ascending:!1});if(s)throw s;ae(t||[])}catch(t){h.error("获取小节模版失败"),console.error(t)}},le=async()=>{try{const{data:t,error:s}=await y.from("resources").select("*").eq("type","categories").order("created_at",{ascending:!1});if(s)throw s;const a=(t||[]).map(n=>({value:n.id,label:n.attributes.name}));ce(a)}catch(t){h.error("获取分类数据失败"),console.error(t)}},I=async()=>{try{const{data:t,error:s}=await y.from("resources").select("*").eq("type","units").order("created_at",{ascending:!1});if(s)throw s;const a=(t||[]).map(n=>({value:n.attributes.name,label:n.attributes.name}));P(a)}catch(t){console.error("获取单位数据失败:",t),h.error("获取单位数据失败")}},oe=async t=>{try{w(!0);const s=t.sections.reduce((c,j)=>c+(j.items||[]).reduce((N,f)=>N+(f.quantity*f.price||0),0),0),a=t.category.map(c=>{const j=U.find(N=>N.value===c);return{id:c,name:j.label}}),n={type:"serviceTemplate",attributes:{templateName:t.templateName,description:t.description,sections:t.sections,category:a,totalAmount:s}};let r;if(i?r=await y.from("resources").update(n).eq("id",i).select():r=await y.from("resources").insert([n]).select(),r.error)throw r.error;h.success("保存成功"),k("/company/serviceTeamplate")}catch(s){console.error("保存失败:",s),h.error("保存失败")}finally{w(!1)}},de=[{value:"CNY",label:"人民币 (¥)"},{value:"TWD",label:"台币 (NT$)"},{value:"USD",label:"美元 ($)"}],A=d.useMemo(()=>(t,s)=>{const a=Number(t)||0,n=Number(s)||0;return a*n},[]),D=d.useMemo(()=>(t=[])=>Array.isArray(t)?t.reduce((s,a)=>a?s+A(a.quantity,a.price):s,0):0,[A]),me=d.useMemo(()=>(t=[])=>Array.isArray(t)?t.reduce((s,a)=>a?s+D(a.items):s,0):0,[D]),b=d.useMemo(()=>(t,s=l.getFieldValue("currency"))=>{const a=Number(t)||0;return`${{CNY:"¥",TWD:"NT$",USD:"$"}[s]||""}${a.toLocaleString("zh-CN",{minimumFractionDigits:2,maximumFractionDigits:2})}`},[]),ue=(t,s)=>{p(s)},[F,C]=d.useState(""),xe=(t,s)=>{q(t),C(s||"")},z=()=>{if(!F.trim()){h.error("请输入小节名称");return}const s=[...l.getFieldValue("sections")];s[_]={...s[_],sectionName:F.trim()},l.setFieldValue("sections",s),q(null),C("")},he=()=>{q(null),C("")},ge=t=>{const s=l.getFieldValue("sections")||[],a={sectionName:t.attributes.name,items:(t.attributes.items||[]).map(r=>({name:r.name||"",description:r.description||"",price:r.price||0,quantity:r.quantity||1,unit:r.unit||""}))},n=[...s,a];l.setFieldValue("sections",n),p(r=>({...r,sections:n})),S(!1),h.success("套用模版成功")},ye=()=>{const t=l.getFieldValue("sections")||[],s={sectionName:`服务类型 ${t.length+1}`,items:[{name:"",description:"",price:0,quantity:1,unit:""}]},a=[...t,s];l.setFieldValue("sections",a),p(n=>({...n,sections:a})),S(!1)},pe=()=>{S(!0),Y()},be=t=>{const a=(l.getFieldValue("sections")||[]).filter((n,r)=>r!==t);l.setFieldValue("sections",a),p(n=>({...n,sections:a})),h.success("删除成功")},je=()=>e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:M.map(t=>{var s;return e.jsxs("div",{className:`group relative bg-white dark:bg-gray-800 rounded-lg shadow-sm 
              border border-gray-200 dark:border-gray-700 hover:shadow-lg 
              transition-all duration-300 cursor-pointer`,onClick:()=>ge(t),children:[e.jsxs("div",{className:"p-6",children:[e.jsxs("div",{className:"text-center mb-4",children:[e.jsx("h3",{className:`text-lg font-medium text-gray-900 dark:text-gray-100 
                  group-hover:text-blue-500 transition-colors`,children:t.attributes.name}),e.jsxs("div",{className:"text-sm text-gray-500 dark:text-gray-400 mt-1",children:[((s=t.attributes.items)==null?void 0:s.length)||0," 个项目"]})]}),e.jsxs("div",{className:"space-y-2 mt-4 border-t dark:border-gray-700 pt-4",children:[(t.attributes.items||[]).slice(0,3).map((a,n)=>e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-300 truncate flex-1",children:a.name}),e.jsx("span",{className:"text-sm text-gray-500 dark:text-gray-400 ml-2",children:b(a.price)})]},n)),(t.attributes.items||[]).length>3&&e.jsxs("div",{className:"text-sm text-gray-500 dark:text-gray-400 text-center",children:["还有 ",t.attributes.items.length-3," 个项目..."]})]}),e.jsxs("div",{className:"mt-4 pt-4 border-t dark:border-gray-700 flex justify-between items-center",children:[e.jsx("span",{className:"text-sm text-gray-600 dark:text-gray-300",children:"总金额"}),e.jsx("span",{className:"text-base font-medium text-blue-500",children:b((t.attributes.items||[]).reduce((a,n)=>a+(n.price*(n.quantity||1)||0),0))})]})]}),e.jsx("div",{className:`absolute inset-0 border-2 border-transparent 
              group-hover:border-blue-500 rounded-lg transition-colors duration-300`}),e.jsx("div",{className:`absolute top-3 right-3 opacity-0 group-hover:opacity-100 
              transition-opacity duration-300`,children:e.jsx(m,{type:"primary",size:"small",className:"flex items-center gap-1",icon:e.jsx(T,{}),children:"套用"})})]},t.id)})}),M.length===0&&e.jsx("div",{className:"text-center py-8",children:e.jsx("div",{className:"text-gray-500 dark:text-gray-400",children:"暂无可用模版"})}),e.jsx(Ae,{className:"dark:border-gray-700"}),e.jsx("div",{className:"flex justify-center",children:e.jsx(m,{type:"dashed",icon:e.jsx(T,{}),onClick:ye,className:`w-1/3 border-2 hover:border-blue-400 hover:text-blue-500 
            dark:border-gray-600 dark:text-gray-400 dark:hover:text-blue-400
            transition-all duration-300`,children:"自定义小节"})})]}),Ne=(t,s,a)=>{const n=async r=>{try{const{error:c}=await y.from("resources").insert([{type:"units",attributes:{name:r},schema_version:1}]);if(c)throw c;h.success("新增单位成功"),await I(),l.setFieldValue(["sections",s,"items",a,"unit"],r)}catch(c){h.error("新增单位失败"),console.error(c)}};return e.jsx(o.Item,{...t,name:[t.name,"unit"],className:"!mb-0",children:e.jsx(E,{placeholder:"选择或输入单位",options:V,showSearch:!0,allowClear:!0,onSearch:r=>{if(r&&!V.find(c=>c.value===r)){const c={value:r,label:`添加 "${r}"`};P([...V,c])}},onSelect:(r,c)=>{c.label.startsWith('添加 "')&&n(r)},filterOption:(r,c)=>((c==null?void 0:c.label)??"").toLowerCase().includes(r.toLowerCase())})})};return e.jsx(Z,{title:e.jsx("span",{className:"text-gray-800 dark:text-gray-200",children:i?g?"编辑服务模版":"查看服务模版":"新增服务模版"}),className:"dark:bg-gray-800",extra:e.jsxs(we,{children:[e.jsx(m,{icon:e.jsx(Se,{}),onClick:()=>k("/company/serviceTeamplate"),className:"flex items-center",children:"返回列表"}),i&&!g&&e.jsx(m,{type:"primary",onClick:()=>k(`/company/serviceTemplateInfo/${i}?edit=true`),className:"flex items-center",children:"编辑"})]}),children:e.jsxs(o,{form:l,layout:"vertical",onFinish:oe,initialValues:{sections:[{items:[{}]}],currency:"CNY"},disabled:i&&!g,onValuesChange:ue,children:[e.jsxs("div",{className:"bg-white dark:bg-gray-800 p-2 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 mb-6",children:[e.jsx(re,{level:5,className:"mb-4 dark:text-gray-200",children:"基本信息"}),e.jsxs("div",{className:"grid grid-cols-2 gap-6",children:[e.jsx(o.Item,{label:"模版名称",name:"templateName",rules:[{required:!0,message:"请输入模版名称"}],children:e.jsx(v,{placeholder:"请输入模版名称"})}),e.jsx(o.Item,{label:"货币单位",name:"currency",rules:[{required:!0,message:"请选择货币单位"}],initialValue:"CNY",children:e.jsx(E,{options:de,placeholder:"请选择货币单位"})}),e.jsx(o.Item,{label:"模版分类",name:"category",rules:[{required:!0,message:"请选择或输入分类"}],children:e.jsx(E,{placeholder:"请选择或输入分类",showSearch:!0,allowClear:!0,mode:"tags",options:U,loading:L})}),e.jsx(o.Item,{label:"模版描述",name:"description",className:"col-span-2",children:e.jsx(v.TextArea,{rows:4,placeholder:"请输入模版描述"})})]})]}),e.jsxs("div",{className:"bg-white dark:bg-gray-800 p-2 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700",children:[e.jsx("div",{className:"flex items-center justify-between mb-6",children:e.jsx(re,{level:5,className:"!mb-0 dark:text-gray-200",children:"报价明细"})}),e.jsx(o.List,{name:"sections",children:(t,{add:s,remove:a})=>e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"space-y-6",children:t.map((n,r)=>e.jsxs(Z,{className:`!border-gray-200 dark:!border-gray-700 
                        dark:bg-gray-800 hover:shadow-md transition-shadow duration-300`,children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("div",{className:"flex items-center gap-2",children:_===r?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(v,{value:F,onChange:c=>C(c.target.value),onPressEnter:z,autoFocus:!0,className:"w-48"}),e.jsx(m,{type:"link",icon:e.jsx(Ce,{}),onClick:z,className:"text-green-500 hover:text-green-600"}),e.jsx(m,{type:"link",icon:e.jsx(ee,{}),onClick:he,className:"text-red-500 hover:text-red-600"})]}):e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(O,{strong:!0,className:"text-lg dark:text-gray-200",children:l.getFieldValue(["sections",r,"sectionName"])||`服务类型 ${r+1}`}),(!i||g)&&e.jsx(m,{type:"link",icon:e.jsx(Te,{}),onClick:()=>xe(r,l.getFieldValue(["sections",r,"sectionName"])||`服务类型 ${r+1}`),className:"text-gray-400 hover:text-blue-500"})]})}),e.jsx("div",{className:"flex items-center gap-4",children:(!i||g)&&e.jsx(_e,{title:"确认删除",description:"确定要删除这个小节吗？",onConfirm:()=>be(r),okText:"确定",cancelText:"取消",okButtonProps:{className:"bg-red-500 hover:bg-red-600 border-red-500"},children:e.jsx(m,{type:"text",danger:!0,icon:e.jsx(te,{}),className:"hover:text-red-500 transition-colors"})})})]}),e.jsxs("div",{className:"grid grid-cols-[3fr_4fr_1fr_1fr_2fr_1fr_40px] gap-4 mb-2 text-gray-500 dark:text-gray-400 px-2",children:[e.jsx("div",{children:"项目明细"}),e.jsx("div",{children:"描述/备注"}),e.jsx("div",{className:"text-center",children:"数量"}),e.jsx("div",{className:"text-center",children:"单位"}),e.jsx("div",{className:"text-center",children:"单价"}),e.jsx("div",{className:"text-right",children:"小计"}),e.jsx("div",{})]}),e.jsx(o.List,{name:[n.name,"items"],children:(c,{add:j,remove:N})=>{var f,B;return e.jsxs(e.Fragment,{children:[c.map((x,$)=>{var W,Q,R,K,X,G,H,J;return e.jsxs("div",{className:"grid grid-cols-[3fr_4fr_1fr_1fr_2fr_1fr_40px] gap-4 mb-4 items-start",children:[e.jsx(o.Item,{...x,name:[x.name,"name"],className:"!mb-0",children:e.jsx(v,{placeholder:"服务项目名称"})}),e.jsx(o.Item,{...x,name:[x.name,"description"],className:"!mb-0",children:e.jsx(v,{placeholder:"请输入描述/备注"})}),e.jsx(o.Item,{...x,name:[x.name,"quantity"],className:"!mb-0",children:e.jsx(se,{placeholder:"数量",min:0,className:"w-full"})}),Ne(x,r,$),e.jsx(o.Item,{...x,name:[x.name,"price"],className:"!mb-0",children:e.jsx(se,{placeholder:"单价",min:0,className:"w-full"})}),e.jsx("div",{className:"text-right",children:e.jsx(O,{className:"text-gray-500 dark:text-gray-400",children:b(A((K=(R=(Q=(W=u==null?void 0:u.sections)==null?void 0:W[r])==null?void 0:Q.items)==null?void 0:R[$])==null?void 0:K.quantity,(J=(H=(G=(X=u==null?void 0:u.sections)==null?void 0:X[r])==null?void 0:G.items)==null?void 0:H[$])==null?void 0:J.price))})}),(!i||g)&&c.length>1&&e.jsx(m,{type:"text",danger:!0,icon:e.jsx(te,{}),onClick:()=>N(x.name),className:"flex items-center justify-center"})]},x.key)}),(!i||g)&&e.jsx(m,{type:"dashed",onClick:()=>j(),icon:e.jsx(T,{}),className:"w-full hover:border-blue-400 hover:text-blue-500 mb-4 dark:border-gray-600 dark:text-gray-400 dark:hover:text-blue-400",children:"添加服务项目"}),e.jsx("div",{className:"flex justify-end border-t dark:border-gray-700 pt-4",children:e.jsxs(O,{className:"text-gray-500 dark:text-gray-400",children:["小计总额：",e.jsx("span",{className:"text-blue-500 dark:text-blue-400 font-medium ml-2",children:b(D((B=(f=u==null?void 0:u.sections)==null?void 0:f[r])==null?void 0:B.items))})]})})]})}})]},n.key))}),(!i||g)&&e.jsx("div",{className:"mt-6 flex justify-center",children:e.jsx(m,{type:"dashed",onClick:pe,icon:e.jsx(T,{}),className:`w-1/3 border-2 hover:border-blue-400 hover:text-blue-500 
                        dark:border-gray-600 dark:text-gray-400 dark:hover:text-blue-400
                        transition-all duration-300`,children:"新建小节"})}),e.jsx("div",{className:"mt-8 pt-6 border-t border-gray-200 dark:border-gray-700",children:e.jsx("div",{className:"flex flex-col gap-2",children:e.jsxs("div",{className:"flex justify-between items-center mt-4 pt-4 border-t border-gray-200 dark:border-gray-700",children:[e.jsx("span",{className:"text-base font-medium text-gray-700 dark:text-gray-300",children:"总金额"}),e.jsx("span",{className:"text-xl font-semibold text-blue-500 dark:text-blue-400",children:b(me(u==null?void 0:u.sections))})]})})})]})})]}),(!i||g)&&e.jsxs("div",{className:"mt-6 flex justify-end gap-4",children:[e.jsx(m,{onClick:()=>k("/company/serviceTeamplate"),className:"px-6 hover:bg-gray-100 dark:hover:bg-gray-700 dark:text-gray-200",children:"取消"}),e.jsx(m,{type:"primary",htmlType:"submit",loading:L,className:"px-6 hover:opacity-90",children:"保存"})]}),e.jsx(qe,{title:e.jsx("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100",children:"选择小节模版"}),open:ne,onCancel:()=>S(!1),footer:null,width:800,className:"dark:bg-gray-800",closeIcon:e.jsx(ee,{className:"text-gray-500 dark:text-gray-400"}),children:je()})]})})};export{Ee as default};
//# sourceMappingURL=index-2eaa6d56.js.map
