<?php
include_once __DIR__."/config.php";

/**
 * 显示股票代码选择列表 (datalist widget)
 * @param string $email: default null
 * @return bool  true
 */
function tradeStocksList($email=null){
    $mysqli = get_mysqli_connection();
    if($_REQUEST['t_vendor']=='方正证券'){
    $sql= <<< EOF
        select distinct(ts_code) ts_code,ts_name from trade_record
        where tprice>0 and (ts_code not like '7%' and ts_code not like '1%')  and length(trade_id)=5 order by ts_code
EOF;
}elseif($_REQUEST['t_vendor']=='长江证券'){
        //length(0+ts_name)!=length(ts_name) 判断ts_name 不能为纯数字
    $sql= <<< EOF
    select distinct(ts_code) ts_code,ts_name from trade_record_cj
where tprice>0  and ts_code not like '7%' and length(0+ts_name)!=length(ts_name) order by ts_code
EOF;
}
    $result = $mysqli->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    $option='';
    foreach ( $data as $item){
        $option .= "<option label='{$item['ts_name']}' value='{$item['ts_code']}'></option>\n";
    }
    $ts_code_val = htmlspecialchars($_REQUEST['ts_code'] ?? '', ENT_QUOTES);
    $html = <<< EOF
<input id="ts_code" name= "ts_code" list="codeList" autocomplete="off"/>
<datalist id="codeList">
$option
</datalist>
<script>
$("#ts_code").val('$ts_code_val');
</script>

EOF;
    echo $html;
    return true;
}

function vendorList($id='t_vendor'){
    $val = htmlspecialchars($_REQUEST[$id] ?? '', ENT_QUOTES);
    $html = <<<EOF
<select id="{$id}" name= "{$id}"  autocomplete="off" >
<option value="方正证券">方正证券</option>
<option value="长江证券">长江证券</option>
</select>
<script>
$("#{$id}").val('{$val}');
</script>
EOF;
echo $html;
return true;
}

/**
 * 根据年份区间，获取财报日期清单
 * @param $yst
 * @param $yed
 */
function yearList($yst,$yed){
    $n = $yed-$yst+1;
    $thisYear=date('Y');    //yyyy,eg.2022
    $thisMonDay=date('md'); //mmdd,eg.0331
    $years=array();
    for($i=0;$i<$n;$i++){
        if(($yst+$i)<($thisYear-1) ) $years[]=($yst+$i)."1231";  //前年及以前

        //去年
        if(($yst+$i)==($thisYear-1)and $thisMonDay<='0430')  $years[]=($yst+$i)."0930";
        if(($yst+$i)==($thisYear-1)and $thisMonDay >'0430')  $years[]=($yst+$i)."1231";
        //今年
        if(($yst+$i)==$thisYear){
            switch ($thisMonDay){
                case $thisMonDay<='0430':
                    //一季报未公布do nothing
                    break;
                case $thisMonDay>'0430'  && $thisMonDay<= '0831':
                    //公布一季报
                    $years[]=($yst+$i)."0331";
                    break;
                case $thisMonDay>'0831'  && $thisMonDay<= '1031':
                //公布一季报
                    $years[]=($yst+$i)."0630";
                    break;
                case $thisMonDay>'1031':
                    //公布一季报
                    $years[]=($yst+$i)."0930";
                    break;
                default: break;
            }
        }
    }
    return $years;
}

/**
 * $year 20201231 转为2020年报
 * @param $year
 */
function yearToname($year){
    switch (substr($year,4,4)){
        case '1231':
            return substr($year,0,4).'年报';
        case '0930':
            return substr($year,0,4).'三季';
        case '0630':
            return substr($year,0,4).'半年';
        case '0331':
            return substr($year,0,4).'一季';
        default:
            return substr($year,4,4);
    }
}
