<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Master Control - AIO Uploader & Scraper</title>
  <style>
    body { font-family: Arial, sans-serif; background: #0f0f0f; color: #eee; margin: 0; padding: 20px; }
    .container { max-width: 1200px; margin: auto; }
    h1 { text-align: center; color: #ff4444; }
    .tabs { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
    .tab { padding: 12px 24px; background: #222; cursor: pointer; border-radius: 8px; }
    .tab.active { background: #ff4444; color: white; }
    .panel { display: none; background: #1f1f1f; padding: 25px; border-radius: 12px; }
    .panel.active { display: block; }
    input, select, textarea, button { width: 100%; padding: 12px; margin: 10px 0; border-radius: 6px; }
    button { background: #ff4444; color: white; border: none; cursor: pointer; font-weight: bold; }
    button:hover { background: #cc0000; }
    #log { background: #111; padding: 15px; height: 400px; overflow-y: auto; font-family: monospace; white-space: pre-wrap; }
    .success { color: #4caf50; }
    .error { color: #f44336; }
  </style>
</head>
<body>
  <div class="container">
    <h1>🚀 Master Control Center</h1>
    <p style="text-align:center;">Upload • Scrape • Update JSON Database</p>

    <div class="tabs">
      <div class="tab active" onclick="showPanel(0)">📤 Upload New Content</div>
      <div class="tab" onclick="showPanel(1)">🔍 Scrape Folders</div>
      <div class="tab" onclick="showPanel(2)">📋 View / Manage</div>
    </div>

    <!-- Panel 1: Upload -->
    <div id="panel0" class="panel active">
      <?php include 'master.php?mode=upload_form'; ?>
    </div>

    <!-- Panel 2: Scrape -->
    <div id="panel1" class="panel">
      <?php include 'master.php?mode=scrape_form'; ?>
    </div>

    <!-- Panel 3: View / Manage -->
    <div id="panel2" class="panel">
      <h2>Database Status</h2>
      <button onclick="refreshAll()">Refresh All Databases</button>
      <div id="statusLog"></div>
    </div>

    <h3>Operation Log</h3>
    <div id="log"></div>
  </div>

  <script>
    function showPanel(n) {
      document.querySelectorAll('.tab').forEach((t,i) => t.classList.toggle('active', i===n));
      document.querySelectorAll('.panel').forEach((p,i) => p.classList.toggle('active', i===n));
    }

    function log(message, type = '') {
      const time = new Date().toLocaleTimeString();
      document.getElementById('log').innerHTML += `<span class="\( {type}">[ \){time}] ${message}</span><br>`;
      document.getElementById('log').scrollTop = 999999;
    }

    async function refreshAll() {
      log("Refreshing all databases...");
      try {
        const res = await fetch('master.php?mode=refresh');
        const data = await res.json();
        log(data.message, "success");
      } catch(e) {
        log("Error refreshing databases", "error");
      }
    }

    // Auto-load log on start
    window.onload = () => log("Master Control Center ready.");
  </script>
</body>
</html>