PageSourceSearch

https://stone-profit-flow.base44.app/assets/JobsList-PRM5ZuXD.js

js stone-profit-flow.base44.app collected 2026-10-03 03:11:04 UTC 73,825 bytes, 7 lines download raw bytes

1import{u as Le,a as y,d as le,e as Ze,j as e}from"./vendor-query-B-_nDQQY.js";import{y as Ye,e as K,ax as ve,ah as ae,I as f,h as O,i as I,j as z,k as J,l as F,B as k,t as V,b as Q,X,v as Me,n as ge,S as Xe,J as be,C as W,d as G,aC as Se,Q as et,a as ne,c as oe,T as ye,_ as Te,aI as tt,x as st,U as at,D as rt,m as lt,r as nt,F as Oe,O as ot,f as it,s as ct,ao as dt}from"./index-D8ww6b7J.js";import{D as mt,a as ut}from"./dialog-BQWtAG_x.js";import{s as ee,a as _e,M as te,J as Ie}from"./jobStatuses-DT5mJrVz.js";import{C as fe}from"./calendar-days-B-0UCuni.js";import{P as se}from"./plus-MQo5wr8n.js";import{DragDropContext as xt,Droppable as pt,Draggable as ht}from"./vendor-dnd-D8GD0lRu.js";import{f as ze}from"./format-C2V2JF1Z.js";import{A as we,m as ie}from"./vendor-framer-DvY-ewtn.js";import{S as gt}from"./square-check-big-BDW8CBj-.js";import{C as bt}from"./checkbox-DYv8y0li.js";import{U as Ae}from"./user-CHo1Apvy.js";import{C as je}from"./circle-check-BXjz6L8_.js";import{b as Je}from"./vendor-react-BUIlT3ms.js";import{E as _t}from"./eye-DQ9YWbv-.js";import{P as ft}from"./pencil-iBu5MVPj.js";import{T as jt}from"./trash-2-LBknmkIl.js";import{E as Nt}from"./vendor-pdf-Bwkk3Ew0.js";import{T as vt}from"./trending-down-BIipizug.js";import{C as yt}from"./circle-check-big-4Ew-rpqG.js";import{H as wt}from"./hammer-Burg9xO0.js";import{L as Ct}from"./LocationSelector-DvzSfogd.js";import{A as kt}from"./arrow-right-BeikyhzV.js";import{P as St}from"./PageHeader-DOQecRZi.js";import{L as At}from"./list-Ff9VLIUP.js";import{L as $t}from"./layout-grid-DZ51q6Z2.js";import"./vendor-three-1K-S22da.js";import"./vendor-charts-i-522T5A.js";import"./vendor-radix-X0WMATEu.js";import"./constants-yGZMfuMC.js";/**
2 * @license lucide-react v1.21.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const Ft=[["path",{d:"M17 3a2 2 0 0 1 2 2v15a1 1 0 0 1-1.496.868l-4.512-2.578a2 2 0 0 0-1.984 0l-4.512 2.578A1 1 0 0 1 5 20V5a2 2 0 0 1 2-2z",key:"oz39mx"}]],Et=Ye("bookmark",Ft),xe=["template","installation","cutting","polishing"],$e={account_id:"",account_name:"",customer_name:"",customer_phone:"",job_address:"",zip_code:"",description:"",material_type:"",square_feet:"",material_cost:"",selling_price:"",status:"completed",job_date:new Date().toISOString().split("T")[0],notes:"",labor_template_rate:"",labor_template_hours:"",labor_template_employee:"",labor_template_employees:[],labor_installation_rate:"",labor_installation_hours:"",labor_installation_employee:"",labor_installation_employees:[],labor_cutting_rate:"",labor_cutting_hours:"",labor_cutting_employee:"",labor_cutting_employees:[],labor_polishing_rate:"",labor_polishing_hours:"",labor_polishing_employee:"",labor_polishing_employees:[]};function qt({open:s,onOpenChange:c}){const n=Le(),[a,x]=y.useState($e),[t,r]=y.useState(!0),{data:u=[]}=le({queryKey:["employees"],queryFn:()=>Q.entities.Employee.list(),enabled:s}),{data:p=[]}=le({queryKey:["accounts"],queryFn:()=>Q.entities.Account.list(),enabled:s}),d=y.useMemo(()=>{const m=parseFloat(a.selling_price)||0,i=parseFloat(a.material_cost)||0,v={};let E=0;xe.forEach(w=>{const P=parseFloat(a[`labor_${w}_rate`])||0,S=parseFloat(a[`labor_${w}_hours`])||0,N=P*S;v[w]=N,E+=N});const q=i+E,o=m-q,g=m>0?o/m*100:0;return{price:m,profit:o,margin:g,laborCost:E,stageCosts:v}},[a]),_=Ze({mutationFn:m=>Q.entities.Job.create(m),onSuccess:()=>{n.invalidateQueries({queryKey:["jobs"]}),n.invalidateQueries({queryKey:["jobs-all"]}),V.success("Job added successfully"),c(!1),x($e),r(!0)},onError:()=>V.error("Failed to add job.")}),l=()=>{if(!a.customer_name){V.error("Customer name is required");return}const m={account_id:a.account_id||void 0,account_name:a.account_name,customer_name:a.customer_name,customer_phone:a.customer_phone||void 0,job_address:a.job_address||void 0,zip_code:a.zip_code||void 0,description:a.description,material_type:a.material_type||void 0,square_feet:parseFloat(a.square_feet)||0,material_cost:parseFloat(a.material_cost)||0,selling_price:parseFloat(a.selling_price)||0,status:a.status,job_date:a.job_date,notes:a.notes,labor_cost:d.laborCost};xe.forEach(i=>
6{const v=parseFloat(a[`labor_${i}_rate`])||0,E=parseFloat(a[`labor_${i}_hours`])||0,q=d.stageCosts[i],o=a[`labor_${i}_employee`]||"";m[`labor_${i}_rate`]=v,m[`labor_${i}_hours`]=E,m[`labor_${i}_employee`]=o,m[`labor_${i}_employees`]=o?[o]:[],i==="template"&&(m.labor_template=q),i==="installation"&&(m.labor_installation=q),i==="cutting"&&(m.labor_cutting=q),i==="polishing"&&(m.labor_polishing=q)}),_.mutate(m)},j=(m,i)=>x(v=>({...v,[m]:i}));return e.jsx(mt,{open:s,onOpenChange:c,children:e.jsx(ut,{className:"sm:max-w-[640px] max-h-[90vh] overflow-y-auto p-0",children:e.jsxs("div",{className:"bg-card border border-border rounded-lg",children:[e.jsxs("div",{className:"px-5 pt-5 pb-4 border-b border-border",children:[e.jsx("div",{className:"flex items-start justify-between",children:e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("h2",{className:"text-lg font-bold text-foreground truncate",children:a.customer_name||"New Job"}),e.jsx(K,{className:`text-xs font-medium ${_e[a.status]||"bg-muted text-muted-foreground"}`,children:ee[a.status]||a.status})]}),e.jsxs("div",{className:"flex items-center gap-3 mt-1 text-sm text-muted-foreground",children:[e.jsx("span",{children:a.job_date}),e.jsx("span",{className:"text-border",children:"|"}),e.jsxs("span",{children:[te[a.material_type]||"Other"," - ",a.square_feet||0," sqft"]})]}),e.jsxs("div",{className:"flex items-center gap-6 mt-3",children:[e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Price"}),e.jsxs("span",{className:"text-base font-bold text-foreground",children:["$",d.price.toLocaleString()]})]}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Profit"}),e.jsxs("span",{className:`text-base font-bold ${d.profit>=0?"text-accent":"text-destructive"}`,children:["$",d.profit.toLocaleString()]})]}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Margin"}),e.jsxs("span",{className:`text-base font-bold ${d.margin>=20?"text-accent":d.margin>=0?"text-muted-foreground":"text-destructive"}`,children:[d.margin.toFixed(1),"%"]})]})]})]})}),e.jsxs("button",{onClick:()=>r(!t),className:"flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground mt-3 transition-colors",children:[t?e.jsx(ve,{className:"w-4 h-4"}):e.jsx(ae,{className:"w-4 h-4"}),t?"Hide details":"Show details"]})]}),t&&e.jsxs("div",{className:"px-5 pb-5 space-y-5",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3",children:"EDITING JOB"}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(f,{placeholder:"Customer address",value:a.job_address,onChange:m=>j("job_address",m.target.value),className:"h-10 border-border rounded-lg"}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsx(f,{placeholder:"ZIP",value:a.zip_code,onChange:m=>j("zip_code",m.target.value),className:"h-10 border-border rounded-lg"}),e.jsxs(O,{value:a.status,onValueChange:m=>j("status",m),children:[e.jsx(I,{className:"h-10 border-border rounded-lg",children:e.jsx(z,{})}),e.jsx(J,{children:Object.entries(ee).map(([m,i])=>e.jsx(F,{value:m,children:i},m))})]})]}),e.jsx(f,{placeholder:"Description",value:a.description,onChange:m=>j("description",m.target.value),className:"h-10 border-border rounded-lg"}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(O,{value:a.material_type,onValueChange:m=>j("material_type",m),children:[e.jsx(I,{className:"h-10 border-border rounded-lg",children:e.jsx(z,{placeholder:"Material"})}),e.jsx(J,{children:Object.entries(te).map(([m,i])=>e.jsx(F,{value:m,children:i},m))})]}),e.jsx(f,{type:"number",placeholder:"Sq Ft",value:a.square_feet,onChange:m=>j("square_feet",m.target.value),className:"h-10 border-border rounded-lg"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsx(f,{type:"number",placeholder:"Material $",value:a.material_cost,onChange:m=>j("material_cost",m.target.value),className:"h-10 border-border rounded-lg"}),e.jsx(f,{type:"number",placeholder:"Selling Price",value:a.selling_price,onChange:m=>j("selling_price",m.target.value),className:"h-10 border-border rounded-lg"})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs(O,{value:a.account_id,onValueChange:m=>{const i=p.find(v=>v.id===m);j("account_id",m),j("account_name",(i==null?void 0:i.account_name)||"")},children:[e.jsx(I,{className:"h-10 border-border rounded-lg",children:e.jsx(z,{placeholder:"Select account"})}),e.jsx(J,{children:p.map(m=>e.jsx(F,{value:m.id,children:m.account_name},m.id))})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsx(f,{placeholder:"Customer name *",value:a.customer_name,onChange:m=>j("customer_name",m.target.value),className:"h-10 border-border rounded-lg"}),e.jsx(f,{placeholder:"Phone",value:a.customer_phone,onChange:m=>j("customer_phone",m.target.value),className:"h-10 border-border rounded-lg"})]})]}),e.jsx(f,{type:"date",value:a.job_date,onChange:m=>j("job_date",m.target.value),className:"h-10 border-border rounded-lg"})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3",children:"LABOR ($/HR × HRS · EMPLOYEE)"}),e.jsx("div",{className:"grid grid-cols-2 gap-3",children:[0,2,1,3].map(m=>{const i=xe[m];return e.jsxs("div",{className:"bg-muted/40 border border-border rounded-lg p-2.5",children:[e.jsx("div",{className:"text-xs text-foreground mb-1.5 capitalize font-semibold",children:i}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(f,{type:"number",placeholder:"$/hr",value:a[`labor_${i}_rate`],onChange:v=>j(`labor_${i}_rate`,v.target.value),className:"h-8 text-xs flex-1 min-w-0 border-border rounded"}),e.jsx("span",{className:"text-xs text-muted-foreground shrink-0",children:"×"}),e.jsx(f,{type:"number",placeholder:"hrs",value:a[`labor_${i}_hours`],onChange:v=>j(`labor_${i}_hours`,v.target.value),className:"h-8 text-xs flex-1 min-w-0 border-border rounded"}),e.jsx("span",{className:"text-xs text-muted-foreground shrink-0",children:"·"}),e.jsxs(O,{value:a[`labor_${i}_employee`],onValueChange:v=>j(`labor_${i}_employee`,v),children:[e.jsx(I,{className:"h-8 text-xs flex-1 border-border rounded min-w-0",children:e.jsx(z,{placeholder:""})}),e.jsx(J,{children:u.map(v=>e.jsx(F,{value:v.name,className:"text-xs",children:v.name},v.id))})]})]})]},i)})})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2 border-t border-border",children:[e.jsx(k,{variant:"outline",onClick:()=>c(!1),className:"rounded-lg",children:"Cancel"}),e.jsxs(k,{onClick:l,disabled:_.isPending,className:"rounded-lg gap-2",children:[_.isPending?e.jsx("div",{className:"w-4 h-4 border-2 border-primary-foreground border-t-transparent roun
6ded-full animate-spin"}):e.jsx(fe,{className:"w-4 h-4"}),"Add Old Job"]})]})]})]})})})}const Ne="job-saved-views";function Pt(){try{return JSON.parse(localStorage.getItem(Ne)||"[]")}catch{return[]}}function Dt({currentFilters:s,onApplyView:c}){const[n,a]=y.useState(Pt),[x,t]=y.useState(!1),[r,u]=y.useState(""),p=()=>{if(!r.trim())return;const _=[...n,{name:r.trim(),filters:s}];localStorage.setItem(Ne,JSON.stringify(_)),a(_),u(""),t(!1)},d=_=>{const l=n.filter((j,m)=>m!==_);localStorage.setItem(Ne,JSON.stringify(l)),a(l)};return e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[n.map((_,l)=>e.jsxs("div",{className:"flex items-center gap-1 bg-muted rounded-full pl-3 pr-1 py-1",children:[e.jsxs("button",{className:"text-xs font-medium hover:text-primary",onClick:()=>c(_.filters),children:[e.jsx(Et,{className:"w-3 h-3 inline mr-1"}),_.name]}),e.jsx("button",{className:"hover:text-destructive",onClick:()=>d(l),children:e.jsx(X,{className:"w-3 h-3"})})]},l)),x?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(f,{className:"h-7 w-32 text-xs",autoFocus:!0,value:r,onChange:_=>u(_.target.value),onKeyDown:_=>{_.key==="Enter"&&p(),_.key==="Escape"&&t(!1)},placeholder:"View name..."}),e.jsx(k,{size:"sm",className:"h-7 text-xs",onClick:p,children:"Save"}),e.jsx(k,{size:"sm",variant:"ghost",className:"h-7 px-1",onClick:()=>t(!1),children:e.jsx(X,{className:"w-3 h-3"})})]}):e.jsxs(k,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>t(!0),children:[e.jsx(se,{className:"w-3 h-3 mr-1"})," Save View"]})]})}const Lt=Ie.map(s=>({id:s.value,label:s.label,color:s.kanbanColor}));function Mt({jobs:s,onUpdate:c}){const n=a=>{if(!a.destination)return;const x=a.draggableId,t=a.destination.droppableId,r=s.find(u=>u.id===x);r&&r.status!==t&&c(x,{status:t})};return e.jsx(xt,{onDragEnd:n,children:e.jsx("div",{className:"flex gap-3 overflow-x-auto pb-4",children:Lt.map(a=>{const x=s.filter(t=>t.status===a.id);return e.jsx(pt,{droppableId:a.id,children:t=>e.jsxs("div",{ref:t.innerRef,...t.droppableProps,className:"min-w-[220px] w-[220px] shrink-0",children:[e.jsxs("div",{className:`rounded-t-lg px-3 py-2 ${a.color}`,children:[e.jsx("span",{className:"text-xs font-bold uppercase tracking-wider",children:a.label}),e.jsx("span",{className:"text-xs ml-2 text-muted-foreground",children:x.length})]}),e.jsxs("div",{className:"space-y-2 p-2 bg-muted/30 rounded-b-lg min-h-[120px] max-h-[65vh] overflow-y-auto",children:[x.map((r,u)=>e.jsx(ht,{draggableId:r.id,index:u,children:p=>{var d;return e.jsxs("div",{ref:p.innerRef,...p.draggableProps,...p.dragHandleProps,className:"bg-card border rounded-lg p-3 shadow-sm cursor-grab hover:shadow-md transition-shadow",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:r.customer_name}),e.jsxs("div",{className:"flex items-center justify-between mt-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground font-mono",children:r.job_number||""}),e.jsxs("span",{className:"text-xs font-medium",children:["$",(r.selling_price||0).toLocaleString()]})]}),r.job_date&&e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:ze(new Date(r.job_date),"MMM d")}),((d=r.job_tags)==null?void 0:d.length)>0&&e.jsx("div",{className:"flex gap-1 mt-1 flex-wrap",children:r.job_tags.slice(0,3).map(_=>e.jsx(K,{variant:"outline",className:"text-[10px] px-1 py-0",children:_},_))})]})}},r.id)),t.placeholder]})]})},a.id)})})})}function Tt({jobs:s=[]}){const c=()=>{if(!s.length)return;const n=["Job Number","Customer","Status","Material","Sq Ft","Material Cost","Labor Cost","Overhead","Selling Price","Job Date","Salesperson","Address"],a=s.map(p=>[p.job_number||"",`"${(p.customer_name||"").replace(/"/g,'""')}"`,p.status||"",p.material_type||"",p.square_feet||0,p.material_cost||0,p.labor_cost||0,p.overhead_cost||0,p.selling_price||0,p.job_date||"",p.salesperson_name||"",`"${(p.job_address||"").replace(/"/g,'""')}"`].join(",")),x=[n.join(","),...a].join(`
7`),t=new Blob([x],{type:"text/csv"}),r=URL.createObjectURL(t),u=document.createElement("a");u.href=r,u.download=`jobs-export-${ze(new Date,"yyyy-MM-dd")}.csv`,u.click(),URL.revokeObjectURL(r)};return e.jsxs(k,{variant:"outline",size:"sm",onClick:c,disabled:!s.length,children:[e.jsx(Me,{className:"w-3.5 h-3.5 mr-1"})," Export CSV"]})}function Ot({selectedCount:s,onApplyStatus:c,onApplySalesperson:n,onClear:a,onSelectAll:x,totalCount:t}){const[r,u]=y.useState(""),[p,d]=y.useState(""),_=()=>{r&&(c(r),u(""))},l=()=>{p.trim()&&(n(p.trim()),d(""))};return e.jsx(we,{children:s>0&&e.jsxs(ie.div,{initial:{opacity:0,y:-8}
7,animate:{opacity:1,y:0},exit:{opacity:0,y:-8},className:"flex flex-wrap items-center gap-2 px-4 py-2.5 bg-primary/5 border-b border-primary/20",children:[e.jsxs("div",{className:"flex items-center gap-2 mr-2",children:[e.jsx(gt,{className:"w-4 h-4 text-primary"}),e.jsxs("span",{className:"text-sm font-medium text-primary",children:[s," selected"]}),s<t&&e.jsxs("button",{className:"text-xs text-muted-foreground underline underline-offset-2 hover:text-foreground",onClick:x,children:["Select all ",t]})]}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsxs(O,{value:r,onValueChange:u,children:[e.jsx(I,{className:"h-7 text-xs w-[130px]",children:e.jsx(z,{placeholder:"Set status…"})}),e.jsx(J,{children:Object.entries(ee).map(([j,m])=>e.jsx(F,{value:j,children:m},j))})]}),e.jsx(k,{size:"sm",className:"h-7 text-xs px-2.5",disabled:!r,onClick:_,children:"Apply"})]}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(f,{className:"h-7 text-xs w-[140px]",placeholder:"Assign salesperson…",value:p,onChange:j=>d(j.target.value),onKeyDown:j=>j.key==="Enter"&&l()}),e.jsx(k,{size:"sm",className:"h-7 text-xs px-2.5",disabled:!p.trim(),onClick:l,children:"Assign"})]}),e.jsx(k,{size:"sm",variant:"ghost",className:"h-7 w-7 p-0 ml-auto",onClick:a,children:e.jsx(X,{className:"w-3.5 h-3.5"})})]})})}function Re({value:s=[],onChange:c,onSelectEmployee:n,placeholder:a="Add employee..."}){const[x,t]=y.useState(!1),[r,u]=y.useState(""),p=y.useRef(null),{data:d=[]}=le({queryKey:["employees"],queryFn:()=>Q.entities.Employee.list("name",100),staleTime:6e4}),_=Array.isArray(s)?s:[],l=d.filter(i=>!_.includes(i.name)&&(!r||i.name.toLowerCase().includes(r.toLowerCase())));y.useEffect(()=>{const i=v=>{p.current&&!p.current.contains(v.target)&&(t(!1),u(""))};return document.addEventListener("mousedown",i),()=>document.removeEventListener("mousedown",i)},[]);const j=i=>{const v=[..._,i.name];c(v),n==null||n(i),u(""),t(!1)},m=i=>{c(_.filter(v=>v!==i))};return e.jsxs("div",{ref:p,className:"relative",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-1 p-1 border border-input rounded-md bg-background min-h-[34px]",children:[_.map(i=>e.jsxs(K,{variant:"secondary",className:"gap-1 text-[10px] h-6 font-normal",children:[e.jsx(Ae,{className:"w-2.5 h-2.5"}),i,e.jsx("button",{type:"button",onClick:()=>m(i),className:"ml-0.5 hover:text-destructive",children:e.jsx(X,{className:"w-2.5 h-2.5"})})]},i)),e.jsx("div",{className:"flex-1 flex items-center min-w-[80px]",children:e.jsx(f,{value:r,onChange:i=>{u(i.target.value),t(!0)},onFocus:()=>t(!0),placeholder:_.length===0?a:"",className:"h-6 text-xs border-0 shadow-none p-0 focus-visible:ring-0 min-w-0"})}),e.jsx("button",{type:"button",className:"shrink-0 text-muted-foreground hover:text-foreground",onClick:()=>t(i=>!i),children:e.jsx(ae,{className:"w-3.5 h-3.5"})})]}),x&&e.jsx("div",{className:"absolute z-50 top-full left-0 right-0 mt-0.5 bg-popover border border-border rounded-md shadow-lg overflow-hidden max-h-44 overflow-y-auto",children:d.length===0?e.jsx("div",{className:"px-3 py-2 text-xs text-muted-foreground",children:"No employees set up yet."}):l.length===0?e.jsx("div",{className:"px-3 py-2 text-xs text-muted-foreground",children:r?"No match found.":"All employees selected."}):l.map(i=>e.jsxs("button",{type:"button",className:"w-full flex items-center justify-between px-3 py-1.5 text-xs hover:bg-accent/50 transition-colors text-left",onMouseDown:v=>{v.preventDefault(),j(i)},children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(Ae,{className:"w-3 h-3 text-muted-foreground"}),e.jsx("span",{className:"font-medium",children:i.name}),i.role&&e.jsxs("span",{className:"text-muted-foreground",children:["· ",i.role]})]}),e.jsxs("span",{className:"flex items-center gap-1",children:[i.cost_per_hour!=null&&e.jsxs("span",{className:"text-primary font-semibold",children:["$",i.cost_per_hour,"/hr"]}),e.jsx(se,{className:"w-3 h-3 text-muted-foreground"})]})]},i.id))})]})}const Fe={ai_reviewed:{label:"AI Reviewed",icon:Xe,className:"bg-purple-100 text-purple-700 border-purple-200"},human_confirmed:{label:"Human Confirmed",icon:je,className:"bg-green-100 text-green-700 border-green-200"},unreviewed:{label:"Not Reviewed",icon:ge,className:"bg-amber-100 text-amber-700 border-amber-200"}};function It({status:s,size:c="default"}){const n=Fe[s]||Fe.unreviewed,a=n.icon;return e.jsxs(K,{variant:"outline",className:be("gap-1 font-normal",n.className,c==="sm"&&"text-[10px] px-1.5 py-0",c==="default"&&"text-xs"),children:[e.jsx(a,{className:be(c==="sm"?"w-2.5 h-2.5":"w-3 h-3")}),n.label]})}function zt(s){var c;if(s.customer_signature||s.signature_date||(c=s.customer_survey)!=null&&c.submitted_at||s.contract_status==="signed")return"human_confirmed";if(s.notes)try{const n=typeof s.notes=="string"?JSON.parse(s.notes):s.notes;if(n!=null&&n.ai_breakdown||n!=null&&n.ai_quote)return"ai_reviewed"}catch{}return s.margin_pct!=null&&s.selling_price>0&&(s.material_cost||s.labor_cost)?"ai_reviewed":"unreviewed"}const B=s=>
7s!=null?`$${Number(s).toLocaleString("en-US",{minimumFractionDigits:0,maximumFractionDigits:0})}`:"—",Jt=s=>(s.labor_template_rate||0)*(s.labor_template_hours||0)+(s.labor_installation_rate||0)*(s.labor_installation_hours||0)+(s.labor_cutting_rate||0)*(s.labor_cutting_hours||0)+(s.labor_polishing_rate||0)*(s.labor_polishing_hours||0),pe=s=>{const c={...s};return["template","installation","cutting","polishing"].forEach(n=>{const a=`labor_${n}_employees`,x=`labor_${n}_employee`,t=c[a],r=c[x];Array.isArray(t)&&t.length>0?c[a]=t:r&&String(r).trim()?c[a]=[String(r).trim()]:c[a]=Array.isArray(t)?t:[]}),c};function Ve({job:s,overheadPerSqFt:c=0,onUpdate:n,onDelete:a,selected:x=!1,onToggleSelect:t}){const r=Je(),[u,p]=y.useState(!1),[d,_]=y.useState(!1),[l,j]=y.useState(()=>pe(s));y.useEffect(()=>{u||j(pe(s))},[s.id,s.updated_date,u]);const m=(s.square_feet||0)*c,i=Jt(s),v=(s.selling_price||0)*(s.commission_pct||0)/100,E=(s.material_cost||0)+i+m+v,q=(s.selling_price||0)-E,o=s.selling_price>0?q/s.selling_price*100:0,g=(b,T)=>j(D=>({...D,[b]:T})),w=(l.labor_template_rate||0)*(l.labor_template_hours||0),P=(l.labor_installation_rate||0)*(l.labor_installation_hours||0),S=(l.labor_cutting_rate||0)*(l.labor_cutting_hours||0),N=(l.labor_polishing_rate||0)*(l.labor_polishing_hours||0),M=b=>Array.isArray(b)?b:typeof b=="string"&&b.trim()?[b.trim()]:[],Z=()=>{const b=w+P+S+N;n(s.id,{customer_name:l.customer_name,zip_code:l.zip_code||"",description:l.description,material_type:l.material_type||void 0,square_feet:l.square_feet?Number(l.square_feet):null,material_cost:Number(l.material_cost)||0,labor_template_rate:Number(l.labor_template_rate)||0,labor_template_hours:Number(l.labor_template_hours)||0,labor_template_employee:M(l.labor_template_employees)[0]||"",labor_template_employees:M(l.labor_template_employees),labor_installation_rate:Number(l.labor_installation_rate)||0,labor_installation_hours:Number(l.labor_installation_hours)||0,labor_installation_employee:M(l.labor_installation_employees)[0]||"",labor_installation_employees:M(l.labor_installation_employees),labor_installation:P,labor_cutting_rate:Number(l.labor_cutting_rate)||0,labor_cutting_hours:Number(l.labor_cutting_hours)||0,labor_cutting_employee:M(l.labor_cutting_employees)[0]||"",labor_cutting_employees:M(l.labor_cutting_employees),labor_cutting:S,labor_polishing_rate:Number(l.labor_polishing_rate)||0,labor_polishing_hours:Number(l.labor_polishing_hours)||0,labor_polishing_employee:M(l.labor_polishing_employees)[0]||"",labor_polishing_employees:M(l.labor_polishing_employees),labor_polishing:N,labor_cost:b,overhead_cost:l.overhead_cost?Number(l.overhead_cost):0,salesperson_name:l.salesperson_name||"",commission_pct:Number(l.commission_pct)||0,selling_price:Number(l.selling_price)||0,deposit_amount:l.deposit_amount?Number(l.deposit_amount):null,is_paid:l.is_paid||!1,status:l.status,job_date:l.job_date||void 0,notes:l.notes||""}),p(!1)};return u?e.jsx(W,{className:"border shadow-sm",children:e.jsxs(G,{className:"p-4 space-y-3",children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wider",children:"Editing Job"}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Customer"}),e.jsx(f,{value:l.customer_name||"",onChange:b=>g("customer_name",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"ZIP"}),e.jsx(f,{value:l.zip_code||"",onChange:b=>g("zip_code",b.target.value),className:"h-8 text-sm",maxLength:10})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Status"}),e.jsxs(O,{value:l.status||"quote",onValueChange:b=>g("status",b),children:[e.jsx(I,{className:"h-8 text-sm",children:e.jsx(z,{})}),e.jsx(J,{children:Object.entries(ee).map(([b,T])=>e.jsx(F,{value:b,children:T},b))})]})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Description"}),e.jsx(f,{value:l.description||"",onChange:b=>g("description",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Material"}),e.jsxs(O,{value:l.material_type||"",onValueChange:b=>g("material_type",b),children:[e.jsx(I,{className:"h-8 text-sm",children:e.jsx(z,{placeholder:"Type"})}),e.jsx(J,{children:Object.entries(te).map(([b,T])=>e.jsx(F,{value:b,children:T},b))})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Sq Ft"}),e.jsx(f,{type:"number",value:l.square_feet||"",onChange:b=>g("square_feet",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Material $"}),e.jsx(f,{type:"number",value:l.material_cost||"",onChange:b=>g("material_cost",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Selling Price"}),e.jsx(f,{type:"number",value:l.selling_price||"",onChange:b=>g("selling_price",b.target.value),className:"h-8 text-sm"})]})]}),e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wider pt-1",children:"Labor ($/hr × hrs · employees)"}),e.jsx("div",{className:"grid grid-cols-2 gap-2",children:[{label:"Template",rate:"labor_template_rate",hours:"labor_template_hours",emp:"labor_template_employees"},{label:"Install",rate:"labor_installation_rate",hours:"labor_installation_hours",emp:"labor_installation_employees"},{label:"Cutting",rate:"labor_cutting_rate",hours:"labor_cutting_hours",emp:"labor_cutting_employees"},{label:"Polishing",rate:"labor_polishing_rate",hours:"labor_polishing_hours",emp:"labor_polishing_employees"}].map(({label:b,rate:T,hours:D,emp:$})=>e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:b}),e.jsxs("div",{className:"flex gap-1 mb-1",children:[e.jsx(f,{type:"number",value:l[T]||"",onChange:L=>g(T,L.target.value),className:"h-8 text-sm w-full",placeholder:"$/h"}
7),e.jsx(f,{type:"number",value:l[D]||"",onChange:L=>g(D,L.target.value),className:"h-8 text-sm w-full",placeholder:"h"})]}),e.jsx(Re,{value:Array.isArray(l[$])?l[$]:l[$]?[l[$]]:[],onChange:L=>g($,L),onSelectEmployee:L=>{L.cost_per_hour!=null&&g(T,L.cost_per_hour)},placeholder:"Add employee..."})]},b))}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Salesperson"}),e.jsx(f,{value:l.salesperson_name||"",onChange:b=>g("salesperson_name",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Commission %"}),e.jsx(f,{type:"number",value:l.commission_pct||"",onChange:b=>g("commission_pct",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Deposit $"}),e.jsx(f,{type:"number",value:l.deposit_amount||"",onChange:b=>g("deposit_amount",b.target.value),className:"h-8 text-sm",placeholder:"$0"})]}),e.jsxs("div",{className:"flex flex-col justify-end",children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Paid in Full"}),e.jsxs("button",{type:"button",onClick:()=>g("is_paid",!l.is_paid),className:`flex items-center gap-2 h-8 px-3 rounded-md border text-xs font-medium transition-colors ${l.is_paid?"bg-primary/10 border-primary text-primary":"border-input text-muted-foreground"}`,children:[l.is_paid?e.jsx(je,{className:"w-3.5 h-3.5"}):e.jsx(Se,{className:"w-3.5 h-3.5"}),l.is_paid?"Paid":"Unpaid"]})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Install Date"}),e.jsx(f,{type:"date",value:l.job_date||"",onChange:b=>g("job_date",b.target.value),className:"h-8 text-sm"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground mb-1 block",children:"Notes"}),e.jsx(f,{value:l.notes||"",onChange:b=>g("notes",b.target.value),className:"h-8 text-sm",placeholder:"Notes..."})]})]}),e.jsxs("div",{className:"flex gap-2 pt-1",children:[e.jsxs(k,{size:"sm",className:"flex-1 h-8 text-xs",onClick:Z,children:[e.jsx(et,{className:"w-3 h-3 mr-1"})," Save"]}),e.jsxs(k,{size:"sm",variant:"outline",className:"flex-1 h-8 text-xs",onClick:()=>{j(pe(s)),p(!1)},children:[e.jsx(X,{className:"w-3 h-3 mr-1"})," Cancel"]})]})]})}):e.jsx(W,{className:"border border-border/60 shadow-sm hover:shadow-md transition-shadow duration-200 rounded-xl bg-card",children:e.jsxs(G,{className:"p-4 sm:p-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2 mb-2",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[t&&e.jsx(bt,{checked:x,onCheckedChange:t,className:"h-4 w-4 shrink-0",onClick:b=>b.stopPropagation()}),e.jsx("p",{className:"font-semibold text-sm truncate",children:s.customer_name}),s.is_paid?e.jsx(je,{className:"w-3.5 h-3.5 text-green-600 dark:text-green-400 shrink-0"}):e.jsx(Se,{className:"w-3.5 h-3.5 text-muted-foreground shrink-0"}),e.jsx("span",{className:`inline-flex items-center px-2 py-0.5 rounded-full text-[11px] font-semibold ${_e[s.status]||_e.quote}`,children:ee[s.status]||"Quote"}),e.jsx(It,{status:zt(s),size:"sm"})]}),e.jsxs("div",{className:"flex items-center gap-2 mt-1 flex-wrap text-xs text-muted-foreground",children:[s.job_date&&e.jsx("span",{children:s.job_date}),s.zip_code&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-border",children:"·"}),e.jsxs("span",{children:["ZIP ",s.zip_code]})]}),s.description&&e.jsx("span",{className:"truncate max-w-[200px]",children:s.description}),s.material_type&&e.jsx(e.Fragment,{children:s.material_type!=="other"&&e.jsx("span",{className:"inline-flex items-center px-1.5 py-0 text-[10px] font-normal bg-muted rounded",children:te[s.material_type]})})]})]}),e.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[e.jsx(k,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:()=>r(`/job/${s.id}`),children:e.jsx(_t,{className:"w-3.5 h-3.5"})}),e.jsx(k,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:()=>p(!0),children:e.jsx(ft,{className:"w-3.5 h-3.5"})}),e.jsx(k,{size:"icon",variant:"ghost",className:"h-7 w-7 text-destructive",onClick:()=>
7a(s.id),children:e.jsx(jt,{className:"w-3.5 h-3.5"})})]})]}),e.jsxs("div",{className:"grid grid-cols-3 sm:grid-cols-6 gap-2 mt-3 pt-3 border-t",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Price"}),e.jsx("p",{className:"text-sm font-semibold tabular-nums",children:B(s.selling_price)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Profit"}),e.jsx("p",{className:`text-sm font-semibold tabular-nums ${q>=0?"text-accent":"text-destructive"}`,children:B(q)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Margin"}),e.jsxs("p",{className:`text-sm font-semibold tabular-nums ${o>=20?"text-accent":o>=0?"text-muted-foreground":"text-destructive"}`,children:[o.toFixed(1),"%"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Sq Ft"}),e.jsx("p",{className:"text-sm font-semibold tabular-nums",children:s.square_feet||"—"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Mat $"}),e.jsx("p",{className:"text-sm font-semibold tabular-nums",children:B(s.material_cost)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-[10px] text-muted-foreground uppercase tracking-wider font-medium",children:"Labor $"}),e.jsx("p",{className:"text-sm font-semibold tabular-nums",children:B(i)})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2 mt-2",children:[(()=>{const b=(D,$)=>{const L=D[`labor_${$}_employees`],U=D[`labor_${$}_employee`];return Array.isArray(L)&&L.length>0?L:U&&U.trim()?[U.trim()]:[]},T=[{key:"template",emps:b(s,"template"),rate:s.labor_template_rate,color:"bg-blue-500 text-white"},{key:"cutting",emps:b(s,"cutting"),rate:s.labor_cutting_rate,color:"bg-amber-500 text-white"},{key:"installation",emps:b(s,"installation"),rate:s.labor_installation_rate,color:"bg-purple-500 text-white"},{key:"polishing",emps:b(s,"polishing"),rate:s.labor_polishing_rate,color:"bg-emerald-500 text-white"}].filter(D=>D.emps.length>0);return T.length===0?e.jsx("span",{className:"text-[10px] text-muted-foreground italic",children:"No employees assigned"}):T.map(D=>e.jsxs("span",{className:`inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[10px] font-semibold ${D.color}`,children:[D.emps.join(", "),D.rate?e.jsxs("span",{className:"opacity-80 font-normal",children:[" @ $",D.rate,"/h"]}
7):null]},D.key))})(),s.salesperson_name&&e.jsxs("span",{className:"text-[10px] text-muted-foreground ml-1",children:["Sales: ",s.salesperson_name,s.commission_pct?` (${s.commission_pct}%)`:""]}),s.deposit_amount>0&&e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:["Deposit: ",B(s.deposit_amount)]})]}),c>0&&m>0&&e.jsxs("div",{className:"text-[10px] text-muted-foreground mt-1",children:["Overhead: ",B(m)," ",e.jsxs("span",{className:"opacity-60",children:["($",c.toFixed(2),"/sqft)"]})]}),e.jsxs("button",{className:"flex items-center gap-1 text-[10px] text-muted-foreground mt-2 hover:text-foreground transition-colors",onClick:()=>_(b=>!b),children:[d?e.jsx(ve,{className:"w-3 h-3"}):e.jsx(ae,{className:"w-3 h-3"}),d?"Less":"More details"]}),d&&e.jsxs("div",{className:"mt-2 pt-2 border-t grid grid-cols-2 sm:grid-cols-4 gap-x-3 gap-y-1.5 text-[11px]",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Commission $: "}),e.jsx("span",{children:v>0?B(v):"—"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Total Cost: "}),e.jsx("span",{children:B(E)})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Payment: "}),e.jsx("span",{className:s.is_paid?"text-primary font-medium":"text-destructive",children:s.is_paid?"Paid":"Unpaid"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Date: "}),e.jsx("span",{children:s.job_date||"—"})]}),s.notes&&e.jsxs("div",{className:"col-span-full",children:[e.jsx("span",{className:"text-muted-foreground",children:"Notes: "}),e.jsx("span",{children:s.notes})]})]})]})})}const Qe=new Date().toISOString().split("T")[0],Ee={template:25,installation:15,cutting:20,polishing:20},he={job_address:"",zip_code:"",description:"",material_type:"",square_feet:"",material_cost:"",selling_price:"",customer_name:"",customer_phone:"",status:"quote",job_date:Qe,labor_template_rate:"",labor_template_hours:"",labor_template_employee:"",labor_template_employees:[],labor_installation_rate:"",labor_installation_hours:"",labor_installation_employee:"",labor_installation_employees:[],labor_cutting_rate:"",labor_cutting_hours:"",labor_cutting_employee:"",labor_cutting_employees:[],labor_polishing_rate:"",labor_polishing_hours:"",labor_polishing_employee:"",labor_polishing_employees:[],salesperson_name:"",commission_pct:"",deposit_amount:"",is_paid:!1,notes:""},qe=[{label:"Template",rate:"labor_template_rate",hours:"labor_template_hours",emp:"labor_template_employees",key:"template"},{label:"Installation",rate:"labor_installation_rate",hours:"labor_installation_hours",emp:"labor_installation_employees",key:"installation"},{label:"Cutting",rate:"labor_cutting_rate",hours:"labor_cutting_hours",emp:"labor_cutting_employees",key:"cutting"},{label:"Polishing",rate:"labor_polishing_rate",hours:"labor_polishing_hours",emp:"labor_polishing_employees",key:"polishing"}];function Rt({onCreate:s,overheadPerSqFt:c=0,jobs:n=[],onClose:a,companyDefaults:x}){const[t,r]=y.useState(he),[u,p]=y.useState(!1),d=(o,g)=>r(w=>({...w,[o]:g}));y.useEffect(()=>{const o=Number(t.square_feet);if(!o||o<=0||u)return;const g={};qe.forEach(({hours:w,key:P})=>{if(!t[w]||t[w]===""){const S=Ee[P]||20;g[w]=Math.max(1,Math.round(o/S))}}),Object.keys(g).length>0&&r(w=>({...w,...g}))},[t.square_feet]);const _=t.customer_name.trim().toLowerCase(),l=_.length>2&&n.some(o=>(o.customer_name||"").trim().toLowerCase()===_),j=(Number(t.labor_template_rate)||0)*(Number(t.labor_template_hours)||0),m=(Number(t.labor_installation_rate)||0)*(Number(t.labor_installation_hours)||0),i=(Number(t.labor_cutting_rate)||0)*(Number(t.labor_cutting_hours)||0),v=(Number(t.labor_polishing_rate)||0)*(Number(t.labor_polishing_hours)||0),E=(Number(t.square_feet)||0)*c,q=()=>{if(!t.customer_name.trim())return;const o=j+m+i+v;s({customer_name:t.customer_name,job_address:t.job_address||void 0,description:t.description,zip_code:t.zip_code||void 0,customer_phone:t.customer_phone||void 0,material_type:t.material_type||void 0,square_feet:t.square_feet?Number(t.square_feet):void 0,material_cost:Number(t.material_cost)||0,selling_price:Number(t.selling_price)||0,status:t.status,job_date:t.job_date||Qe,labor_template_rate:Number(t.labor_template_rate)||0,labor_template_hours:Number(t.labor_template_hours)||0,labor_template_employee:t.labor_template_employee||"",labor_template_employees:Array.isArray(t.labor_template_employees)?t.labor_template_employees:t.labor_template_employee?[t.labor_template_employee]:[],labor_installation_rate:Number(t.labor_installation_rate)||0,labor_installation_hours:Number(t.labor_installation_hours)||0,labor_installation_employee:t.labor_installation_employee||"",labor_installation_employees:Array.isArray(t.labor_installation_employees)?t.labor_installation_employees:t.labor_installation_employee?[t.labor_installation_employee]:[],labor_installation:m,labor_cutting_rate:Number(t.labor_cutting_rate)||0,labor_cutting_hours:Number(t.labor_cutting_hours)||0,labor_cutting_employee:t.labor_cutting_employee||"",labor_cutting_employees:Array.isArray(t.labor_cutting_employees)?t.labor_cutting_employees:t.labor_cutting_employee?[t.labor_cutting_employee]:[],labor_cutting:i,labor_polishing_rate:Number(t.labor_polishing_rate)||0,labor_polishing_hours:Number(t.labor_polishing_hours)||0,labor_polishing_employee:t.labor_polishing_employee||"",labor_polishing_employees:Array.isArray(t.labor_polishing_employees)?t.labor_polishing_employees:t.labor_polishing_employee?[t.labor_polishing_employee]:[],labor_polishing:v,labor_cost:o,overhead_cost:E,salesperson_name:t.salesperson_name||"",commission_pct:Number(t.commission_pct)||0,deposit_amount:t.deposit_amount?Number(t.deposit_amount):null,is_paid:t.is_paid||!1,notes:t.notes||""}),r(he),a==null||a()};return e.jsxs(W,{className:"border shadow-md",children:[e.jsx(ne,{className:"pb-2 pt-4 px-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(oe,{className:"text-xs font-bold uppercase tracking-[0.15em] text-foreground",children:"Add Old Job"}),a&&e.jsx(k,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:a,children:e.jsx(X,{className:"w-4 h-4"})})]})}),e.jsxs(G,{className:"px-4 pb-4 space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Customer Address"}),e.jsx(f,{value:t.job_address,onChange:o=>d("job_address",o.target.value),placeholder:"Address...",className:"h-9 text-sm"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"ZIP"}),e.jsx(f,{value:t.zip_code,onChange:o=>d("zip_code",o.target.value),placeholder:"ZIP...",className:"h-9 text-sm",maxLength:10})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Status"}),e.jsxs(O,{value:t.status,onValueChange:o=>d("status",o),children:[e.jsx(I,{className:"h-9 text-sm",children:e.jsx(z,{})}),e.jsx(J,{children:Object.entries(ee).map(([o,g])=>e.jsx(F,{value:o,children:g},o))})]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Description"}),e.jsx(f,{value:t.description,onChange:o=>d("description",o.target.value),placeholder:"Description...",className:"h-9 text-sm"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Material"}),e.jsxs(O,{value:t.material_type,onValueChange:o=>d("material_type",o),children:[e.jsx(I,{className:"h-9 text-sm",children:e.jsx(z,{placeholder:"Type"})}),e.jsx(J,{children:Object.entries(te).map(([o,g])=>e.jsx(F,{value:o,children:g},o))})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Sq Ft"}),e.jsx(f,{type:"number",value:t.square_feet,onChange:o=>d("square_feet",o.target.value),placeholder:"0",className:"h-9 text-sm"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Material $"}),e.jsx(f,{type:"number",value:t.material_cost,onChange:o=>d("material_cost",o.target.value),placeholder:"$0",className:"h-9 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Selling Price"}),e.jsx(f,{type:"number",value:t.selling_price,onChange:o=>d("selling_price",o.target.value),placeholder:"$0",className:"h-9 text-sm"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Customer Name *"}),e.jsxs("div",{className:"relative",children:[e.jsx(f,{value:t.customer_name,onChange:o=>d("customer_name",o.target.value),placeholder:"Name...",className:`h-9 text-sm ${l?"border-yellow-400 pr-8":""}`}),l&&e.jsx(ye,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-yellow-500"})]}),l&&e.jsx("p",{className:"text-xs text-yellow-600 mt-0.5",children:"Duplicate name exists"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Phone"}),e.jsx(f,{value:t.customer_phone,onChange:o=>d("customer_phone",o.target.value),placeholder:"Phone...",className:"h-9 text-sm"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Install Date"}),e.jsx(f,{type:"date",value:t.job_date,onChange:o=>d("job_date",o.target.value),className:"h-9 text-sm"})]}),e.jsx("p",{className:"text-[10px] font-bold uppercase tracking-[0.1em] text-foreground pt-1",children:"Labor ($/HR × HRS · EMPLOYEES)"}),e.jsx("div",{className:"grid grid-cols-2 gap-2",children:qe.map(({label:o,rate:g,hours:w,emp:P,key:S})=>e.jsxs("div",{className:"border rounded-md p-2 space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-[11px] font-semibold text-foreground",children:o}),e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:[Ee[S]," sf/hr"]})]}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(f,{type:"number",value:t[g],onChange:N=>d(g,N.target.value),className:"h-8 text-xs flex-1 min-w-0",placeholder:"$/hr"}),e.jsx("span",{className:"text-muted-foreground text-xs shrink-0",children:"×"}),e.jsx(f,{type:"number",value:t[w],onChange:N=>d(w,N.target.value),className:"h-8 text-xs flex-1 min-w-0",placeholder:"h"}),e.jsx("span",{className:"text-muted-foreground text-xs shrink-0",children:"·"}),e.jsx("div",{className:"flex-1 min-w-0",children:e.jsx(Re,{value:(()=>{const N=t[P];if(Array.isArray(N))return N;const M=P.replace("_employees","_employee"),Z=t[M];return Z?[Z]:[]})(),onChange:N=>{d(P,N),N&&N.length>0},onSelectEmployee:N=>{N.cost_per_hour!=null&&(!t[g]||t[g]==="")&&d(g,N.cost_per_hour)},placeholder:"Add..."})})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{type:"button",onClick:()=>{const N=Number(t[w])||0;d(w,Math.max(0,N-1))},className:"h-5 w-5 rounded border text-[10px] text-muted-foreground hover:bg-muted flex items-center justify-center",children:"-"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"±1h"}),e.jsx("button",{type:"button",onClick:()=>{const N=Number(t[w])||0;d(w,N+1)},className:"h-5 w-5 rounded border text-[10px] text-muted-foreground hover:bg-muted flex items-center justify-center",children:"+"}),e.jsx("span",{className:"text-[10px] text-muted-foreground ml-2",children:(Number(t[g])||0)*(Number(t[w])||0)>0?`$${(Number(t[g])||0)*(Number(t[w])||0)}`:""})]})]},o))}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Salesperson"}),e.jsx(f,{value:t.salesperson_name,onChange:o=>d("salesperson_name",o.target.value),placeholder:"Name...",className:"h-9 text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Commission %"}),e.jsx(f,{type:"number",value:t.commission_pct,onChange:o=>d("commission_pct",o.target.value),placeholder:"%",className:"h-9 text-sm"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Deposit $"}),e.jsx(f,{type:"number",value:t.deposit_amount,onChange:o=>d("deposit_amount",o.target.value),placeholder:"$0",className:"h-9 text-sm"})]}),e.jsxs("div",{className:"flex flex-col justify-end",children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Paid in Full"}),e.jsxs("button",{type:"button",onClick:()=>d("is_paid",!t.is_paid),className:`flex items-center justify-center gap-1.5 h-9 px-3 rounded-md border text-xs font-medium transition-colors ${t.is_paid?"bg-primary/10 border-primary text-primary":"border-input text-muted-foreground hover:bg-muted/50"}`,children:[e.jsx("div",{className:`w-3 h-3 rounded-full border-2 flex items-center justify-center ${t.is_paid?"border-primary bg-primary":"border-muted-foreground"}`,children:t.is_paid&&e.jsx("div",{className:"w-1.5 h-1.5 rounded-sm bg-primary-foreground"})}),t.is_paid?"Paid":"Unpaid"]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[10px] uppercase tracking-[0.1em] text-muted-foreground mb-1 block",children:"Notes"}),e.jsx(f,{value:t.notes,onChange:o=>d("notes",o.target.value),placeholder:"Notes...",className:"h-9 text-sm"})]}),e.jsxs("div",{className:"flex gap-2 pt-1",children:[e.jsx(k,{variant:"outline",className:"flex-1 h-10 text-sm",onClick:()=>{r(he),a==null||a()},children:"Cancel"}),e.jsx(k,{className:"flex-1 h-10 text-sm bg-primary hover:bg-primary/90",onClick:q,disabled:!t.customer_name.trim(),children:"Add Old Job"})]})]})]})}function Vt(s){const c={};
7return s.forEach(n=>{const a=n.job_date?n.job_date.slice(0,7):"no-date";c[a]||(c[a]=[]),c[a].push(n)}),Object.entries(c).sort(([n],[a])=>n==="no-date"?1:a==="no-date"?-1:a.localeCompare(n))}function Qt(s){if(s==="no-date")return"No Date";const[c,n]=s.split("-");return new Date(Number(c),Number(n)-1,1).toLocaleDateString("en-US",{month:"long",year:"numeric"})}function Ut({jobs:s,monthlyCosts:c=[],onCreate:n,onUpdate:a,onDelete:x,onSelectJob:t,selectedJobId:r,selectedIds:u=new Set,onToggleSelect:p}){const[d,_]=y.useState(!1),[l,j]=y.useState(new Set),m=y.useMemo(()=>Vt(s),[s]),i=o=>j(g=>{const w=new Set(g);return w.has(o)?w.delete(o):w.add(o),w}),v=c.reduce((o,g)=>o+(Number(g.amount)||0),0),E=s.filter(o=>o.status!=="cancelled").reduce((o,g)=>o+(Number(g.square_feet)||0),0),q=E>0?v/E:0;return e.jsx(ie.div,{initial:{opacity:0,y:12},animate:{opacity:1,y:0},transition:{delay:.3},children:e.jsxs("div",{className:"px-0 sm:px-3 pb-24 space-y-4 sm:space-y-5",children:[e.jsx(we,{children:d&&e.jsx(ie.div,{initial:{opacity:0,y:-10},animate:{opacity:1,y:0},exit:{opacity:0,y:-10},children:e.jsx(Rt,{onCreate:n,overheadPerSqFt:q,jobs:s,onClose:()=>_(!1)})})}),s.length===0&&!d&&e.jsxs("div",{className:"text-center py-16 text-muted-foreground",children:[e.jsx("p",{className:"text-base font-medium",children:"No jobs yet"}),e.jsx("p",{className:"text-sm mt-1",children:"Tap the + button to add your first job"})]}),m.map(([o,g])=>{const w=!l.has(o),P=g.reduce((N,M)=>N+(M.selling_price||0),0),S=g.reduce((N,M)=>N+(M.square_feet||0),0);return e.jsxs("div",{children:[e.jsxs("button",{className:"w-full flex items-center gap-3 py-3 px-1 sticky top-0 bg-background/80 backdrop-blur-sm z-10 text-left",onClick:()=>i(o),children:[w?e.jsx(Te,{className:"w-4 h-4 text-muted-foreground shrink-0"}):e.jsx(ae,{className:"w-4 h-4 text-muted-foreground shrink-0"}),e.jsx("span",{className:"text-[11px] font-bold uppercase tracking-[0.15em] text-muted-foreground",children:Qt(o)}),e.jsxs("span",{className:"text-[10px] font-medium text-muted-foreground/60 bg-muted px-2 py-0.5 rounded-full",children:[g.length," job",g.length!==1?"s":""]}),e.jsxs("span",{className:"text-[10px] text-muted-foreground ml-auto",children:["$",P.toLocaleString()," · ",S.toLocaleString()," sqft"]})]}),!w&&g.map(N=>e.jsx(Ve,{job:N,overheadPerSqFt:q,onUpdate:a,onDelete:x,selected:u.has(N.id),onToggleSelect:()=>p&&p(N.id)},N.id))]},o)}),s.length>0&&e.jsx("div",{className:"py-3 px-3 border-t bg-muted/20",children:e.jsxs("div",{className:"flex items-center gap-3 text-[10px] text-muted-foreground",children:[e.jsx("span",{className:"font-semibold uppercase tracking-wider",children:"Totals"}),e.jsxs("span",{className:"ml-auto font-medium",children:["$",s.reduce((o,g)=>o+(g.selling_price||0),0).toLocaleString()," · ",s.reduce((o,g)=>o+(g.square_feet||0),0)," sqft · ",s.length," job",s.length!==1?"s":""]})]})}),!d&&e.jsx(k,{className:"fixed bottom-6 right-4 h-14 w-14 rounded-full shadow-xl z-50",onClick:()=>_(!0),children:e.jsx(se,{className:"w-6 h-6"})})]})})}function Bt(s,c,n,a,x){const t=new Nt({orientation:"landscape"}),r=t.internal.pageSize.getWidth(),u=14,p=(r-u*2)/n.length;t.setFontSize(14),t.setFont(void 0,"bold"),t.text(s,u,18),t.setFontSize(9),t.setFont(void 0,"normal"),t.setTextColor(120,120,120),t.text(`Generated: ${new Date().toLocaleDateString()}`,u,25),t.setTextColor(0,0,0);let d=33;t.setFontSize(9),t.setFont(void 0,"bold"),t.setFillColor(240,240,245),t.rect(u,d-5,r-u*2,8,"F"),n.forEach((_,l)=>{t.text(String(_),u+l*p+2,d)}),t.setFont(void 0,"normal"),x.forEach((_,l)=>{d+=9,d>t.internal.pageSize.getHeight()-15&&(t.addPage(),d=20),l%2===0&&(t.setFillColor(250,250,252),t.rect(u,d-5,r-u*2,8,"F")),a.forEach((j,m)=>{const i=_[j]==null?"":String(_[j]);t.text(i,u+m*p+2,d,{maxWidth:p-3})})}),t.save(c)}const H=[{key:"template",label:"Template",est:"labor_template_hours",act:"labor_template_actual_hours"},{key:"installation",label:"Installation",est:"labor_installation_hours",act:"labor_installation_actual_hours"},{key:"cutting",label:"Cutting",est:"labor_cutting_hours",act:"labor_cutting_actual_hours"},{key:"polishing",label:"Polishing",est:"labor_polishing_hours",act:"labor_polishing_actual_hours"}],ce=25;function R(s){return s!=null?`${Number(s).toFixed(1)}h`:"—"}function Pe({est:s,act:c}){if(s==null||c==null)return e.jsx("span",{className:"text-muted-foreground text-xs",children:"—"});const n=c-s,a=s>0?(n/s*100).toFixed(0):null;return Math.abs(n)<.05?e.jsxs("span",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(tt,{className:"w-3 h-3"})," On target"]}):n>0?e.jsxs("span",{className:"flex items-center gap-1 text-xs text-destructive font-medium",children:[e.jsx(st,{className:"w-3 h-3"})," +",R(n),a?` (${a}%)`:""]}):e.jsxs("span",{className:"flex items-center gap-1 text-xs text-accent font-medium",children:[e.jsx(vt,{className:"w-3 h-3"})," ",R(n),a?` (${a}%)`:""]})}function Kt({pct:s}){if(s==null)return null;const c=Math.abs(s);return c<=10?e.jsx(K,{className:"bg-accent/15 text-accent border-0 text-xs",children:"Accurate"}):c<=25?e.jsx(K,{className:"bg-yellow-500/15 text-yellow-700 dark:text-yellow-400 border-0 text-xs",children:"Off"}):e.jsx(K,{className:"bg-destructive/15 text-destructive border-0 text-xs",children:"Way off"})}function de(s){let c=0,n=0,a=!1;return H.forEach(({est:x,act:t})=>{const r=s[x],u=s[t];r!=null&&u!=null&&(c+=r,n+=u,a=!0)}),a?{totalEst:c,totalAct:n,diff:n-c}:null}function Y(s){const c=de(s);return!c||c.totalEst===0?null:Math.round((c.totalAct-c.totalEst)/c.totalEst*100)}function Ht({jobs:s}){const c=s.map(n=>{const a=Y(n),x=de(n);return{job:n,pct:a,t:x}}).filter(({pct:n})=>n!=null&&n>ce).sort((n,a)=>a.pct-n.pct);return c.length===0?null:e.jsxs("div",{className:"mb-5 rounded-lg border border-destructive/30 bg-destructive/5 p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx(ye,{className:"w-4 h-4 text-destructive"}),e.jsxs("p",{className:"text-sm font-semibold text-destructive",children:[c.length," job",c.length>
71?"s":""," significantly over estimate (>",ce,"%)"]})]}),e.jsx("div",{className:"space-y-2",children:c.map(({job:n,pct:a,t:x})=>{const t=H.map(r=>{const u=n[r.est],p=n[r.act];return u==null||p==null||u===0?null:{label:r.label,pct:(p-u)/u*100,diff:p-u}}).filter(Boolean).sort((r,u)=>u.pct-r.pct)[0];return e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-2 rounded-md bg-background border border-border px-3 py-2",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium",children:n.customer_name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[n.job_date?n.job_date:"No date",n.material_type?` · ${n.material_type}`:""]}),t&&e.jsxs("p",{className:"text-xs text-destructive mt-0.5",children:["Worst phase: ",e.jsx("span",{className:"font-medium",children:t.label})," (+",t.diff.toFixed(1),"h, ",t.pct.toFixed(0),"% over)"]})]}),e.jsxs("div",{className:"text-right shrink-0",children:[e.jsxs("p",{className:"text-sm font-bold text-destructive",children:["+",a,"% over"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[R(x.totalEst)," est → ",R(x.totalAct)," actual"]}),e.jsxs("p",{className:"text-xs text-destructive font-medium",children:["+",R(x.diff)," extra"]})]})]},n.id)})})]})}function Wt({jobs:s}){if(s.filter(r=>H.some(u=>r[u.est]!=null&&r[u.act]!=null)).length===0)return null;const n=H.map(({key:r,label:u,est:p,act:d})=>{const _=s.filter(i=>i[p]!=null&&i[d]!=null);if(_.length===0)return null;const l=_.reduce((i,v)=>i+(v[p]||0),0),j=_.reduce((i,v)=>i+(v[d]||0),0),m=l>0?Math.round((j-l)/l*100):null;return{key:r,label:u,totalEst:l,totalAct:j,avgPct:m,count:_.length}}).filter(Boolean),a=n.reduce((r,u)=>r+u.totalEst,0),x=n.reduce((r,u)=>r+u.totalAct,0),t=a>0?Math.round((x-a)/a*100):null;return e.jsxs("div",{className:"mb-5",children:[e.jsxs("div",{className:`rounded-lg p-3 mb-3 flex items-center justify-between ${t!=null&&t>0?"bg-destructive/8 border border-destructive/20":"bg-accent/8 border border-accent/20"}`,children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Overall Labor Variance"}),e.jsxs("p",{className:"text-sm font-semibold",children:[R(a)," estimated → ",R(x)," actual"]})]}),e.jsxs("div",{className:"text-right",children:[e.jsx("p",{className:`text-lg font-bold ${t!=null&&t>0?"text-destructive":"text-accent"}`,children:t!=null?`${t>0?"+":""}${t}%`:"—"}),e.jsxs("p",{className:`text-xs font-medium ${x-a>0?"text-destructive":"text-accent"}`,children:[x-a>0?"+":"",R(x-a)," total"]})]})]}),e.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-4 gap-3",children:n.map(r=>e.jsxs("div",{className:"bg-muted/40 rounded-lg p-3 text-center",children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:r.label}),e.jsxs("p",{className:"text-sm font-semibold",children:[R(r.totalEst)," est"]}),e.jsxs("p",{className:"text-sm font-semibold",children:[R(r.totalAct)," actual"]}),r.avgPct!=null&&e.jsxs("p",{className:`text-xs mt-1 font-medium ${r.avgPct>0?"text-destructive":r.avgPct<0?"text-accent":"text-muted-foreground"}`,children:[r.avgPct>0?"+":"",r.avgPct,"% overall"]})]},r.key))})]})}function Gt({jobs:s}){const[c,n]=y.useState(!1),a=r=>{Bt("Labor Estimate Accuracy Report",`labor-accuracy-report-${new Date().toISOString().slice(0,10)}.pdf`,["Customer","Date","Template Est","Template Act","Install Est","Install Act","Cutting Est","Cutting Act","Polishing Est","Polishing Act","Total Est","Total Act","Variance","Var %","Rating"],["customer_name","job_date","labor_template_hours","labor_template_actual_hours","labor_installation_hours","labor_installation_actual_hours","labor_cutting_hours","labor_cutting_actual_hours","labor_polishing_hours","labor_polishing_actual_hours","totalEst","totalAct","diff","variancePct","rating"],r.map(u=>{const p=de(u)||{totalEst:0,totalAct:0,diff:0},d=Y(u),_=d==null?"":Math.abs(d)<=10?"Accurate":Math.abs(d)<=25?"Off":"Way off";return{...u,...p,variancePct:d!=null?`${d>0?"+":""}${d}%`:"",rating:_}}))},x=s.filter(r=>H.some(u=>r[u.est]!=null&&r[u.act]!=null)).sort((r,u)=>{const p=Y(r)??0;return(Y(u)??0)-p}),t=x.filter(r=>(Y(r)??0)>ce).length;return e.jsxs(W,{className:"border-0 shadow-sm",children:[e.jsx(ne,{className:"pb-3 pt-4 px-5 cursor-pointer select-none",onClick:()=>n(r=>!r),children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(oe,{className:"text-sm font-semibold flex items-center gap-2",children:[e.jsx(ge,{className:"w-4 h-4 text-primary"}),"Labor Estimate Accuracy",t>0&&e.jsxs(K,{className:"bg-destructive/15 text-destructive border-0 text-xs ml-1",children:[t," over budget"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-xs text-muted-foreground",children:[x.length," jobs with actuals"]}),x.length>0&&e.jsxs(k,{size:"sm",variant:"outline",className:"h-7 text-xs px-2 gap-1",onClick:r=>{r.stopPropagation(),a(x)},children:[e.jsx(Me,{className:"w-3 h-3"})," Export"]}),c?e.jsx(ve,{className:"w-4 h-4 text-muted-foreground"}):e.jsx(ae,{className:"w-4 h-4 text-muted-foreground"})]})]})}),e.jsx(we,{initial:!1,children:c&&e.jsx(ie.div,{initial:{height:0,opacity:0}
7,animate:{height:"auto",opacity:1},exit:{height:0,opacity:0},transition:{duration:.2},style:{overflow:"hidden"},children:e.jsx(G,{className:"px-5 pb-5",children:x.length===0?e.jsxs("div",{className:"text-center py-10 text-muted-foreground",children:[e.jsx(ge,{className:"w-8 h-8 mx-auto mb-2 opacity-30"}),e.jsx("p",{className:"text-sm",children:"No actual hours logged yet."}),e.jsx("p",{className:"text-xs mt-1",children:'Edit a job and fill in the "Actual Hours" fields to start tracking accuracy.'})]}):e.jsxs(e.Fragment,{children:[e.jsx(Wt,{jobs:s}),e.jsx(Ht,{jobs:x}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-xs",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("th",{className:"text-left py-2 pr-3 text-muted-foreground font-medium",children:"Job"}),H.map(r=>e.jsx("th",{className:"text-center py-2 px-2 text-muted-foreground font-medium whitespace-nowrap",children:r.label},r.key)),e.jsx("th",{className:"text-right py-2 pl-3 text-muted-foreground font-medium",children:"Total Variance"}),e.jsx("th",{className:"text-right py-2 pl-2 text-muted-foreground font-medium",children:"Rating"})]})}),e.jsx("tbody",{children:x.map(r=>{const u=Y(r),p=u!=null&&u>ce;return e.jsxs("tr",{className:`border-b border-border/50 transition-colors ${p?"bg-destructive/5 hover:bg-destructive/10":"hover:bg-muted/30"}`,children:[e.jsxs("td",{className:"py-2.5 pr-3",children:[e.jsx("p",{className:"font-medium text-foreground",children:r.customer_name}),r.job_date&&e.jsx("p",{className:"text-muted-foreground",children:r.job_date})]}),H.map(d=>{const _=r[d.est],l=r[d.act];return _==null&&l==null?e.jsx("td",{className:"py-2.5 px-2 text-center text-muted-foreground",children:"—"},d.key):e.jsxs("td",{className:"py-2.5 px-2 text-center",children:[e.jsxs("div",{className:"text-foreground",children:[R(_)," → ",R(l)]}),e.jsx(Pe,{est:_,act:l})]},d.key)}),e.jsx("td",{className:"py-2.5 pl-3 text-right",children:(()=>{const d=de(r);return d?e.jsx(Pe,{est:d.totalEst,act:d.totalAct}):e.jsx("span",{className:"text-muted-foreground",children:"—"})})()}),e.jsx("td",{className:"py-2.5 pl-2 text-right",children:e.jsx(Kt,{pct:u})})]},r.id)})})]})})]})})})})]})}const De=[{key:"customer_name",label:"Customer Name",icon:at},{key:"selling_price",label:"Selling Price",icon:rt},{key:"job_address",label:"Job Address",icon:lt},{key:"job_date",label:"Job Date",icon:nt},{key:"square_feet",label:"Square Feet",icon:Oe},{key:"material_type",label:"Material Type",icon:wt}];function Zt(s){const c=[],n=[];return De.forEach(a=>{const x=s[a.key];x==null||x===""?c.push(a):n.push(a)}),s.pdf_url||c.push({key:"pdf_url",label:"PDF Invoice/Drawing",icon:Oe}),{missing:c,present:n,completeness:Math.round(n.length/De.length*100)}}function Yt({jobs:s,onUpdate:c,onDelete:n}){if(!s||s.length===0)return null;const a=s.filter(x=>x.status!=="completed"&&x.status!=="cancelled").map(x=>({...x,...Zt(x)})).filter(x=>x.missing.length>0).sort((x,t)=>x.completeness-t.completeness);return a.length===0?e.jsxs(W,{className:"border-green-200 bg-green-50 dark:bg-green-950/20 dark:border-green-800",children:[e.jsx(ne,{className:"pb-2",children:e.jsxs(oe,{className:"text-base flex items-center gap-2 text-green-700 dark:text-green-400",children:[e.jsx(yt,{className:"w-4 h-4"}),"All Jobs Complete"]})}),e.jsx(G,{children:e.jsx("p",{className:"text-sm text-green-600 dark:text-green-400",children:"All active jobs have complete information. Great job!"})})]}):e.jsxs(W,{className:"border-orange-200 bg-orange-50 dark:bg-orange-950/20 dark:border-orange-800",children:[e.jsx(ne,{className:"pb-2",children:e.jsxs(oe,{className:"text-base flex items-center gap-2 text-orange-700 dark:text-orange-400",children:[e.jsx(ye,{className:"w-4 h-4"}),"Incomplete Jobs (",a.length,")"]})}),e.jsxs(G,{className:"space-y-3",children:[e.jsxs("p",{className:"text-xs text-orange-600 dark:text-orange-400",children:[a.length," job",a.length>1?"s":""," missing required information"]}),e.jsx("div",{className:"space-y-2 max-h-[600px] overflow-y-auto",children:a.slice(0,10).map(x=>e.jsx(Ve,{job:x,overheadPerSqFt:0,onUpdate:c,onDelete:n},x.id))}),a.length>10&&e.jsxs("p",{className:"text-xs text-muted-foreground text-center",children:["+ ",a.length-10," more incomplete jobs"]})]})]})}function Xt({onNewJob:s}){const c=Je();return e.jsx(W,{className:"border-dashed",children:e.jsxs(G,{className:"flex flex-col items-center justify-center py-16 px-4",children:[e.jsx("div",{className:"w-16 h-16 rounded-full bg-muted flex items-center justify-center mb-4",children:e.jsx(se,{className:"w-8 h-8 text-muted-foreground"})}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"No jobs yet"}),e.jsx("p",{className:"text-muted-foreground text-center max-w-md mb-6",children:"Get started by creating your first job. You can add customer details, measurements, and track progress through production."}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(k,{onClick:s,className:"gap-2",children:[e.jsx(se,{className:"w-4 h-4"}),"Create Job"]}),e.jsxs(k,{variant:"outline",onClick:()=>c("/quote"),className:"gap-2",children:["Create Quote First",e.jsx(kt,{className:"w-4 h-4"})]})]})]})})}function es({size:s=80,className:c}){const a=Array.from({length:24}).map((x,t)=>{const r=t*360/24;return e.jsx("polygon",{points:"47,8 50,2 53,8",fill:"hsl(var(--primary))",transform:`rotate(${r} 50 50)`},t)});return e.jsxs("div",{className:be("relative flex items-center justify-center",c),style:{width:s,height:s},children:[e.jsxs("svg",{className:"animate-spin",style:{animationDuration:"1.2s"},viewBox:"0 0 100 100",width:s,height:s,children:[a,e.jsx("circle",{cx:"50",cy:"50",r:"34",fill:"hsl(var(--primary) / 0.08)",stroke:"hsl(var(--primary))",strokeWidth:"2.5"}),e.jsx("circle",{cx:"50",cy:"50",r:"27",fill:"none",stroke:"hsl(var(--primary) / 0.3)",strokeWidth:"1",strokeDasharray:"2 3"}),e.jsx("circle",{cx:"50",cy:"50",r:"9",fill:"hsl(var(--primary))"}),e.jsx("circle",{cx:"50",cy:"50",r:"3.5",fill:"hsl(var(--background))"}),[0,60,120,180,240,300].map(x=>e.jsx("line",{x1:"50",y1:"33",x2:"50",y2:"41",stroke:"hsl(var(--primary) / 0.5)",strokeWidth:"1.5",transform:`rotate(${x} 50 50)`},x))]}),e.jsx("div",{className:"absolute inset-0 rounded-full pointer-events-none",style:{boxShadow:"0 0 24px hsl(var(--primary) / 0.25)"}})]})}function Es(){const[s,c]=y.useState("all"),[n,a]=y.useState(""),[x,t]=y.useState(""),[r,u]=y.useState("all"),[p,d]=y.useState(null),[_,l]=y.useState(!1),[j,m]=y.useState(1),[i,v]=y.useState(25),[E,q]=y.useState("list"),[o,g]=y.useState("all"),[w,P]=y.useState(new Set),S=Le();y.useEffect(()=>{const h=setTimeout(()=>t(n),300);return()=>clearTimeout(h)},[n]),y.useEffect(()=>{m(1)},[s,x,r,p,o,i]);const{data:N=[],isLoading:M,isError:Z,refetch:b}=le({queryKey:["jobs"],queryFn:()=>Q.entities.Job.list("-created_date",500),retry:1,staleTime:3e4,refetchOnWindowFocus:!1}),T=y.useMemo(()=>{const h=[...new Set(N.map(C=>{var A;return(A=C.job_date)==null?void 0:A.slice(0,7)}
7).filter(Boolean))].sort((C,A)=>A.localeCompare(C));return r!=="all"&&!h.includes(r)&&h.unshift(r),h},[N,r]),D=y.useMemo(()=>{const h=new Set;return N.forEach(C=>(C.job_tags||[]).forEach(A=>h.add(A))),[...h].sort()},[N]),$=y.useMemo(()=>{const h=x.toLowerCase();return N.filter(C=>{var ke;const A=!h||(C.customer_name||"").toLowerCase().includes(h)||(C.job_address||"").toLowerCase().includes(h)||(C.job_number||"").toLowerCase().includes(h)||(C.account_name||"").toLowerCase().includes(h)||(C.description||"").toLowerCase().includes(h),ue=s==="all"||C.status===s,He=!p||C.location_id===p,We=r==="all"||((ke=C.job_date)==null?void 0:ke.slice(0,7))===r,Ge=o==="all"||(C.job_tags||[]).includes(o);return A&&ue&&We&&He&&Ge})},[N,x,s,r,p,o]),L=Math.max(1,Math.ceil($.length/i)),U=Math.min(j,L),re=(U-1)*i,Ue=$.slice(re,re+i),me=async(h,C)=>{try{await Q.entities.Job.update(h,C),S.invalidateQueries({queryKey:["jobs"]}),S.invalidateQueries({queryKey:["jobs-all"]}),S.invalidateQueries({queryKey:["accounts"]}),S.invalidateQueries({queryKey:["leads"]}),S.invalidateQueries({queryKey:["customer-quotes"]})}catch(A){V.error("Failed to update job"),console.error(A)}},Ce=async h=>{try{await Q.entities.Job.delete(h),S.invalidateQueries({queryKey:["jobs"]}),S.invalidateQueries({queryKey:["jobs-all"]}),S.invalidateQueries({queryKey:["accounts"]}),S.invalidateQueries({queryKey:["leads"]}),S.invalidateQueries({queryKey:["customer-quotes"]}),V.success("Job deleted")}catch(C){V.error("Failed to delete job"),console.error(C)}},Be=async h=>{const C=[...w];try{await Promise.all(C.map(A=>Q.entities.Job.update(A,{status:h}))),P(new Set),S.invalidateQueries({queryKey:["jobs"]}),V.success(`Updated ${C.length} job${C.length!==1?"s":""}`)}catch{V.error("Some jobs failed to update"),S.invalidateQueries({queryKey:["jobs"]})}},Ke=async h=>{const C=[...w];try{await Promise.all(C.map(A=>Q.entities.Job.update(A,{salesperson_name:h}))),P(new Set),S.invalidateQueries({queryKey:["jobs"]}),V.success(`Assigned salesperson to ${C.length} job${C.length!==1?"s":""}`)}catch{V.error("Some jobs failed to update"),S.invalidateQueries({queryKey:["jobs"]})}};return e.jsxs("div",{className:"min-h-screen bg-background p-3 sm:p-6 space-y-4",children:[e.jsx(St,{icon:ot,title:"Jobs",subtitle:"Manage and track all job progress",className:"mb-4"}),e.jsx(Dt,{currentFilters:{statusFilter:s,searchQuery:n,periodFilter:r,selectedLocationId:p,tagFilter:o},onApplyView:h=>{c(h.statusFilter||"all"),a(h.searchQuery||""),u(h.periodFilter||"all"),d(h.selectedLocationId||null),g(h.tagFilter||"all")}}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx(Ct,{onLocationChange:d,selectedLocationId:p}),e.jsxs("div",{className:"relative w-full sm:w-64 sm:flex-shrink-0",children:[e.jsx(it,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(f,{className:"pl-9 rounded-xl border-primary/20",placeholder:"Search jobs...",value:n,onChange:h=>a(h.target.value)})]}),e.jsxs(O,{value:r,onValueChange:u,children:[e.jsxs(I,{className:"w-[170px] rounded-xl border-primary/20",children:[e.jsx(fe,{className:"w-4 h-4 mr-1.5 text-muted-foreground"}),e.jsx(z,{placeholder:"Month"})]}),e.jsxs(J,{children:[e.jsx(F,{value:"all",children:"All Months"}),T.map(h=>{const[C,A]=h.split("-"),ue=new Date(parseInt(C),parseInt(A)-1,1).toLocaleDateString("en-US",{month:"long",year:"numeric"});return e.jsx(F,{value:h,children:ue},h)})]})]}),e.jsxs(O,{value:s,onValueChange:c,children:[e.jsx(I,{className:"w-[150px] rounded-xl border-primary/20",children:e.jsx(z,{placeholder:"All"})}),e.jsxs(J,{children:[e.jsx(F,{value:"all",children:"All"}),Ie.map(h=>e.jsx(F,{value:h.value,children:h.label},h.value))]})]}),e.jsxs(O,{value:o,onValueChange:g,children:[e.jsx(I,{className:"w-[140px] rounded-xl border-primary/20",children:e.jsx(z,{placeholder:"Tags"})}),e.jsxs(J,{children:[e.jsx(F,{value:"all",children:"All Tags"}),D.length>0?D.map(h=>e.jsx(F,{value:h,children:h},h)):e.jsx(F,{value:"rush",disabled:!0,children:"No tags yet"})]})]}),e.jsxs("div",{className:"flex items-center gap-1 ml-auto",children:[e.jsx(k,{variant:E==="list"?"default":"outline",size:"icon",className:"h-9 w-9",onClick:()=>q("list"),title:"List view",children:e.jsx(At,{className:"w-4 h-4"})}),e.jsx(k,{variant:E==="kanban"?"default":"outline",size:"icon",className:"h-9 w-9",onClick:()=>q("kanban"),title:"Kanban view",children:e.jsx($t,{className:"w-4 h-4"})}),e.jsx(Tt,{jobs:$})]}),e.jsxs(k,{onClick:()=>l(!0),className:"gap-1 rounded-xl bg-gradient-to-r from-primary to-primary/70 hover:from-primary/90 hover:to-primary/60 transition-all shadow-md",children:[e.jsx(fe,{className:"w-4 h-4"})," + New Job"]})]}),M&&N.length===0?e.jsx("div",{className:"flex justify-center py-20",children:e.jsx(es,{})}):Z&&N.length===0?e.jsxs("div",{className:"text-center py-20 space-y-3",children:[e.jsx(ct,{className:"w-10 h-10 text-muted-foreground mx-auto"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Couldn't load jobs. Check your connection and try again."}),e.jsx(k,{variant:"outline",size:"sm",onClick:()=>b(),children:"Retry"})]}):$.length===0?e.jsx(Xt,{onNewJob:()=>l(!0)}):e.jsxs(e.Fragment,{children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[$.length," jobs loaded",N.length>=500?" (showing first 500)":""]}),e.jsx(Gt,{jobs:$})]}),E==="list"&&$.length>0&&e.jsxs("div",{className:"flex flex-col sm:flex-row items-center justify-between gap-3 pt-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx("span",{children:"Rows per page"}),e.jsxs(O,{value:String(i),onValueChange:h=>v(Number(h)),children:[e.jsx(I,{className:"w-[72px] h-8",children:e.jsx(z,{})}),e.jsxs(J,{children:[e.jsx(F,{value:"25",children:"25"}),e.jsx(F,{value:"50",children:"50"}),e.jsx(F,{value:"100",children:"100"})]})]}),e.jsxs("span",{className:"ml-2",children:["Showing ",re+1,"–",Math.min(re+i,$.length)," of ",$.length]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(k,{size:"sm",variant:"outline",disabled:U<=1,onClick:()=>m(h=>Math.max(1,h-1)),children:[e.jsx(dt,{className:"w-4 h-4"})," Previous"]}),e.jsxs("span",{className:"text-sm text-muted-foreground tabular-nums",children:["Page ",U," of ",L]}),e.jsxs(k,{size:"sm",variant:"outline",disabled:U>=L,onClick:()=>m(h=>Math.min(L,h+1)),children:["Next ",e.jsx(Te,{className:"w-4 h-4"})]})]})]}),e.jsx(Yt,{jobs:$,onUpdate:me,onDelete:Ce}),w.size>0&&e.jsx(Ot,{selectedCount:w.size,totalCount:$.length,onApplyStatus:Be,onApplySalesperson:Ke,onClear:()=>P(new Set),onSelectAll:()=>P(new Set($.map(h=>h.id)))}),E==="kanban"?e.jsx(Mt,{jobs:$,onUpdate:me}):e.jsx(Ut,{jobs:Ue,onUpdate:me,onDelete:Ce,selectedIds:w,onToggleSelect:h=>{P(C=>{const A=new Set(C);return A.has(h)?A.delete(h):A.add(h),A})}}),e.jsx(qt,{open:_,onOpenChange:h=>{l(h),h||S.invalidateQueries({queryKey:["jobs"]})}})]})}export{Es 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.