import { DownOutlined } from "@ant-design/icons";
import { Dropdown, Menu } from "antd";
import React from "react";
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, key, keyPath }) => {
    const rate = item.props.value;
    form.setFieldsValue({ ...rate, labor_rate_desc: rate.rate_label });
  };

  const menu = (
    <div>
      <Menu onClick={handleClick}>
        {bodyshop.md_labor_rates.map((rate, idx) => (
          <Menu.Item value={rate} key={idx}>
            {rate.rate_label}
          </Menu.Item>
        ))}
      </Menu>
    </div>
  );

  return (
    <Dropdown overlay={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);
