import{j as e,m as C}from"./vendor-animation-0s3FMHwK.js";import{g as $,r,L as M}from"./vendor-react-BVs3cwbi.js";import{a as q,u as B,F as c,A as T,c as p}from"./index-CCZhiEoc.js";import{F as U}from"./Forbidden-D25jV3Oq.js";import"./vendor-utils-Dyr8OjFr.js";const h="/api/admin";function G(){const S=$(),d=q(),{hasPermission:z}=B(),[t,l]=r.useState({project_number:"",name:"",customer_id:null,customer_name:"",start_date:new Date().toISOString().split("T")[0],responsible_user_id:""}),[D,E]=r.useState([]),[f,y]=r.useState(!1),[b,j]=r.useState({}),[F,L]=r.useState(!0),[x,P]=r.useState([]),[i,N]=r.useState(""),[v,m]=r.useState(!1);r.useEffect(()=>{(async()=>{try{const[a,n,o]=await Promise.all([p(`${h}/projects.php?action=next_number`),p(`${h}/customers.php`),p(`${h}/projects.php?action=users`)]),_=await a.json();_.success&&l(Z=>({...Z,project_number:_.data.number}));const w=await n.json();w.success&&P(w.data.customers);const g=await o.json();g.success&&E(g.data.users)}catch{d.error("Chyba při načítání dat")}finally{L(!1)}})()},[d]);const k=r.useMemo(()=>{if(!i)return x;const s=i.toLowerCase();return x.filter(a=>(a.name||"").toLowerCase().includes(s)||(a.company_id||"").includes(i)||(a.city||"").toLowerCase().includes(s))},[x,i]);if(r.useEffect(()=>{const s=()=>m(!1);if(v)return document.addEventListener("click",s),()=>document.removeEventListener("click",s)},[v]),!z("projects.create"))return e.jsx(U,{});const O=s=>{l(a=>({...a,customer_id:s.id,customer_name:s.name})),j(a=>({...a,customer_id:void 0})),N(""),m(!1)},A=()=>{l(s=>({...s,customer_id:null,customer_name:""}))},u=(s,a)=>{l(n=>({...n,[s]:a})),j(n=>({...n,[s]:void 0}))},R=async()=>{const s={};if(t.name.trim()||(s.name="Název projektu je povinný"),t.customer_id||(s.customer_id="Vyberte zákazníka"),j(s),!(Object.keys(s).length>0)){y(!0);try{const a={name:t.name.trim(),customer_id:t.customer_id,start_date:t.start_date,project_number:t.project_number.trim(),responsible_user_id:t.responsible_user_id||null},o=await(await p(`${h}/projects.php`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)})).json();o.success?S(`/projects/${o.data.project_id}`,{state:{created:!0}}):d.error(o.error||"Nepodařilo se vytvořit projekt")}catch{d.error("Chyba připojení")}finally{y(!1)}}};return F?e.jsxs("div",{className:"admin-skeleton",style:{padding:0,gap:"1.5rem"},children:[e.jsx("div",{className:"admin-skeleton-row",style:{justifyContent:"space-between"},children:e.jsx("div",{className:"admin-skeleton-line h-8",style:{width:"200px"}})}),e.jsx("div",{className:"admin-card",children:e.jsx("div",{className:"admin-skeleton",style:{gap:"1.25rem"},children:[0,1,2,3].map(s=>e.jsxs("div",{className:"admin-skeleton-row",children:[e.jsx("div",{className:"admin-skeleton-line w-1/4"}),e.jsx("div",{className:"admin-skeleton-line w-1/2"})]},s))})})]}):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",{className:"flex-row gap-4",children:[e.jsx(M,{to:"/projects",className:"admin-btn-icon",title:"Zpět","aria-label":"Zpět",children:e.jsx("svg",{width:"20",height:"20",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e.jsx("path",{d:"M19 12H5M12 19l-7-7 7-7"})})}),e.jsxs("div",{children:[e.jsx("h1",{className:"admin-page-title",children:"Nový projekt"}),e.jsx("p",{className:"admin-page-subtitle",children:"Ruční vytvoření projektu"})]})]}),e.jsx("div",{className:"admin-page-actions",children:e.jsx("button",{onClick:R,disabled:f,className:"admin-btn admin-btn-primary",children:f?"Ukládám...":"Uložit"})})]}),e.jsx(C.div,{className:"admin-card",initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{duration:.4,delay:.1},style:{overflow:"visible"},children:e.jsxs("div",{className:"admin-card-body",children:[e.jsx("h3",{className:"admin-card-title",children:"Základní údaje"}),e.jsxs("div",{className:"admin-form",children:[e.jsxs("div",{className:"admin-form-row",children:[e.jsx(c,{label:"Číslo projektu",children:e.jsx("input",{type:"text",value:t.project_number,onChange:s=>u("project_number",s.target.value),className:"admin-form-input",placeholder:"Ponechte prázdné pro automatické"})}),e.jsx(c,{label:"Název",error:b.name,required:!0,children:e.jsx("input",{type:"text",value:t.name,onChange:s=>u("name",s.target.value),className:"admin-form-input",placeholder:"Název projektu"})})]}),e.jsxs("div",{className:"admin-form-row",children:[e.jsx(c,{label:"Zákazník",error:b.customer_id,required:!0,children:t.customer_id?e.jsxs("div",{className:"offers-customer-selected",children:[e.jsx("span",{children:t.customer_name}),e.jsx("button",{type:"button",onClick:A,className:"admin-btn-icon",title:"Odebrat zákazníka","aria-label":"Odebrat zákazníka",children:e.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),e.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})})]}):e.jsxs("div",{className:"offers-customer-select",onClick:s=>s.stopPropagation(),children:[e.jsx("input",{type:"text",value:i,onChange:s=>{N(s.target.value),m(!0)},onFocus:()=>m(!0),className:"admin-form-input",placeholder:"Hledat zákazníka..."}),v&&e.jsx("div",{className:"offers-customer-dropdown",children:k.length===0?e.jsx("div",{className:"offers-customer-dropdown-empty",children:"Žádní zákazníci"}):k.slice(0,20).map(s=>e.jsxs("div",{className:"offers-customer-dropdown-item",onMouseDown:()=>O(s),children:[e.jsx("div",{children:s.name}),s.city&&e.jsx("div",{children:s.city})]},s.id))})]})}),e.jsx(c,{label:"Datum zahájení",children:e.jsx(T,{mode:"date",value:t.start_date,onChange:s=>u("start_date",s)})})]}),e.jsx("div",{className:"admin-form-row",children:e.jsx(c,{label:"Zodpovědná osoba",children:e.jsxs("select",{value:t.responsible_user_id,onChange:s=>u("responsible_user_id",s.target.value),className:"admin-form-select",children:[e.jsx("option",{value:"",children:"— Nevybráno —"}),D.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]})})})]})]})})]})}export{G as default};