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 ContractsRatesChangeButton({ disabled, form, bodyshop }) {
  const { t } = useTranslation();

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

  const menuItems = bodyshop.md_ccc_rates.map((i, idx) => ({
    key: idx,
    label: i.label,
    value: i
  }));

  const menu = { items: menuItems, onClick: handleClick };

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

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