<?php
require_once '../connection/db.php';
requireLogin();

$user     = getCurrentUser();
$memberId = (int)$user['member_id'];
$db       = getDB();

// All activation details for this member from meddolic_user_activation_details
$actQ = $db->prepare("
    SELECT ad.*, pl.poolName, pl.upgradePrice, pl.poolLevel
    FROM meddolic_user_activation_details ad
    LEFT JOIN meddolic_config_pool_list pl ON pl.id = ad.packageId
    WHERE ad.member_id = ?
    ORDER BY ad.date_time DESC
");
$actQ->execute([$memberId]);
$activations = $actQ->fetchAll();

$pageTitle  = 'My Activation';
$activePage = 'active';
require_once 'Include/header.php';
?>
<div class="layout">
  <?php require_once 'Include/menu.php'; ?>
  <div class="main-content">
    <?php require_once 'Include/topbar.php'; ?>
    <div class="page-content">
      <div class="page-header">
        <div class="breadcrumb"><i class="fas fa-home"></i> <i class="fas fa-chevron-right"></i> My Activation</div>
        <h3>Activation History</h3>
        <div class="subtitle">All package purchases &amp; activation records</div>
      </div>

      <div class="card">
        <div class="card-title"><i class="fas fa-history"></i> Activation Details</div>
        <div class="table-responsive">
          <table class="data-table">
            <thead>
              <tr>
                <th>#</th>
                <th>Package Name</th>
                <th>Level</th>
                <th>Amount ($)</th>
                <th>TX Hash</th>
                <th>Type</th>
                <th>Date</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($activations)): ?>
                <tr class="empty-row"><td colspan="8"><i class="fas fa-inbox"></i> No activation records found</td></tr>
              <?php else: foreach ($activations as $i => $a):
                $typeLabel = ['1'=>'Register','2'=>'Upgrade','3'=>'Matrix Buy'];
                $tl = $typeLabel[$a['buyType']] ?? 'Unknown';
              ?>
              <tr>
                <td><?= $i+1 ?></td>
                <td><span class="badge badge-blue"><?= e($a['poolName'] ?? 'Package '.$a['packageId']) ?></span></td>
                <td><?= (int)($a['poolLevel'] ?? 0) ?></td>
                <td class="amount">$<?= number_format($a['investPrice'], 3) ?></td>
                <td style="font-family:monospace;font-size:11px;color:#555">
                  <?php if ($a['transHash']): ?>
                    <?= e(substr($a['transHash'],0,18)) ?>…
                  <?php else: ?>—<?php endif; ?>
                </td>
                <td>
                  <?php
                  $colors=['Register'=>'badge-green','Upgrade'=>'badge-gold','Matrix Buy'=>'badge-blue'];
                  echo '<span class="badge '.($colors[$tl]??'badge-blue').'">'.$tl.'</span>';
                  ?>
                </td>
                <td style="font-size:12px;color:var(--text-muted)"><?= date('d M Y H:i', strtotime($a['date_time'])) ?></td>
                <td>
                  <?= (int)$a['status'] ? '<span class="badge badge-green">Active</span>' : '<span class="badge badge-red">Inactive</span>' ?>
                </td>
              </tr>
              <?php endforeach; endif; ?>
            </tbody>
          </table>
        </div>
      </div>

    </div>
    <?php require_once 'Include/footer.php'; ?>
  </div>
</div>
