import{a as e,t}from"./react-DK-MTtD8.js";import{r as n,s as r}from"./utils-CXcsdpX-.js";import{r as i}from"./api-DcvYaav1.js";import{t as a}from"./list-table-CQD2C7IB.js";import{t as o}from"./funnel-BT4pjjO7.js";import{t as s}from"./plus-CuMPbIyn.js";import{n as c,t as l}from"./filter-sheet-omdfG0qY.js";import{r as u}from"./Helper-CgZLd8U2.js";import{Hr as d,da as f,ia as p,rs as m,xo as h,za as g}from"./index-Ct5uU080.js";import{t as _}from"./page-content-Bh5Zxb_Y.js";import{t as v}from"./badge-CL1e8lO_.js";import{a as y,i as b,n as x,o as S,t as C}from"./dialog-DuXDR687.js";import{t as w}from"./form-field-BuHvZ1sK.js";import{t as T}from"./form-select-DIeUz5Nk.js";import{t as E}from"./list-page-header-CePoWpko.js";import{t as D}from"./auth-status-badge-BlJK5R24.js";import{t as O}from"./row-actions-bhl00K6X.js";import{F as k,P as A,_ as j,k as M}from"./serviceApi-Bn3V4hja.js";var N=e(t(),1),P=r();function F(e){let t=[];for(let n of e.photos||[])t.push({id:n.id,file:n.file,file_name:n.file_name,file_type:n.file_type,isImage:!0});for(let n of e.attachments||[]){let e=n.file_type?.startsWith(`image/`)||/\.(jpg|jpeg|png|gif|webp|svg|bmp)$/i.test(n.file_name);t.push({id:n.id,file:n.file,file_name:n.file_name,file_type:n.file_type,isImage:e})}return t}function I({open:e,onOpenChange:t}){let[n,r]=(0,N.useState)(``),[i,a]=(0,N.useState)(``),[o,s]=(0,N.useState)(``),[c,l]=(0,N.useState)(`filter`),[d,m]=(0,N.useState)({}),[_,v]=(0,N.useState)([]),[E,D]=(0,N.useState)(!1),O=(0,N.useRef)(null),{data:j=[]}=k(),{data:I}=A(n,{skip:!n}),{data:L}=f(),R={log_type:`service`};n&&(R.service_request=n);let{data:z=[]}=M(n?R:void 0,{skip:!n}),B=(0,N.useMemo)(()=>z.filter(e=>!(i&&e.date<i||o&&e.date>o)),[z,i,o]),V=async()=>{if(n){D(!0);try{let e=[];for(let t of B)try{let n=await u(`/service/daily-logs/${t.id}/`);e.push(n.data)}catch{}v(e);let t={};for(let n of e)t[n.id]=new Set;m(t),l(`preview`)}catch(e){console.error(`Failed to fetch log details:`,e)}finally{D(!1)}}},H=(e,t)=>{m(n=>{let r=new Set(n[e]||[]);return r.has(t)?r.delete(t):r.add(t),{...n,[e]:r}})},U=()=>{let e=O.current;if(!e)return;let t=window.open(``,`_blank`);t&&(t.document.write(`<!DOCTYPE html><html><head><title>Service Work Report — ${I?.code||``}</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:'Segoe UI',Arial,sans-serif;color:#1a1a1a;padding:30px;font-size:12px}
.company-header{display:flex;align-items:center;gap:16px;border-bottom:2px solid #1a3c6e;padding-bottom:12px;margin-bottom:16px}
.company-header img{max-height:50px;max-width:160px;object-fit:contain}
.company-header .company-info{flex:1}
.company-header .company-name{font-size:18px;font-weight:700;color:#1a3c6e}
.company-header .company-details{font-size:10px;color:#555;margin-top:2px}
h1{font-size:20px;text-align:center;margin-bottom:4px}
.subtitle{text-align:center;color:#666;font-size:12px;margin-bottom:20px}
.sr-grid{display:grid;grid-template-columns:1fr 1fr;gap:4px 16px;margin-bottom:16px;font-size:12px}
.sr-grid .lbl{color:#666}
.sr-grid .val{font-weight:500}
.day{margin-bottom:18px;border:1px solid #ccc;border-radius:4px;overflow:hidden;page-break-inside:avoid}
.day-hdr{background:#e8e8e8;padding:7px 12px;font-size:13px;font-weight:600;border-bottom:1px solid #ccc;display:flex;justify-content:space-between}
.day-body{padding:10px 12px}
.day-body p{margin:0 0 6px;font-size:12px}
.day-body .lbl{color:#666;font-size:11px}
.people{font-size:11px;margin-bottom:6px}
.media-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:8px;margin-top:8px}
.media-grid img{width:100%;max-height:220px;object-fit:contain;border:1px solid #ddd;border-radius:2px}
.media-grid .file-box{padding:14px;border:1px solid #ddd;background:#f9f9f9;text-align:center;font-size:10px;border-radius:2px}
.media-grid .cap{text-align:center;font-size:9px;color:#666;margin-top:2px}
@media print{body{padding:15px}.day{page-break-inside:avoid}}
</style></head><body>${e.innerHTML}</body></html>`),t.document.close(),t.focus(),setTimeout(()=>{t.print(),t.close()},400))},W=()=>{l(`filter`),r(``),a(``),s(``),v([]),m({}),t(!1)};return(0,P.jsx)(C,{open:e,onOpenChange:e=>{e||W()},children:(0,P.jsxs)(x,{className:c===`preview`?`sm:max-w-5xl max-h-[90vh] overflow-y-auto`:`sm:max-w-lg`,children:[(0,P.jsx)(y,{children:(0,P.jsx)(S,{children:c===`filter`?`Print Service Work Report`:`Report Preview`})}),c===`filter`&&(0,P.jsxs)(`div`,{className:`space-y-4 py-2`,children:[(0,P.jsx)(T,{label:`Service Request`,required:!0,value:n,onChange:e=>r(e.target.value),options:j.map(e=>({value:e.id,label:`${e.code} — ${e.client_name}`})),placeholder:`Select Service Request`}),(0,P.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,P.jsx)(w,{label:`From Date`,type:`date`,value:i,onChange:e=>a(e.target.value)}),(0,P.jsx)(w,{label:`To Date`,type:`date`,value:o,onChange:e=>s(e.target.value)})]}),n&&(0,P.jsxs)(`p`,{className:`text-sm text-muted-foreground`,children:[B.length,` service log(s) found.`]}),(0,P.jsxs)(b,{children:[(0,P.jsx)(g,{variant:`outline`,onClick:W,children:`Cancel`}),(0,P.jsx)(g,{onClick:V,disabled:!n||B.length===0||E,children:E?`Loading...`:`Generate Report`})]})]}),c===`preview`&&(0,P.jsxs)(`div`,{className:`space-y-4`,children:[(0,P.jsxs)(`div`,{className:`space-y-5`,children:[(0,P.jsxs)(`div`,{className:`bg-muted/30 rounded-lg p-4 text-sm space-y-1`,children:[(0,P.jsxs)(`p`,{className:`font-semibold text-base text-center`,children:[I?.code,` — `,I?.client_name]}),(0,P.jsxs)(`div`,{className:`grid grid-cols-2 gap-x-6 gap-y-1 mt-2`,children:[(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Branch:`}),` `,I?.branch_name]}),(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Service Type:`}),` `,I?.service_type]}),(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Location:`}),` `,I?.location_name||`—`]}),(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Period:`}),` `,I?.start_date,` to `,I?.end_date]})]})]}),_.map((e,t)=>{let n=F(e),r=(e.engineers||[]).map(e=>e.full_name||e.username),i=(e.technicians||[]).map(e=>e.full_name||e.username);return(0,P.jsxs)(`div`,{className:`border rounded-lg overflow-hidden`,children:[(0,P.jsxs)(`div`,{className:`bg-muted/50 px-4 py-2 flex justify-between items-center border-b`,children:[(0,P.jsxs)(`span`,{className:`font-semibold text-sm`,children:[`Day `,t+1,` — `,e.date]}),(0,P.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:e.code})]}),(0,P.jsxs)(`div`,{className:`px-4 py-3 space-y-2 text-sm`,children:[(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Work:`}),` `,e.work_description]}),r.length>0&&(0,P.jsxs)(`p`,{children:[(0,P.jsxs)(`span`,{className:`text-muted-foreground`,children:[`Engineers (`,r.length,`):`]}),` `,r.join(`, `)]}),i.length>0&&(0,P.jsxs)(`p`,{children:[(0,P.jsxs)(`span`,{className:`text-muted-foreground`,children:[`Technicians (`,i.length,`):`]}),` `,i.join(`, `)]}),e.labour_count>0&&(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Site Workers:`}),` `,e.labour_count]}),e.remarks&&(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`text-muted-foreground`,children:`Remarks:`}),` `,e.remarks]}),n.length>0&&(0,P.jsxs)(`div`,{className:`pt-2`,children:[(0,P.jsx)(`p`,{className:`text-xs text-muted-foreground mb-2`,children:`Select images/files to include in print:`}),(0,P.jsx)(`div`,{className:`grid grid-cols-3 md:grid-cols-4 gap-3`,children:n.map(t=>{let n=d[e.id]?.has(t.id)||!1;return(0,P.jsxs)(`label`,{className:`relative cursor-pointer rounded border overflow-hidden ${n?`ring-2 ring-primary`:`opacity-80 hover:opacity-100`}`,onClick:()=>H(e.id,t.id),children:[t.isImage?(0,P.jsx)(`img`,{src:t.file,alt:t.file_name,className:`w-full h-28 object-cover`}):(0,P.jsxs)(`div`,{className:`h-28 flex items-center justify-center bg-muted/20 text-xs text-muted-foreground px-2 text-center`,children:[`📎 `,t.file_name]}),(0,P.jsx)(`div`,{className:`absolute top-1.5 left-1.5`,children:(0,P.jsx)(p,{checked:n,className:`bg-white/90 border-gray-400`})}),(0,P.jsx)(`p`,{className:`text-[10px] truncate px-1 py-0.5 border-t`,children:t.file_name})]},t.id)})})]})]})]},e.id)})]}),(0,P.jsxs)(`div`,{ref:O,style:{position:`absolute`,left:-9999,top:0},children:[L&&(L.company_name||L.company_logo)&&(0,P.jsxs)(`div`,{className:`company-header`,children:[L.company_logo&&(0,P.jsx)(`img`,{src:L.company_logo,alt:`Company Logo`}),(0,P.jsxs)(`div`,{className:`company-info`,children:[L.company_name&&(0,P.jsx)(`div`,{className:`company-name`,children:L.company_name}),(0,P.jsxs)(`div`,{className:`company-details`,children:[[L.address,L.city_name,L.state_name,L.pincode].filter(Boolean).join(`, `),L.phone&&` | Phone: ${L.phone}`,L.email&&` | ${L.email}`,L.gstin&&` | GSTIN: ${L.gstin}`]})]})]}),(0,P.jsx)(`h1`,{children:`Service Work Report`}),(0,P.jsxs)(`p`,{className:`subtitle`,children:[I?.code,` — `,I?.client_name,i||o?` | ${i||`...`} to ${o||`...`}`:``]}),(0,P.jsxs)(`div`,{className:`sr-grid`,children:[(0,P.jsx)(`span`,{className:`lbl`,children:`Code:`}),(0,P.jsx)(`span`,{className:`val`,children:I?.code}),(0,P.jsx)(`span`,{className:`lbl`,children:`Client:`}),(0,P.jsx)(`span`,{className:`val`,children:I?.client_name}),(0,P.jsx)(`span`,{className:`lbl`,children:`Branch:`}),(0,P.jsx)(`span`,{className:`val`,children:I?.branch_name}),(0,P.jsx)(`span`,{className:`lbl`,children:`Service Type:`}),(0,P.jsx)(`span`,{className:`val`,children:I?.service_type}),(0,P.jsx)(`span`,{className:`lbl`,children:`Location:`}),(0,P.jsx)(`span`,{className:`val`,children:I?.location_name||`—`}),(0,P.jsx)(`span`,{className:`lbl`,children:`Period:`}),(0,P.jsxs)(`span`,{className:`val`,children:[I?.start_date,` to `,I?.end_date]})]}),I?.scope_of_work&&(0,P.jsxs)(`p`,{style:{marginBottom:12},children:[(0,P.jsx)(`span`,{className:`lbl`,children:`Scope of Work: `}),I.scope_of_work]}),_.map((e,t)=>{let n=F(e).filter(t=>d[e.id]?.has(t.id)),r=(e.engineers||[]).map(e=>e.full_name||e.username),i=(e.technicians||[]).map(e=>e.full_name||e.username);return(0,P.jsxs)(`div`,{className:`day`,children:[(0,P.jsxs)(`div`,{className:`day-hdr`,children:[(0,P.jsxs)(`span`,{children:[`Day `,t+1,` — `,e.date]}),(0,P.jsx)(`span`,{style:{fontWeight:400,fontSize:11},children:e.code})]}),(0,P.jsxs)(`div`,{className:`day-body`,children:[(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`lbl`,children:`Work: `}),e.work_description]}),r.length>0&&(0,P.jsxs)(`p`,{className:`people`,children:[(0,P.jsxs)(`span`,{className:`lbl`,children:[`Engineers (`,r.length,`): `]}),r.join(`, `)]}),i.length>0&&(0,P.jsxs)(`p`,{className:`people`,children:[(0,P.jsxs)(`span`,{className:`lbl`,children:[`Technicians (`,i.length,`): `]}),i.join(`, `)]}),e.labour_count>0&&(0,P.jsxs)(`p`,{className:`people`,children:[(0,P.jsx)(`span`,{className:`lbl`,children:`Site Workers: `}),e.labour_count]}),e.remarks&&(0,P.jsxs)(`p`,{children:[(0,P.jsx)(`span`,{className:`lbl`,children:`Remarks: `}),e.remarks]}),n.length>0&&(0,P.jsx)(`div`,{className:`media-grid`,children:n.map(e=>(0,P.jsxs)(`div`,{children:[e.isImage?(0,P.jsx)(`img`,{src:e.file,alt:e.file_name}):(0,P.jsxs)(`div`,{className:`file-box`,children:[`📎 `,e.file_name]}),(0,P.jsx)(`p`,{className:`cap`,children:e.file_name})]},e.id))})]})]},e.id)})]}),(0,P.jsxs)(b,{className:`gap-2`,children:[(0,P.jsx)(g,{variant:`outline`,onClick:()=>l(`filter`),children:`Back`}),(0,P.jsxs)(g,{onClick:U,children:[(0,P.jsx)(h,{className:`mr-1 h-4 w-4`}),` Print`]})]})]})]})})}var L=[{value:`1`,label:`Pending`},{value:`2`,label:`Approved`},{value:`3`,label:`Rejected`}];function R(){let e=m(),[t,r]=(0,N.useState)(``),[u,f]=(0,N.useState)(`all`),[p,w]=(0,N.useState)(`all`),[T,A]=(0,N.useState)(`all`),[F,R]=(0,N.useState)(!1),z={log_type:`service`};u!==`all`&&(z.service_request=u),p!==`all`&&(z.authorized_status=p),T!==`all`&&(z.branch=T),t&&(z.search=t);let{data:B=[],isLoading:V,refetch:H}=M(z),{data:U=[]}=k(),{data:W=[]}=d(),[G]=j(),[K,q]=(0,N.useState)(null),[J,Y]=(0,N.useState)(!1),X=async()=>{K&&(await G(K).unwrap().catch(()=>{}),q(null),H())},Z=()=>{f(`all`),w(`all`),A(`all`)},Q=[u,p,T].filter(e=>e!==`all`).length;return(0,P.jsxs)(_,{children:[(0,P.jsx)(E,{title:`Service Logs`,description:`Service work records for service requests`,searchPlaceholder:`Search by code, description...`,searchValue:t,onSearchChange:r,action:(0,P.jsxs)(`div`,{className:`flex gap-2`,children:[(0,P.jsxs)(g,{variant:`outline`,onClick:()=>Y(!0),children:[(0,P.jsx)(h,{className:`size-4 mr-2`}),` Print Report`]}),(0,P.jsxs)(g,{onClick:()=>e(`/service-logs/new`),children:[(0,P.jsx)(s,{className:`size-4 mr-2`}),` New Service Log`]})]}),filter:(0,P.jsxs)(g,{variant:`outline`,size:`sm`,onClick:()=>R(!0),className:`relative`,children:[(0,P.jsx)(o,{className:`mr-2 h-4 w-4`}),` Filters`,Q>0&&(0,P.jsx)(v,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:Q})]})}),(0,P.jsx)(a,{data:B,onRowClick:t=>e(`/service-logs/${t.id}`),columns:[{key:`code`,label:`Code`,render:e=>(0,P.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`service_request_code`,label:`Service Request`,render:e=>(0,P.jsx)(`span`,{className:`font-mono text-xs`,children:e.service_request_code})},{key:`client_name`,label:`Client`},{key:`date`,label:`Date`,render:e=>n(e.date)},{key:`engineer_count`,label:`Engineers`},{key:`technician_count`,label:`Technicians`},{key:`work_description`,label:`Description`,render:e=>(0,P.jsx)(`span`,{className:`truncate max-w-[200px] block`,children:e.work_description})},{key:`created_by_name`,label:`Created By`,render:e=>(0,P.jsx)(`span`,{className:`truncate block max-w-[120px]`,title:e.created_by_name||``,children:e.created_by_name||`—`})},{key:`authorized_status`,label:`Approval Status`,render:e=>(0,P.jsx)(D,{status:e.authorized_status})},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,P.jsx)(O,{onView:()=>e(`/service-logs/${t.id}`),onEdit:()=>e(`/service-logs/${t.id}/edit`),onDelete:()=>q(t.id),approval:{entityType:`DailyLog`,objectId:t.id,contentTypeId:i.DAILY_LOG,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>H()}})}],isLoading:V,rowKey:e=>e.id,emptyMessage:`No service logs found`}),(0,P.jsxs)(c,{open:F,onOpenChange:R,onClear:Z,children:[(0,P.jsx)(l,{label:`Service Request`,value:u,onChange:f,placeholder:`All Requests`,options:U.map(e=>({value:e.id,label:`${e.code} — ${e.client_name}`}))}),(0,P.jsx)(l,{label:`Auth Status`,value:p,onChange:w,placeholder:`All Status`,options:L}),(0,P.jsx)(l,{label:`Branch`,value:T,onChange:A,placeholder:`All Branches`,options:W.map(e=>({value:String(e.id),label:e.name}))})]}),(0,P.jsx)(C,{open:!!K,onOpenChange:e=>{e||q(null)},children:(0,P.jsxs)(x,{className:`sm:max-w-md`,children:[(0,P.jsx)(y,{children:(0,P.jsx)(S,{children:`Delete Service Log`})}),(0,P.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this service log?`}),(0,P.jsxs)(b,{children:[(0,P.jsx)(g,{variant:`outline`,size:`sm`,onClick:()=>q(null),children:`Cancel`}),(0,P.jsx)(g,{variant:`destructive`,size:`sm`,onClick:X,children:`Delete`})]})]})}),(0,P.jsx)(I,{open:J,onOpenChange:Y})]})}export{R as default};