import React from 'react';

interface Field {
  key: string;
  label: string;
}

interface Props {
  items: any[];
  fields: Field[];
  handleRowClick?: (index: number) => any;
}


const ListTable: React.FC<Props> = ({ items, fields, handleRowClick = () => {} }) => {
  return (
    <table className="m-2 table-auto w-full">
      <thead>
        <tr className="border-b select-none">
          {fields.map(field => <th>{field.label}</th>)}
        </tr>
      </thead>
      <tbody>
        {items.map((item, index) => (
          <tr
            className={`border-b hover:bg-gray-100 cursor-pointer ${index % 2 && 'bg-gray-50'}`}
            onClick={() => handleRowClick(index)}
          >
            {fields.map(field => <td className="p-3">{item[field.key]}</td>)}
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default ListTable;
