<?php
$ADMIN_PASS = "1234"; // troque
$dbPath = __DIR__ . "/storage/devices_db.json";//////caminho do estoque

$pass = $_POST["pass"] ?? $_GET["pass"] ?? "";
if ($pass !== $ADMIN_PASS) {
  http_response_code(401);
  echo "401 - senha incorreta. Use ?pass=SUASENHA";
  exit;
}

if (!file_exists($dbPath)) {
  file_put_contents($dbPath, json_encode(new stdClass(), JSON_PRETTY_PRINT));
}

$db = json_decode(file_get_contents($dbPath), true);
if (!is_array($db)) $db = [];

function norm_mac($mac) {
  $mac = strtoupper(trim($mac));
  $mac = str_replace(["-", "."], ":", $mac);
  return $mac;
}

function save_db($path, $db) {
  file_put_contents($path, json_encode($db, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

$msg = "";

$action = $_POST["action"] ?? "";

if ($action === "save") {
  $mac = norm_mac($_POST["mac"] ?? "");
  $active = ($_POST["active"] ?? "0") === "1";
  $note = trim($_POST["note"] ?? "");

  if ($mac === "") {
    $msg = "MAC invalido";
  } else {
    if (!isset($db[$mac])) {
      $db[$mac] = ["active" => $active, "token" => "", "note" => $note, "updated_at" => gmdate("c")];
    } else {
      $db[$mac]["active"] = $active;
      $db[$mac]["note"] = $note;
      $db[$mac]["updated_at"] = gmdate("c");
    }
    save_db($dbPath, $db);
    $msg = "Salvo: $mac";
  }
}

if ($action === "delete") {
  $mac = norm_mac($_POST["mac"] ?? "");
  unset($db[$mac]);
  save_db($dbPath, $db);
  $msg = "Removido: $mac";
}

if ($action === "reset") {
  $mac = norm_mac($_POST["mac"] ?? "");
  if (isset($db[$mac])) {
    $db[$mac]["token"] = "";
    $db[$mac]["updated_at"] = gmdate("c");
    save_db($dbPath, $db);
    $msg = "Token resetado: $mac";
  }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>viso360 - MAC admin</title>
<style>
body { font-family: Arial; background:#f4f6f8; margin:30px; }
h2 { margin-bottom:10px; }
.card { background:white; padding:20px; border-radius:6px; box-shadow:0 2px 6px rgba(0,0,0,0.08); }
input, select { padding:6px; margin-right:10px; }
button { padding:6px 12px; cursor:pointer; }
table { border-collapse: collapse; width:100%; margin-top:20px; }
th, td { border:1px solid #ddd; padding:8px; text-align:left; }
th { background:#f0f0f0; }
.msg { margin:10px 0; color:green; }
.small { font-size:12px; color:#666; }
</style>
</head>
<body>

<div class="card">
<h2>MAC management</h2>

<?php if ($msg) echo "<div class='msg'>$msg</div>"; ?>

<form method="post">
<input type="hidden" name="pass" value="<?php echo htmlspecialchars($pass); ?>">
<input type="hidden" name="action" value="save">

<label>MAC</label>
<input name="mac" placeholder="AA:BB:CC:DD:EE:FF" required>

<label>Status</label>
<select name="active">
<option value="1">Active</option>
<option value="0">Disabled</option>
</select>

<label>Note</label>
<input name="note" placeholder="Client or test">

<button type="submit">Save</button>
</form>

<table>
<tr>
<th>MAC</th>
<th>Status</th>
<th>Token set</th>
<th>Note</th>
<th>Updated</th>
<th>Action</th>
</tr>

<?php foreach ($db as $mac => $rec): ?>
<tr>
<td><?php echo htmlspecialchars($mac); ?></td>
<td><?php echo !empty($rec["active"]) ? "Active" : "Disabled"; ?></td>
<td><?php echo !empty($rec["token"]) ? "Yes" : "No"; ?></td>
<td><?php echo htmlspecialchars($rec["note"] ?? ""); ?></td>
<td><?php echo htmlspecialchars($rec["updated_at"] ?? ""); ?></td>
<td>
<form method="post" style="display:inline;">
<input type="hidden" name="pass" value="<?php echo htmlspecialchars($pass); ?>">
<input type="hidden" name="action" value="reset">
<input type="hidden" name="mac" value="<?php echo htmlspecialchars($mac); ?>">
<button type="submit">Reset token</button>
</form>
<form method="post" style="display:inline;">
<input type="hidden" name="pass" value="<?php echo htmlspecialchars($pass); ?>">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="mac" value="<?php echo htmlspecialchars($mac); ?>">
<button type="submit">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>

</table>

<p class="small">Access via: /luiz/admin.php?pass=YOURPASS</p>

</div>

</body>
</html>