import { SyncOutlined } from "@ant-design/icons";
import { Button, Card, Input, Space, Table, Typography } from "antd";
import queryString from "query-string";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { Link, useHistory, useLocation } from "react-router-dom";
import { pageLimit } from "../../utils/config";
import VehicleVinDisplay from "../vehicle-vin-display/vehicle-vin-display.component";
import { alphaSort } from './../../utils/sorters';
export default function VehiclesListComponent({
  loading,
  vehicles,
  total,
  refetch,
}) {
  const search = queryString.parse(useLocation().search);
  const {
    page,
    //sortcolumn, sortorder,
  } = search;
  const history = useHistory();

  const [state, setState] = useState({
    sortedInfo: {},
    filteredInfo: { text: "" },
  });

  const { t } = useTranslation();

  const columns = [
    {
      title: t("vehicles.fields.v_vin"),
      dataIndex: "v_vin",
      key: "v_vin",
      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}>
          <VehicleVinDisplay>{record.v_vin || "N/A"}</VehicleVinDisplay>
        </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_no",
      key: "plate_no",
      sorter: (a, b) => alphaSort(a.plate_no, b.plate_no),
      sortOrder: state.sortedInfo.columnKey === "plate_no" && state.sortedInfo.order,
      render: (text, record) => {
        return (
          <span>{`${record.plate_st || ""} | ${record.plate_no || ""}`}</span>
        );
      },
    },
  ];

  const handleTableChange = (pagination, filters, sorter) => {
    setState({ ...state, filteredInfo: filters, sortedInfo: sorter });
    search.page = pagination.current;
    search.sortcolumn = sorter.columnKey;
    search.sortorder = sorter.order;
    history.push({ search: queryString.stringify(search) });
  };

  return (
    <Card
      title={t("menus.header.vehicles")}
      extra={
        <Space wrap>
          {search.search && (
            <>
              <Typography.Title level={4}>
                {t("general.labels.searchresults", { search: search.search })}
              </Typography.Title>
              <Button
                onClick={() => {
                  delete search.search;
                  history.push({ search: queryString.stringify(search) });
                }}
              >
                {t("general.actions.clear")}
              </Button>
            </>
          )}
          <Button onClick={() => refetch()}>
            <SyncOutlined />
          </Button>
          <Input.Search
            placeholder={search.search || t("general.labels.search")}
            onSearch={(value) => {
              search.search = value;
              history.push({ search: queryString.stringify(search) });
            }}
            enterButton
          />
        </Space>
      }
    >
      <Table
        loading={loading}
        pagination={{
          position: "top",
          pageSize: pageLimit,
          current: parseInt(page || 1),
          total: total,
        }}
        columns={columns}
        rowKey="id"
        scroll={{ x: true }}
        dataSource={vehicles}
        onChange={handleTableChange}
      />
    </Card>
  );
}
