import React, { useContext, useEffect, useState } from "react";
import { Grid } from "../../../../components/grid/Grid";
import { useDispatch } from "react-redux";
import { cityFetchSamasatTransportChickensDetails } from "../../../visors/services/fetch-samasat-transport-chickens";
import { AdvancedTable } from "../../../../components/advanced-table/AdvancedTable";
import { AppContext } from "../../../../contexts/AppContext";

export const TransportChickensDetails = ({ code, cookie }) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);

  const [tableData, setTableData] = useState([]);

  useEffect(() => {
    dispatch(
      cityFetchSamasatTransportChickensDetails({
        cookie,
        certId: code,
      })
    ).then((r) => {
      if (r.error) {
        openNotif({
          vertical: "top",
          horizontal: "center",
          msg: "کوکی معتبر نیست!",
          severity: "error",
        });
      } else {
        const d = r.payload.data?.map((item, i) => {
          return [
            i + 1,
            item?.TrackingCode,
            item?.ResideDatePersian,
            item?.GoodAmount?.toLocaleString(),
            item?.TrackingStatusDescription,
            item?.GoodName,
            item?.DesPartIdCode,
            item?.DesUnitName,
            item?.IssueDatePersian,
          ];
        });
        setTableData(d);
      }
    });
  }, []);

  return (
    <Grid container xs={12}>
      <AdvancedTable
        columns={[
          "ردیف",
          "کد رهگیری",
          "تاریخ ثبت",
          "تعداد",
          "وضعیت",
          "نام کالا",
          "شناسه یکتا کشتارگاه مقصد",
          "نام کشتارگاه",
          "تاریخ ثبت وضعیت",
        ]}
        data={tableData}
        name="جزئیات حمل مرغ زنده"
      />
    </Grid>
  );
};
