import{j as e,m as C,A as ie}from"./vendor-animation-0s3FMHwK.js";import{g as oe,r as n,L as d}from"./vendor-react-BVs3cwbi.js";import{a as re,u as le,b as de,d as ce,e as S,g as he,C as H,F as E,c as f}from"./index-BrM8fzBu.js";import{F as me}from"./Forbidden-D25jV3Oq.js";import{u as xe,a as ue,S as y}from"./useListData-Bz9Xh_CU.js";import{P as pe}from"./Pagination-B1sbY6V7.js";import"./vendor-utils-Dyr8OjFr.js";const v="/api/admin",I="boha_offer_draft";function we(){const a=re(),{hasPermission:r}=le(),q=oe(),{sort:R,order:c,handleSort:x,activeSort:u}=xe("quotation_number"),[b,K]=n.useState(""),[J,O]=n.useState(1),[k,z]=n.useState({show:!1,quotation:null}),[U,W]=n.useState(!1),[g,_]=n.useState({show:!1,quotation:null}),[Y,V]=n.useState(!1),[G,A]=n.useState(null),[D,T]=n.useState(null),[l,$]=n.useState(null),[h,w]=n.useState({show:!1,quotation:null});de(h.show);const[N,F]=n.useState(""),[p,M]=n.useState(null),[o,L]=n.useState(null),{items:j,loading:Q,pagination:P,refetch:B}=ue("offers.php",{dataKey:"quotations",search:b,sort:R,order:c,page:J,errorMsg:"Nepodařilo se načíst nabídky"});n.useEffect(()=>{try{const t=localStorage.getItem(I);if(!t)return;const s=JSON.parse(t);s&&s.form&&Array.isArray(s.items)&&L(s)}catch{}},[]);const X=()=>{try{localStorage.removeItem(I)}catch{}L(null)},ee=(t,s)=>t?"offers-invalidated-row":s?"offers-expired-row":"";if(!r("offers.view"))return e.jsx(me,{});const te=async t=>{A(t.id);try{const i=await(await f(`${v}/offers.php?action=duplicate&id=${t.id}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({})})).json();i.success?(a.success(i.message||"Nabídka byla duplikována"),B()):a.error(i.error||"Nepodařilo se duplikovat nabídku")}catch{a.error("Chyba připojení")}finally{A(null)}},Z=async()=>{if(!(!N.trim()||!h.quotation)){$(h.quotation.id);try{const t=new FormData;t.append("quotationId",h.quotation.id),t.append("customerOrderNumber",N.trim()),p&&t.append("attachment",p);const i=await(await f(`${v}/orders.php`,{method:"POST",body:t})).json();i.success?(w({show:!1,quotation:null}),a.success(i.message||"Objednávka byla vytvořena"),q(`/orders/${i.data.order_id}`)):a.error(i.error||"Nepodařilo se vytvořit objednávku")}catch{a.error("Chyba připojení")}finally{$(null)}}},se=async()=>{if(k.quotation){W(!0);try{const s=await(await f(`${v}/offers.php?id=${k.quotation.id}`,{method:"DELETE"})).json();s.success?(z({show:!1,quotation:null}),a.success(s.message||"Nabídka byla smazána"),B()):a.error(s.error||"Nepodařilo se smazat nabídku")}catch{a.error("Chyba připojení")}finally{W(!1)}}},ae=async()=>{if(g.quotation){V(!0);try{const s=await(await f(`${v}/offers.php?action=invalidate&id=${g.quotation.id}`,{method:"POST"})).json();s.success?(_({show:!1,quotation:null}),a.success(s.message||"Nabídka byla zneplatněna"),B()):a.error(s.error||"Nepodařilo se zneplatnit nabídku")}catch{a.error("Chyba připojení")}finally{V(!1)}}},ne=async t=>{if(!D){T(t.id);try{const s=await f(`${v}/offers-pdf.php?id=${t.id}`);if(s.status===401)return;if(!s.ok){a.error("Nepodařilo se vygenerovat PDF");return}const i=await s.text(),m=window.open("","_blank");m?(m.document.open(),m.document.write(i),m.document.close(),m.onload=()=>m.print()):a.error("Prohlížeč zablokoval vyskakovací okno")}catch{a.error("Chyba při generování PDF")}finally{T(null)}}};return Q?e.jsxs("div",{className:"admin-skeleton",style:{padding:0,gap:"1.5rem"},children:[e.jsxs("div",{className:"admin-skeleton-row",style:{justifyContent:"space-between"},children:[e.jsxs("div",{children:[e.jsx("div",{className:"admin-skeleton-line h-8",style:{width:"200px",marginBottom:"0.5rem"}}),e.jsx("div",{className:"admin-skeleton-line",style:{width:"140px"}})]}),e.jsxs("div",{style:{display:"flex",gap:"0.5rem"},children:[e.jsx("div",{className:"admin-skeleton-line h-10",style:{width:"120px",borderRadius:"8px"}}),e.jsx("div",{className:"admin-skeleton-line h-10",style:{width:"140px",borderRadius:"8px"}})]})]}),e.jsx("div",{className:"admin-card",children:e.jsxs("div",{className:"admin-skeleton",style:{gap:"1.25rem"},children:[e.jsx("div",{className:"admin-skeleton-line h-10",style:{width:"100%",borderRadius:"8px",marginBottom:"0.5rem"}}),[0,1,2,3,4].map(t=>e.jsxs("div",{className:"admin-skeleton-row",children:[e.jsx("div",{className:"admin-skeleton-line circle"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("div",{className:"admin-skeleton-line w-1/3",style:{marginBottom:"0.5rem"}}),e.jsx("div",{className:"admin-skeleton-line w-1/4",style:{height:"10px"}})]}),e.jsx("div",{className:"admin-skeleton-line w-1/4"})]},t))]})})]}):e.jsxs("div",{children:[e.jsxs(C.div,{className:"admin-page-header",initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{duration:.4},children:[e.jsxs("div",{children:[e.jsx("h1",{className:"admin-page-title",children:"Nabídky"}),e.jsxs("p",{className:"admin-page-subtitle",children:[P?.total??j.length," ",ce(P?.total??j.length,"nabídka","nabídky","nabídek")]})]}),e.jsxs("div",{className:"admin-page-actions",children:[r("offers.settings")&&e.jsxs(d,{to:"/offers/templates",className:"admin-btn admin-btn-secondary",children:[e.jsxs("svg",{width:"20",height:"20",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("rect",{x:"3",y:"3",width:"18",height:"18",rx:"2"}),e.jsx("path",{d:"M3 9h18M9 21V9"})]}),"Šablony"]}),r("offers.create")&&e.jsxs(d,{to:"/offers/new",className:"admin-btn admin-btn-primary",children:[e.jsxs("svg",{width:"20",height:"20",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("line",{x1:"12",y1:"5",x2:"12",y2:"19"}),e.jsx("line",{x1:"5",y1:"12",x2:"19",y2:"12"})]}),"Nová nabídka"]})]})]}),e.jsx(C.div,{className:"admin-card",initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{duration:.4,delay:.1},children:e.jsxs("div",{className:"admin-card-body",children:[e.jsx("div",{className:"admin-search-bar mb-4",children:e.jsx("input",{type:"text",value:b,onChange:t=>{K(t.target.value),O(1)},className:"admin-form-input",placeholder:"Hledat podle čísla, projektu nebo zákazníka..."})}),j.length===0&&!o?e.jsxs("div",{className:"admin-empty-state",children:[e.jsx("div",{className:"admin-empty-icon",children:e.jsxs("svg",{width:"28",height:"28",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"}),e.jsx("line",{x1:"12",y1:"18",x2:"12",y2:"12"}),e.jsx("line",{x1:"9",y1:"15",x2:"15",y2:"15"})]})}),e.jsx("p",{children:"Zatím nejsou žádné nabídky."}),r("offers.create")&&e.jsx(d,{to:"/offers/new",className:"admin-btn admin-btn-primary",children:"Vytvořit první nabídku"})]}):e.jsx("div",{className:"admin-table-responsive",children:e.jsxs("table",{className:"admin-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsxs("th",{style:{cursor:"pointer"},onClick:()=>x("quotation_number"),children:["Číslo ",e.jsx(y,{column:"quotation_number",sort:u,order:c})]}),e.jsxs("th",{style:{cursor:"pointer"},onClick:()=>x("project_code"),children:["Projekt ",e.jsx(y,{column:"project_code",sort:u,order:c})]}),e.jsx("th",{children:"Zákazník"}),e.jsxs("th",{style:{cursor:"pointer"},onClick:()=>x("created_at"),children:["Datum ",e.jsx(y,{column:"created_at",sort:u,order:c})]}),e.jsxs("th",{style:{cursor:"pointer"},onClick:()=>x("valid_until"),children:["Platnost ",e.jsx(y,{column:"valid_until",sort:u,order:c})]}),e.jsxs("th",{style:{cursor:"pointer"},onClick:()=>x("currency"),children:["Měna ",e.jsx(y,{column:"currency",sort:u,order:c})]}),e.jsx("th",{className:"text-right",children:"Celkem"}),e.jsx("th",{children:"Akce"})]})}),e.jsxs("tbody",{children:[o&&!b&&e.jsxs("tr",{className:"offers-draft-row",children:[e.jsx("td",{children:e.jsxs("span",{className:"offers-draft-row-label",children:["Koncept",o.savedAt&&e.jsxs("span",{style:{fontWeight:400,opacity:.8},children:[" · ",new Date(o.savedAt).toLocaleTimeString("cs-CZ",{hour:"2-digit",minute:"2-digit"})]})]})}),e.jsx("td",{children:o.form.project_code||"—"}),e.jsx("td",{children:o.form.customer_name||"—"}),e.jsx("td",{className:"admin-mono",children:o.form.created_at?S(o.form.created_at):"—"}),e.jsx("td",{className:"admin-mono",children:o.form.valid_until?S(o.form.valid_until):"—"}),e.jsx("td",{children:e.jsx("span",{className:"admin-badge admin-badge-secondary",children:o.form.currency||"—"})}),e.jsx("td",{}),e.jsx("td",{children:e.jsxs("div",{className:"admin-table-actions",children:[e.jsx(d,{to:"/offers/new",className:"admin-btn-icon",title:"Pokračovat v konceptu","aria-label":"Pokračovat v konceptu",children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"}),e.jsx("path",{d:"M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"})]})}),e.jsx("button",{onClick:X,className:"admin-btn-icon danger",title:"Zahodit koncept",children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("polyline",{points:"3 6 5 6 21 6"}),e.jsx("path",{d:"M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"})]})})]})})]}),j.map(t=>{const s=t.status==="invalidated",i=!s&&!t.order_id&&t.valid_until&&new Date(t.valid_until)te(t),className:"admin-btn-icon",title:"Duplikovat",disabled:G===t.id,children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),e.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]})}),!s&&t.order_id?e.jsx(d,{to:`/orders/${t.order_id}`,className:"admin-btn-icon accent",title:"Zobrazit objednávku","aria-label":"Zobrazit objednávku",children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"}),e.jsx("text",{x:"12",y:"16.5",textAnchor:"middle",fill:"currentColor",stroke:"none",fontSize:"9",fontWeight:"700",children:"O"})]})}):!s&&r("orders.create")&&e.jsx("button",{onClick:()=>{F(""),M(null),w({show:!0,quotation:t})},className:"admin-btn-icon",title:"Vytvořit objednávku",disabled:l===t.id,children:l===t.id?e.jsx("div",{className:"admin-spinner",style:{width:18,height:18,borderWidth:2}}):e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"}),e.jsx("line",{x1:"12",y1:"11",x2:"12",y2:"17"}),e.jsx("line",{x1:"9",y1:"14",x2:"15",y2:"14"})]})}),i&&!s&&r("offers.edit")&&e.jsx("button",{onClick:()=>_({show:!0,quotation:t}),className:"admin-btn-icon",title:"Zneplatnit",children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("circle",{cx:"12",cy:"12",r:"10"}),e.jsx("line",{x1:"4.93",y1:"4.93",x2:"19.07",y2:"19.07"})]})}),r("offers.export")&&e.jsx("button",{onClick:()=>ne(t),className:"admin-btn-icon",title:"PDF",disabled:D===t.id,children:D===t.id?e.jsx("div",{className:"admin-spinner",style:{width:18,height:18,borderWidth:2}}):e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"}),e.jsx("line",{x1:"16",y1:"13",x2:"8",y2:"13"}),e.jsx("line",{x1:"16",y1:"17",x2:"8",y2:"17"})]})}),r("offers.delete")&&e.jsx("button",{onClick:()=>z({show:!0,quotation:t}),className:"admin-btn-icon danger",title:"Smazat",children:e.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("polyline",{points:"3 6 5 6 21 6"}),e.jsx("path",{d:"M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"})]})})]})})]},t.id)}),j.length===0&&o&&b&&e.jsx("tr",{children:e.jsx("td",{colSpan:8,className:"text-muted",style:{textAlign:"center",padding:"1.5rem"},children:"Žádné nabídky odpovídající hledání."})})]})]})}),e.jsx(pe,{pagination:P,onPageChange:O})]})}),e.jsx(H,{isOpen:k.show,onClose:()=>z({show:!1,quotation:null}),onConfirm:se,title:"Smazat nabídku",message:`Opravdu chcete smazat nabídku "${k.quotation?.quotation_number}"? Budou smazány i všechny položky a sekce. Tato akce je nevratná.`,confirmText:"Smazat",cancelText:"Zrušit",type:"danger",loading:U}),e.jsx(H,{isOpen:g.show,onClose:()=>_({show:!1,quotation:null}),onConfirm:ae,title:"Zneplatnit nabídku",message:`Opravdu chcete zneplatnit nabídku "${g.quotation?.quotation_number}"? Nabídka bude pouze pro čtení a nepůjde upravovat.`,confirmText:"Zneplatnit",cancelText:"Zrušit",type:"danger",loading:Y}),e.jsx(ie,{children:h.show&&e.jsxs(C.div,{className:"admin-modal-overlay",initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},transition:{duration:.2},children:[e.jsx("div",{className:"admin-modal-backdrop",onClick:()=>!l&&w({show:!1,quotation:null})}),e.jsxs(C.div,{className:"admin-modal",initial:{opacity:0,scale:.95,y:20},animate:{opacity:1,scale:1,y:0},exit:{opacity:0,scale:.95,y:20},transition:{duration:.2},children:[e.jsxs("div",{className:"admin-modal-header",children:[e.jsx("h2",{className:"admin-modal-title",children:"Vytvořit objednávku"}),e.jsxs("p",{className:"text-secondary",style:{marginTop:"0.25rem",fontSize:"0.875rem"},children:["Nabídka: ",e.jsx("strong",{children:h.quotation?.quotation_number})]})]}),e.jsx("div",{className:"admin-modal-body",children:e.jsxs("div",{className:"admin-form",children:[e.jsx(E,{label:"Číslo objednávky zákazníka",required:!0,children:e.jsx("input",{type:"text",value:N,onChange:t=>F(t.target.value),onKeyDown:t=>t.key==="Enter"&&!l&&Z(),className:"admin-form-input",placeholder:"Např. PO-2026-001",autoFocus:!0})}),e.jsxs(E,{label:"Příloha (PDF)",children:[p?e.jsxs("div",{className:"flex-row gap-2",children:[e.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"var(--accent-color)",strokeWidth:"2",children:[e.jsx("path",{d:"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"}),e.jsx("polyline",{points:"14 2 14 8 20 8"})]}),e.jsxs("span",{style:{fontSize:"0.875rem"},children:[p.name," ",e.jsxs("span",{className:"text-tertiary",children:["(",(p.size/1024).toFixed(0)," KB)"]})]}),e.jsx("button",{type:"button",onClick:()=>M(null),className:"admin-btn-icon",title:"Odebrat",style:{marginLeft:"auto"},children:e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M18 6L6 18M6 6l12 12"})})})]}):e.jsxs("label",{className:"admin-btn admin-btn-secondary admin-btn-sm",style:{cursor:"pointer",display:"inline-flex",alignItems:"center",gap:"0.4rem"},children:[e.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"}),e.jsx("polyline",{points:"17 8 12 3 7 8"}),e.jsx("line",{x1:"12",y1:"3",x2:"12",y2:"15"})]}),"Vybrat soubor",e.jsx("input",{type:"file",accept:"application/pdf",onChange:t=>M(t.target.files[0]||null),style:{display:"none"}})]}),e.jsx("small",{className:"admin-form-hint",style:{marginTop:"0.25rem"},children:"Max 10 MB"})]})]})}),e.jsxs("div",{className:"admin-modal-footer",children:[e.jsx("button",{onClick:()=>w({show:!1,quotation:null}),className:"admin-btn admin-btn-secondary",disabled:!!l,children:"Zrušit"}),e.jsx("button",{onClick:Z,className:"admin-btn admin-btn-primary",disabled:!!l||!N.trim(),children:l?"Vytváření...":"Vytvořit"})]})]})]})})]})}export{we as default};