import ResponsiveTable from "../responsive-table/responsive-table.component";
import Dinero from "dinero.js";
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { alphaSort } from "../../utils/sorters";

export default function JobTotalsTableOther({ job }) {
  const { t } = useTranslation();
  const [state, setState] = useState({
    sortedInfo: {},
    filteredInfo: {}
  });

  const data = useMemo(() => {
    return [
      ...((job.job_totals.additional.additionalCostItems &&
        job.job_totals.additional.additionalCostItems.map((i) => {
          return {
            key: i.key,
            total: i.total
          };
        })) ||
        []),
      {
        key: t("jobs.fields.adjustment_bottom_line"),
        total: job.job_totals.additional.adjustments
      },
      {
        key: t("jobs.fields.towing_payable"),
        total: job.job_totals.additional.towing
      },
      {
        key: t("jobs.fields.storage_payable"),
        total: job.job_totals.additional.storage
      }
      // {
      //   key: t("jobs.fields.ca_bc_pvrt"),
      //   total: job.job_totals.additional.pvrt,
      // },
    ];
  }, [job.job_totals, t]);

  const columns = [
    {
      title: t("general.labels.item"),
      dataIndex: "key",
      key: "key",
      sorter: (a, b) => alphaSort(a.key, b.key),
      sortOrder: state.sortedInfo.columnKey === "key" && state.sortedInfo.order,
      width: "0%"
    },
    {
      title: t("joblines.fields.total"),
      dataIndex: "total",
      key: "total",
      sorter: (a, b) => a.total.amount - b.total.amount,
      sortOrder: state.sortedInfo.columnKey === "total" && state.sortedInfo.order,
      width: "20%",
      align: "right",
      render: (text, record) => Dinero(record.total).toFormat()
    }
  ];

  const handleTableChange = (pagination, filters, sorter) => {
    setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
  };
  return (
    <ResponsiveTable
      columns={columns}
      mobileColumnKeys={["total", "key"]}
      rowKey="key"
      pagination={false}
      onChange={handleTableChange}
      dataSource={data}
      scroll={{
        x: true
      }}
      summary={() => (
        <>
          <ResponsiveTable.Summary.Row>
            <ResponsiveTable.Summary.Cell>
              <strong>{t("jobs.labels.additionaltotal")}</strong>
            </ResponsiveTable.Summary.Cell>

            <ResponsiveTable.Summary.Cell align="right">
              <strong>{Dinero(job.job_totals.additional.total).toFormat()}</strong>
            </ResponsiveTable.Summary.Cell>
          </ResponsiveTable.Summary.Row>
          <ResponsiveTable.Summary.Row>
            <ResponsiveTable.Summary.Cell>
              <strong>{t("jobs.labels.subletstotal")}</strong>
            </ResponsiveTable.Summary.Cell>

            <ResponsiveTable.Summary.Cell align="right">
              <strong>{Dinero(job.job_totals.parts.sublets.total).toFormat()}</strong>
            </ResponsiveTable.Summary.Cell>
          </ResponsiveTable.Summary.Row>
        </>
      )}
    />
  );
}
