<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dashboard</title>
  <!-- CSS only -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
  <script src="https://code.jquery.com/jquery-3.6.3.js"></script>
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons+Sharp" rel="stylesheet">
  <link rel="stylesheet" href="./style.css">
  <link rel="stylesheet" href="choices.min.css" />
</head>
<body>
  <div class="loader-wrapper" id="loader">
    <span class="loader">
      <span class="loader-inner"></span>
    </span>
  </div>

  <!-- Success Box -->
  <div id="successBox" class="success-box">
    <button class="close-btn" onclick="closeSuccessBox()">×</button>
    <div class="icon">✔️</div>
    <h2>Successfully!</h2>
    <p>This action was successfully completed.</p>
  </div>

  <div class="container">
    <aside>
      <div class="top">
        <div class="logo">
          <img src="./img/1.png">
          <h2><span class="danger">FEIN</span> SHOP</h2>
        </div>
        <div class="close" id="close-btn">
          <span class="material-icons-sharp">close</span>
        </div>
      </div>
      <div class="sidebar">
        <a href="dashboard.php">
          <span class="material-icons-sharp">grid_view</span>
          <h3>Dashboard</h3>
        </a>
        <div class="sidebar">
        <a href="redeem.php">
          <span class="material-icons-sharp">grid_view</span>
          <h3>Redeem Key</h3>
        </a>
		<a href="support.php">
          <span class="material-icons-sharp">grid_view</span>
          <h3>Support</h3>
        </a>
        <a href="logout.php">
          <span class="material-icons-sharp">logout</span>
          <h3>Logout</h3>
        </a>
      </div>
    </aside>

    <main>
      <h1>Dashboard</h1>
      <div class="insights">
        <!-- Total Global Bans and other insights -->
        <div class="bans">
          <span class="material-icons-sharp" style="margin-bottom: 1rem">person_off</span>
          <div class="middle">
            <div class="left">
              <h3>Total Blacklists</h3>
              <h1>
                <?php
                $stmt = $pdo->prepare('SELECT count(*) FROM globalbans');
                $stmt->execute();
                $count = $stmt->fetchColumn();
                echo $count;
                ?>
              </h1>
            </div>
          </div>
          <small class="text-muted">Last 24 Hours</small>
        </div>

        <!-- Total Users -->
        <div class="users">
          <span class="material-icons-sharp" style="margin-bottom: 1rem">person</span>
          <div class="middle">
            <div class="left">
              <h3>Total Users</h3>
              <h1>
                <?php
                $stmt = $pdo->prepare('SELECT count(*) FROM licenses');
                $stmt->execute();
                $count = $stmt->fetchColumn();
                echo $count;
                ?>
              </h1>
            </div>
          </div>
          <small class="text-muted">Last 24 Hours</small>
        </div>

        <!-- Other Insights -->
        <div class="discord-members">
          <span class="material-icons-sharp" style="margin-bottom: 1rem">person</span>
          <div class="middle">
            <div class="left">
              <h3>Online Discord Members</h3>
              <h1 id="discord-member-count">Loading...</h1>
            </div>
          </div>
          <small class="text-muted">Last 24 Hours</small>
        </div>
      </div>
      <div class="order">
        <h2>Subscriptions</h2>
        <table>
          <thead>
            <tr>
              <th>Server Name</th>
              <th>Expire</th>
              <th>IP</th>
              <th>Framework</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $stmt = $pdo->prepare('SELECT * FROM licenses WHERE discord_id = ? and status = 1');
            $stmt->execute([$_SESSION['user']->id]);
            $licenses = $stmt->fetchAll(PDO::FETCH_OBJ);
            foreach ($licenses as $license) {
              echo '
                <tr>
                  <td>'.$license->server_name.'</td>
                  <td>'.$license->script.'</td>
                  <td>'.$license->expire.'</td>
                  <td>'.$license->ip.'</td>
                  <td>'.$license->framework.'</td>
                  <td>
                  <form action="change_ip.php" method="POST" class="change-ip-form">
                    <input type="text" name="new_ip" placeholder="New IP" required class="ip-input">
                    <button type="submit" class="ip-submit">Change IP</button>
                  </form>
                  </td>
                </tr>
              ';
            }
            ?>
          </tbody>
        </table>
      </div>
    </main>

    <div class="right">
      <div class="top">
        <button id="menu-btn">
          <span class="material-icons-sharp">menu</span>
        </button>
        <div class="theme-toggler">
          <span class="material-icons-sharp active">light_mode</span>
          <span class="material-icons-sharp">dark_mode</span>
        </div>
        <div class="profile">
          <div class="info">
            <p>
              Hey, <b><?php echo $_SESSION['user']->username; ?></b>
            </p>
            <?php
              $stmt = $pdo->prepare('SELECT * FROM licenses WHERE discord_id = ?');
              $stmt->execute([$_SESSION['user']->id]);
              $license = $stmt->fetch(PDO::FETCH_ASSOC);
              if ($license) { ?>
              <small class="text-muted">Customer</small>
            <?php } else { ?>
              <small class="text-muted">Guest</small>
            <?php }
            ?>
          </div>
          <div class="profile-photo">
            <img src="https://cdn.discordapp.com/avatars/<?php echo $_SESSION['user']->id ?>/<?php echo $_SESSION['user']->avatar ?>.png">
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="https://unpkg.com/@popperjs/core@2"></script>
  <script src="https://unpkg.com/tippy.js@6"></script>
  <script src="choices.min.js"></script>
  <script src="./index.js"></script>
  <script>
    if (<?php echo $showSuccess ? 'true' : 'false'; ?>) {
      showSuccessBox();
    }
    setTimeout(closeSuccessBox, 5000);
  </script>
</body>
</html>
