import { Card, Table } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import OwnerDetailUpdateJobsComponent from "../owner-detail-update-jobs/owner-detail-update-jobs.component";

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

function OwnerDetailJobsComponent({ bodyshop, owner }) {
  const { t } = useTranslation();
  const [selectedJobs, setSelectedJobs] = useState([]);
  const columns = [
    {
      title: t("jobs.fields.ro_number"),
      dataIndex: "ro_number",
      key: "ro_number",
      ellipsis: true,
      render: (text, record) => (
        <Link to={`/manage/jobs/${record.id}`}>
          {record.ro_number || t("general.labels.na")}
        </Link>
      ),
    },
    {
      title: t("jobs.fields.vehicle"),
      dataIndex: "vehicleid",
      key: "vehicleid",
      render: (text, record) =>
        record.vehicleid ? (
          <Link to={`/manage/vehicles/${record.vehicleid}`}>
            {`${record.v_model_yr} ${record.v_make_desc} ${record.v_model_desc}`}
          </Link>
        ) : (
          t("jobs.errors.novehicle")
        ),
    },
    {
      title: t("jobs.fields.clm_no"),
      dataIndex: "clm_no",
      key: "clm_no",
    },
    {
      title: t("jobs.fields.status"),
      dataIndex: "status",
      key: "status",
    },

    {
      title: t("jobs.fields.clm_total"),
      dataIndex: "clm_total",
      key: "clm_total",
      render: (text, record) => (
        <CurrencyFormatter>{record.clm_total}</CurrencyFormatter>
      ),
    },
  ];

  return (
    <Card
      title={t("owners.labels.relatedjobs")}
      extra={
        <OwnerDetailUpdateJobsComponent
          selectedJobs={selectedJobs}
          owner={owner}
          disabled={selectedJobs.length === 0}
        />
      }
    >
      <Table
        pagination={{ position: "bottom" }}
        columns={columns}
        scroll={{ x: true }}
        rowKey="id"
        dataSource={owner.jobs}
        rowSelection={{
          onSelect: (record, selected, selectedRows) => {
            setSelectedJobs(selectedRows ? selectedRows.map((i) => i.id) : []);
          },
          onSelectAll: (selected, selectedRows, changeRows) => {
            setSelectedJobs(
              selectedRows
                ? selectedRows
                    .filter((i) =>
                      bodyshop.md_ro_statuses.active_statuses.includes(i.status)
                    )
                    .map((i) => i.id)
                : []
            );
          },
          selectedRowKeys: selectedJobs,
          getCheckboxProps: (record) => ({
            disabled: bodyshop.md_ro_statuses.active_statuses
              ? !bodyshop.md_ro_statuses.active_statuses.includes(record.status)
              : true,
          }),
        }}
      />
    </Card>
  );
}

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