import { Input, Table } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { alphaSort } from "../../utils/sorters";

export default function VehiclesListComponent({ loading, vehicles, refetch }) {
  const [state, setState] = useState({
    sortedInfo: {},
    filteredInfo: { text: "" }
  });

  const { t } = useTranslation();

  const columns = [
    {
      title: t("vehicles.fields.v_vin"),
      dataIndex: "v_vin",
      key: "v_vin",
      // onFilter: (value, record) => record.ro_number.includes(value),
      // filteredValue: state.filteredInfo.text || null,
      sorter: (a, b) => alphaSort(a.v_vin, b.v_vin),
      sortOrder:
        state.sortedInfo.columnKey === "v_vin" && state.sortedInfo.order,

      render: (text, record) => (
        <Link to={"/manage/vehicles/" + record.id}>{record.v_vin}</Link>
      )
    },
    {
      title: t("vehicles.fields.description"),
      dataIndex: "description",
      key: "description",
      render: (text, record) => {
        return (
          <span>{`${record.v_model_yr} ${record.v_make_desc} ${record.v_model_desc} ${record.v_color}`}</span>
        );
      }
    },
    {
      title: t("vehicles.fields.plate_no"),
      dataIndex: "plate",
      key: "plate",
      render: (text, record) => {
        return <span>{`${record.plate_st} | ${record.plate_no}`}</span>;
      }
    }
  ];

  const handleTableChange = (pagination, filters, sorter) => {
    setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
  };
  //TODO Implement searching & pagination
  return (
    <Table
      loading={loading}
      title={() => {
        return (
          <Input.Search
            placeholder="Search..."
            onSearch={value => {
              console.log(value);
            }}
            enterButton
          />
        );
      }}
      size="small"
      pagination={{ position: "top" }}
      columns={columns.map(item => ({ ...item }))}
      rowKey="id"
      dataSource={vehicles}
      onChange={handleTableChange}
    />
  );
}
