import { Card, Statistic } from "antd";
import Dinero from "dinero.js";
import dayjs from "../../../utils/day";
import { useTranslation } from "react-i18next";
import DashboardRefreshRequired from "../refresh-required.component";

export default function DashboardProjectedMonthlySales({ data, ...cardProps }) {
  const { t } = useTranslation();
  if (!data) return null;
  if (!data.projected_monthly_sales) return <DashboardRefreshRequired {...cardProps} />;

  const dollars =
    data.projected_monthly_sales &&
    data.projected_monthly_sales.reduce(
      (acc, val) => acc.add(Dinero(val.job_totals && val.job_totals.totals && val.job_totals.totals.subtotal)),
      Dinero()
    );
  return (
    <Card title={t("dashboard.titles.projectedmonthlysales")} {...cardProps}>
      <Statistic value={dollars.toFormat()} />
    </Card>
  );
}

export const DashboardProjectedMonthlySalesGql = `
    projected_monthly_sales: jobs(where: {
      voided: {_eq: false},
      _or: [
        {_and: [
          {date_invoiced:{_is_null: false }},
          {date_invoiced: {_gte: "${dayjs()
            .startOf("month")
            .startOf("day")
            .toISOString()}"}}, {date_invoiced: {_lte: "${dayjs().endOf("month").endOf("day").toISOString()}"}}]},
  {

_and:[
  {date_invoiced:{_is_null: true }},
  {actual_completion: {_gte: "${dayjs()
    .startOf("month")
    .startOf("day")
    .toISOString()}"}}, {actual_completion: {_lte: "${dayjs().endOf("month").endOf("day").toISOString()}"}}

]
  },

  {_and: [
    {date_invoiced: {_is_null: true}}, 
    {actual_completion: {_is_null: true}}
    {scheduled_completion: {_gte: "${dayjs()
      .startOf("month")
      .startOf("day")
      .toISOString()}"}}, {scheduled_completion: {_lte: "${dayjs().endOf("month").endOf("day").toISOString()}"}}


]}

]}) {
    id
    ro_number
    voided
    date_invoiced
    job_totals
  }
`;
