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

export default function JobsCreateVehicleInfoSearchComponent({
  loading,
  vehicles
}) {
  const [state, setState] = useContext(JobCreateContext);
  const [tableState, setTableState] = 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:
        tableState.sortedInfo.columnKey === "v_vin" &&
        tableState.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) => {
    setTableState({ ...tableState, filteredInfo: filters, sortedInfo: sorter });
  };
  //TODO Implement searching & pagination

  return (
    <Table
      loading={loading}
      title={() => {
        return (
          <Input.Search
            placeholder="Search..."
            onSearch={value => {
              setState({
                ...state,
                vehicle: { ...state.vehicle, search: value }
              });
            }}
            enterButton
          />
        );
      }}
      size="small"
      pagination={{ position: "top" }}
      columns={columns.map(item => ({ ...item }))}
      rowKey="id"
      dataSource={vehicles}
      onChange={handleTableChange}
      rowSelection={{
        onSelect: props => {
          setState({
            ...state,
            vehicle: {
              ...state.vehicle,
              new: false,
              selectedid: props.id,
              vehicleObj: props
            }
          });
        },
        type: "radio",
        selectedRowKeys: [state.vehicle.selectedid]
      }}
      onRow={(record, rowIndex) => {
        return {
          onClick: event => {
            if (record) {
              if (record.id) {
                setState({
                  ...state,
                  vehicle: {
                    ...state.vehicle,
                    new: false,
                    selectedid: record.id,
                    vehicleObj: record
                  }
                });

                return;
              }
            }
            setState({
              ...state,
              vehicle: { ...state.vehicle, selectedid: null, vehicleObj: null }
            });
          }
        };
      }}
    />
  );
}
