import React from "react";
import SortableTable from "./SortableTable";
import {THeader} from "./SortableTable";
import {CFlight} from '../classes/CFlight';

function Link2Flight(u_key: string, key: string, row: any, data: string | null)
{
  var val: JSX.Element = <></>;
  const link: string = 'https://de.dhv-xc.de/flight/' + row['IDFlight'];
  val = <td className='link' key={u_key}><a href={link} target="_blank" rel="noopener noreferrer" >{data}</a></td>;

  return val;
}

export function Meter2Km(u_key: string, key: string, row: any, data: string | null)
{
  var val: JSX.Element = <></>;
  const numdata: number = +data! / 1000;
  val = <td key={u_key}>{numdata.toFixed(2)}</td>;

  return val;
}

export function FixNachKomma00(u_key: string, key: string, row: any, data: string | null)
{
  var val: JSX.Element = <></>;
  const numdata: number = +data!;
  val = <td key={u_key}>{numdata.toFixed(2)}</td>;

  return val;
}

export default function FlightsTable({flightdata}: {flightdata: CFlight[]}) 
{
  const headers: THeader[] = [
    { key: "IDFlight", label: "ID", visible: false },
    { key: "FlightDate", label: "Datum", callback: Link2Flight },
    { key: "TakeoffWaypointName", label: "Start" },
    { key: "Glider", label: "Gleitschirm" },
    { key: "BestTaskDistance", label: "Strecke", callback: Meter2Km },
    { key: "BestTaskType", label: "Streckentyp" },
    { key: "BestTaskPoints", label: "Punkte", callback: FixNachKomma00 },
    { key: "FlightDurationDispl", label: "Dauer" }
  ];

  return(
<div className='App'>
  <SortableTable<CFlight> headers={headers} dataTbl={flightdata} ></SortableTable>
</div>
  );
}
