import { useQuery } from "@apollo/client/react";
import queryString from "query-string";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { useLocation } from "react-router-dom";
import AlertComponent from "../../components/alert/alert.component";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import { QUERY_ACTIVE_CONTRACTS_PAGINATED } from "../../graphql/cccontracts.queries";
import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import ContractsPageComponent from "./contracts.page.component";
import { pageLimit } from "../../utils/config";
import FeatureWrapperComponent from "../../components/feature-wrapper/feature-wrapper.component";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import { Card } from "antd";
import UpsellComponent, { upsellEnum } from "../../components/upsell/upsell.component";

const mapDispatchToProps = (dispatch) => ({
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs)),
  setSelectedHeader: (key) => dispatch(setSelectedHeader(key))
});

export function ContractsPageContainer({ setBreadcrumbs, setSelectedHeader }) {
  const searchParams = queryString.parse(useLocation().search);
  const { search, page, sortcolumn, sortorder } = searchParams;

  const { loading, error, data, refetch } = useQuery(QUERY_ACTIVE_CONTRACTS_PAGINATED, {
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only",
    variables: {
      search: search || "",
      offset: page ? (page - 1) * pageLimit : 0,
      limit: pageLimit,
      order: [
        {
          [sortcolumn || "start"]: sortorder ? (sortorder === "descend" ? "desc" : "asc") : "desc"
        }
      ]
    }
  });
  const { t } = useTranslation();
  useEffect(() => {
    document.title = t("titles.contracts", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)"
      })
    });
    setSelectedHeader("contracts");
    setBreadcrumbs([
      { link: "/manage/courtesycars", label: t("titles.bc.courtesycars") },
      {
        link: "/manage/courtesycars/contracts",
        label: t("titles.bc.contracts")
      }
    ]);
  }, [setBreadcrumbs, t, setSelectedHeader]);

  if (error) return <AlertComponent title={error.message} type="error" />;
  return (
    <FeatureWrapperComponent
      featureName="courtesycars"
      noauth={
        <Card>
          <UpsellComponent upsell={upsellEnum().courtesycars.general} />
        </Card>
      }
    >
      <RbacWrapper action="contracts:list">
        <ContractsPageComponent
          loading={loading}
          refetch={refetch}
          data={data ? data.search_cccontracts : []}
          total={data ? data.search_cccontracts_aggregate.aggregate.count : 0}
        />
      </RbacWrapper>
    </FeatureWrapperComponent>
  );
}

export default connect(null, mapDispatchToProps)(ContractsPageContainer);
