<?php
/**
 * 宁波各区县List
 */
function districtList(){
?>
<select name='district' id='district'>
    <option value='北仑区'>北仑区</option>
    <option value='余姚市'>余姚市</option>
	<option value='海曙区'>海曙区</option>
	<option value='江北区'>江北区</option>
	<option value='镇海区'>镇海区</option>
	<option value='鄞州区'>鄞州区</option>
	<option value='奉化区'>奉化区</option>
	<option value='慈溪市'>慈溪市</option>
	<option value='宁海县'>宁海县</option>
	<option value='象山县'>象山县</option>
	<option value='合计'>合计</option>
</select>
<?php
}

function indexList($id='code'): void{
?>
<select name='<?=$id?>' id='<?=$id?>'>
    <option value='000001.SH'>上证指数</option>
    <option value='399001.SZ'>深圳成指</option>
    <option value='399005.SZ'>中小板指</option>
    <option value='399006.SZ'>创业板指</option>
    <option value='000016.SH'>上证50</option>
    <option value='399300.SZ'>沪深300</option>
    <option value='399905.SZ'>中证500</option>
    <option value='899050.BJ'>北证50</option>
</select>
<?php
}

/* 证券交易所list */
function seList($id='se'): void{
?>
<select name='<?=$id?>' id='<?=$id?>'>
    <option value='SSE'>上交所</option>
    <option value='SZSE'>深交所</option>
    <option value='BSE'>北交所</option>
</select>
<?php
}

/* select list by Day  month  */
function byDM($id,$oid,$nid){
    $str=<<<EOF
    <select name='{$id}' id='{$id}' onchange="dmChange('{$id}','{$oid}','{$nid}');">
    <option value='Daily'>Daily</option>
    <option value='Monthly'>Monthly</option>
    </select>
EOF;
    echo $str;
 }


 // 辅助函数：调用TuShare API
function callTushareApi($method, $params){
	$token = '1bc28452ba375da19320cda845ae6307578964cb3ae473d0dc702aea';
	// 将参数编码为JSON格式
    $postData = json_encode([
        "api_name" => $method,
        "token" => $token,
        "params" => $params,
        "fields" => "" // 其他字段需要根据具体API调整
    ]);
	print_r($postData);
	// 初始化cURL会话
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api.tushare.pro');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($postData)
    ]);
	
	 // 执行cURL请求
    $response = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
	
	 // 调试输出：打印请求和响应信息
	 if (debug_enabled()) { // 假设这是一个函数，用于检查是否启用了调试模式
        error_log("TuShare API Request: " . $postData);
        error_log("TuShare API Response: " . $response);
    }

    // 错误处理
    if ($err) {
        error_log("cURL error: " . $err);
        return "cURL error: " . $err;
    }
   
   

    $responseData = json_decode($response, true);
	// 检查API调用是否成功
    if (isset($responseData['code']) && $responseData['code'] != 200) {
        error_log("TuShare API Error: " . $responseData['msg']);
        return "TuShare API Error: " . $responseData['msg'];
    }

    if (isset($responseData['data']) && isset($responseData['data']['items'])) {
        return $responseData['data']['items'];
    } else {
        return [];
    }
}

// 假设的调试模式开关函数
function debug_enabled()
{
    // 这里可以根据实际情况来设置调试模式的开关条件
    // 例如，可以检查环境变量、配置文件或HTTP请求头等
    return true; // 或者返回一个条件表达式的结果
}
?>