import { Space, Table } from "antd";
import Dinero from "dinero.js";
import React, { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { alphaSort } from "../../utils/sorters";

export default function JobTotalsTableLabor({ job }) {
  const { t } = useTranslation();
  const [state, setState] = useState({
    sortedInfo: {
      columnKey: "profitcenter_labor",
      field: "profitcenter_labor",
      order: "ascend",
    },
    filteredInfo: {},
  });

  const data = useMemo(() => {
    return Object.keys(job.job_totals.rates)
      .filter(
        (key) =>
          key !== "mapa" &&
          key !== "mash" &&
          key !== "subtotal" &&
          key !== "rates_subtotal"
      )
      .map((key) => {
        return {
          id: key,
          ...job.job_totals.rates[key],
        };
      });
  }, [job.job_totals.rates]);

  const columns = [
    {
      title: t("joblines.fields.profitcenter_labor"),
      dataIndex: "profitcenter_labor",
      key: "profitcenter_labor",
      defaultSortOrder: "ascend",
      sorter: (a, b) =>
        alphaSort(
          t(`jobs.fields.rate_${a.id.toLowerCase()}`),
          t(`jobs.fields.rate_${b.id.toLowerCase()}`)
        ),
      sortOrder:
        state.sortedInfo.columnKey === "profitcenter_labor" &&
        state.sortedInfo.order,
      render: (text, record) =>
        t(`jobs.fields.rate_${record.id.toLowerCase()}`),
    },
    {
      title: t("jobs.labels.rates"),
      dataIndex: "rate",
      key: "rate",
      align: "right",
      sorter: (a, b) => a.rate - b.rate,
      sortOrder:
        state.sortedInfo.columnKey === "rate" && state.sortedInfo.order,
      render: (text, record) => (
        <CurrencyFormatter>{record.rate}</CurrencyFormatter>
      ),
    },
    {
      title: t("joblines.fields.mod_lb_hrs"),
      dataIndex: "mod_lb_hrs",

      key: "mod_lb_hrs",
      sorter: (a, b) => a.mod_lb_hrs - b.mod_lb_hrs,
      sortOrder:
        state.sortedInfo.columnKey === "mod_lb_hrs" && state.sortedInfo.order,

      render: (text, record) => record.hours.toFixed(1),
    },
    {
      title: t("joblines.fields.total"),
      dataIndex: "total",
      key: "total",
      align: "right",
      sorter: (a, b) => a.total.amount - b.total.amount,
      sortOrder:
        state.sortedInfo.columnKey === "total" && state.sortedInfo.order,

      render: (text, record) => Dinero(record.total).toFormat(),
    },
  ];

  const handleTableChange = (pagination, filters, sorter) => {
    setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
  };
  return (
    <Table
      columns={columns}
      rowKey="id"
      pagination={false}
      onChange={handleTableChange}
      dataSource={data}
      scroll={{
        x: true,
      }}
      summary={() => (
        <>
          <Table.Summary.Row>
            <Table.Summary.Cell>
              <strong>{t("jobs.labels.labor_rates_subtotal")}</strong>
            </Table.Summary.Cell>
            <Table.Summary.Cell />
            <Table.Summary.Cell>
              {(
                job.job_totals.rates.mapa.hours +
                job.job_totals.rates.mash.hours
              ).toFixed(1)}
            </Table.Summary.Cell>
            <Table.Summary.Cell align="right">
              <strong>
                {Dinero(job.job_totals.rates.rates_subtotal).toFormat()}
              </strong>
            </Table.Summary.Cell>
          </Table.Summary.Row>
          <Table.Summary.Row>
            <Table.Summary.Cell>
              <Space>
                {t("jobs.labels.mapa")}
                {job.materials &&
                  job.materials.mapa &&
                  job.materials.mapa.cal_maxdlr &&
                  job.materials.mapa.cal_maxdlr > 0 &&
                  t("jobs.labels.threshhold", {
                    amount: job.materials.mapa.cal_maxdlr,
                  })}
              </Space>
            </Table.Summary.Cell>
            <Table.Summary.Cell align="right">
              <CurrencyFormatter>
                {job.job_totals.rates.mapa.rate}
              </CurrencyFormatter>
            </Table.Summary.Cell>
            <Table.Summary.Cell>
              {job.job_totals.rates.mapa.hours.toFixed(1)}
            </Table.Summary.Cell>
            <Table.Summary.Cell align="right">
              {Dinero(job.job_totals.rates.mapa.total).toFormat()}
            </Table.Summary.Cell>
          </Table.Summary.Row>
          <Table.Summary.Row>
            <Table.Summary.Cell>
              <Space wrap>
                {t("jobs.labels.mash")}
                {job.materials &&
                  job.materials.mash &&
                  job.materials.mash.cal_maxdlr &&
                  job.materials.mash.cal_maxdlr > 0 &&
                  t("jobs.labels.threshhold", {
                    amount: job.materials.mash.cal_maxdlr,
                  })}
              </Space>
            </Table.Summary.Cell>
            <Table.Summary.Cell align="right">
              <CurrencyFormatter>
                {job.job_totals.rates.mash.rate}
              </CurrencyFormatter>
            </Table.Summary.Cell>
            <Table.Summary.Cell>
              {job.job_totals.rates.mash.hours.toFixed(1)}
            </Table.Summary.Cell>
            <Table.Summary.Cell align="right">
              {Dinero(job.job_totals.rates.mash.total).toFormat()}
            </Table.Summary.Cell>
          </Table.Summary.Row>
          <Table.Summary.Row>
            <Table.Summary.Cell>
              <strong>{t("jobs.labels.rates_subtotal")}</strong>
            </Table.Summary.Cell>
            <Table.Summary.Cell />
            <Table.Summary.Cell />
            <Table.Summary.Cell align="right">
              <strong>
                {Dinero(job.job_totals.rates.subtotal).toFormat()}
              </strong>
            </Table.Summary.Cell>
          </Table.Summary.Row>
        </>
      )}
    />
  );
}
