import { Button, Popconfirm } from "antd";
import { createStructuredSelector } from "reselect";
import { selectJobReadOnly } from "../../redux/application/application.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import _ from "lodash";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
  jobRO: selectJobReadOnly
});

export function JobsMarkPstExempt({ jobRO, form }) {
  const { t } = useTranslation();

  const handleConfirm = () => {
    const newPartRates = _.cloneDeep(form.getFieldValue("parts_tax_rates"));

    Object.keys(newPartRates).forEach((key) => {
      newPartRates[key] = {
        ...newPartRates[key],
        prt_tax_in: false,
        prt_tax_rt: 0
      };
    });

    form.setFieldsValue({
      state_tax_rate: 0,
      tax_lbr_rt: 0,
      tax_levies_rt: 0,
      tax_sub_rt: 0,
      tax_shop_mat_rt: 0,
      tax_paint_mat_rt: 0,
      tax_str_rt: 0,
      tax_tow_rt: 0,
      parts_tax_rates: newPartRates
    });
  };

  return (
    <Popconfirm
      onConfirm={handleConfirm}
      disabled={jobRO}
      okText={t("general.labels.yes")}
      cancelText={t("general.labels.no")}
      title={t("jobs.actions.markpstexemptconfirm")}
    >
      <Button type="link" disabled={jobRO}>
        {t("jobs.actions.markpstexempt")}
      </Button>
    </Popconfirm>
  );
}

export default connect(mapStateToProps, null)(JobsMarkPstExempt);
