import { Table } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import CurrencyFormatter from "../../utils/CurrencyFormatter";
import { alphaSort } from "../../utils/sorters";
import InvoiceAddLineButton from "../invoice-add-line-button/invoice-add-line-button.component";

export default function InvoiceEnterModalTableComponent({
  lineData,
  form,
  vendor
}) {
  const [state, setState] = useState({
    sortedInfo: {}
  });
  const { t } = useTranslation();

  const columns = [
    {
      title: t("joblines.fields.line_desc"),
      dataIndex: "line_desc",
      key: "line_desc",
      sorter: (a, b) => alphaSort(a.line_desc, b.line_desc),
      sortOrder:
        state.sortedInfo.columnKey === "line_desc" && state.sortedInfo.order,
      ellipsis: true
    },
    {
      title: t("joblines.fields.oem_partno"),
      dataIndex: "oem_partno",
      key: "oem_partno",
      sorter: (a, b) =>
        alphaSort(
          a.oem_partno ? a.oem_partno : a.op_code_desc,
          b.oem_partno ? b.oem_partno : b.op_code_desc
        ),
      sortOrder:
        state.sortedInfo.columnKey === "oem_partno" && state.sortedInfo.order,
      ellipsis: true,
      render: (text, record) => (
        <span>
          {record.oem_partno ? record.oem_partno : record.op_code_desc}
        </span>
      )
    },
    {
      title: t("joblines.fields.part_type"),
      dataIndex: "part_type",
      key: "part_type",
      sorter: (a, b) => alphaSort(a.part_type, b.part_type),
      sortOrder:
        state.sortedInfo.columnKey === "part_type" && state.sortedInfo.order
    },
    {
      title: t("joblines.fields.db_price"),
      dataIndex: "db_price",
      key: "db_price",
      sorter: (a, b) => a.db_price - b.db_price,
      sortOrder:
        state.sortedInfo.columnKey === "db_price" && state.sortedInfo.order,
      render: (text, record) => (
        <CurrencyFormatter>{record.db_price}</CurrencyFormatter>
      )
    },
    {
      title: t("joblines.fields.act_price"),
      dataIndex: "act_price",
      key: "act_price",
      sorter: (a, b) => a.act_price - b.act_price,
      sortOrder:
        state.sortedInfo.columnKey === "act_price" && state.sortedInfo.order,
      render: (text, record) => (
        <CurrencyFormatter>{record.act_price}</CurrencyFormatter>
      )
    },
    {
      title: t("joblines.fields.part_qty"),
      dataIndex: "part_qty",
      key: "part_qty",
      sorter: (a, b) => a.part_qty - b.part_qty,
      sortOrder:
        state.sortedInfo.columnKey === "part_qty" && state.sortedInfo.order
    },
    {
      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
    },
    {
      title: t("invoices.labels.actions"),
      dataIndex: "actions",
      key: "actions",
      render: (text, record) => (
        <div>
          <InvoiceAddLineButton
            jobLine={record}
            form={form}
            discount={vendor ? vendor.discount : null}
            disabled={vendor}
          />
        </div>
      )
    }
  ];

  const handleTableChange = (pagination, filters, sorter) => {
    setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
  };

  const formItemLayout = {
    labelCol: {
      xs: { span: 12 },
      sm: { span: 5 }
    },
    wrapperCol: {
      xs: { span: 24 },
      sm: { span: 12 }
    }
  };

  return (
    <Table
      {...formItemLayout}
      size="small"
      pagination={{ position: "top", defaultPageSize: 25 }}
      columns={columns.map(item => ({ ...item }))}
      rowKey="id"
      dataSource={lineData}
      onChange={handleTableChange}
    />
  );
}
