<?php
    if ( !$user['admin'] ) {
        exit;
    }

    $pdo = $db->getPdo();

    $projects = $db->run("SELECT * FROM project");
    foreach ( $projects as $project ) {
        // $clientIds = $db->run("SELECT clientId FROM client_project WHERE projectId = ?", $project['id']);
        // foreach ( $clientIds as $clientId ) {
        //     $project['clients'][] = $db->run("SELECT * FROM client WHERE id = ?", $clientIds);
        // }
        $stmt               = $pdo->prepare("SELECT client.id, client.userId, client.email, client.name, client.createdBy, client.createdAt FROM client, client_project WHERE client_project.projectId = ? AND client_project.clientId = client.id;");
        $exec               = $stmt->execute([$project['id']]);
        $project['clients'] = $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    foreach ( $project['clients'] as $client ) {
        $client['user'] = $db->row("SELECT * FROM user WHERE id = ?", $client['userId']);
    }
?>


