import React from 'react';
import './DataDisplay.css';

function DataDisplay({ readings, selectedPort }) {
  if (!readings || readings.length === 0) {
    return (
      <div className="data-display empty">
        <p>No data available. Waiting for COM port readings...</p>
      </div>
    );
  }

  const latestReading = readings[0];

  return (
    <div className="data-display">
      <div className="reading-card latest">
        <h2>Latest Reading</h2>
        <div className="reading-info">
          <div className="info-item">
            <label>Port:</label>
            <span>{latestReading.port}</span>
          </div>
          <div className="info-item">
            <label>Data:</label>
            <span className="data-value">{latestReading.data}</span>
          </div>
          <div className="info-item">
            <label>Time:</label>
            <span>{new Date(latestReading.timestamp).toLocaleString()}</span>
          </div>
        </div>
      </div>

      {/* <div className="readings-history">
        <h2>Recent Readings ({readings.length})</h2>
        <div className="table-container">
          <table>
            <thead>
              <tr>
                <th>Port</th>
                <th>Data</th>
                <th>Timestamp</th>
              </tr>
            </thead>
            <tbody>
              {readings.map((reading, idx) => (
                <tr key={reading.id || idx}>
                  <td>{reading.port}</td>
                  <td className="data-cell">{reading.data}</td>
                  <td>{new Date(reading.timestamp).toLocaleString()}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div> */}
    </div>
  );
}

export default DataDisplay;
