import React from 'react';
import CardBox from './CardBox';
import { useRouter } from 'next/router';
import { useTranslation } from 'react-i18next';
import { translateCrudLabel } from '../helpers/translateCrudLabel';

const SearchResults = ({ searchResults, searchQuery }) => {
  const router = useRouter();
  const { t } = useTranslation('common');

  return (
    <>
      <p className={'block font-bold mb-2'}>{t('labels.matches_with', { query: searchQuery })}</p>
      {Object.keys(searchResults).map((tableName) => (
        <>
          <p className={'block font-bold mb-2'}>{translateCrudLabel(tableName, t)}</p>
          <CardBox
            className='mb-6 border border-gray-300 rounded overflow-hidden'
            hasTable
            key={tableName}
          >
            <div className='overflow-x-auto'>
              <table>
                <thead>
                  <tr>
                    {searchResults[tableName].length > 0 &&
                      Object.keys(searchResults[tableName][0]).map((key) => {
                        if (
                          key !== 'tableName' &&
                          key !== 'id' &&
                          key !== 'matchAttribute'
                        ) {
                          return (
                            <th data-label={key} key={key}>
                              {translateCrudLabel(key, t)}
                            </th>
                          );
                        }
                        return null;
                      })}
                  </tr>
                </thead>
                <tbody>
                  {searchResults[tableName].map((item, index) => (
                    <tr key={index}>
                      {Object.keys(item).map((key) => {
                        if (
                          key !== 'tableName' &&
                          key !== 'id' &&
                          key !== 'matchAttribute'
                        ) {
                          return (
                            <td
                              key={key}
                              onClick={() =>
                                router.push(
                                  `/${tableName}/${tableName}-view/?id=${item['id']}`,
                                )
                              }
                            >
                              {item[key]}
                            </td>
                          );
                        }
                        return null;
                      })}
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
            {!Object.keys(searchResults).length && (
              <div className={'text-center py-4'}>{translateCrudLabel('No data', t)}</div>
            )}
          </CardBox>
        </>
      ))}
      {!Object.keys(searchResults).length && (
        <div className={'py-4'}>{translateCrudLabel('No matches', t)}</div>
      )}
    </>
  );
};

export default SearchResults;
