import { useQuery } from "@apollo/client";
import { Col, Row, Space } from "antd";
import moment from "moment";
import queryString from "query-string";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { useLocation } from "react-router-dom";
import { createStructuredSelector } from "reselect";
import AlertComponent from "../../components/alert/alert.component";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import TimeTicketsDatesSelector from "../../components/ticket-tickets-dates-selector/time-tickets-dates-selector.component";
import TimeTicketList from "../../components/time-ticket-list/time-ticket-list.component";
import TimeTicketsPayrollTable from "../../components/time-tickets-payroll-table/time-tickets-payroll-table.component";
import TimeTicketsSummaryEmployees from "../../components/time-tickets-summary-employees/time-tickets-summary-employees.component";
import { QUERY_TIME_TICKETS_IN_RANGE } from "../../graphql/timetickets.queries";
import TimeTicketsAttendanceTable from "../../components/time-tickets-attendance-table/time-tickets-attendance-table.component";
import {
  setBreadcrumbs,
  setSelectedHeader,
} from "../../redux/application/application.actions";

const mapStateToProps = createStructuredSelector({});

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

export function TimeTicketsContainer({
  bodyshop,
  setBreadcrumbs,
  setSelectedHeader,
}) {
  const { t } = useTranslation();
  useEffect(() => {
    document.title = t("titles.timetickets");
    setSelectedHeader("timetickets");
    setBreadcrumbs([
      {
        link: "/manage/timetickets",
        label: t("titles.bc.timetickets"),
      },
    ]);
  }, [t, setBreadcrumbs, setSelectedHeader]);

  const searchParams = queryString.parse(useLocation().search);
  const { start, end } = searchParams;

  const startDate = start
    ? moment(start)
    : moment().startOf("week").subtract(7, "days");
  const endDate = end ? moment(end) : moment().endOf("week");

  const { loading, error, data } = useQuery(QUERY_TIME_TICKETS_IN_RANGE, {
    variables: {
      start: startDate,
      end: endDate,
    },
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only",
  });

  if (error) return <AlertComponent message={error.message} type="error" />;

  return (
    <RbacWrapper action="timetickets:list">
      <Row gutter={[16, 16]}>
        <Col span={24}>
          <TimeTicketList
            loading={loading}
            timetickets={data ? data.timetickets : []}
            extra={
              <Space wrap>
                <TimeTicketsAttendanceTable />
                <TimeTicketsPayrollTable />
                <TimeTicketsDatesSelector />
              </Space>
            }
          />
        </Col>
        <Col span={24}>
          <TimeTicketsSummaryEmployees
            loading={loading}
            timetickets={data ? data.timetickets : []}
            startDate={startDate}
            endDate={endDate}
          />
        </Col>
      </Row>
    </RbacWrapper>
  );
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(TimeTicketsContainer);
