import { DownOutlined } from "@ant-design/icons";
import { Dropdown } from "antd";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";

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

export function JobsDetailRatesChangeButton({ disabled, form, bodyshop }) {
  const { t } = useTranslation();

  const handleClick = ({ item }) => {
    const rate = item.props.value;
    form.setFieldsValue({ ...rate, labor_rate_desc: rate.rate_label });
  };

  const menu = {
    items: bodyshop.md_labor_rates.map((rate, idx) => ({
      key: idx,
      label: rate.rate_label,
      value: rate
    })),
    onClick: handleClick
  };

  return (
    <Dropdown menu={menu} disabled={disabled}>
      <a className="ant-dropdown-link" href=" #" onClick={(e) => e.preventDefault()}>
        {t("jobs.actions.changelaborrate")} <DownOutlined />
      </a>
    </Dropdown>
  );
}

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