import { SyncOutlined } from "@ant-design/icons";
import { Checkbox, Divider, Input, Table, Button } from "antd";
import React from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import PhoneFormatter from "../../utils/PhoneFormatter";

export default function JobsFindModalComponent({
  selectedJob,
  setSelectedJob,
  jobsList,
  jobsListLoading,
  importOptionsState,
  modalSearchState,
  jobsListRefetch,
}) {
  const { t } = useTranslation();
  const [modalSearch, setModalSearch] = modalSearchState;
  const [importOptions, setImportOptions] = importOptionsState;

  const columns = [
    {
      title: t("jobs.fields.ro_number"),
      dataIndex: "ro_number",
      key: "ro_number",
      width: "8%",
      render: (text, record) => (
        <span>
          <Link to={"/manage/jobs/" + record.id}>
            {record.ro_number || t("general.labels.na")}
          </Link>
        </span>
      ),
    },
    {
      title: t("jobs.fields.owner"),
      dataIndex: "owner",
      key: "owner",
      ellipsis: true,

      width: "25%",

      render: (text, record) => {
        return record.owner ? (
          <Link to={"/manage/owners/" + record.owner.id}>
            {`${record.ownr_fn || ""} ${record.ownr_ln || ""} ${
              record.ownr_co_nm || ""
            }`}
          </Link>
        ) : (
          // t("jobs.errors.noowner")
          <span>{`${record.ownr_fn || ""} ${record.ownr_ln || ""} ${
            record.ownr_co_nm || ""
          }`}</span>
        );
      },
    },
    {
      title: t("jobs.fields.ownr_ph1"),
      dataIndex: "ownr_ph1",
      key: "ownr_ph1",
      width: "12%",
      ellipsis: true,
      render: (text, record) => {
        return record.ownr_ph1 ? (
          <PhoneFormatter>{record.ownr_ph1}</PhoneFormatter>
        ) : (
          t("general.labels.unknown")
        );
      },
    },
    {
      title: t("jobs.fields.status"),
      dataIndex: "status",
      key: "status",
      width: "10%",
      ellipsis: true,
      render: (text, record) => {
        return record.status || t("general.labels.na");
      },
    },

    {
      title: t("jobs.fields.vehicle"),
      dataIndex: "vehicle",
      key: "vehicle",
      width: "15%",
      ellipsis: true,
      render: (text, record) => (
        <Link to={"/manage/vehicles/" + record.vehicleid}>
          {`${record.v_model_yr || ""} ${record.v_make_desc || ""} ${
            record.v_model_desc || ""
          }`}
        </Link>
      ),
    },
    {
      title: t("vehicles.fields.plate_no"),
      dataIndex: "plate_no",
      key: "plate_no",
      width: "8%",
      ellipsis: true,
      render: (text, record) => {
        return record.plate_no ? (
          <span>{record.plate_no}</span>
        ) : (
          t("general.labels.unknown")
        );
      },
    },
    {
      title: t("jobs.fields.clm_no"),
      dataIndex: "clm_no",
      key: "clm_no",
      width: "12%",
      ellipsis: true,
      render: (text, record) => {
        return record.clm_no ? (
          <span>{record.clm_no}</span>
        ) : (
          t("general.labels.unknown")
        );
      },
    },
  ];

  const handleOnRowClick = (record) => {
    if (record) {
      if (record.id) {
        setSelectedJob(record.id);
        return;
      }
    }
    setSelectedJob(null);
  };

  return (
    <div>
      <Table
        title={() => (
          <div style={{ display: "flex" }}>
            {t("jobs.labels.existing_jobs")}
            <Button
              onClick={() => {
                jobsListRefetch();
              }}
            >
              <SyncOutlined />
            </Button>
            <Input
              value={modalSearch}
              onChange={(e) => {
                setModalSearch(e.target.value);
              }}
            />
          </div>
        )}
        pagination={{ position: "bottom" }}
        columns={columns}
        rowKey="id"
        loading={jobsListLoading}
        dataSource={jobsList}
        rowSelection={{
          onSelect: (props) => {
            setSelectedJob(props.id);
          },
          type: "radio",
          selectedRowKeys: [selectedJob],
        }}
        onRow={(record, rowIndex) => {
          return {
            onClick: (event) => {
              handleOnRowClick(record);
            },
          };
        }}
      />
      <Divider />
      <Checkbox
        defaultChecked={importOptions.overrideHeader}
        onChange={(e) =>
          setImportOptions({
            ...importOptions,
            overrideHeaders: e.target.checked,
          })
        }
      >
        {t("jobs.labels.override_header")}
      </Checkbox>
    </div>
  );
}
