import{jsxs as M,jsx as t}from"react/jsx-runtime";import{A as N}from"./layout-CssQHtfO.js";import{Head as Q}from"@inertiajs/react";import{useReactTable as V,getFilteredRowModel as j,getSortedRowModel as k,getPaginationRowModel as H,getCoreRowModel as I}from"@tanstack/react-table";import r,{useState as i}from"react";import{FactionTable as q,createFactionColumns as z}from"./FactionTable-dx0T3tqO.js";import{SearchBar as E}from"./SearchBar-pAK0oRsn.js";import{BonusDialog as G}from"./BonusDialog-CCFw4RZa.js";import{PaginationInfo as J}from"./PaginationInfo-BbUp9Akf.js";import{TablePagination as K}from"./TablePagination-ChXr3vSS.js";import C from"axios";import"./app-sidebar-header-BX2s-HO_.js";import"@radix-ui/react-collapsible";import"lucide-react";import"./dropdown-menu-BP80qPHw.js";import"@radix-ui/react-dropdown-menu";import"./utils-CMbmcP35.js";import"clsx";import"tailwind-merge";import"./app-logo-icon-beRg1XGs.js";import"./fonts-CqawFh47.js";import"@radix-ui/react-slot";import"class-variance-authority";import"./button-BvFBRYHq.js";import"@radix-ui/react-dialog";import"@radix-ui/react-tooltip";import"@radix-ui/react-avatar";import"../app.js";/* empty css     */import"sonner";import"react-dom/client";import"./toaster-DuLpwsc2.js";import"./table-BrXe3s2z.js";import"./input-qMvM9qch.js";import"./select-DfhewM4T.js";import"@radix-ui/react-select";import"./dialog-BfGkTFdF.js";import"./label-DMnntKPk.js";import"@radix-ui/react-label";function U(){const[a,n]=i({open:!1,faction:null,bonusPercentage:"0",loading:!1}),[c,s]=i(!1),[u,o]=i(null);return{bonusDialog:a,savingBonus:c,bonusMessage:u,handleOpenBonusDialog:async e=>{n({open:!0,faction:e,bonusPercentage:"0",loading:!0}),o(null);try{const g=(await C.get(route("admin.manage-factions.get-bonus",e.id))).data.bonus?.bonus_percentage??0;n(b=>({...b,bonusPercentage:g.toString(),loading:!1}))}catch{n(g=>({...g,loading:!1}))}},handleSaveBonus:async()=>{if(a.faction){s(!0),o(null);try{const e=await C.post(route("admin.manage-factions.set-bonus",a.faction.id),{faction_name:a.faction.name,bonus_percentage:parseFloat(a.bonusPercentage)||0});o({type:"success",text:e.data.message}),setTimeout(()=>{n({open:!1,faction:null,bonusPercentage:"0",loading:!1}),o(null)},2e3)}catch(e){o({type:"error",text:e.response?.data?.message||"Failed to save bonus"})}finally{s(!1)}}},handleCloseBonusDialog:()=>{n({open:!1,faction:null,bonusPercentage:"0",loading:!1}),o(null)},handleBonusPercentageChange:e=>{n({...a,bonusPercentage:e})}}}function W(){const[a,n]=i([]),[c,s]=i(!1),[u,o]=i(!1),[m,d]=i({current_page:1,has_more:!1});return{allFactions:a,loadingAll:c,showAll:u,allFactionsPagination:m,handleLoadAllFactions:async(e=1)=>{s(!0);try{const l=await C.get(route("admin.manage-factions.all-factions"),{params:{page:e}});n(l.data.factions||[]),d(l.data.pagination),o(!0)}catch{}finally{s(!1)}},handleClearShowAll:()=>{o(!1)},setShowAll:o}}const X=[{title:"Dashboard",href:"/admin"},{title:"Manage",href:"#"},{title:"Factions",href:"/admin/manage/factions"}];function Ve({factions:a,filters:n}){const[c,s]=r.useState([]),[u,o]=r.useState([]),[m,d]=r.useState({}),[h,f]=r.useState({}),[e,l]=r.useState(n?.search||""),[g,b]=r.useState(n?.type||"id"),{allFactions:F,loadingAll:B,showAll:S,allFactionsPagination:w,handleLoadAllFactions:y,handleClearShowAll:R,setShowAll:v}=W(),{bonusDialog:p,savingBonus:_,bonusMessage:x,handleOpenBonusDialog:P,handleSaveBonus:T,handleCloseBonusDialog:L,handleBonusPercentageChange:O}=U(),A=z(P),D=V({data:S?F:a,columns:A,getCoreRowModel:I(),getPaginationRowModel:H(),onSortingChange:s,getSortedRowModel:k(),onColumnFiltersChange:o,getFilteredRowModel:j(),onColumnVisibilityChange:d,onRowSelectionChange:f,state:{sorting:c,columnFilters:u,columnVisibility:m,rowSelection:h}});return M(N,{breadcrumbs:X,children:[t(Q,{title:"Manage Factions"}),M("div",{className:"space-y-2",children:[t("div",{className:"mb-2",children:t("h2",{className:"text-1xl font-bold tracking-tight",children:"Factions"})}),t(E,{searchQuery:e,searchType:g,showAll:S,loadingAll:B,onSearchQueryChange:l,onSearchTypeChange:b,onLoadAllFactions:()=>{v(!1),y(1)},onClearShowAll:R}),S&&t(J,{totalFactions:F.length,currentPage:w.current_page,hasMore:w.has_more,loadingAll:B,onPageChange:y}),t(q,{table:D,columns:A,searchQuery:e,onOpenBonusDialog:P}),t(K,{table:D})]}),t(G,{open:p.open,faction:p.faction,bonusPercentage:p.bonusPercentage,loading:p.loading,savingBonus:_,bonusMessage:x,onOpenChange:L,onBonusPercentageChange:O,onSave:T})]})}export{Ve as default};
