<?php
/**
 * TomGames — Square Location ID Finder
 * Upload this file, visit it in your browser ONCE to get your Location ID.
 * Then paste the ID into includes/config.php and DELETE this file.
 */

$token = 'EAAAl1ECweOVgNiwhC2SuA56QFjlfRLkYxo4xe4r2fMLvqwLT0IKGUZNNOYy1NXn';
$locations = [];
$error = '';

$ch = curl_init('https://connect.squareup.com/v2/locations');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $token,
        'Square-Version: 2024-01-18',
        'Content-Type: application/json',
    ],
    CURLOPT_TIMEOUT => 15,
]);
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($code === 200) {
    $data = json_decode($resp, true);
    $locations = $data['locations'] ?? [];
} else {
    $error = "HTTP $code: " . htmlspecialchars($resp);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Square Location ID Finder</title>
<style>
body{font-family:'Segoe UI',sans-serif;background:#0a0a12;color:#e8e8f0;max-width:600px;margin:40px auto;padding:20px}
h1{font-size:22px;background:linear-gradient(135deg,#f0c040,#00e5ff);-webkit-background-clip:text;-webkit-text-fill-color:transparent;margin-bottom:6px}
.sub{color:#8888aa;font-size:13px;margin-bottom:28px}
.loc{background:#1a1a2e;border:1px solid rgba(255,255,255,.1);border-radius:12px;padding:20px;margin-bottom:16px}
.loc-name{font-size:18px;font-weight:700;color:#e8e8f0;margin-bottom:6px}
.loc-id-wrap{display:flex;align-items:center;gap:10px;margin:10px 0}
.loc-id{font-family:monospace;font-size:18px;font-weight:700;color:#f0c040;background:#0a0a12;border:1px solid rgba(240,192,64,.4);border-radius:8px;padding:10px 16px;flex:1;letter-spacing:1px}
.copy-btn{padding:10px 16px;background:linear-gradient(135deg,#f0c040,#d4a017);border:none;border-radius:8px;color:#000;font-weight:700;font-size:13px;cursor:pointer}
.copy-btn:hover{opacity:.9}
.loc-meta{font-size:12px;color:#8888aa;margin-top:6px}
.next{background:rgba(0,229,255,.08);border:1px solid rgba(0,229,255,.2);border-radius:12px;padding:18px;margin-top:24px}
.next h2{color:#00e5ff;font-size:15px;margin-bottom:10px}
.next ol{padding-left:18px;line-height:2;color:#c0c0d8;font-size:13px}
.next code{background:#0a0a12;border:1px solid rgba(255,255,255,.15);border-radius:4px;padding:2px 7px;font-family:monospace;color:#f0c040}
.err{background:rgba(255,68,68,.1);border:1px solid rgba(255,68,68,.3);border-radius:10px;padding:16px;color:#ff6666}
.warn{background:rgba(255,214,10,.08);border:1px solid rgba(255,214,10,.2);border-radius:8px;padding:12px;margin-top:20px;font-size:12px;color:#ffd60a}
</style>
</head>
<body>
<h1>🔑 Square Location Finder</h1>
<div class="sub">TomGames — Run once, then delete this file</div>

<?php if ($error): ?>
  <div class="err"><strong>Error fetching locations:</strong><br><?= $error ?></div>
<?php elseif (empty($locations)): ?>
  <div class="err">No locations found on this Square account.</div>
<?php else: ?>
  <p style="color:#8888aa;font-size:13px;margin-bottom:16px">Found <?= count($locations) ?> location(s). Copy the ID for your main location:</p>
  <?php foreach ($locations as $loc): ?>
  <div class="loc">
    <div class="loc-name"><?= htmlspecialchars($loc['name']) ?> <?= $loc['status'] === 'ACTIVE' ? '✅' : '⚠️ ' . $loc['status'] ?></div>
    <div class="loc-id-wrap">
      <div class="loc-id" id="id-<?= $loc['id'] ?>"><?= htmlspecialchars($loc['id']) ?></div>
      <button class="copy-btn" onclick="copyId('<?= $loc['id'] ?>', this)">COPY</button>
    </div>
    <div class="loc-meta">
      <?= htmlspecialchars($loc['address']['address_line_1'] ?? '') ?>
      <?= htmlspecialchars($loc['address']['city'] ?? '') ?> ·
      Currency: <?= htmlspecialchars($loc['currency'] ?? 'USD') ?> ·
      Country: <?= htmlspecialchars($loc['country'] ?? '—') ?>
    </div>
  </div>
  <?php endforeach; ?>

  <div class="next">
    <h2>📋 Next Steps</h2>
    <ol>
      <li>Copy your Location ID above</li>
      <li>Open <code>includes/config.php</code></li>
      <li>Replace <code>YOUR_LOCATION_ID</code> with your copied ID</li>
      <li>Also update <code>DB_PASS</code> with your MySQL password</li>
      <li><strong>Delete this file from your server!</strong></li>
    </ol>
  </div>
<?php endif; ?>

<div class="warn">⚠️ <strong>Security:</strong> Delete <code>get_location.php</code> from your server after use. It exposes your access token.</div>

<script>
function copyId(id, btn) {
  navigator.clipboard.writeText(id).then(() => {
    btn.textContent = 'COPIED!';
    btn.style.background = '#00e676';
    setTimeout(() => { btn.textContent = 'COPY'; btn.style.background = ''; }, 2000);
  });
}
</script>
</body>
</html>
