<?php

function getProductsData($directory) {
    // 检查目录是否存在
    if (!is_dir($directory)) {
        return null;
    }

    $files = glob($directory . '/*.json');
    
    // 如果没有找到任何JSON文件，返回null
    if (empty($files)) {
        return null;
    }

    $productsData = [];
    $allHidden = true;

    foreach ($files as $file) {
        $content = file_get_contents($file);
        $data = json_decode($content, true);

        // 跳过hidden为1的文件
        if (isset($data['hidden']) && $data['hidden'] == 1) {
            continue;
        } else {
            $allHidden = false;
        }

        // 获取文件名（不包括后缀）
        $id = basename($file, '.json');

        // 只获取指定键的值，如果不存在则设为null
        $productInfo = [
            'name' => isset($data['name']) ? $data['name'] : null,
            'describe' => isset($data['describe']) ? $data['describe'] : null,
            'fid' => isset($data['fid']) ? $data['fid'] : null,
            'sid' => isset($data['sid']) ? $data['sid'] : null,
            'nid' => isset($data['sid']) ? $data['nid'] : null,
            'day' => isset($data['day']) ? $data['day'] : null,
            'have' => isset($data['have']) ? $data['have'] : null,
            'star' => isset($data['star']) ? $data['star'] : null,
            'bi' => isset($data['bi']) ? $data['bi'] : null
        ];

        $productsData[$id] = $productInfo;
    }

    // 如果所有内容都是被隐藏的，返回null
    if ($allHidden) {
        return null;
    }

    return json_encode($productsData, JSON_UNESCAPED_UNICODE);
}

$directory = './products';
echo getProductsData($directory);
?>
