<?php
ini_set("display_errors","0");
include_once "../inc/config.php";
include_once dirname(__FILE__) . "/../inc/getBasic.inc.php";
include_once dirname(__FILE__) . "/../inc/tradeRec.inc.php";
include_once __DIR__ . "/../inc/postJson.inc.php";
//include_once dirname(__FILE__) . "/class/basic_info.class.php";
$mysqli = get_mysqli_connection();
$_REQUEST['t_vendor']=($_REQUEST['t_vendor'])?$_REQUEST['t_vendor']:'方正证券';
if($_REQUEST['ts_code']) {
    $_REQUEST['t_start'] = $_REQUEST['t_start'] ? $_REQUEST['t_start'] : '2020-01-01';
    $_REQUEST['t_end'] = $_REQUEST['t_end'] ? $_REQUEST['t_end'] : '';
//$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'002273';
    $ts_name = tscodeToName(ts_code_conv($_REQUEST['ts_code']));
    $_REQUEST['fitDays'] = $_REQUEST['fitDays'] ? $_REQUEST['fitDays'] : '0';
    if($_REQUEST['t_vendor']=='方正证券') {$flg3 = " and flg='买入'"; $flg4 = " and flg='卖出'";}
    elseif($_REQUEST['t_vendor']=='长江证券') {$flg3 = " and flg in ('证券买入','新股申购确认缴款')"; $flg4 = " and flg='证券卖出'";}
    //复权操作

    $trdData = trade_rec(ts_code_conv($_REQUEST['ts_code']), $_REQUEST['t_start'], $_REQUEST['t_end'], '');
    if($trdData){
        $data3 = trade_rec(ts_code_conv($_REQUEST['ts_code']), $_REQUEST['t_start'], $_REQUEST['t_end'], $flg3);
        $data4 = trade_rec(ts_code_conv($_REQUEST['ts_code']), $_REQUEST['t_start'], $_REQUEST['t_end'], $flg4);
        //复权操作
        if($_REQUEST['adj']) {
            $adjArr = getAdj();
            $trdData = adjData($adjArr, $trdData);
            $data3 = adjData($adjArr, $data3);
            $data4 = adjData($adjArr, $data4);
            //var_dump($trdData);
        }

        $lastKey = count($trdData) - 1;
        $tradeSummary = tradeSummary($data3, $data4,$trdData);

        if ($_REQUEST['fitDays'] == '1') {
            $_REQUEST['t_start'] = dateGap($trdData[0]['tdate'], -5);
            //$_REQUEST['t_end'] = dateGap($trdData[$lastKey]['tdate'], +5); 
        }
    }

}
$title="个股交易记录:".$ts_name.'('.$_REQUEST['ts_code'].')';
include_once "../html/head.php";

?>
<form name="main" id="main" method="get"> <div style="width:100%;text-align:center">

    证券公司: <?php vendorList('t_vendor'); ?>
    股票代码:<?php tradeStocksList(); ?>
    复权:<select id="adj" name="adj">
            <option value="0">不复权</option>
            <option value="1">前复权</option>
        </select>
        <script>
            $("#adj").val(<?=$_REQUEST['adj']?>);
        </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']?>' >
        <input type='submit' value=" 给我查! ">
        <input type="button" id="fitBtn" value=" 匹配交易时间 " onclick="fitBtnClick();">
        <input type="hidden" name='fitDays' id="fitDays" value="0">
<div >&nbsp;</div>
</div> </form>


<?php
if($_REQUEST['ts_code'] and $trdData)  {
    echo "<div class='div_parent'>";
    $i=0;
    foreach ($tradeSummary as $key=>$val){
        if(fmod($i,5)==0) echo "<div class='div_cell' style='clear: both;'>".$key." : ".$val." </div>\n";
        else echo "<div class='div_cell' >".$key." : ".$val." </div>\n";
        $i++;
        if(fmod($i,5)==0) print("<br />\n");
    }
    $url="https://echart.doorcome.cn/charts/chartStDetail.php?";
    $url.="ts_code={$_REQUEST['ts_code']}";
    $url.="&s={$_REQUEST['t_start']}&e={$_REQUEST['t_end']}";
    $url.="&t_vendor={$_REQUEST['t_vendor']}";
    $url.="&adj={$_REQUEST['adj']}";
echo <<<EOF
</div>
<div style="width:100%; text-align:center;margin:0 auto;">
<iframe src="{$url}
height="400px" width="1000px"></iframe>
</div>
EOF;
}
?>
    <div>&nbsp;</div>
<form id="form1" name="form1" method="post" enctype="multipart/form-data">
    <div style="width:100%; text-align:center;margin:0 auto;">
        <input type="file" id="file" name="file">
        证券公司: <?php vendorList('up_vendor'); ?>
        <input type="hidden" id="fpath" name="fpath" value="">
        <input type="button" id="btnUpload" value=" 上传并导入 ">
        <input type="button" id="btnClearMsg" value="清除导入信息">
    </div>
    <div style="width:100%; text-align:center;margin:0 auto; display: none;" id="ftxt">

    </div>
</form>
<script>
    //if($("#fitDays").val()=="1") $("#fitBtn").val("取消交易时间匹配");
    //else if($("#fitDays").val()=="0") $("#fitBtn").val("时间交易匹配");
    function fitBtnClick(){
        if($("#fitDays").val()=="0") $("#fitDays").val("1");
        $(document).ready(function(){
            $("#main").submit();
        });
    }
    $(function (){
        $("#btnClearMsg").click(function (){
            console.log("clear massage button clicked");
            $("#ftxt").html("");
            $("#ftxt").css("display","none");
        });
    });
    // ajax process file upload
    $(function () {
        $("#btnUpload").click(function () {
            console.log("Upload button clicked!");
            var formData = new FormData($('#form1')[0]);
            $.ajax({
                type: 'post',
                url: "https://echart.doorcome.cn/inc/upload.php", //上传文件的请求路径必须是绝对路劲
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
            success:function (data) {
                afterUpload(data);
            },
            error: function () {
                alert("上传失败");
            }
        });
        });
    });

    //process after file upload
    function afterUpload(data){
        var arr = $.parseJSON(data);
        if(arr['status']!=1) {
            //alert(data);
            alert(arr.msg);
            return false;
        }
        console.log("Clear file area");
        $("#file").val('');  //set input(file) to null
        $("#ftxt").css("display","block");
        $("#ftxt").html("文件上传成功！点击下面按钮开始执行<br />"+arr['fpath']+"<br />");
        $("#fpath").val(arr['fpath']);
        var tmp = $("#fpath").val();
        console.log("fpath value:"+tmp);
        console.log("Start import data!");
        // Handle excel data import; ajax
        var formData = new FormData($('#form1')[0]);
        $.ajax({
            type: 'post',
            url: "https://echart.doorcome.cn/charts/myexcel.php", //上传文件的请求路径必须是绝对路劲
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
        success: function (data) {
            console.log("get in excel ajax!");
            $("#ftxt").css("display","block");
            console.log(data);
            var farr = $.parseJSON(data);
            //alert(data);
            $("#ftxt").html(farr.msg);
        },
            error:function () {
            alert("数据解析失败！");
        }
        });
    }
    $(function(){
        $("#t_vendor").change(function (){
                console.log("Trade vendor changed!");
                var t_vendor=$("#t_vendor").val();
                console.log("Get vendor value: "+t_vendor);
                var formData = new FormData($('#main')[0]);
                $.ajax({
                    type: 'post',
                    url: "https://echart.doorcome.cn/inc/ajax.inc.php?t=stockList&t_vendor="+t_vendor, //上传文件的请求路径必须是绝对路劲
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log("get in ajax!");
                        var farr = $.parseJSON(data);
                        $("#codeList").empty();
                        for(let i=0;i<farr.data.stocks.length;i++)
                            $("#codeList").append("<option label='"+farr.data.stocks[i]['ts_name']+"' value='"+farr.data.stocks[i]['ts_code']+"'></option>");
                    },
                    error:function () {
                        alert("数据解析失败！");
                    }
                });
            });
    });
</script>
</div>
<?php include_once "../html/footer.php"; ?>