import { useMutation } from "@apollo/client";
import { Dropdown, Menu, Spin } from "antd";
import React, { useState } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { logImEXEvent } from "../../firebase/firebase.utils";
import { UPDATE_JOB } from "../../graphql/jobs.queries";
import { insertAuditTrail } from "../../redux/application/application.actions";
import { selectBodyshop } from "../../redux/user/user.selectors";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({
  insertAuditTrail: ({ jobid, operation }) =>
    dispatch(insertAuditTrail({ jobid, operation })),
});
export function ProductionListColumnCategory({ record, bodyshop }) {
  const [updateJob] = useMutation(UPDATE_JOB);
  const [loading, setLoading] = useState(false);

  const handleSetStatus = async (e) => {
    logImEXEvent("production_change_status");

    setLoading(true);
    const { key } = e;
    await updateJob({
      variables: {
        jobId: record.id,
        job: {
          category: key,
        },
      },
    });

    setLoading(false);
  };

  return (
    <Dropdown
      overlay={
        <Menu
          style={{ maxHeight: "200px", overflowY: "auto" }}
          onClick={handleSetStatus}
        >
          {bodyshop.md_categories.map((item) => (
            <Menu.Item key={item}>{item}</Menu.Item>
          ))}
        </Menu>
      }
      trigger={["click"]}
    >
      <div style={{ width: "100%", height: "19px", cursor: "pointer" }}>
        {record.category}
        {loading && <Spin />}
      </div>
    </Dropdown>
  );
}
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ProductionListColumnCategory);
