import{j as e,s as N}from"./index-3808da28.js";import{d as J,r,C as L,y as g,H as S,B as n,P as M,T as V,X as Y,a9 as F,aa as U,j as W,ab as H,m as c,a8 as K,ac as X,ad as G,a5 as Z,E as ee,Q as te,V as se}from"./vendor-61f0f29a.js";import{u as ae}from"./useResource-9c541112.js";const re={CNY:"¥",TWD:"NT$",USD:"$"},ce=()=>{const o=J(),[y,w]=r.useState({current:1,pageSize:10}),[v,C]=r.useState({field:"created_at",order:"descend"}),[h,p]=r.useState(!1),[d,f]=r.useState(null),[l,k]=r.useState([]),[T,u]=r.useState(!1);r.useState("all");const[ie,z]=r.useState([]),{resources:E,loading:I,total:j,fetchResources:m,deleteResource:q}=ae(y,v,"quota");r.useEffect(()=>{m()},[]);const O=(t,s,a)=>{w(t),C(a),m({current:t.current,pageSize:t.pageSize,field:a.field,order:a.order})},A=async t=>{try{await q(t),c.success("删除成功"),m()}catch(s){c.error("删除失败:"+s.message)}},$=async()=>{try{u(!0);const{data:t,error:s}=await N.from("resources").select("*").eq("type","serviceTemplate").order("created_at",{ascending:!1});if(s)throw s;k(t)}catch(t){console.error("获取服务模板失败:",t),c.error("获取服务模板失败")}finally{u(!1)}};r.useEffect(()=>{h&&$()},[h]),r.useEffect(()=>{l.length>0&&z(Q(l))},[l]);const P=t=>{f(t)},b=()=>{o(d?`/company/quotaInfo?templateId=${d}`:"/company/quotaInfo"),p(!1),f(null)},Q=t=>{const s=new Set;return t.forEach(a=>{var i;(i=a.attributes.category)==null||i.forEach(x=>{s.add(JSON.stringify(x))})}),Array.from(s).map(a=>JSON.parse(a))},R=[{title:"报价单名称",dataIndex:["attributes","quataName"],key:"quataName",ellipsis:!0},{title:"客户信息",dataIndex:["attributes","customers"],key:"customers",render:(t,s)=>e.jsx(g,{children:t==null?void 0:t.map(a=>e.jsx(S,{color:"blue",className:"cursor-pointer",onClick:()=>{o(`/company/customerInfo/${a.id}`)},children:a.name},a.id))})},{title:"报价总额",dataIndex:["attributes"],key:"totalAmount",align:"right",render:t=>{var a;const s=re[t.currency]||"¥";return e.jsxs("div",{className:"space-y-1",children:[e.jsx("div",{className:"flex justify-between items-center text-sm",children:e.jsxs(K.Text,{type:"secondary",style:{fontSize:"12px"},children:["税前:",s,(a=t.beforeTaxAmount)==null?void 0:a.toLocaleString()]})}),e.jsx("div",{className:"flex justify-between items-center",children:e.jsx(X,{value:t.afterTaxAmount,prefix:s,precision:2,valueStyle:{fontSize:"16px",fontWeight:600,color:"#1890ff"},className:"!mb-0"})})]})}},{title:"创建日期",dataIndex:"created_at",key:"created_at",sorter:!0,render:t=>e.jsx("span",{children:new Date(t).toLocaleString("zh-CN",{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit"})})},{title:"操作",key:"action",width:280,render:(t,s)=>e.jsxs(g,{size:0,children:[e.jsx(n,{size:"small",type:"link",icon:e.jsx(G,{}),onClick:()=>B(s),children:"复制"}),e.jsx(n,{size:"small",type:"link",icon:e.jsx(Z,{}),onClick:()=>o(`/company/quotaInfo/preview/${s.id}`),children:"查看"}),e.jsx(n,{size:"small",type:"link",icon:e.jsx(ee,{}),onClick:()=>o(`/company/quotaInfo/${s.id}?edit=true`),children:"编辑"}),e.jsx(te,{title:"确定要删除这个报价单吗?",description:"删除后将无法恢复!",onConfirm:()=>A(s.id),okText:"确定",cancelText:"取消",okButtonProps:{danger:!0},children:e.jsx(n,{size:"small",type:"link",danger:!0,icon:e.jsx(se,{}),children:"删除"})})]})}],_=()=>{const t=new Map;return t.set("uncategorized",{name:"未分类",templates:l.filter(s=>!s.attributes.category||s.attributes.category.length===0)}),l.forEach(s=>{s.attributes.category&&s.attributes.category.forEach(a=>{t.has(a.id)||t.set(a.id,{name:a.name,templates:[]}),t.get(a.id).templates.push(s)})}),Array.from(t.values()).filter(s=>s.templates.length>0)},B=async t=>{try{u(!0);const s=JSON.parse(JSON.stringify(t.attributes));s.quataName=`${s.quataName} (副本)`;const{data:a,error:i}=await N.from("resources").insert([{type:"quota",attributes:s}]);if(i)throw i;c.success("复制成功"),m()}catch(s){console.error("复制报价单失败:",s),c.error("复制失败:"+s.message)}finally{u(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx(L,{title:e.jsxs(g,{children:[e.jsx("span",{children:"报价单管理"}),e.jsxs(S,{color:"blue",children:[j," 个报价单"]})]}),className:"h-full w-full overflow-auto",extra:e.jsx(n,{type:"primary",icon:e.jsx(M,{}),onClick:()=>p(!0),children:"新增报价单"}),children:e.jsx(V,{className:"w-full",columns:R,dataSource:E,rowKey:"id",loading:I,onChange:O,pagination:{...y,total:j,showSizeChanger:!0,showQuickJumper:!0,showTotal:t=>`共 ${t} 条记录`}})}),e.jsx(Y,{title:"选择报价单模板",open:h,onCancel:()=>{p(!1),f(null)},footer:[e.jsxs(n,{onClick:()=>b(),children:[e.jsx(F,{})," 自定义创建"]},"custom"),e.jsxs(n,{type:"primary",disabled:!d,onClick:b,children:[e.jsx(U,{})," 使用选中模板"]},"submit")],width:800,children:T?e.jsx("div",{className:"flex justify-center items-center h-[400px]",children:e.jsx(W,{size:"large"})}):l.length===0?e.jsx(H,{description:"暂无可用模板"}):e.jsx("div",{className:"max-h-[600px] overflow-y-auto px-1",children:_().map((t,s)=>e.jsxs("div",{className:"mb-6 last:mb-2",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx("div",{className:"h-6 w-1 bg-blue-500 rounded-full"}),e.jsxs("h3",{className:"text-base font-medium text-gray-700",children:[t.name,e.jsxs("span",{className:"ml-2 text-sm text-gray-400 font-normal",children:["(",t.templates.length,")"]})]})]}),e.jsx("div",{className:"grid grid-cols-3 gap-3",children:t.templates.map(a=>{var i;return e.jsxs("div",{className:` p-3 border rounded-lg cursor-pointer transition-all ${d===a.id?"border-blue-500 bg-blue-50/50":"border-gray-200 hover:border-blue-300 hover:bg-gray-50/50"} `,onClick:()=>P(a.id),children:[e.jsxs("div",{className:"flex justify-between items-start gap-2 mb-2",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h4",{className:"font-medium text-gray-800 truncate",children:a.attributes.templateName}),e.jsx("p",{className:"text-xs text-gray-500 mt-1 line-clamp-1",children:a.attributes.description||"暂无描述"})]}),e.jsxs("span",{className:"text-red-500 font-medium whitespace-nowrap text-sm",children:["¥",(i=a.attributes.totalAmount)==null?void 0:i.toLocaleString()]})]}),e.jsx("div",{className:"space-y-1",children:a.attributes.sections.map((x,D)=>e.jsx("div",{className:"bg-white/80 px-2 py-1 rounded text-xs border border-gray-100",children:e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"font-medium text-blue-600 truncate flex-1",children:x.sectionName}),e.jsxs("span",{className:"text-gray-400 ml-1",children:[x.items.length,"项"]})]})},D))})]},a.id)})})]},s))})})]})};export{ce as default}; //# sourceMappingURL=index-7a916509.js.map