<?php
/** @var array $ref */
/** @var array $u */
if (!function_exists('site_url')) {
    include dirname(__DIR__, 2) . '/config/url_helper.php';
}
$kycUrl = site_url('m/profile/kyc');
?>
<style>
.member-ref-box { background:#111; padding:15px; border-radius:10px; margin-bottom:16px; color:#fff; }
.member-ref-box h4 { color:#ffc107; margin-bottom:10px; font-size:16px; }
.member-ref-stat { background:#1a1a1a; border-radius:8px; padding:12px; text-align:center; margin-bottom:10px; }
.member-ref-stat .num { font-size:20px; font-weight:700; color:#ffc107; }
.member-ref-stat .lbl { font-size:12px; color:#aaa; }
.member-ref-alert { background:#2a2a2a; border-left:4px solid #ffc107; padding:12px; border-radius:6px; margin-bottom:16px; color:#eee; }
.member-ref-alert a { color:#ffc107; font-weight:600; }
</style>

<div class="tab-pane active">
    <div class="container-b3">
        <h3 class="mb-3" style="color:#ffc107;">Referral Member</h3>
        <p class="text-muted small">Ajak teman daftar lewat link Anda dan pantau downline di sini.</p>

        <?php if (!$ref['is_member']) { ?>
            <div class="member-ref-alert">Fitur referral hanya untuk akun member.</div>
        <?php } elseif (!$ref['kyc_verified']) { ?>
            <div class="member-ref-alert">
                <?php if ($ref['kyc_pending']) { ?>
                    Pengajuan KYC Anda sedang diproses admin. Setelah disetujui, kode & link referral akan aktif.
                <?php } else { ?>
                    Aktifkan kode referral dengan verifikasi KYC terlebih dahulu.
                    <br><a href="<?php echo htmlspecialchars($kycUrl); ?>">Klik di sini — Formulir Aktivasi Kode Referral</a>
                <?php } ?>
            </div>
        <?php } else { ?>
            <div class="member-ref-box">
                <h4>Kode Referral Anda</h4>
                <input type="text" class="form-control text-center mb-2" value="<?php echo htmlspecialchars($ref['code']); ?>" readonly id="memberRefCode">
                <h4>Link Referral Anda</h4>
                <input type="text" class="form-control text-center mb-2" value="<?php echo htmlspecialchars($ref['invite_url']); ?>" readonly id="memberRefLink">
                <button type="button" class="btn btn-warning btn-block font-weight-bold" onclick="memberCopyRefLink()">Salin Link</button>
            </div>
        <?php } ?>

        <div class="row mb-3">
            <div class="col-4">
                <div class="member-ref-stat">
                    <div class="num"><?php echo (int) $ref['stats']['total']; ?></div>
                    <div class="lbl">Total downline</div>
                </div>
            </div>
            <div class="col-4">
                <div class="member-ref-stat">
                    <div class="num"><?php echo (int) $ref['stats']['pernah_deposit']; ?></div>
                    <div class="lbl">Sudah deposit</div>
                </div>
            </div>
            <div class="col-4">
                <div class="member-ref-stat">
                    <div class="num"><?php echo (int) $ref['stats']['belum_deposit']; ?></div>
                    <div class="lbl">Belum deposit</div>
                </div>
            </div>
        </div>

        <h4 class="mb-2" style="color:#fff;">Downline saya</h4>
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped" id="member-referral-table">
                <thead>
                    <tr>
<th>CUID</th>
<th>Username</th>
<th>Nama</th>
<th>Tanggal Daftar</th>
<th>Deposit Pertama</th>
<th>Deposit Terakhir</th>
<th>Deposit</th>
<th>KYC</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if (empty($ref['downline'])) {
                        echo '<tr><td colspan="8" class="text-center">Belum ada member yang mendaftar lewat referral Anda.</td></tr>';
                    } else {
                        foreach ($ref['downline'] as $row) {
                            $joinFmt = !empty($row['join_date']) ? date('d/m/Y', strtotime($row['join_date'])) : '-';

$firstDep = !empty($row['first_dep'])
    ? date('d/m/Y H:i', strtotime($row['first_dep']))
    : '-';

$lastDep = !empty($row['last_tx'])
    ? date('d/m/Y H:i', strtotime($row['last_tx']))
    : '-';

$dep = ((float) $row['total_deposit'] > 0) ? 'Sudah' : 'Belum';
$kyc = ((int) $row['status_kyc'] === 1) ? 'Verified' : 'Pending';
                            echo '<tr>';
                            echo '<td>' . (int) $row['cuid'] . '</td>';
                            echo '<td>' . htmlspecialchars($row['username']) . '</td>';
                            echo '<td>' . htmlspecialchars($row['full_name'] ?: '-') . '</td>';
                            echo '<td>' . $joinFmt . '</td>';
echo '<td>' . $firstDep . '</td>';
echo '<td>' . $lastDep . '</td>';
echo '<td>' . $dep . '</td>';
echo '<td>' . $kyc . '</td>';
                            echo '</tr>';
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
function memberCopyRefLink() {
    var el = document.getElementById('memberRefLink');
    if (!el || !el.value) {
        alert('Link belum aktif. Selesaikan KYC terlebih dahulu.');
        return;
    }
    var text = el.value;
    if (navigator.clipboard && window.isSecureContext) {
        navigator.clipboard.writeText(text).then(function() { alert('Link referral berhasil disalin!'); });
    } else {
        el.select();
        document.execCommand('copy');
        alert('Link referral berhasil disalin!');
    }
}
</script>
