<?php
include_once "../inc/getBasic.inc.php";
include_once "../inc/getData.inc.php";
include_once "../inc/postJson.inc.php";
$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'000001';
$_REQUEST['tp']=$_REQUEST['tp']?$_REQUEST['tp']:'ratio';
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2015-06-01';
$_REQUEST['t_end']=$_REQUEST['t_end']?$_REQUEST['t_end']:date('Y-m-d');
$ts_name = tscodeToName(ts_code_conv($_REQUEST['ts_code']));
$title="股价VS北向资金持股:".$ts_name.'('.$_REQUEST['ts_code'].')';

$unit = '';
$data = getStockHist(ts_code_conv($_REQUEST['ts_code']),$_REQUEST['t_start'],$_REQUEST['t_end']);
$getData = getHKHoldByCode();
$data2 = hkHoldReform($getData,$_REQUEST['tp']);
#var_dump($data2['data']);
$subtext_ext = "Max: ".$data2['data_max'];
$subtext_ext .= ", Min: ".$data2['data_min'];
$subtext_ext .= ", Average: ".$data2['data_avg'];
$subtext_ext .= ", Recent: ".$data2['data_last'];

$legend=array('不复权股价',hkholdConv($_REQUEST['tp']));
include_once "../html/head.php";
?>
<script src="../js/renderCharts.js"></script>
<form name="main" id="main" method="get">
    <div style="width:100%;text-align:center">
        股票代码: <input type='text' name='ts_code' id='ts_code' width="30px" value='<?=$_REQUEST['ts_code']?>' >
        数据选择：
        <select name="tp" id="tp">
            <option value="ratio">持股比例(%)</option>
            <option value="vol">持股数(万)</option>
        </select>

        <script>
            $("#tp").val('<?=$_REQUEST['tp']?>');
        </script>
开始时间: <input type='date' name='t_start' id='t_start' width='30px' value='<?=$_REQUEST['t_start']?>' >
结束时间: <input type='date' name='t_end' id='t_end' width='30px' value='<?=$_REQUEST['t_end']?>' >
 <button id="submitBtn" class="btn-submit"><span>查询数据</span></button>
<div >&nbsp;</div>

</div> </form>
<div >&nbsp;</div>
<div id="container" style="margin:0 auto;height: 400px;width: 1000px"></div>
 <div class="loading-overlay" id="loadingOverlay">
            <div class="spinner"></div>
            <p style="font-size:18px; color:#2c3e50;">正在加载数据，请稍候...</p>
    </div>
<script type="text/javascript">
    // 显示加载动画
        $('#loadingOverlay').addClass('active');
        let t_start = $('#t_start').val();
        let t_end = $('#t_end').val();
        let ts_code = $("#ts_code").val();
        url =   "https://api.doorcome.cn/api/stockbasic/?tscode="+ts_code;
        url +=  "&start_date="+t_start+"&end_date="+t_end;

        url1 =   "https://api.doorcome.cn/api/stockmargin/?tscode="+ts_code;
        url1 +=  "&start_date="+t_start+"&end_date="+t_end;
        
        url2 = "https://api.doorcome.cn/api/stockinfo/?tscode="+ts_code;
        Promise.all([
            fetch(url),
            fetch(url1),
            fetch(url2)
            ])
            .then(responses => Promise.all(responses.map(r => r.json())))
            .then(([dailyData,hkData,stockInfo]) => {
                // 图表配置:pe_ttm
                let codeName = stockInfo[0].name;
                var params = {};
                params.chartid='container';
                params.legend = ['不复权股价','北向资金(%)'];
                params.text = codeName+'('+ts_code+')';
                params.data1 = pickData(dailyData, 'trade_date', 'close');
                params.data2 = pickData(margindData, 'trade_date', 'rzrqye');
                params.data2 = params.data2.map(item => {
                    return {
                        value: [
                            item.value[0], 
                            parseFloat((parseFloat(item.value[1]) / 10000 /10000).toFixed(2))
                        ]
                    };
                });
                dataCal = calculateStats(params.data2);
                params.sub_text = "TTM PE Max:"+dataCal['max'];
                params.sub_text += ",  Min:"+dataCal['min'];
                params.sub_text += ",  Average:"+dataCal['avg'];
                params.sub_text += ",  Recent:"+dataCal['last'];
                doubleLineChart(params);
            })
            .catch(error => console.error('数据加载失败:', error))
            .finally(() => {
                // 隐藏加载动画
                $('#loadingOverlay').removeClass('active');
            }
        );
    }
</script>
<script type="text/javascript" src="../js/hkhold.js?version=3.14"></script>
<?php include_once "../html/footer.php"; ?>

