<?php

$parentFolderId = SS_TASK_LIST_FOLDER; // "/BTB Client Workspace/BTB Client Folders"

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/folders/$parentFolderId");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = json_decode(curl_exec($ch));
curl_close($ch);

// echo "<pre>";
// print_r($response);
// echo "</pre>";

$folders = $response->folders;

$i      = 0;
$sheets = [];
foreach ( $folders as $folder ) {
    $folderId = $folder->id;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.smartsheet.com/2.0/folders/$folderId");
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer qJc6jnLsHoqznwCbkApNwLMQamt6ITcykpgen", 'Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = json_decode(curl_exec($ch));
    curl_close($ch);

    echo "<pre>";
    print_r($response);
    echo "</pre>";

    $sheetsInFolder = $response->sheets;
    foreach ( $sheetsInFolder as $sheet ) {
        if ( str_contains(strtolower($sheet->name), 'task list') && !str_contains(strtolower($sheet->name), 'template') ) {
            $sheets[$i] = $sheet->name;

            $i++;
        }
    }
}
echo "<pre>";
print_r($sheets);
echo "</pre>";