<?php
session_start();
error_reporting(0);
if (!isset($_SESSION["mikhmon"])) {
  header("Location:../admin.php?id=login");
} else {
  $session = $_GET['session'];
  include('../include/config.php');
  include('../include/readcfg.php');
  include('../include/lang.php');
  include('../lang/'.$langid.'.php');
  $base = $genieacs_url ?: 'http://localhost:7557';
  $url = rtrim($base, '/').'/devices?limit=50';
  $devices = [];
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  if (!empty($genieacs_user) && !empty($genieacs_pass)) {
    curl_setopt($ch, CURLOPT_USERPWD, $genieacs_user.':'.$genieacs_pass);
  }
  $resp = curl_exec($ch);
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  if ($code >= 200 && $code < 300 && $resp) {
    $parsed = json_decode($resp, true);
    if (is_array($parsed)) {
      $devices = $parsed;
    }
  }
}
?>
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h3><i class="fa fa-plug"></i> GenieACS</h3>
      </div>
      <div class="card-body overflow">
        <table class="table table-bordered table-hover text-nowrap">
          <thead>
            <tr>
              <th>ID</th>
              <th>Serial Number</th>
              <th>Product Class</th>
              <th>Software Version</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($devices as $d) {
              $id = isset($d['_id']) ? $d['_id'] : '';
              $sn = isset($d['serialNumber']) ? $d['serialNumber'] : '';
              $pc = isset($d['productClass']) ? $d['productClass'] : '';
              $sv = isset($d['softwareVersion']) ? $d['softwareVersion'] : '';
              echo "<tr><td>{$id}</td><td>{$sn}</td><td>{$pc}</td><td>{$sv}</td></tr>";
            } ?>
          </tbody>
        </table>
        <?php if (empty($devices)) { ?>
          <div class="cl-w">Tidak ada data dari GenieACS di <?= htmlspecialchars($base); ?></div>
        <?php } ?>
      </div>
    </div>
  </div>
</div>
