import { SimpleTable } from "../../../../components/simple-table/SimpleTable";
import { PropTypes } from "prop-types";
import { useEffect, useState } from "react";
import { Grid } from "../../../../components/grid/Grid";
import { format } from "date-fns-jalali";

export const ProvinceInformationContent = ({ file, quantity }) => {
  const [provinceInfoData, setProvinceInfoData] = useState({
    provinceOperatorName: "اپراتور استان",
    provinceOperatorMobile: "-",
    acceptedRejectedDate: null,
    provinceState: "در انتظار تایید",
  });

  const { province, provinceOperator } = file;

  useEffect(() => {
    let provinceOperatorName,
      provinceOperatorMobile,
      acceptedRejectedDate,
      provinceState;
    if (province) {
      provinceOperatorName = province.provinceOperatorName;
      provinceOperatorMobile = province.provinceOperatorMobile;
      acceptedRejectedDate = province.acceptedRejectedDate;
      provinceState =
        province.provinceState === "accept" ? "تایید شده" : "رد شده";
    } else {
      provinceOperatorName = provinceOperator?.provinceOperatorName;
      provinceOperatorMobile = provinceOperator?.provinceOperatorMobile;
      acceptedRejectedDate = null;
      provinceState = "در انتظار تایید";
    }
    setProvinceInfoData({
      provinceOperatorName,
      provinceOperatorMobile,
      acceptedRejectedDate,
      provinceState,
    });
  }, [province]);

  return (
    <>
      <Grid container direction="column" flexWrap="nowrap" flex="1">
        <Grid xs={12}>
          <SimpleTable
            columns={[
              "نام اپراتور",
              "تلفن",
              "استان",
              "تاریخ بررسی",
              "وضعیت درخواست",
              "وضعیت تخصیص",
              province?.provinceMessage ? "دلیل رد" : "",
            ]}
            data={[
              [
                provinceInfoData.provinceOperatorName,
                provinceInfoData.provinceOperatorMobile,
                provinceOperator.provinceOperatorProvinc,
                format(
                  new Date(provinceInfoData.acceptedRejectedDate),
                  "yyyy/MM/dd"
                ),
                provinceInfoData.provinceState,
                quantity !== 0 ? "در انتظار تخصیص استان" : "تخصیص داده شده",
                province?.provinceMessage ? province?.provinceMessage : "",
              ],
            ]}
          />
        </Grid>
      </Grid>
    </>
  );
};

ProvinceInformationContent.propTypes = {
  file: PropTypes.any,
  quantity: PropTypes.any,
};
