1import{r as m,b as de,j as e,s as f,f as W,B as C,e as $,a5 as ce,d as me}from"./index-CJAQG0mx.js";import{u as ue}from"./useAdminAuth-BsozdsBx.js";import{T as pe,a as he,b as L,c as R}from"./tabs-D9T7aGSY.js";import{S as z,a as B,b as H,c as J,d as q}from"./select-oesuobRU.js";import{C as I,a as F,b as A,c as P}from"./card-BmKXugNV.js";import{T as K}from"./triangle-alert-Dy8t86oZ.js";import{C as V}from"./circle-check-DSxh95sS.js";import{F as xe}from"./file-text-WfGF3to2.js";import{C as Y}from"./checkbox-BRcB7zSO.js";import{B as v}from"./badge-C6vkf5dU.js";import{D as X,a as Z,b as ee,c as te,d as se}from"./dialog-BTb2S04g.js";import{R as re,a as G}from"./radio-group-CZ_NEkoa.js";import{L as y}from"./label-DugJCyav.js";import{F as ge}from"./file-spreadsheet-VLzdGoQj.js";import{G as je}from"./gem-CqGkEPYM.js";import{I as U}from"./input-sKAvpFBT.js";import{T as fe}from"./textarea-FB0AUn4o.js";import{D as ae}from"./download-BQKmuj_m.js";import{C as _e}from"./circle-check-big-mWuUGSpb.js";import{T as Ne}from"./trash-2-BXRBuvYt.js";import"./index-CJjE5PV9.js";import"./index-BdQq_4o_.js";import"./check-Cfj_Q-47.js";import"./circle-Yh_3jUEz.js";const be=({year:n,month:o})=>{const[l,t]=m.useState({total:0,unreported:0,reported:0,reportStatus:"none"});m.useEffect(()=>{(async()=>{const u=`${n}-${String(o).padStart(2,"0")}-01`,c=o===12?`${n+1}-01-01`:`${n}-${String(o+1).padStart(2,"0")}-01`,{data:p}=await f.from("purchase_orders").select("id, order_number").in("status",["payout_completed","completed"]).gte("created_at",u).lt("created_at",c),x=(p==null?void 0:p.length)||0,{data:g}=await f.from("police_report_items").select("purchase_order_id"),N=new Set((g||[]).map(d=>d.purchase_order_id)),a=(p==null?void 0:p.filter(d=>!N.has(d.id)).length)||0,s=x-a,{data:_}=await f.from("police_reports").select("status").eq("report_period_year",n).eq("report_period_month",o).order("created_at",{ascending:!1}).limit(1);let h="none";if(_&&_.length>0){const d=_[0].status;d==="submitted"||d==="confirmed"?h="submitted":d==="generated"&&(h="generated")}t({total:x,unreported:a,reported:s,reportStatus:h})})()},[n,o]);const i=[{label:"Totalt inköp denna period",value:l.total,icon:de,color:"text-foreground"},{label:"Ej rapporterade",value:l.unreported,icon:K,color:l.unreported>0?"text-yellow-600":"text-muted-foreground",bg:l.unreported>0?"bg-yellow-50 border-yellow-200":""},{label:"Rapporterade",value:l.reported,icon:V,color:"text-green-600"},{label:"Rapportstatus",value:l.reportStatus==="submitted"?"â
Inskickad":l.reportStatus==="generated"?"ð¡ Genererad":"⬠Ej genererad",icon:xe,color:l.reportStatus==="submitted"?"text-green-600":l.reportStatus==="generated"?"text-yellow-600":"text-destructive",isText:!0}];return e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:i.map(r=>e.jsx(I,{className:`border ${r.bg||""}`,children:e.jsxs(F,{className:"p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx("span",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wide",children:r.label}),e.jsx(r.icon,{className:`h-4 w-4 ${r.color}`})]}),e.jsx("p",{className:`text-2xl font-bold ${r.color}`,children:(r.isText,r.value)})]})},r.label))})},ve=({open:n,onClose:o,year:l,month:t,periodLabel:i,transactions:r,selectedIds:u})=>{const{profile:c}=W(),[p,x]=m.useState("all"),[g,N]=m.useState(!1),a=p==="all"?r:r.filter(h=>u.has(h.id)),s=a.reduce((h,d)=>h+(d.payout_amount||d.final_value||0),0),_=async()=>{if(a.length===0){$.error("Inga transaktioner att inkludera");return}N(!0);try{const h=c?`${c.first_name||""} ${c.last_name||""}`.trim():"Admin",{data:d,error:S}=await f.from("police_reports").insert({report_period_year:l,report_period_month:t,status:"generated",generated_at:new Date().toISOString(),generated_by:h,transaction_count:a.length,total_amount:s}).select("id").single();if(S)throw S;const D=a.map(E=>({police_report_id:d.id,purchase_order_id:E.id,purchase_order_number:E.order_number})),{error:w}=await f.from("police_report_items").insert(D);if(w)throw w;try{await f.functions.invoke("generate-police-report",{body:{year:l,month:t,report_id:d.id}})}catch(E){console.warn("Excel generation failed, report created without file",E)}$.success(`Polisrapport genererad för ${i} med ${a.length} transaktioner`),o(),window.location.reload()}catch(h){$.error("Fel vid generering: "+(h.message||"Försök igen"))}finally{N(!1)}};return e.jsx(X,{open:n,onOpenChange:o,children:e.jsxs(Z,{className:"max-w-md",children:[e.jsx(ee,{children:e.jsx(te,{children:"Generera polisrapport"})}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-2 text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"Period:"}),e.jsx("span",{className:"font-medium",children:i}),e.jsx("span",{className:"text-muted-foreground",children:"Antal transaktioner:"}),e.jsx("span",{className:"font-medium",children:a.length}),e.jsx("span",{className:"text-muted-foreground",children:"Totalt utbetalt:"}),e.jsxs("span",{className:"font-medium",children:[s.toLocaleStr
1ing("sv-SE")," SEK"]})]}),e.jsx("div",{className:"border-t pt-4",children:e.jsxs(re,{value:p,onValueChange:h=>x(h),children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(G,{value:"all",id:"all"}),e.jsxs(y,{htmlFor:"all",children:["Inkludera alla orapporterade (",r.length,")"]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(G,{value:"selected",id:"selected",disabled:u.size===0}),e.jsxs(y,{htmlFor:"selected",className:u.size===0?"text-muted-foreground":"",children:["Bara markerade (",u.size," av ",r.length,")"]})]})]})})]}),e.jsxs(se,{children:[e.jsx(C,{variant:"outline",onClick:o,children:"Avbryt"}),e.jsx(C,{onClick:_,disabled:g||a.length===0,children:g?"Genererar...":"Generera rapport"})]})]})})},Se=({year:n,month:o,periodLabel:l})=>{const[t,i]=m.useState([]),[r,u]=m.useState(new Set),[c,p]=m.useState(!0),[x,g]=m.useState(!1);m.useEffect(()=>{(async()=>{p(!0);const _=`${n}-${String(o).padStart(2,"0")}-01`,h=o===12?`${n+1}-01-01`:`${n}-${String(o+1).padStart(2,"0")}-01`,{data:d}=await f.from("purchase_orders").select("id, order_number, created_at, customer_id, customer_number, payout_amount, final_value, status").in("status",["payout_completed","completed"]).gte("created_at",_).lt("created_at",h).order("created_at",{ascending:!1});if(!d||d.length===0){i([]),p(!1);return}const{data:S}=await f.from("police_report_items").select("purchase_order_id"),D=new Set((S||[]).map(b=>b.purchase_order_id)),w=d.filter(b=>!D.has(b.id)),E=[...new Set(w.map(b=>b.customer_id))],j=w.map(b=>b.id),[M,T]=await Promise.all([f.from("profiles").select("user_id, first_name, last_name, phone, personal_number, id_verification_method").in("user_id",E),f.from("gemstones").select("source_purchase_order_id").in("source_purchase_order_id",j)]),ne=M.data,oe=T.data||[],O=new Map;oe.forEach(b=>{b.source_purchase_order_id&&O.set(b.source_purchase_order_id,(O.get(b.source_purchase_order_id)||0)+1)});const le=new Map((ne||[]).map(b=>[b.user_id,b])),ie=w.map(b=>{const k=le.get(b.customer_id);return{...b,customerName:k?`${k.first_name||""} ${k.last_name||""}`.trim():"Okänd",hasPersonnummer:!!(k!=null&&k.personal_number),idVerificationMethod:(k==null?void 0:k.id_verification_method)||null,gemstoneCount:O.get(b.id)||0}});i(ie),p(!1)})()},[n,o]);const N=s=>{u(_=>{const h=new Set(_);return h.has(s)?h.delete(s):h.add(s),h})},a=()=>{r.size===t.length?u(new Set):u(new Set(t.map(s=>s.id)))};return c?e.jsx("div",{className:"p-6 text-muted-foreground",children:"Laddar transaktioner..."}):e.jsxs(I,{children:[e.jsxs(A,{className:"flex flex-row items-center justify-between",children:[e.jsx(P,{className:"text-lg",children:"Inköp som ännu inte rapporterats"}),e.jsxs(C,{onClick:()=>g(!0),disabled:t.length===0,className:"gap-2",children:[e.jsx(ge,{className:"h-4 w-4"}),"Generera polisrapport för ",l]})]}),e.jsx(F,{children:t.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-4",children:"Inga orapporterade inköp för denna period."}):e.jsxs(e.Fragment,{children:[t.some(s=>!s.hasPersonnummer)&&e.jsxs("div",{className:"mb-4 p-3 bg-yellow-50 border border-yellow-200 rounded-sm flex items-start gap-2",children:[e.jsx(K,{className:"h-4 w-4 text-yellow-600 mt-0.5 shrink-0"}),e.jsxs("div",{className:"text-sm text-yellow-800",children:[e.jsx("strong",{children:"Varning:"})," Personnummer saknas för vissa kunder. Personnummer krävs för polisrapportering."]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-left text-muted-foreground",children:[e.jsx("th",{className:"p-2 w-10",children:e.jsx(Y,{checked:r.size===t.length&&t.length>0,onCheckedChange:a})}),e.jsx("th",{className:"p-2",children:"GK-nummer"}),e.jsx("th",{className:"p-2",children:"Datum"}),e.jsx("th",{className:"p-2",children:"Kund"}),e.jsx("th",{className:"p-2",children:"Kundnr"}),e.jsx("th",{className:"p-2 text-right",children:"Utbetalt"}),e.jsx("th",{className:"p-2",children:"Stenar"}),e.jsx("th",{className:"p-2",children:"ID verifierat"})]})}),e.jsx("tbody",{children:t.map(s=>e.jsxs("tr",{className:"border-b hover:bg-muted/30",children:[e.jsx("td",{className:"p-2",children:e.jsx(Y,{checked:r.has(s.id),onCheckedChange:()=>N(s.id)})}),e.jsx("td",{className:"p-2 font-mono font-medium",children:s.order_number}),e.jsx("td",{className:"p-2",children:new Date(s.created_at).toLocaleDateString("sv-SE")}),e.jsx("td",{className:"p-2",children:s.customerName}),e.jsx("td",{className:"p-2 font-mono",children:s.customer_number}),e.jsxs("td",{className:"p-2 text-right font-medium",children:[(s.payout_amount||s.final_value||0).toLocaleStr
1ing("sv-SE")," SEK"]}),e.jsx("td",{className:"p-2",children:s.gemstoneCount>0&&e.jsxs(v,{variant:"outline",className:"text-xs gap-1 text-amber-700 border-amber-300 bg-amber-50",children:[e.jsx(je,{className:"h-3 w-3"})," ",s.gemstoneCount]})}),e.jsx("td",{className:"p-2",children:s.hasPersonnummer?s.idVerificationMethod==="bankid"?e.jsxs(v,{variant:"outline",className:"text-green-700 border-green-300 bg-green-50",children:[e.jsx(V,{className:"h-3 w-3 mr-1"})," BankID"]}):e.jsx(v,{variant:"outline",className:"text-blue-700 border-blue-300 bg-blue-50",children:"Manuellt inlagt"}):e.jsxs(v,{variant:"outline",className:"text-yellow-700 border-yellow-300 bg-yellow-50",children:[e.jsx(K,{className:"h-3 w-3 mr-1"})," Saknas"]})})]},s.id))})]})}),e.jsxs("div",{className:"mt-4 flex items-center justify-between text-sm text-muted-foreground",children:[e.jsxs("span",{children:[r.size," av ",t.length," valda"]}),e.jsxs("span",{children:["Totalt: ",t.reduce((s,_)=>s+(_.payout_amount||_.final_value||0),0).toLocaleString("sv-SE")," SEK"]})]})]})}),x&&e.jsx(ve,{open:x,onClose:()=>g(!1),year:n,month:o,periodLabel:l,transactions:t,selectedIds:r})]})}
1,we=({year:n,month:o})=>{const[l,t]=m.useState([]),[i,r]=m.useState(!0);return m.useEffect(()=>{(async()=>{r(!0);const c=`${n}-${String(o).padStart(2,"0")}-01`,p=o===12?`${n+1}-01-01`:`${n}-${String(o+1).padStart(2,"0")}-01`,{data:x}=await f.from("purchase_orders").select("id, order_number, created_at, customer_id, customer_number, payout_amount, final_value").in("status",["payout_completed","completed"]).gte("created_at",c).lt("created_at",p);if(!x||x.length===0){t([]),r(!1);return}const{data:g}=await f.from("police_report_items").select("purchase_order_id, police_report_id"),N=new Set((g||[]).map(j=>j.purchase_order_id)),a=x.filter(j=>N.has(j.id));if(a.length===0){t([]),r(!1);return}const s=[...new Set(a.map(j=>j.customer_id))],{data:_}=await f.from("profiles").select("user_id, first_name, last_name").in("user_id",s),h=new Map((_||[]).map(j=>[j.user_id,j])),d=[...new Set((g||[]).map(j=>j.police_report_id))],{data:S}=await f.from("police_reports").select("id, report_period_year, report_period_month, status").in("id",d),D=new Map((S||[]).map(j=>[j.id,j])),w=new Map;(g||[]).forEach(j=>{w.set(j.purchase_order_id,D.get(j.police_report_id))});const E=a.map(j=>{const M=h.get(j.customer_id),T=w.get(j.id);return{order_number:j.order_number,created_at:j.created_at,customerName:M?`${M.first_name||""} ${M.last_name||""}`.trim():"Okänd",amount:j.payout_amount||j.final_value||0,reportPeriod:T?`${T.report_period_year}-${String(T.report_period_month).padStart(2,"0")}`:"â",reportStatus:(T==null?void 0:T.status)||"unknown"}});t(E),r(!1)})()},[n,o]),i?e.jsx("div",{className:"p-6 text-muted-foreground",children:"Laddar..."}):e.jsxs(I,{children:[e.jsx(A,{children:e.jsx(P,{className:"text-lg",children:"Redan rapporterade inköp"})}),e.jsx(F,{children:l.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-4",children:"Inga rapporterade inköp för denna period."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-left text-muted-foreground",children:[e.jsx("th",{className:"p-2",children:"GK-nummer"}),e.jsx("th",{className:"p-2",children:"Datum"}),e.jsx("th",{className:"p-2",children:"Kund"}),e.jsx("th",{className:"p-2 text-right",children:"Utbetalt"}),e.jsx("th",{className:"p-2",children:"Rapportperiod"}),e.jsx("th",{className:"p-2",children:"Status"})]})}),e.jsx("tbody",{children:l.map(u=>e.jsxs("tr",{className:"border-b bg-green-50/50",children:[e.jsx("td",{className:"p-2 font-mono font-medium",children:u.order_number}),e.jsx("td",{className:"p-2",children:new Date(u.created_at).toLocaleDateString("sv-SE")}),e.jsx("td",{className:"p-2",children:u.customerName}),e.jsxs("td",{className:"p-2 text-right font-medium",children:[u.amount.toLocaleString("sv-SE")," SEK"]}),e.jsx("td",{className:"p-2 font-mono",children:u.reportPeriod}),e.jsx("td",{className:"p-2",children:e.jsxs(v,{variant:"outline",className:"text-green-700 border-green-300 bg-green-50",children:[e.jsx(V,{className:"h-3 w-3 mr-1"}),u.reportStatus==="submitted"?"Inskickad":"Genererad"]})})]},u.order_number))})]})})})]})},ke=({open:n,onClose:o,reportId:l,periodLabel:t})=>{const{profile:i}=W(),[r,u]=m.useState("email"),[c,p]=m.useState(""),[x,g]=m.useState(new Date().toISOString().split("T")[0]),[N,a]=m.useState(""),[s,_]=m.useState(!1),h=async()=>{var d;_(!0);try{const S=i?`${i.first_name||""} ${i.last_name||""}`.trim():"Admin",{error:D}=await f.from("police_reports").update({status:"submitted",submitted_at:new Date(x).toISOString(),submitted_by:S,submission_method:r,submission_reference:c||null,notes:N||null}).eq("id",l);if(D)throw D;await f.from("admin_activity_log").insert({admin_user_id:((d=(await f.auth.getUser()).data.user)==null?void 0:d.id)||"",action_type:"police_report_submitted",description:`Polisrapport för ${t} markerad som inskickad via ${r}`,target_table:"police_reports",target_record_id:l}),$.success("Rapporten markerad som inskickad"),o()}catch(S){$.error("Fel: "+(S.message||"Försök igen"))}finally{_(!1)}};return e.jsx(X,{open:n,onOpenChange:o,children:e.jsxs(Z,{className:"max-w-md",children:[e.jsx(ee,{children:e.jsx(te,{children:"Bekräfta inskickning"})}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{children:[e.jsx(y,{className:"text-sm font-medium mb-2 block",children:"Hur skickades rapporten?"}),e.jsxs(re,{value:r,onValueChange:u,children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(G,{value:"email",id:"email"}),e.jsx(y,{htmlFor:"email",children:"Via e-post till Polismyndigheten"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(G,{value:"portal",id:"portal"}),e.jsx(y,{htmlFor:"portal",children:"Via Polisens portal/webbplats"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(G,{value:"physical",id:"physical"}),e.jsx(y,{htmlFor:"physical",children:"Fysiskt/utskrivet"})]})]})]}),e.jsxs("div",{children:[e.jsx(y,{htmlFor:"ref",className:"text-sm",children:"Ãrendenummer frÃ¥n Polisen (valfritt)"}),e.jsx(U,{id:"ref",value:c,onChange:d=>p(d.target.value),placeholder:"t.ex. P-2026-12345"})]}),e.jsxs("div",{children:[e.jsx(y,{htmlFor:"date",className:"text-sm",children:"Datum för inskickning"}),e.jsx(U,{id:"date",type:"date",value:x,onChange:d=>g(d.target.value)})]}),e.jsxs("div",{children:[e.jsx(y,{htmlFor:"notes",className:"text-sm",children:"Notering (valfritt)"}),e.jsx(fe,{id:"notes",value:N,onChange:d=>
1a(d.target.value),rows:2})]})]}),e.jsxs(se,{children:[e.jsx(C,{variant:"outline",onClick:o,children:"Avbryt"}),e.jsx(C,{onClick:h,disabled:s,children:s?"Sparar...":"Bekräfta inskickning"})]})]})})},ye=({year:n,month:o,periodLabel:l})=>{const[t,i]=m.useState(null),[r,u]=m.useState(!0),[c,p]=m.useState(!1),x=async()=>{u(!0);const{data:a}=await f.from("police_reports").select("*").eq("report_period_year",n).eq("report_period_month",o).order("created_at",{ascending:!1}).limit(1);i(a&&a.length>0?a[0]:null),u(!1)};if(m.useEffect(()=>{x()},[n,o]),r)return e.jsx("div",{className:"p-6 text-muted-foreground",children:"Laddar..."});if(!t)return e.jsx(I,{children:e.jsxs(F,{className:"py-12 text-center",children:[e.jsxs("p",{className:"text-muted-foreground",children:["Ingen rapport har genererats för ",l," ännu."]}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:'GÃ¥ till fliken "Ej rapporterade" för att generera en rapport.'})]})});const g=()=>{switch(t.status){case"submitted":case"confirmed":return e.jsx(v,{className:"bg-green-100 text-green-800 border-green-300",children:"â
Inskickad"});case"generated":return e.jsx(v,{className:"bg-yellow-100 text-yellow-800 border-yellow-300",children:"ð¡ Genererad (ej inskickad)"});default:return e.jsx(v,{variant:"outline",children:t.status})}},N=async()=>{if(confirm("Ãr du säker pÃ¥ att du vill ta bort denna rapport?")){if(t.status==="submitted"){$.error("Kan inte ta bort en inskickad rapport");return}await f.from("police_report_items").delete().eq("police_report_id",t.id),await f.from("police_reports").delete().eq("id",t.id),$.success("Rapport borttagen"),x()}};return e.jsxs(e.Fragment,{children:[e.jsxs(I,{children:[e.jsx(A,{children:e.jsxs(P,{className:"text-lg flex items-center gap-2",children:["ð Polisrapport â ",l]})}),e.jsxs(F,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3 text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"Status:"}),e.jsx("div",{children:g()}),e.jsx("span",{className:"text-muted-foreground",children:"Genererad:"}),e.jsxs("span",{children:[new Date(t.generated_at).toLocaleString("sv-SE")," av ",t.generated_by]}),e.jsx("span",{className:"text-muted-foreground",children:"Transaktioner:"}),e.jsx("span",{className:"font-medium",children:t.transaction_count}),e.jsx("span",{className:"text-muted-foreground",children:"Totalt utbetalt:"}),e.jsxs("span",{className:"font-medium",children:[t.total_amount.toLocaleString("sv-SE")," SEK"]}),t.submitted_at&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-muted-foreground",children:"Inskickad:"}),e.jsxs("span",{children:[new Date(t.submitted_at).toLocaleString("sv-SE")," av ",t.submitted_by]})]}),t.submission_method&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-muted-foreground",children:"Inskickningsmetod:"}),e.jsx("span",{className:"capitalize",children:t.submission_method})]}),t.submission_reference&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-muted-foreground",children:"Ãrendenummer:"}),e.jsx("span",{className:"font-mono",children:t.submission_reference})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 pt-4 border-t",children:[t.file_url&&e.jsx(C,{variant:"outline",size:"sm",asChild:!0,children:e.jsxs("a",{href:t.file_url,download:t.file_name||"rapport.xlsx",children:[e.jsx(ae,{className:"h-4 w-4 mr-1"})," Ladda ner Excel"]})}),t.status==="generated"&&e.jsxs(e.Fragment,{children:[e.jsxs(C,{size:"sm",onClick:()=>p(!0),className:"gap-1",children:[e.jsx(_e,{className:"h-4 w-4"})," Markera som inskickad"]}),e.jsxs(C,{variant:"outline",size:"sm",onClick:N,className:"gap-1 text-destructive",children:[e.jsx(Ne,{className:"h-4 w-4"})," Ta bort rapport"]})]})]})]})]}),c&&e.jsx(ke,{open:c,onClose:()=>{p(!1),x()},reportId:t.id,periodLabel:l})]})},Ce=["Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep","Okt","Nov","Dec"],De=()=>{const[n,o]=m.useState([]),[l,t]=m.useState(!0);return m.useEffect(()=>{(async()=>{const{data:r}=await f.from("police_reports").select("*").order("report_period_year",{ascending:!1}).order("report_period_month",{ascending:!1});o(r||[]),t(!1)})()},[]),l?e.jsx("div",{className:"p-6 text-muted-foreground",children:"Laddar..."}):e.jsxs(I,{children:[e.jsx(A,{children:e.jsx(P,{className:"text-lg",children:"Historik â Tidigare rapporter"})}),e.jsx(F,{children:n.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-4",children:"Inga rapporter har genererats ännu."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-left text-muted-foreground",children:[e.jsx("th",{className:"p-2",children:"Period"}),e.jsx("th",{className:"p-2",children:"Transaktioner"}),e.jsx("th",{className:"p-2 text-right",children:"Totalt utbetalt"}),e.jsx("th",{className:"p-2",children:"Genererad"}),e.jsx("th",{className:"p-2",children:"Inskickad"}),e.jsx("th",{className:"p-2",children:"Status"}),e.jsx("th",{className:"p-2",children:"Fil"})]})}),e.jsx("tbody",{children:n.map(i=>e.jsxs("tr",{className:"border-b hover:bg-muted/30",children:[e.jsxs("td",{className:"p-2 font-medium",children:[Ce[i.report_period_month-1]," ",i.report_period_year]}),e.jsx("td",{className:"p-2",children:i.transaction_count}),e.jsxs("td",{className:"p-2 text-right font-medium",children:[i.total_amount.toLocaleStr
1ing("sv-SE")," SEK"]}),e.jsx("td",{className:"p-2",children:new Date(i.generated_at).toLocaleDateString("sv-SE")}),e.jsx("td",{className:"p-2",children:i.submitted_at?new Date(i.submitted_at).toLocaleDateString("sv-SE"):"â"}),e.jsx("td",{className:"p-2",children:i.status==="submitted"||i.status==="confirmed"?e.jsx(v,{className:"bg-green-100 text-green-800 border-green-300",children:"â
Inskickad"}):e.jsx(v,{className:"bg-yellow-100 text-yellow-800 border-yellow-300",children:"ð¡ Genererad"})}),e.jsx("td",{className:"p-2",children:i.file_url?e.jsx(C,{variant:"ghost",size:"sm",asChild:!0,children:e.jsx("a",{href:i.file_url,download:i.file_name,children:e.jsx(ae,{className:"h-4 w-4"})})}):"â"})]},i.id))})]})})})]})},Ee=["Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December"],Te=({year:n})=>{const[o,l]=m.useState([]),[t,i]=m.useState(!0);if(m.useEffect(()=>{(async()=>{i(!0);const{data:p}=await f.from("purchase_orders").select("id, created_at, payout_amount, final_value").in("status",["payout_completed","completed"]).gte("created_at",`${n}-01-01`).lt("created_at",`${n+1}-01-01`),{data:x}=await f.from("police_reports").select("report_period_month, status, transaction_count, total_amount").eq("report_period_year",n),g=new Map;(x||[]).forEach(a=>{(!g.get(a.report_period_month)||a.status==="submitted")&&g.set(a.report_period_month,a)});const N=[];for(let a=1;a<=12;a++){const s=(p||[]).filter(h=>new Date(h.created_at).getMonth()+1===a),_=g.get(a);N.push({month:a,transactions:s.length,amount:s.reduce((h,d)=>h+(d.payout_amount||d.final_value||0),0),status:_?_.status==="submitted"||_.status==="confirmed"?"submitted":"generated":"none"})}l(N),i(!1)})()},[n]),t)return e.jsx("div",{className:"p-6 text-muted-foreground",children:"Laddar..."});const r=o.reduce((c,p)=>c+p.transactions,0),u=o.reduce((c,p)=>c+p.amount,0);return e.jsxs(I,{children:[e.jsx(A,{children:e.jsxs(P,{className:"text-lg",children:["Ã
rsöversikt ",n]})}),e.jsx(F,{children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-left text-muted-foreground",children:[e.jsx("th",{className:"p-2",children:"MÃ¥nad"}),e.jsx("th",{className:"p-2 text-right",children:"Transaktioner"}),e.jsx("th",{className:"p-2 text-right",children:"Utbetalt"}),e.jsx("th",{className:"p-2",children:"Rapportstatus"})]})}),e.jsxs("tbody",{children:[o.map(c=>e.jsxs("tr",{className:"border-b hover:bg-muted/30",children:[e.jsx("td",{className:"p-2 font-medium",children:Ee[c.month-1]}),e.jsx("td",{className:"p-2 text-right",children:c.transactions}),e.jsx("td",{className:"p-2 text-right font-medium",children:c.amount>0?`${c.amount.toLocaleString("sv-SE")} SEK`:"â"}),e.jsx("td",{className:"p-2",children:c.transactions===0?e.jsx("span",{className:"text-muted-foreground",children:"â"}):c.status==="submitted"?e.jsx(v,{className:"bg-green-100 text-green-800 border-green-300",children:"â
Inskickad"}):c.status==="generated"?e.jsx(v,{className:"bg-yellow-100 text-yellow-800 border-yellow-300",children:"ð¡ Genererad"}):e.jsx(v,{variant:"outline",className:"text-muted-foreground",children:"⬠Ej rapporterad"})})]},c.month)),e.jsxs("tr",{className:"border-t-2 font-bold",children:[e.jsxs("td",{className:"p-2",children:["Totalt ",n]}),e.jsx("td",{className:"p-2 text-right",children:r}),e.jsxs("td",{className:"p-2 text-right",children:[u.toLocaleString("sv-SE")," SEK"]}),e.jsx("td",{className:"p-2"})]})]})]})})})]})},Q=["Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December"],tt=()=>{const{isAdmin:n,loading:o}=ue(),l=new Date,[t,i]=m.useState(l.getFullYear().toString()),[r,u]=m.useState((l.getMonth()+1).toString()),[c,p]=m.useState("overview");if(o)return e.jsx("div",{className:"p-8 text-muted-foreground",children:"Laddar..."});if(!n)return e.jsx(ce,{to:"/admin",replace:!0});const x=parseInt(t),g=parseInt(r),N=`${Q[g-1]} ${x}`;return e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(me,{className:"h-6 w-6 text-primary"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"Polisrapportering"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Register över inköp av begagnade ädelmetaller (Lag 1999:271)"})]})]})}),e.jsxs("div",{className:"flex items-center gap-3 p-4 bg-card border border-border rounded-sm",children:[e.jsx("span",{className:"text-sm font-medium text-muted-foreground",children:"Period:"}),e.jsxs(z,{value:t,onValueChange:i,children:[e.jsx(B,{className:"w-[100px]",children:e.jsx(H,{})}),e.jsx(J,{children:[l.getFullYear()-1,l.getFullYear(),l.getFullYear()+1].map(a=>e.jsx(q,{value:a.toString(),children:a},a))})]}),e.jsxs(z,{value:r,onValueChange:u,children:[e.jsx(B,{className:"w-[140px]",children:e.jsx(H,{})}),e.jsx(J,{children:Q.map((a,s)=>e.jsx(q,{value:(s+1).toString(),children:a},s))})]})]}),e.jsx(be,{year:x,month:g}),e.jsxs(pe,{value:c,onValueChange:p,children:[e.jsxs(he,{className:"bg-muted/50 p-1",children:[e.jsx(L,{value:"overview",children:"Ej rapporterade"}),e.jsx(L,{value:"reported",children:"Rapporterade"}),e.jsx(L,{value:"report",children:"Rapport"}),e.jsx(L,{value:"history",children:"Historik"}),e.jsx(L,{value:"yearly",children:"Ã
rsöversikt"})]}),e.jsx(R,{value:"overview",children:e.jsx(Se,{year:x,month:g,periodLabel:N})}),e.jsx(R,{value:"reported",children:e.jsx(we,{year:x,month:g})}),e.jsx(R,{value:"report",children:e.jsx(ye,{year:x,month:g,periodLabel:N})}),e.jsx(R,{value:"history",children:e.jsx(De,{})}),e.jsx(R,{value:"yearly",children:e.jsx(Te,{year:x})})]})]})};export{tt 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.