import { useQuery } from "@apollo/client/react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { QUERY_PARTS_BILLS_BY_JOBID } from "../../graphql/bills.queries";
import AlertComponent from "../alert/alert.component";
import JobBillsTotalComponent from "../job-bills-total/job-bills-total.component";

const mapStateToProps = createStructuredSelector({});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(JobCloseRoGuardBills);

export function JobCloseRoGuardBills({ job, warningCallback }) {
  const { loading, error, data } = useQuery(QUERY_PARTS_BILLS_BY_JOBID, {
    variables: { jobid: job.id },
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only"
  });

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

  return (
    <JobBillsTotalComponent
      loading={loading}
      bills={data ? data.bills : []}
      partsOrders={data ? data.parts_orders : []}
      jobTotals={job.job_totals}
      showWarning
      warningCallback={warningCallback}
    />
  );
}
