<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Device Approval</title>
  <style>
    /* General Reset */
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: Arial, sans-serif;
      background-color: #f0f2f5;
      color: #333;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;
    }

    h1 {
      margin-bottom: 20px;
      font-size: 2em;
      color: #4a90e2;
      text-align: center;
    }

    /* Table Styling */
    table {
      width: 100%;
      max-width: 900px;
      border-collapse: collapse;
      margin-top: 20px;
      background-color: #fff;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
    }

    th, td {
      padding: 12px 20px;
      text-align: left;
    }

    th {
      background-color: #4a90e2;
      color: #fff;
      font-weight: 600;
      font-size: 1em;
    }

    td {
      border-bottom: 1px solid #e0e0e0;
      font-size: 0.95em;
    }

    tr:last-child td {
      border-bottom: none;
    }

    /* Button Styling */
    button {
      padding: 8px 16px;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      font-size: 0.9em;
      font-weight: bold;
      transition: background-color 0.3s ease, transform 0.2s ease;
      color: #fff;
      margin-right: 5px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
    }

    button:hover {
      transform: translateY(-2px);
      opacity: 0.95;
    }

    button:active {
      transform: translateY(1px);
      opacity: 1;
    }

    button:first-of-type {
      background-color: #4CAF50; /* Approve */
    }

    button:nth-of-type(2) {
      background-color: #f0ad4e; /* Reject */
    }

    button:last-of-type {
      background-color: #d9534f; /* Delete */
    }

    /* Responsive Design */
    @media (max-width: 600px) {
      th, td {
        padding: 8px 12px;
        font-size: 0.9em;
      }

      h1 {
        font-size: 1.5em;
      }

      button {
        font-size: 0.8em;
        padding: 6px 12px;
      }
    }
  </style>
</head>
<body>
  <h1>Device Approval Panel</h1>
  <table>
    <thead>
      <tr>
        <th>Device ID</th>
        <th>Device Name</th>
        <th>Status</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody id="deviceTableBody">
      <!-- Device rows will be injected here -->
    </tbody>
  </table>

  <script>
    async function fetchPendingDevices() {
      try {
        const response = await fetch('api/device-list/get');
        const devices = await response.json();

        const tableBody = document.getElementById('deviceTableBody');
        tableBody.innerHTML = ''; // Clear existing rows

        devices.forEach(device => {
          const row = document.createElement('tr');
          
          row.innerHTML = `
            <td>${device.deviceId}</td>
            <td>${device.deviceName || 'Unknown Device'}</td>
            <td>${device.status}</td>
            <td>
              <button onclick="approveDevice('${device.deviceId}', 'approve')">Approve</button>
              <button onclick="approveDevice('${device.deviceId}', 'reject')">Reject</button>
              <button onclick="deleteDevice('${device.deviceId}')">Delete</button>
            </td>
          `;

          tableBody.appendChild(row);
        });
      } catch (error) {
        console.error('Error fetching pending devices:', error);
      }
    }

    async function approveDevice(deviceId, action) {
      try {
        const response = await fetch('api/device-list/approve-device', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ deviceId, action })
        });

        if (response.ok) {
          alert(`Device ${action}d successfully.`);
          fetchPendingDevices();
        } else {
          const errorData = await response.json();
          alert(`Failed to ${action} device: ${errorData.error}`);
        }
      } catch (error) {
        console.error(`Error trying to ${action} device:`, error);
      }
    }

    async function deleteDevice(deviceId) {
      if (!confirm("Are you sure you want to delete this device?")) return;

      try {
        const response = await fetch(`api/device-list/delete-device`, {
          method: 'DELETE',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ deviceId })
        });

        if (response.ok) {
          alert("Device deleted successfully.");
          fetchPendingDevices();
        } else {
          const errorData = await response.json();
          alert(`Failed to delete device: ${errorData.error}`);
        }
      } catch (error) {
        console.error("Error deleting device:", error);
      }
    }

    fetchPendingDevices();
  </script>
</body>
</html>
