<?php
include 'session.php';
include 'header.php';
include '../config/koneksi.php';

// Ambil data user login
if (isset($_SESSION['user'])) {
    $user = mysqli_query($conn, "SELECT * FROM tb_user WHERE username = '" . $_SESSION['user'] . "'");
    $u = mysqli_fetch_array($user);
    $userID = $u['cuid'];
    $myCode = $u['referral_code'];
    $myLink = "https://".$_SERVER['HTTP_HOST']."/m/register/?reff=".$myCode;
}
?>

<div class="tab-pane active">
    <div class="container-b3">

        <!-- BOX REFERRAL -->
        <div style="background:#111;padding:15px;border-radius:10px;margin-bottom:20px;color:#fff;">
            <h4>Kode Referral Anda</h4>
            <input type="text" value="<?= $myCode ?>" readonly style="width:100%;padding:8px;margin-bottom:10px;text-align:center;">

            <h4>Link Referral Anda</h4>
            <input type="text" id="refLink" value="<?= $myLink ?>" readonly style="width:100%;padding:8px;text-align:center;">

            <button onclick="copyRef()" style="margin-top:10px;padding:8px 15px;background:#ffc107;border:none;border-radius:5px;font-weight:bold;">
                Copy Link
            </button>
        </div>

        <!-- FORM CARI -->
        <div class="row history">
            <div class="pb-4 pb-md-0 col-md-9">
                <form id="searchhistory" class="needs-validation searchhistory">
                    <div class="box-wrapper plr-15">
                        <div class="row d-flex align-items-center">
                            <div class="col-md-9 col-xs-8 d-flex flex-wrap">
                                <button type="submit" class="btn btn-primary m-15">Cari</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- TABEL DOWNLINE -->
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped" id="referral-table">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Deposit</th>
                        <th>Tanggal Transaksi Pertama</th>
                        <th>Tanggal Transaksi Terakhir</th>
                    </tr>
                </thead>
                <tbody>
<?php
$sql = "SELECT u.*, 
               IFNULL(SUM(t.total), '-') AS total_transaksi,
               IFNULL(MIN(t.date), '-') AS tanggal_transaksi_pertama,
               IFNULL(MAX(t.date), '-') AS tanggal_transaksi_terakhir
        FROM tb_user u
        LEFT JOIN tb_transaksi t 
            ON u.cuid = t.userID 
            AND t.jenis = 1 
            AND t.status = 1
        WHERE u.referral_user_id = '$userID'
        GROUP BY u.cuid";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {

        echo "<tr style='color:red;font-weight:bold;'>";
        echo "<td>" . htmlspecialchars($row["username"]) . "</td>";

        $deposit = ($row["total_transaksi"] !== '-') ? "Sudah" : "Belum";
        echo "<td>$deposit</td>";

        $tanggal_pertama = ($row["tanggal_transaksi_pertama"] !== '-') ? $row["tanggal_transaksi_pertama"] : "-";
        echo "<td>$tanggal_pertama</td>";

        $tanggal_terakhir = ($row["tanggal_transaksi_terakhir"] !== '-') ? $row["tanggal_transaksi_terakhir"] : "-";
        echo "<td>$tanggal_terakhir</td>";

        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='4'>Tidak ada data referral.</td></tr>";
}
?>
                </tbody>
            </table>
        </div>

    </div>
</div>

<script>
function copyRef() {
  var copyText = document.getElementById("refLink");
  copyText.select();
  copyText.setSelectionRange(0, 99999);
  document.execCommand("copy");
  alert("Link referral berhasil disalin!");
}
</script>

<?php include 'footer.php'; ?>