import React, { useEffect, useState } from "react";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { Box, Tab, Tabs } from "@mui/material";
import { SlaughterSellCarcassOutProvinceBuyers } from "../slaughter-sell-carcass-out-province-buyers/SlaughterSellCarcassOutProvinceBuyers";
import { SlaughterSellCarcassOutProvinceSell } from "../slaughter-sell-carcass-out-province-sell/SlaughterSellCarcassOutProvinceSell";
import { useDispatch } from "react-redux";
import { getKillhouseApprovedPriceState } from "../../../province/services/get-approved-price-state";
import { SlaughterShowProducts } from "../slaughter-show-products/SlaughterShowProducts";
import { fetchSlaughterBroadcastAndProducts } from "../../services/handle-fetch-slaughter-products";

export const SlaughterSellCarcassOutProvincePage = () => {
  const dispatch = useDispatch();

  const [valueOutProvince, setValueOutProvince] = useState(0);

  const handleChangeTabsetOutProvince = (event, newValue) => {
    setValueOutProvince(newValue);
  };

  const fetchData = () => {
    dispatch(fetchSlaughterBroadcastAndProducts());
    dispatch(getKillhouseApprovedPriceState());
  };

  useEffect(() => {
    fetchData();
  }, [dispatch]);

  return (
    <Grid
      container
      direction="column"
      alignItems="center"
      justifyContent="space-between"
      gap={SPACING.SMALL}
      mt={SPACING.MEDIUM}
      width="100%"
    >
      <Grid container width="100%" isDashboard>
        <SlaughterShowProducts />
      </Grid>

      <Grid
        container
        direction="column"
        alignItems="center"
        justifyContent="space-between"
        gap={SPACING.SMALL}
        mb={SPACING.SMALL}
        width="100%"
      >
        <Box
          sx={{
            borderBottom: 1,
            borderColor: "divider",
          }}
        >
          <Tabs
            className="insidetabs"
            size="small"
            value={valueOutProvince}
            onChange={handleChangeTabsetOutProvince}
            aria-label="basic tabs example"
          >
            <Tab label="فروش" />
            <Tab label="خریداران" />
          </Tabs>
        </Box>

        {valueOutProvince === 0 && (
          <SlaughterSellCarcassOutProvinceSell fetchData={fetchData} />
        )}

        {valueOutProvince === 1 && <SlaughterSellCarcassOutProvinceBuyers />}
      </Grid>
    </Grid>
  );
};
