import { gql, useMutation } from "@apollo/client";
import { Button, notification } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import {
  selectAuthLevel,
  selectBodyshop,
} from "../../redux/user/user.selectors";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
  authLevel: selectAuthLevel,
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});

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

export function BillMarkSelectedExported({
  billids,
  disabled,
  loadingCallback,
  completedCallback,
  refetch,
}) {
  const { t } = useTranslation();
  const [loading, setLoading] = useState(false);

  const [updateBill] = useMutation(gql`
    mutation UPDATE_BILL($billIds: [uuid!]!) {
      update_bills(where: { id: { _in: $billIds } }, _set: { exported: true }) {
        returning {
          id
          exported
          exported_at
        }
      }
    }
  `);

  const handleUpdate = async () => {
    setLoading(true);
    loadingCallback(true);
    const result = await updateBill({
      variables: { billIds: billids },
      update(cache){
          
      }
    });

    if (!result.errors) {
      notification["success"]({
        message: t("bills.successes.markexported"),
      });
    } else {
      notification["error"]({
        message: t("bills.errors.saving", {
          error: JSON.stringify(result.errors),
        }),
      });
    }
    loadingCallback(false);
    completedCallback && completedCallback([]);
    setLoading(false);
    refetch && refetch();
  };

  return (
    <Button loading={loading} disabled={disabled} onClick={handleUpdate}>
      {t("bills.labels.markexported")}
    </Button>
  );
}
