<?php
include_once "../inc/functions.inc.php";
$_REQUEST['code']=$_REQUEST['code']?$_REQUEST['code']:'sh';
$_REQUEST['hsgt']=$_REQUEST['hsgt']?$_REQUEST['hsgt']:'north_money';
$_REQUEST['stacked']=$_REQUEST['stacked']?$_REQUEST['stacked']:'1';
$_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');
$title="股指VS沪深港通资金流向";
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">
指数代码:
<select name='code' id='code'>
    <option value='sh'>上证指数</option>
    <option value='sz'>深圳成指</option>
    <option value='zx'>中小板指</option>
    <option value='cy'>创业板指</option>
</select>&nbsp;
资金选择：
<select name="hsgt" id="hsgt">
    <option value="ggt_ss">港股通（上海）</option>
    <option value="ggt_sz">港股通（深圳）</option>
    <option value="hgt">沪股通</option>
    <option value="sgt">深股通</option>
    <option value="north_money">北向资金</option>
    <option value="south_money">南向资金</option>
</select>&nbsp;
数据累积：
<select name="stacked" id="stacked">
    <option value="-1">每日值</option>
    <option value="1">累积值</option>
</select>&nbsp;
开始时间: <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']?>' >
 &nbsp;&nbsp;
 <button id="submitBtn" class="btn-submit"><span>查询数据</span></button>
</div> </form>
<div >&nbsp;</div>

<div id="container" style="width: 1000px; height:400px; margin: 0 auto 20px;">
    <div class="loading-overlay" id="loadingOverlay">
        <div class="spinner"></div>
        <p style="font-size:18px; color:#2c3e50;">正在加载数据，请稍候...</p>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#code').val('<?= $_REQUEST['code'] ?>');
        $('#hsgt').val('<?= $_REQUEST['hsgt'] ?>');
        $('#stacked').val('<?= $_REQUEST['stacked'] ?>');
        loadChartData();
        $('#submitBtn').click(function(e) {
            e.preventDefault();
            loadChartData();
        });
    });

    function loadChartData() {
        $('#loadingOverlay').addClass('active');
        var t_start = $('#t_start').val();
        var t_end = $('#t_end').val();
        var code = $('#code').val();
        var hsgt = $('#hsgt').val();
        var stacked = $('#stacked').val();
        var codeName = $('#code option:selected').text();
        var hsgtName = $('#hsgt option:selected').text();

        var url = "../inc/ajax.inc.php?t=moneyflowData&code="+code+"&hsgt="+hsgt+"&stacked="+stacked+"&t_start="+t_start+"&t_end="+t_end;

        fetch(url)
            .then(function(r) { return r.json(); })
            .then(function(resp) {
                var d = resp.data;
                var indexData = d.indexData;
                var flowData = d.flowData;
                var params = {};
                params.chartid = 'container';
                params.legend = [codeName, hsgtName+'-亿元'];
                params.text = params.legend[0] + ' V.S ' + params.legend[1];
                params.data1 = indexData;
                params.data2 = flowData;
                var dataCal = calculateStats(flowData);
                params.sub_text = hsgtName + " Max:" + dataCal.max;
                params.sub_text += ", Min:" + dataCal.min;
                params.sub_text += ", Average:" + dataCal.avg;
                params.sub_text += ", Recent:" + dataCal.last;
                doubleLineChart(params);
            })
            .catch(function(error) { console.error('数据加载失败:', error); })
            .finally(function() {
                $('#loadingOverlay').removeClass('active');
            });
    }
</script>
<?php include_once "../html/footer.php"; ?>
