1import{k as oe,u as le,r,b as ne,h as V,s as v,J as u,j as e,F as _,C as m,a as x,B as E,Z as ie,X as ce,a6 as de,t as me}from"./index-RxDZGo8S.js";import{I as z}from"./input-CS9s6izM.js";import{L as h}from"./label-Fqe9mk9Y.js";import{T as xe}from"./textarea-ZYM4NL9m.js";import{B as ue}from"./badge-Kwp9j7fd.js";import{S as k,a as L,b as F,c as D,d as P}from"./select-B3f6lReZ.js";import{D as he,a as pe}from"./dialog-Kd5QqSd7.js";import{u as ge}from"./useActivityLog-fCH0wk49.js";import{C as je}from"./chevron-up-DbNI7lUd.js";import{S as ve}from"./search-Dss-hsDb.js";import{E as be}from"./eye-CB4j9jLn.js";import{T as fe}from"./trash-2-DXjGIb-t.js";import"./index-BnR4gmtC.js";import"./index-Da3vg3Bj.js";import"./check-dsJNWiFz.js";const K={Excelente:{color:"bg-emerald-100 text-emerald-800 border-emerald-200",emoji:"ð"},Buena:{color:"bg-sky-100 text-sky-800 border-sky-200",emoji:"â
"},Regular:{color:"bg-amber-100 text-amber-800 border-amber-200",emoji:"â ï¸"},Mala:{color:"bg-red-100 text-red-800 border-red-200",emoji:"â"}},G={1:"Muy bajo",2:"Bajo",3:"Normal",4:"Alto",5:"Muy alto"},qe=()=>{const{user:p}=oe(),U=le(),{logActivity:J}=ge(),[M,R]=r.useState(new Date().toISOString().slice(0,10)),[b,W]=r.useState("3"),[f,X]=r.useState("Buena"),[T,Z]=r.useState("3"),[q,B]=r.useState(""),[H,O]=r.useState(!1),[N,y]=r.useState([]),[w,Y]=r.useState(""),[g,A]=r.useState(!1),[S,$]=r.useState(null),[ee,se]=r.useState(null),{data:i=[],isLoading:I}=ne({queryKey:["orchid_observations"],queryFn:async()=>{const{data:s,error:t}=await v.from("orchid_observations").select("*").order("entry_date",{ascending:!1}).limit(200);if(t)throw t;return s||[]}}),Q=r.useMemo(()=>{const s=w.trim().toLowerCase();return s?i.filter(t=>new Date(t.entry_date).toLocaleDateString().includes(s)||(t.status_text||"").toLowerCase().includes(s)||(t.comments||"").toLowerCase().includes(s)||String(t.level).includes(s)):i},[i,w]),j=r.useMemo(()=>{var d;if(!i.length)return null;const s=i.length,t=(i.reduce((n,o)=>n+o.level,0)/s).toFixed(1),a=(i.reduce((n,o)=>n+(o.average_status||0),0)/s).toFixed(1),l={};i.forEach(n=>{const o=n.status_text||"Sin calidad";l[o]=(l[o]||0)+1});const c=((d=Object.entries(l).sort((n,o)=>o[1]-n[1])[0])==null?void 0:d[0])||"-";return{total:s,avgLevel:t,avgAvg:a,mostCommon:c}},[i]),C=V({mutationFn:async()=>{if(!p)throw new Error("Debes iniciar sesión");const{error:s}=await v.from("orchid_observations").insert({created_by:p.id,entry_date:M,level:Number(b),status_text:f,average_status:Number(T),comments:q,photo_urls:N});if(s)throw s},onSuccess:async()=>{u.success("Observación guardada"),await J("create_observation","agregó una observación",{nivel:b,estado:f}),B(""),y([]),A(!1),await U.invalidateQueries({queryKey:["orchid_observations"]})},onError:s=>u.error(s.message||"Error al guardar")}),te=V({mutationFn:async s=>{const{error:t}=await v.from("orchid_observations").delete().eq("id",s);if(t)throw t},onSuccess:async()=>{u.success("Observación eliminada"),await U.invalidateQueries({queryKey:["orchid_observations"]})},onError:()=>u.error("Error al eliminar")}),ae=r.useCallback(async s=>{if(!(!s||!p)){O(!0);try{const t=[];for(let a=0;a<s.length;a++){const l=s[a];if(!l.type.startsWith("image/"))continue;const c=l.name.split(".").pop(),d=`${p.id}/${Date.now()}-${Math.random().toString(36).slice(2)}.${c}`,{error:n}=await v.storage.from("orchid-observations").upload(d,l,{upsert:!1});if(n)throw n;const{data:o}=v.storage.from("orchid-observations").getPublicUrl(d);o!=null&&o.publicUrl&&t.push(o.publicUrl)}y(a=>[...a,...t]),t.length&&u.success(`${t.length} foto(s) subida(s)`)}catch{u.error("No se pudieron subir las fotos")}finally{O(!1)}}},[p]),re=s=>y(t=>t.filter((a,l)=>l!==s));return e.jsxs("div",{className:"min-h-screen bg-gradient-to-br from-secondary/30 via-background to-accent/20 pb-20",children:[e.jsxs("div",{className:"sticky top-0 z-10 bg-background/80 backdrop-blur-lg border-b border-border/50 px-4 py-3",children:[e.jsxs("h1",{className:"text-xl font-bold text-primary flex items-center gap-2",children:[e.jsx(_,{className:"h-5 w-5"})," Observaciones"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Estado del invernadero y orquÃdeas"})]}),e.jsxs("div",{className:"px-3 pt-3 space-y-3",children:[j&&e.jsxs("div",{className:"grid grid-cols-4 gap-2",children:[e.jsx(m,{className:"border-0 shadow-sm bg-card/80",children:e.jsxs(x,{className:"p-2.5 text-center",children:[e.jsx("div",{className:"text-lg font-bold text-primary",children:j.total}),e.jsx("div",{className:"text-[10px] text-muted-foreground",children:"Total"})]})}),e.jsx(m,{className:"border-0 shadow-sm bg-card/80",children:e.jsxs(x,{className:"p-2.5 text-center",children:[e.jsx("div",{className:"text-lg font-bold text-primary",children:j.avgLevel}),e.jsx("div",{className:"text-[10px] text-muted-foreground",children:"Nivel Prom."})]})}),e.jsx(m,{className:"border-0 shadow-sm bg-card/80",children:e.jsxs(x,{className:"p-2.5 text-center",children:[e.jsx("div",{className:"text-lg font-bold text-primary",children:j.avgAvg}),e.jsx("div",{className:"text-[10px] text-muted-foreground",children:"Estado Prom."})]})}),e.jsx(m,{className:"border-0 shadow-sm bg-card/80",children:e.jsxs(x,{className:"p-2.5 text-center",children:[e.jsx("div",{className:"text-lg font-bold text-primary text-xs leading-tight mt-1",children:j.mostCommon}),e.jsx("div",{className:"text-[10px] text-muted-foreground",children:"Más común"})]})})]}),e.jsxs(E,{onClick:()=>A(!g),className:"w-full",variant:g?"secondary":"default",children:[g?e.jsx(je,{className:"h-4 w-4 mr-2"}):e.jsx(_,{className:"h-4 w-4 mr-2"}),g?"Ocultar formulario":"Nueva observación"]}),g&&e.jsx(m,{className:"border-0 shadow-md bg-card/90 backdrop-blur animate-in slide-in-from-top-2 duration-200",children:e.jsxs(x,{className:"p-4 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Fecha"}),e.jsx(z,{type:"date",value:M,onChange:s=>R(s.target.value),className:"h-9 text-sm"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Calida
1d"}),e.jsxs(k,{value:f,onValueChange:X,children:[e.jsx(L,{className:"h-9 text-sm",children:e.jsx(F,{})}),e.jsx(D,{children:Object.entries(K).map(([s,t])=>e.jsxs(P,{value:s,children:[t.emoji," ",s]},s))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Nivel"}),e.jsxs(k,{value:b,onValueChange:W,children:[e.jsx(L,{className:"h-9 text-sm",children:e.jsx(F,{})}),e.jsx(D,{children:[1,2,3,4,5].map(s=>e.jsxs(P,{value:String(s),children:[s," - ",G[s]]},s))})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Estado promedio"}),e.jsxs(k,{value:T,onValueChange:Z,children:[e.jsx(L,{className:"h-9 text-sm",children:e.jsx(F,{})}),e.jsx(D,{children:[1,2,3,4,5].map(s=>e.jsxs(P,{value:String(s),children:[s," - ",G[s]]},s))})]})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Comentarios"}),e.jsx(xe,{value:q,onChange:s=>B(s.target.value),placeholder:"Notas generales, condiciones, etc.",className:"min-h-[60px] text-sm"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(h,{className:"text-xs",children:"Fotos"}),e.jsxs("label",{className:"flex items-center justify-center gap-2 p-3 border-2 border-dashed border-muted-foreground/30 rounded-lg cursor-pointer hover:border-primary/50 transition-colors text-sm text-muted-foreground",children:[e.jsx(ie,{className:"h-4 w-4"}),H?"Subiendo...":"Toca para agregar fotos",e.jsx("input",{type:"file",accept:"image/*",multiple:!0,onChange:s=>ae(s.target.files),className:"hidden"})]}),N.length>0&&e.jsx("div",{className:"grid grid-cols-4 gap-1.5 mt-1",children:N.map((s,t)=>e.jsxs("div",{className:"relative group",children:[e.jsx("img",{src:s,alt:"",className:"w-full h-16 object-cover rounded-md"}),e.jsx("button",{onClick:()=>re(t),className:"absolute -top-1 -right-1 bg-destructive text-destructive-foreground rounded-full p-0.5 opacity-0 group-hover:opacity-100 transition-opacity",children:e.jsx(ce,{className:"h-3 w-3"})})]},t))})]}),e.jsx(E,{onClick:()=>C.mutate(),disabled:C.isPending,className:"w-full",children:C.isPending?"Guardando...":"Guardar observación"})]})}),e.jsxs("div",{className:"relative",children:[e.jsx(ve,{className:"h-4 w-4 absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground"}),e.jsx(z,{value:w,onChange:s=>Y(s.target.value),placeholder:"Buscar por fecha, calidad o comentario...",className:"pl-9 h-9 text-sm"})]}),e.jsxs("div",{className:"space-y-2",children:[I&&!i.length&&e.jsx("div",{className:"space-y-2",children:[1,2,3].map(s=>e.jsx("div",{className:"h-20 rounded-xl bg-muted/50 animate-pulse"},s))}),Q.map(s=>{const t=K[s.status_text||""]||{color:"bg-muted text-muted-foreground",emoji:"ð"},a=ee===s.id,l=s.photo_urls&&s.photo_urls.length>0;return e.jsx(m,{className:"border-0 shadow-sm overflow-hidden transition-all duration-200 hover:shadow-md",onClick:()=>se(a?null:s.id),children:e.jsx(x,{className:"p-3",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"text-2xl",children:t.emoji}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-semibold text-sm text-foreground",children:new Date(s.entry_date+"T12:00:00").toLocaleDateString("es-CL",{day:"numeric",month:"short",year:"numeric"})}),e.jsx(ue,{variant:"outline",className:`text-[10px] px-1.5 py-0 ${t.color}`,children:s.status_text||"N/A"})]}),e.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground shrink-0",children:[e.jsx(de,{className:"h-3 w-3"}),e.jsxs("span",{children:["Nv.",s.level]}),e.jsx("span",{children:"â¢"}),e.jsxs("span",{children:["Pr.",s.average_status??"-"]}),l&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"â¢"}),e.jsx(be,{className:"h-3 w-3"}),e.jsx("span",{children:s.photo_urls.length})]})]})]}),s.comments&&e.jsx("p",{className:`text-xs text-muted-foreground mt-1 ${a?"":"line-clamp-1"}`,children:s.comments}),a&&l&&e.jsx("div",{className:"grid grid-cols-4 gap-1.5 mt-2 animate-in fade-in duration-200",children:s.photo_urls.map((c,d)=>e.jsx("img",{src:c,alt:"",className:"w-full h-16 object-cover rounded cursor-pointer hover:opacity-80 transition-opacity",loading:"lazy",onClick:n=>{n.stopPropagation(),$(c)}},d))}),a&&e.jsx("div",{className:"flex justify-end mt-2 animate-in fade-in duration-200",children:e.jsxs(E,{variant:"ghost",size:"sm",className:"text-destructive hover:text-destructive h-7 text-xs",onClick:c=>{c.stopPropagation(),confirm("¿Eliminar esta observación?")&&te.mutate(s.id)},children:[e.jsx(fe,{className:"h-3 w-3 mr-1"})," Eliminar"]})})]})]})})},s.id)}),!I&&Q.length===0&&e.jsx(m,{className:"border-dashed",children:e.jsxs(x,{className:"p-8 text-center text-sm text-muted-foreground",children:[e.jsx(_,{className:"h-8 w-8 mx-auto mb-2 opacity-40"}),"Sin observaciones registradas"]})})]})]}),e.jsx(he,{open:!!S,onOpenChange:()=>$(null),children:e.jsx(pe,{className:"max-w-[95vw] max-h-[90vh] p-1 bg-black/90 border-0",children:S&&e.jsx("img",{src:S,alt:"Foto observación",className:"w-full h-full object-contain rounded"})})}),e.jsx(me,{})]})};export{qe as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.