<?php
session_start();
require 'functions.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: giris.php");
    exit;
}

// Oturum açan kullanıcının referans kodunu çek
$stmt = $pdo->prepare("SELECT reference_code FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();

// Kullanıcının referans ağacını çek
$referralTree = getReferralTree($pdo, $user['reference_code']);

// Referans ağacını HTML olarak göstermek için fonksiyon
function displayReferralTree($tree) {
    if (empty($tree)) {
        return;
    }

    echo '<ul class="tree-view">';
    foreach ($tree as $node) {
        echo '<li>';
        echo '<div>' . htmlspecialchars($node['username']) . ' (' . htmlspecialchars($node['reference_code']) . ')</div>';
        if (!empty($node['children'])) {
            displayReferralTree($node['children']); // Yinelemeli olarak alt referansları göster
        }
        echo '</li>';
    }
    echo '</ul>';
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <title>Referans Ağacı</title>
    <style>
        
		body {
            margin: 0;
            font-family: 'Arial', sans-serif;
            background-color: #fff;
            color: #333;
        }
        header {
            background-color: #fff;
            color: #333;
            text-align: center;
            padding: 20px;
        }
        header h1 {
            margin: 0;
            font-size: 24px;
        }
		
		.top-right {
            position: absolute;
            right: 25px;
            top: 40px;
        }

        .material-icons {
            cursor: pointer;
			
        }
		
		.material-icons:active {
            cursor: pointer;
			color: #459D7A
        }
		
        .footer-menu {
            display: flex;
            justify-content: space-around;
            padding: 20px 0;
            background-color: #fff;
            position: fixed;
            bottom: 0;
            width: 100%;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        }
		.active {
			font-weight:600;
            color: #459D7A !important;
			
        }
        .footer-menu a {
            text-align: center;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            color: #333;
        }
		
        .footer-menu a i {
            font-size: 24px; /* İkon boyutu */
        }
        .footer-menu a span {
            font-size: 12px;
            margin-top: 4px;
			
        }
		.menu {
    padding: 20px;
    background-color: #fff;
}
        .tree-view {
            margin: 20px;
            padding: 0;
            list-style: none;
        }
        .tree-view ul {
            margin-left: 20px;
            list-style: none;
        }
        .tree-view li {
            margin: 5px 0;
            position: relative;
            padding-left: 20px;
        }
        .tree-view li::before, .tree-view li::after {
            content: '';
            position: absolute;
            left: 0;
        }
        .tree-view li::before {
            top: 0;
            bottom: 0;
            width: 1px;
            background: #ccc;
        }
        .tree-view li::after {
            top: 10px;
            width: 10px;
            height: 1px;
            background: #ccc;
        }
        .tree-view li div {
            padding: 5px 10px;
            background-color: #e9e9e9;
            border-radius: 5px;
            display: inline-block;
        }
    </style>
</head>
<body>

<header>
    <h1 style="text-align:center;">Referans Ağacınız</h1>
</header>

<div>
    <?php displayReferralTree($referralTree); ?>
</div>
<div class="footer-menu">
    <a href="dashboard.php">
        <i class="material-icons">home</i>
        <span>Ana Sayfa</span>
    </a>
    <a href="sale_list.php">
        <i class="material-icons">monetization_on</i>
        <span>Satışlar</span>
    </a>
    <a href="profil.php">
        <i class="material-icons">person</i>
        <span>Profil</span>
    </a>
</div>
</body>
</html>
