import {
  Table,
  TableCell,
  TableHead,
  TableRow,
  Typography,
} from "@mui/material";
import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { provinceGetManageUsersRolesService } from "../../services/province-get-manage-users-roles";
import { ProvinceManageUsersRowAdmin } from "./ProvinceManageUsersRowAdmin";
import { ProvinceManageUsersRowCityOperator } from "./ProvinceManageUsersRowCityOperator";
import { ProvinceManageUsersRowDriver } from "./ProvinceManageUsersRowDriver";
import { ProvinceManageUsersRowKillhouseVet } from "./ProvinceManageUsersRowDriverKillhouseVet";
import { ProvinceManageUsersRowVetFarm } from "./ProvinceManageUsersRowDriverVetFarm";
import { ProvinceManageUsersRowFinancialOperator } from "./ProvinceManageUsersRowFinancialOperator";
import { ProvinceManageUsersRowInspectorOperator } from "./ProvinceManageUsersRowInspectorOperator";
import { ProvinceManageUsersRowJahadCity } from "./ProvinceManageUsersRowJahadCity";
import { ProvinceManageUsersRowJahadProvince } from "./ProvinceManageUsersRowJahadProvince";
import { ProvinceManageUsersRowKillHouse } from "./ProvinceManageUsersRowKillHouse";
import { ProvinceManageUsersRowPoultry } from "./ProvinceManageUsersRowPoultry";
import { ProvinceManageUsersRowProvinceOperator } from "./ProvinceManageUsersRowProvinceOperator";

export const ProvinceManageUsersRoles = ({ userRoles, row }) => {
  const dispatch = useDispatch();
  const [data, setData] = useState();

  useEffect(() => {
    dispatch(
      provinceGetManageUsersRolesService({ userProfileKey: row.userKey })
    ).then((r) => setData(r.payload.data));
  }, []);

  const poultries = data?.filter((item) => item.roleName === "Poultry");
  const killHouses = data?.filter((item) => item.roleName === "KillHouse");
  const cityOperators = data?.filter(
    (item) => item.roleName === "CityOperator"
  );
  const provinceOperators = data?.filter(
    (item) => item.roleName === "ProvinceOperator"
  );
  const financialOperators = data?.filter(
    (item) => item.roleName === "ProvinceFinancial"
  );
  const drivers = data?.filter((item) => item.roleName === "Driver");
  const killhouseVet = data?.filter((item) => item.roleName === "KillHouseVet");
  const vetFarm = data?.filter((item) => item.roleName === "VetFarm");
  const provinceInspector = data?.filter(
    (item) => item.roleName === "ProvinceInspector"
  );
  const jahadProvince = data?.filter((item) => item.roleName === "Jahad");
  const jahadCity = data?.filter((item) => item.roleName === "CityJahad");
  const admin = data?.filter((item) => item.roleName === "Admin");

  return (
    <>
      {!!poultries?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش های مرغدار
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">شناسه یکتا</TableCell>
                <TableCell align="center">نام مرغداری</TableCell>
                <TableCell align="center">تعاونی</TableCell>
                <TableCell aligrrrn="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">تعداد سالن</TableCell>
                <TableCell align="center">ظرفیت (قطعه)</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {poultries.map((item, i) => {
              return (
                <ProvinceManageUsersRowPoultry key={i} row={row} item={item} />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!killHouses?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش کشتارگاه
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">نام کشتارگاه</TableCell>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">ظرفیت</TableCell>
                <TableCell align="center">کشتارکن ها</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {killHouses.map((item, i) => {
              return (
                <ProvinceManageUsersRowKillHouse
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!cityOperators?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش شهرستان
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">تعاونی</TableCell>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
              </TableRow>
            </TableHead>
            {cityOperators.map((item, i) => {
              return (
                <ProvinceManageUsersRowCityOperator
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!provinceOperators?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش اپراتور تخصیص
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
              </TableRow>
            </TableHead>
            {provinceOperators.map((item, i) => {
              return (
                <ProvinceManageUsersRowProvinceOperator
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!financialOperators?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش اپراتور مالی
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
              </TableRow>
            </TableHead>
            {financialOperators.map((item, i) => {
              return (
                <ProvinceManageUsersRowFinancialOperator
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!drivers?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش راننده
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">نام و نام خانوادگی</TableCell>
                <TableCell align="center">تلفن راننده</TableCell>
                <TableCell align="center">مدل خودرو</TableCell>
                <TableCell align="center">ظرفیت</TableCell>
                <TableCell align="center">کدبهداشتی</TableCell>
                <TableCell align="center">شماره پلاک</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">زیرمجموعه کشتارگاه</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {drivers.map((item, i) => {
              return (
                <ProvinceManageUsersRowDriver key={i} row={row} item={item} />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!killhouseVet?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش دامپزشک کشتارگاه
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">کشتارگاه زیرمجموعه</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {killhouseVet.map((item, i) => {
              return (
                <ProvinceManageUsersRowKillhouseVet
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!vetFarm?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش دامپزشک فارم
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {vetFarm.map((item, i) => {
              return (
                <ProvinceManageUsersRowVetFarm key={i} row={row} item={item} />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!provinceInspector?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش بازرس
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {provinceInspector.map((item, i) => {
              return (
                <ProvinceManageUsersRowInspectorOperator
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!jahadProvince?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش جهاد استان
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {jahadProvince.map((item, i) => {
              return (
                <ProvinceManageUsersRowJahadProvince
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!jahadCity?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش جهاد شهرستان
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اطلاعات بانکی</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {jahadCity.map((item, i) => {
              return (
                <ProvinceManageUsersRowJahadCity
                  key={i}
                  row={row}
                  item={item}
                />
              );
            })}
          </Table>
        </Grid>
      )}
      {!!admin?.length && (
        <Grid container gap={SPACING.TINY} my={SPACING.MEDIUM}>
          <Typography
            variant="body2"
            fontWeight={"bold"}
            gutterBottom
            component="div"
          >
            نقش ادمین استان
          </Typography>
          <Table size="small" aria-label="purchases">
            <TableHead>
              <TableRow>
                <TableCell align="center">استان</TableCell>
                <TableCell align="center">شهر</TableCell>
                <TableCell align="center">آدرس</TableCell>
                <TableCell align="center">اسناد هویتی</TableCell>
                <TableCell align="center">وضعیت</TableCell>
              </TableRow>
            </TableHead>
            {admin.map((item, i) => {
              return (
                <ProvinceManageUsersRowAdmin key={i} row={row} item={item} />
              );
            })}
          </Table>
        </Grid>
      )}
    </>
  );
};
