import { useState } from "react";
import { Grid } from "../components/Grid/Grid";
import Tabs from "../components/Tab/Tab";
import { useUserProfileStore } from "../context/zustand-store/userStore";
import { InventoryStakeHolderAllocations } from "../partials/inventory/InventoryStakeHolderAllocations";
import { InventoryWarehouseEntryTab } from "../partials/inventory/InventoryWarehouseEntryTab";
import { useParams } from "@tanstack/react-router";
import { InventoryEntriesList } from "../partials/inventory/InventoryEntriesList";

export default function Inventory() {
  const [selectedTab, setSelectedTab] = useState<number>(0);
  const handleTabChange = (index: number) => {
    setSelectedTab(index);
  };

  const { profile } = useUserProfileStore();
  const params = useParams({ strict: false });

  const tabItems = [
    {
      label: "ورودی به انبار",
      page: "inventory",
      access: "Entry-Inventory",
    },
    {
      label: "توزیع به زیر مجموعه",
      visible: profile?.role?.type?.key === "CO",
    },
  ];

  return (
    <Grid container column className="justify-center mt-2">
      {params?.code ? (
        <InventoryEntriesList />
      ) : (
        <>
          <Tabs tabs={tabItems} onChange={handleTabChange} size="medium" />
          <Grid container column className="mt-2">
            {selectedTab === 0 ? (
              <InventoryWarehouseEntryTab />
            ) : (
              <InventoryStakeHolderAllocations />
            )}
          </Grid>
        </>
      )}
    </Grid>
  );
}
