1import{bJ as e,W as a,K as s,F as t,b7 as n,bY as l,J as r,b$ as i,bg as d,ct as o,ar as c,d$ as m}from"./CmVwsSB4.js";import{ah as h,o as g,bC as u,L as x,bE as j,ae as p,r as y,s as _,w as v,x as b,y as f,a6 as C,a7 as w,a8 as N,ab as k,a_ as S,ad as M,c as T,F as V,E as L,B as P,a1 as A,d3 as q,a2 as E}from"./Bku_ZNU5.js";import{o as K,g as U,s as F,e as I,Z as R,u as D}from"./CSc5fXZY.js";import{M as O}from"./Dj8EE-rX.js";import{M as Y}from"./CmYxbt8O.js";import{S as $}from"./fqwj5BFo.js";import{S as z}from"./DyeuM7Mg.js";import"./DiXt0M7N.js";import"./Dvdu02qO.js";import"./P1FsCZW1.js";import"./KH3gTha8.js";import"./B-a-68O8.js";import"./y2jpHdmr.js";import"./DZmZcIhl.js";const B=async e=>(await h.post("/v1/migrator/company",e)).data,G=({disabled:e,value:s,onChange:n,placeholder:l="Search...",maxSelectedLabels:r,label:i="Users",className:d,fetchKey:o="system"})=>{const{data:c,isLoading:m}=u({key:o}),h=t.useMemo(()=>{var e;return((null==(e=null==c?void 0:c.data)?void 0:e.users)??[]).map(e=>({label:e.name||e.email||e.user_id,value:e.user_id,showLabel:e.name||e.email||e.user_id}))},[c]);return a.jsxs("div",{className:"grid gap-2",children:[i&&a.jsx(x,{children:i}),a.jsx(j,{options:h,value:s??[],onChange:n,placeholder:l,disabled:e,maxSelectedLabels:r,isLoading:m,noResultsMessage:"User not found!",className:d,triggerLabel:"Users"})]})},J=K({source_tenant_id:F().min(1,"Source tenant is required"),source_company_id:F().min(1,"Source company is required"),target_tenant_id:F().min(1,"Target tenant is required"),target_company_id:F().default(""),migration_method:I(["existing","new"]).default("existing"),driver_mode:I(["all","selected"]).default("all"),user_mode:I(["all","selected"]).default("all"),vehicle_mode:I(["all","selected"]).default("all"),drivers:U(F()).default([]),users:U(F()).default([]),vehicles:U(F()).default([])}).superRefine((e,a)=>{"existing"!==e.migration_method||e.target_company_id||a.addIssue({code:R.custom,message:"Target company is required",path:["target_company_id"]})});function Q({title:e,mode:s,onModeChange:t,selectedCount:n,children:l}){return a.jsxs("div",{className:"space-y-2 rounded-md bg-gray-100 p-3 dark:bg-gray-900",children:[a.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[a.jsx("h4",{className:"font-bold text-gray-900 dark:text-gray-100",children:e}),a.jsxs("div",{className:"flex min-w-65 items-center rounded-md bg-gray-200 p-1 dark:bg-gray-800",children:[a.jsx("button",{type:"button",onClick:()=>t("all"),className:T("h-7 flex-1 rounded-sm px-2 text-center text-sm font-semibold","all"===s?"bg-background text-gray-900 shadow-xs dark:text-gray-100":"text-gray-500"),children:"All"}),a.jsxs("button",{type:"button",onClick:()=>t("selected"),className:T("h-7 flex-1 rounded-sm px-2 text-center text-sm font-semibold","selected"===s?"bg-background text-gray-900 shadow-xs dark:text-gray-100":"text-gray-500 dark:text-gray-300"),children:["Selected (",n,")"]})]})]}),"selected"===s&&l]})}function W({children:e}){var s,d,o;const[c,m]=t.useState(!1),h="48e12894-d3f4-440f-af04-25f743dc4a44",g=t.useMemo(()=>({source_tenant_id:h,source_company_id:"",target_tenant_id:"",target_company_id:"",migration_method:"existing",driver_mode:"all",user_mode:"all",vehicle_mode:"all",drivers:[],users:[],vehicles:[]}),[h]),u=i({resolver:D(J),defaultValues:g}),{mutate:x,isPending:j}=(()=>{const e=n();return l({mutationKey:["create-migration-company"],mutationFn:B,onSuccess:()=>{e.invalidateQueries({predicate:e=>"migration-history"===e.queryKey[0]}),r.success("Migration started successfully!")},onError:e=>{r.error(p(e,"Failed to start migration"))}})})(),A=u.watch("source_tenant_id"),q=u.watch("source_company_id"),E=u.watch("target_tenant_id"),K=u.watch("migration_method"),U=(null==(s=u.watch("drivers"))?void 0:s.length)??0,F=(null==(d=u.watch("vehicles"))?void 0:d.length)??0,I=(null==(o=u.watch("users"))?void 0:o.length)??0;return t.useEffect(()=>{u.setValue("source_tenant_id",h,{shouldValidate:!0})},[h,u]),a.jsxs(y,{open:c,onOpenChange:m,children:[a.jsx(_,{asChild:!0,children:e}),a.jsxs(v,{className:"w-4xl gap-0 p-0 sm:max-w-[78vw]",children:[a.jsx(b,{className:"border-b",children:a.jsx(f,{className:"font-bold tracking-tight",children:"New migration"})}),a.jsx(C,{...u,children:a.jsxs("form",{onSubmit:u.handleSubmit(e=>{x((e=>({all_drivers:"all"===(e.driver_mode??"all"),all_users:"all"===(e.user_mode??"all"),all_vehicles:"all"===(e.vehicle_mode??"all"),source:{company_id:e.source_company_id??"",tenant_id:e.source_tenant_id??""},destination:{company_id:"new"===(e.migration_method??"existing")?"":e.target_company_id??"",tenant_id:e.target_tenant_id??""},drivers:"selected"===(e.driver_mode??"all")?e.drivers??[]:[],users:"selected"===(e.user_mode??"all")?e.users??[]:[],vehicles:"selected"===(e.vehicle_mode??"all")?e.vehicles??[]:[]}))(e),{onSuccess:()=>{m(!1),u.reset(g)}
1})}),className:"flex h-full flex-col",children:[a.jsx("div",{className:"space-y-8 overflow-y-auto p-3",children:a.jsxs("div",{className:"grid grid-cols-1 gap-4 xl:grid-cols-2",children:[a.jsxs("div",{className:"space-y-5",children:[a.jsx("h3",{className:"font-bold text-gray-900 dark:text-gray-100",children:"Source Tenant"}),a.jsx(w,{control:u.control,name:"source_tenant_id",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx(z,{withAsterisk:!1,withClear:!1,label:"Source Tenant",disabled:!0,value:e.value,onChange:a=>{e.onChange(a.value)}})}),a.jsx(S,{children:"Current tenant, migration from this tenant"}),a.jsx(M,{})]})}),a.jsx(w,{control:u.control,name:"source_company_id",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx($,{withAsterisk:!1,withClear:!1,label:"Select Company to Move",value:e.value,tenant_id:A,disabled:!A,onChange:a=>{e.onChange(a.value)}})}),a.jsx(M,{})]})}),a.jsx(w,{control:u.control,name:"driver_mode",render:({field:e})=>a.jsx(N,{children:a.jsx(k,{children:a.jsx(Q,{title:"Driver",mode:e.value??"all",onModeChange:a=>e.onChange(a),allCount:200,selectedCount:U,children:a.jsx(w,{control:u.control,name:"drivers",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx(O,{label:"Drivers",value:e.value??[],onChange:a=>e.onChange(a),tenant_id:A,company_id:q,disabled:!A||!q})}),a.jsx(M,{})]})})})})})}),a.jsx(w,{control:u.control,name:"vehicle_mode",render:({field:e})=>a.jsx(N,{children:a.jsx(k,{children:a.jsx(Q,{title:"Vehicle",mode:e.value??"all",onModeChange:a=>e.onChange(a),allCount:190,selectedCount:F,children:a.jsx(w,{control:u.control,name:"vehicles",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx(Y,{label:"Vehicles",value:e.value??[],onChange:a=>e.onChange(a),company_id:q,disabled:!q})}),a.jsx(M,{})]})})})})})}),a.jsx(w,{control:u.control,name:"user_mode",render:({field:e})=>a.jsx(N,{children:a.jsx(k,{children:a.jsx(Q,{title:"Users",mode:e.value??"all",onModeChange:a=>e.onChange(a),allCount:0,selectedCount:I,children:a.jsx(w,{control:u.control,name:"users",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx(G,{label:"Users",value:e.value??[],onChange:a=>e.onChange(a),fetchKey:"system",disabled:!q})}),a.jsx(M,{})]})})})})})})]}),a.jsxs("div",{className:"space-y-5",children:[a.jsx("h3",{className:"font-bold text-gray-900 dark:text-gray-100",children:"Target Tenant"}),a.jsx(w,{control:u.control,name:"target_tenant_id",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx(z,{withAsterisk:!1,withClear:!1,label:"Tenant",value:e.value,excludeId:A,onChange:a=>{e.onChange(a.value)}})}),a.jsx(S,{children:"The tenant that will receive the data."}),a.jsx(M,{})]})}),a.jsxs("div",{className:"space-y-3 rounded-xl bg-gray-100 p-4 dark:bg-gray-900",children:[a.jsx("h4",{className:"font-bold text-gray-900 dark:text-gray-100",children:"Migration Method"}),a.jsxs("div",{className:"flex items-center rounded-xl bg-gray-200 p-2 dark:bg-gray-800",children:[a.jsx("button",{type:"button",onClick:()=>u.setValue("migration_method","existing",{shouldValidate:!0}),className:T("h-7 flex-1 rounded-md text-sm font-semibold","existing"===K?"bg-background text-gray-900 shadow-xs dark:text-gray-100":"text-gray-500"),children:"Existing Company"}),a.jsx("button",{type:"button",onClick:()=>{u.setValue("migration_method","new",{shouldValidate:!0}),u.setValue("target_company_id","",{shouldValidate:!0})},className:T("h-7 flex-1 rounded-md text-sm font-semibold","new"===K?"bg-background text-gray-900 shadow-xs dark:text-gray-100":"text-gray-500 dark:text-gray-300"),children:"Create New"})]}),"existing"===K&&a.jsx(w,{control:u.control,name:"target_company_id",render:({field:e})=>a.jsxs(N,{children:[a.jsx(k,{children:a.jsx($,{withAsterisk:!1,withClear:!1,label:"Target Company",value:e.value,tenant_id:E,disabled:!E,onChange:a=>e.onChange(a.value)})}),a.jsx(M,{})]})})]})]})]})}),a.jsxs(V,{className:"mt-auto flex-row gap-4 border-t p-8",children:[a.jsx(L,{asChild:!0,children:a.jsx(P,{type:"button",variant:"outline",className:"flex-1 font-semibold",disabled:j,children:"Cancel"})}),a.jsx(P,{type:"submit",className:"flex-1 font-semibold",disabled:!u.formState.isValid||j,children:j?"Migrating...":"Migrate"})]})]})})]})]})}function Z(){const e=d(),s=o({strict:!1}),t=g(e=>e.hasPermission("MIGRATE_COMPANY"));return a.jsxs("div",{className:"w-table flex flex-wrap items-center justify-between gap-4",children:[a.jsx(A,{value:s.search,onChange:a=>{e({to:".",search:e=>({...e,search:a,page:1})})},placeholder:"Search by company"}),t&&a.jsx(W,{children:a.jsxs(P,{type:"button",children:[a.jsx(c,{icon:m,className:"size-4"}),"New migration"]})})]})}function H(){const t=q.useSearch(),n=[{header:"Source company",cell:({row:e})=>a.jsx("span",{className:"font-medium",children:e.original.source.company_name||e.original.source.tenant_name})},{header:"Target tenant - company",cell:({row:e})=>a.jsxs("div",{className:"space-y-1",children:[a.jsx("div",{className:"font-semibold",children:e.original.destination.tenant_name}),a.jsx("div",{children:e.original.destination.company_name})]})},{header:"Migrated by",cell:({row:e})=>a.jsx("span",{className:"font-medium",children:e.original.user_name})},{header:"Migration date",cell:({row:e})=>a.jsx("span",{className:"font-medium",children:e.original.created_at?s(new Date(e.original.created_at),"PP"):""})}],{data:l,isLoading:r}=(({company_name:a,page:s=1,limit:t=20})=>{const n=g.getState().hasPermission;return e({queryKey:["migration-history",a,s,t],queryFn:async()=>(async({company_name:e="",page:a=1,limit:s=20}={})=>{const t=new URLSearchParams;return t.append("page",String(a)),t.append("limit",String(s)),e&&t.append("company_name",e),(await h.get(`/v1/migrator/history?${t.toString()}`)).data})({company_name:a,page:s,limit:t}),enabled:n("GET_COMPANY_LIST")})})({company_name:t.search,page:t.page,limit:t.limit});return a.jsxs("div",{className:"space-y-4",children:[a.jsx(Z,{}),a.jsx(E,{page:t.page,limit:t.limit,columns:n,isLoading:r,data:(null==l?void 0:l.data.items)??[],totalPages:(null==l?void 0:l.data.paging.totalPages)??1})]})}export{H as component};
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.