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 PhoneFormatter from "../../utils/PhoneFormatter";
import OwnerNameDisplay from "../owner-name-display/owner-name-display.component";

export default function OwnersListComponent({
  loading,
  owners,
  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("owners.fields.name"),
      dataIndex: "name",
      key: "name",
      render: (text, record) => (
        <Link to={"/manage/owners/" + record.id}>
          <OwnerNameDisplay ownerObject={record} />
        </Link>
      ),
    },
    {
      title: t("owners.fields.ownr_ph1"),
      dataIndex: "ownr_ph1",
      key: "ownr_ph1",
      render: (text, record) => {
        return <PhoneFormatter>{record.ownr_ph1}</PhoneFormatter>;
      },
    },
    {
      title: t("owners.fields.ownr_ph2"),
      dataIndex: "ownr_ph2",
      key: "ownr_ph2",
      render: (text, record) => {
        return <PhoneFormatter>{record.ownr_ph2}</PhoneFormatter>;
      },
    },
    {
      title: t("owners.fields.ownr_ea"),
      dataIndex: "ownr_ea",
      key: "ownr_ea",
    },
    {
      title: t("owners.fields.address"),
      dataIndex: "address",
      key: "address",
      render: (text, record) => {
        return (
          <div>{`${record.ownr_addr1 || ""} ${record.ownr_addr2 || ""} ${
            record.ownr_city || ""
          } ${record.ownr_st || ""} ${record.ownr_zip || ""}`}</div>
        );
      },
    },
  ];

  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.owners")}
      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: 25,
          current: parseInt(page || 1),
          total: total,
        }}
        columns={columns}
        rowKey="id"
        scroll={{ x: true }}
        dataSource={owners}
        onChange={handleTableChange}
      />
    </Card>
  );
}
