1import{u as M,a as h,d as G,j as e}from"./vendor-query-B-_nDQQY.js";import{ag as Y,B as c,F as R,C as J,d as X,e as A,t as d,b as C,q as k,I as m,h as _,i as q,j as P,k as I,l as u,L as Z,S as ee,r as se,ah as te}from"./index-D8ww6b7J.js";import{D as E,a as L,b as O,c as z}from"./dialog-BQWtAG_x.js";import{T as ae,a as le,b as f,c as ie}from"./tabs-DfRYzyJY.js";import{F as re,a as ne,T as oe,H as ce}from"./FormRevisionsDialog-Di24en18.js";import{P as de}from"./plus-MQo5wr8n.js";import{E as H}from"./eye-DQ9YWbv-.js";import{H as he}from"./history-CLwtiCQB.js";import{C as xe}from"./copy-CeElQ86j.js";import{T as $}from"./trash-2-LBknmkIl.js";import{T as pe}from"./type-D89QavcI.js";import{H as ue}from"./hash-CljE7dAe.js";import{P as me}from"./pen-tool-BEizwLls.js";import{S as je}from"./square-check-big-BDW8CBj-.js";import{U as ve}from"./upload-DN-6vN7H.js";import{G as ge}from"./grip-vertical-DtQAcod7.js";import{S as ye}from"./send-DJBvsBZ5.js";import"./vendor-three-1K-S22da.js";import"./vendor-charts-i-522T5A.js";import"./vendor-react-BUIlT3ms.js";import"./vendor-pdf-Bwkk3Ew0.js";import"./vendor-radix-X0WMATEu.js";import"./file-down-Bo39ASem.js";import"./rotate-ccw-DfQYrjIS.js";import"./user-CHo1Apvy.js";import"./format-C2V2JF1Z.js";import"./constants-yGZMfuMC.js";const B=[{value:"text",label:"Text",icon:pe},{value:"textarea",label:"Text Area",icon:oe},{value:"number",label:"Number",icon:ue},{value:"date",label:"Date",icon:se},{value:"signature",label:"Signature",icon:me},{value:"checkbox",label:"Checkbox",icon:je},{value:"select",label:"Dropdown",icon:te},{value:"file_upload",label:"File Upload",icon:ve},{value:"heading",label:"Section Heading",icon:ce}];function Ge(){const{t}=Y(),a=M(),[j,n]=h.useState(!1),[T,x]=h.useState(null),[N,v]=h.useState(null),[y,o]=h.useState(null),b=i=>{x({...y,title:i.title,description:i.description,fields:i.fields,form_type:i.form_type}),o(null),n(!0),d.info("Revision loaded â review and save to apply")},{data:w=[],isLoading:D}=G({queryKey:["forms"],queryFn:()=>C.entities.Form.list("-created_date")}),F=async i=>{await C.entities.Form.delete(i),a.invalidateQueries({queryKey:["forms"]}),d.success("Form deleted")};return e.jsxs("div",{className:"p-6 max-w-6xl mx-auto space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:t("forms.title")}),e.jsx("p",{className:"text-muted-foreground",children:"Create contracts, waivers, installation forms, and more"})]}),e.jsxs(c,{onClick:()=>{x(null),n(!0)},children:[e.jsx(de,{className:"w-4 h-4 mr-2"}),t("forms.newForm")]})]}),e.jsxs(ae,{defaultValue:"all",children:[e.jsxs(le,{children:[e.jsx(f,{value:"all",children:"All"}),e.jsx(f,{value:"contract",children:"Contracts"}),e.jsx(f,{value:"waiver",children:"Waivers"}),e.jsx(f,{value:"installation",children:"Installation"}),e.jsx(f,{value:"other",children:"Other"})]}),["all","contract","waiver","installation","other"].map(i=>e.jsx(ie,{value:i,children:D?e.jsx("div",{className:"flex justify-center py-12",children:e.jsx("div",{className:"w-8 h-8 border-4 border-primary border-t-transparent rounded-full animate-spin"})}):w.filter(r=>i==="all"||r.form_type===i).length===0?e.jsxs("div",{className:"text-center py-12 text-muted-foreground",children:[e.jsx(R,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),e.jsx("p",{children:"No forms yet"})]}):e.jsx("div",{className:"grid gap-4 mt-4",children:w.filter(r=>i==="all"||r.form_type===i).map(r=>{var S;return e.jsx(J,{className:"hover:shadow-md transition-shadow",children:e.jsxs(X,{className:"flex items-center justify-between p-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-lg bg-primary/10 flex items-center justify-center",children:e.jsx(R,{className:"w-5 h-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold",children:r.title}),e.jsxs("div",{className:"flex items-center gap-2 mt-1",children:[e.jsx(A,{variant:"outline",className:"text-xs capitalize",children:r.form_type}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[((S=r.fields)==null?void 0:S.length)||0," fields"]}),e.jsx(A,{variant:r.status==="published"?"default":"secondary",className:"text-xs",children:r.status})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(c,{variant:"outline",size:"sm",onClick:()=>v(r),children:[e.jsx(H,{className:"w-4 h-4 mr-1"}),"Preview"]}),e.jsx(c,{variant:"outline",size:"sm",onClick:()=>{x(r),n(!0)},children:"Edit"}),e.jsx(re,{form:r}),e.jsx(c,{variant:"outline",size:"sm",onClick:()=>o(r),title:"Revision history",children:e.jsx(he,{className:"w-4 h-4"})}),e.jsx(c,{variant:"outline",size:"sm",onClick:()=>{const g=`${window.location.origin}/form/${r.id}`;navigator.clipboard.writeText(g),d.success("Link copied!")},children:e.jsx(xe,{className:"w-4 h-4"})}),e.jsx(c,{variant:"ghost",size:"sm",className:"text-destructive",onClick:()=>F(r.id),children:e.jsx($,{className:"w-4 h-4"})})]})]})},r.id)})})},i))]}),j&&e.jsx(be,{form:T,onClose:()=>n(!1),onSaved:()=>{n(!1),a.invalidateQueries({queryKey:["forms"]})}}),N&&e.jsx(E,{open:!0,onOpenChange:()=>v(null),children:e.jsxs(L,{className:"max-w-2xl max-h-[85vh] overflow-y-auto",children:[e.jsx(O,{children:e.jsx(z,{children:N.title})}),e.jsx(Ne,{form:N})]})}),y&&e.jsx(ne,{form:y,onRestore:b,open:!!y,onOpenChange:i=>!i&&o(null)})]})}function be({form:t,onClose:a,onSaved:j}
1){const[n,T]=h.useState((t==null?void 0:t.title)||""),[x,N]=h.useState((t==null?void 0:t.description)||""),[v,y]=h.useState((t==null?void 0:t.form_type)||"contract"),[o,b]=h.useState((t==null?void 0:t.fields)||[]),[w,D]=h.useState(!1),[F,i]=h.useState(!1),r=async()=>{if(!n.trim()){d.error("Enter a form title first");return}i(!0);try{const p=((await C.integrations.Core.InvokeLLM({prompt:`You are a form builder for a countertop fabrication company. Based on the form title "${n}" and type "${v}", suggest the appropriate form fields a countertop shop would need. Return a JSON array of field objects. Each field should have: label (string), type (one of: text, textarea, number, date, signature, checkbox, select, file_upload, heading), required (boolean), placeholder (string), and options (array of strings, only for select type). Keep it practical and relevant to countertop fabrication. Limit to 8-12 fields.`,response_json_schema:{type:"object",properties:{fields:{type:"array",items:{type:"object",properties:{label:{type:"string"},type:{type:"string"},required:{type:"boolean"},placeholder:{type:"string"},options:{type:"array",items:{type:"string"}}}}}}}})).fields||[]).map(l=>({id:`field_${Date.now()}_${Math.random().toString(36).slice(2,7)}`,label:l.label||"",type:l.type||"text",required:l.required||!1,placeholder:l.placeholder||"",options:l.type==="select"?l.options||["Option 1"]:void 0}));b([...o,...p]),d.success(`AI added ${p.length} fields`)}catch{d.error("Failed to detect fields")}i(!1)},S=s=>{const p={id:`field_${Date.now()}`,label:"",type:s,required:!1,placeholder:"",options:s==="select"?["Option 1"]:void 0};b([...o,p])},g=(s,p)=>{b(o.map(l=>l.id===s?{...l,...p}:l))},V=s=>{b(o.filter(p=>p.id!==s))},K=async()=>{if(!n.trim()){d.error("Title is required");return}try{await C.functions.invoke("saveFormWithRevision",{form_id:t==null?void 0:t.id,title:n,description:x,form_type:v,fields:o,status:(t==null?void 0:t.status)||"draft"}),d.success("Form saved"),j()}catch(s){d.error("Failed to save: "+s.message)}},Q=async()=>{if(!n.trim()){d.error("Title is required");return}try{await C.functions.invoke("saveFormWithRevision",{form_id:t==null?void 0:t.id,title:n,description:x,form_type:v,fields:o,status:"published"}),d.success("Form published!"),j()}catch(s){d.error("Failed to publish: "+s.message)}};return e.jsx(E,{open:!0,onOpenChange:a,children:e.jsxs(L,{className:"max-w-4xl max-h-[90vh] overflow-y-auto",children:[e.jsx(O,{children:e.jsx(z,{children:t?"Edit Form":"Create Form"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(k,{children:"Form Title"}),e.jsx(m,{value:n,onChange:s=>T(s.target.value),placeholder:"e.g. Installation Agreement"})]}),e.jsxs("div",{children:[e.jsx(k,{children:"Form Type"}),e.jsxs(_,{value:v,onValueChange:y,children:[e.jsx(q,{children:e.jsx(P,{})}),e.jsxs(I,{children:[e.jsx(u,{value:"contract",children:"Contract"}),e.jsx(u,{value:"waiver",children:"Waiver"}),e.jsx(u,{value:"installation",children:"Installation"}),e.jsx(u,{value:"measurement",children:"Measurement"}),e.jsx(u,{value:"survey",children:"Survey"}),e.jsx(u,{value:"other",children:"Other"})]})]})]})]}),e.jsxs("div",{children:[e.jsx(k,{children:"Description"}),e.jsx(m,{value:x,onChange:s=>N(s.target.value),placeholder:"Brief description..."})]}),e.jsxs("div",{className:"border-t pt-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsxs("h3",{className:"font-semibold",children:["Fields (",o.length,")"]}),e.jsxs("div",{className:"flex gap-1 flex-wrap items-center",children:[e.jsxs(c,{variant:"default",size:"sm",onClick:r,disabled:F||!n.trim(),className:"gap-1.5",title:"AI suggests fields based on form title",children:[F?e.jsx(Z,{className:"w-3.5 h-3.5 animate-spin"}):e.jsx(ee,{className:"w-3.5 h-3.5"}),"AI Auto-Detect"]}),e.jsx("div",{className:"w-px h-5 bg-border mx-1"}),B.map(s=>e.jsx(c,{variant:"outline",size:"sm",onClick:()=>S(s.value),title:s.label,children:e.jsx(s.icon,{className:"w-3.5 h-3.5"})},s.value))]})]}),e.jsx("div",{className:"space-y-2 max-h-64 overflow-y-auto border rounded-lg p-3",children:o.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-8",children:"Add fields using the buttons above"}):o.map((s,p)=>e.jsxs("div",{className:"flex items-start gap-2 bg-muted/50 p-2 rounded-lg",children:[e.jsx(ge,{className:"w-4 h-4 mt-2 text-muted-foreground shrink-0"}),e.jsxs("div",{className:"flex-1 space-y-2",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx(m,{className:"h-8 text-sm",placeholder:"Field label",value:s.label,onChange:l=>g(s.id,{label:l.target.value})}),e.jsxs(_,{value:s.type,onValueChange:l=>g(s.id,{type:l,options:l==="select"?["Option 1"]:void 0}),children:[e.jsx(q,{className:"h-8 w-36",children:e.jsx(P,{})}
1),e.jsx(I,{children:B.map(l=>e.jsx(u,{value:l.value,children:l.label},l.value))})]})]}),s.type==="select"&&e.jsx(m,{className:"h-8 text-sm",placeholder:"Options (comma-separated)",value:(s.options||[]).join(", "),onChange:l=>g(s.id,{options:l.target.value.split(",").map(W=>W.trim()).filter(Boolean)})}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("label",{className:"flex items-center gap-1 text-xs",children:[e.jsx("input",{type:"checkbox",checked:s.required,onChange:l=>g(s.id,{required:l.target.checked})}),"Required"]}),e.jsx(m,{className:"h-7 text-xs flex-1",placeholder:"Placeholder text",value:s.placeholder||"",onChange:l=>g(s.id,{placeholder:l.target.value})}),e.jsx(c,{variant:"ghost",size:"icon",className:"h-7 w-7 text-destructive",onClick:()=>V(s.id),children:e.jsx($,{className:"w-3.5 h-3.5"})})]})]})]},s.id))})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-4 border-t",children:[e.jsx(c,{variant:"outline",onClick:a,children:"Cancel"}),e.jsxs(c,{variant:"secondary",onClick:()=>D(!w),children:[e.jsx(H,{className:"w-4 h-4 mr-1"}),"Preview"]}),e.jsx(c,{variant:"outline",onClick:K,children:"Save Draft"}),e.jsxs(c,{onClick:Q,children:[e.jsx(ye,{className:"w-4 h-4 mr-1"}),"Publish"]})]}),w&&e.jsxs("div",{className:"border rounded-lg p-6 bg-white",children:[e.jsx("h2",{className:"text-xl font-bold mb-1",children:n||"Untitled Form"}),x&&e.jsx("p",{className:"text-muted-foreground mb-4",children:x}),e.jsx(U,{fields:o})]})]})})}function Ne({form:t}){return e.jsxs("div",{className:"space-y-4 py-4",children:[t.description&&e.jsx("p",{className:"text-muted-foreground",children:t.description}),e.jsx(U,{fields:t.fields||[]})]})}function U({fields:t}){return e.jsx("div",{className:"space-y-4",children:t.map(a=>a.type==="heading"?e.jsx("h3",{className:"text-lg font-semibold border-b pb-1",children:a.label||"Section"},a.id):e.jsxs("div",{className:"space-y-1",children:[e.jsxs(k,{children:[a.label||"Untitled",a.required&&e.jsx("span",{className:"text-destructive ml-1",children:"*"})]}),a.type==="text"&&e.jsx(m,{placeholder:a.placeholder}),a.type==="textarea"&&e.jsx("textarea",{className:"w-full h-24 rounded-md border border-input bg-transparent px-3 py-2 text-sm",placeholder:a.placeholder}),a.type==="number"&&e.jsx(m,{type:"number",placeholder:a.placeholder}),a.type==="date"&&e.jsx(m,{type:"date"}),a.type==="signature"&&e.jsx("div",{className:"border-2 border-dashed rounded-lg h-32 flex items-center justify-center text-muted-foreground text-sm",children:"Signature area"}),a.type==="checkbox"&&e.jsxs("label",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",className:"rounded"}),a.label]}),a.type==="select"&&e.jsxs(_,{children:[e.jsx(q,{children:e.jsx(P,{placeholder:"Select..."})}),e.jsx(I,{children:(a.options||[]).map((j,n)=>e.jsx(u,{value:j,children:j},n))})]}),a.type==="file_upload"&&e.jsx("div",{className:"border-2 border-dashed rounded-lg p-6 text-center text-muted-foreground text-sm",children:"Click or drag to upload"})]},a.id))})}export{Ge 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.