<?php
$cstaConf=GetCstaConf($sysTypeNo);
if(0)
{
	echo "sel_date_from=$sel_date_from,sel_date_to=$sel_date_to<br>";
	echo "keyTwo=$keyTwo<br>";
	echo "<pre>";
	print_r($cstaConf);
	echo "</pre>";
}
//SPGW ok
$slot=$sel_date_from-$sel_date_to+1;
$startTime=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
$endTime=date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-$sel_date_to,date("Y")));
//echo "startTime=$startTime,endTime=$endTime<br>";


$cstaData=GetCstaDataByDay($cstaConf,$startTime,$endTime,"detail",-1,"key_sys_id=$keyTwo");
if(0)
{
	echo "<pre>";
	print_r($cstaData);
	echo "</pre>";
}

//返回每条数据

function printVolumeWithUnits($bytes) { 
    $units = ["B", "KB", "MB", "GB", "TB"];
    $unit_index = 0; 
    $volume = $bytes;

    // 计算单位
    while ($volume >= 1024 && $unit_index < count($units) - 1) {
        $volume /= 1024; 
        $unit_index++; 
    }
    return round($volume, 2) . " " . $units[$unit_index]; // 返回计算后的值和单位 以及保留两位
}


$echartDown=printVolumeWithUnits($cstaData[0][downlink_total_volume]+0);
$echartUp=printVolumeWithUnits($cstaData[0][uplink_total_volume]+0);

//echo $_REQUEST['keyTwo'];
global $cstaDbConn;
$dataTable=$cstaConf[DataObject][0][DetailDataTable];
$fieldList=$cstaConf[DataObject][0][DetailFieldList];
if(strlen($fieldList)){
	$fieldList="$fieldList,csta_datetime";
}
$sql="select {$fieldList} from CSTA_DB.{$dataTable} where csta_datetime >= '{$startTime}' and csta_datetime <= '{$endTime}' and key_sys_id = {$keyTwo} order by csta_datetime asc ";
$res=mysqli_query($cstaDbConn,$sql);
//echo $sql;
//echo mysqli_error($cstaDbConn);
$num=mysqli_num_rows($res);
//set downArr upArr

$downDataArr=[];
$downDateArr=[];
$upDataArr=[];
$upDateArr=[];
for($i=0;$i<$num;$i++)
{
	$row=@mysqli_fetch_array($res);
	// echo "<pre>";
	// print_r($row);
	// echo "</pre>";
	$downDataArr[]=round($row[downlink_total_volume]/(1024 * 1024),2);
	$downDateArr[]=$row[csta_datetime];
	$upDataArr[]=round($row[uplink_total_volume]/(1024 * 1024),2);
	$upDateArr[]=$row[csta_datetime];
}

$upDateArrJson = json_encode($upDateArr);
$upDataArrJson = json_encode($upDataArr);
$downDataArrJson = json_encode($downDataArr);
$downDateArrArrJson = json_encode($downDateArr);
// echo "<pre>";
// print_r($upDataArr);
// echo "</pre>";
//Table-1
{
	$table_title="SPGW Statistic&nbsp;&nbsp;Date:";
	if($slot>1)
		$table_title.=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")))." to ".date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_to,date("Y")));
	else
		$table_title.=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$sel_date_from,date("Y")));
	echo "<br><b><center>$table_title</center></b>";
	echo "<table width=\"99%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td align=\"center\" width=\"11%\">Attempted Sess Est</td>";
	echo "<td align=\"center\" width=\"11%\">Succ Sess Est</td>";
	echo "<td align=\"center\" width=\"11%\">Attempted Ims Session Est</td>";
    echo "<td align=\"center\" width=\"11%\">Succ Ims Sess Est</td>";
	echo "<td align=\"center\" width=\"11%\">Attempted Delete Sess</td>";
	echo "<td align=\"center\" width=\"11%\">Succ Delete Sess Sucess</td>";
	echo "<td align=\"center\" width=\"11%\">Attempted Create Bearer</td>";
	echo "<td align=\"center\" width=\"11%\">Succ Create Bearer</td>";
    echo "<td align=\"center\" width=\"11%\">Attempted Ggsn Create Pdp</td>";
	echo "</tr>";
	




	$value=$cstaData[0][attempted_sess_est_num]+0;
	echo "<td align=\"center\">$value</td>";
	//Attempt
	$value=$cstaData[0][succ_sess_est_num]+0;
	echo "<td align=\"center\">$value</td>";
	
	//Success
	$value=$cstaData[0][attempted_ims_session_est_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//No Resource
	$value=$cstaData[0][succ_ims_sess_est_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	

	//Negotiation Fail
	$value=$cstaData[0][attempted_delete_sess_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//Media Error
	$value=$cstaData[0][succ_delete_sess_sucess_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	


	//No Response
	$value=$cstaData[0][attempted_create_bearer_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//General Error
	$value=$cstaData[0][succ_create_bearer_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	

	//Total Call Duration
	$value=$cstaData[0][attempted_ggsn_create_pdp_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	echo "</tr>";
	


	echo "<tr bgcolor=\"#E6E6E6\">";
    echo "<td align=\"center\" width=\"11%\">Succ Ggsn Create Pdp</td>";
    echo "<td align=\"center\" width=\"11%\">Attempted Ggsn Delete Pdp</td>";
	echo "<td align=\"center\" width=\"11%\">Succ Ggsn Delete Pdp</td>";
	echo "<td align=\"center\" width=\"11%\">Ps Pagin</td>";
	echo "<td align=\"center\" width=\"11%\">Fail Ps Paging</td>";
	echo "<td align=\"center\" width=\"11%\">Ims Paging</td>";
	echo "<td align=\"center\" width=\"11%\">Fail Ims Paging</td>";
	echo "<td align=\"center\" width=\"11%\">Uplink Total Volume</td>";
	echo "<td align=\"center\" width=\"11%\">Downlink Total Volume</td>";
	echo "</tr>";


	//Average Call Duration
	$value=$cstaData[0][succ_ggsn_create_pdp_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";

	//Attempt
	$value=$cstaData[0][attempted_ggsn_delete_pdp_num]+0;
	echo "<td align=\"center\">$value</td>";
	
	//Success
	$value=$cstaData[0][succ_ggsn_delete_pdp_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//No Resource
	$value=$cstaData[0][ps_pagin_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//Negotiation Fail
	$value=$cstaData[0][fail_ps_paging_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//Media Error
	$value=$cstaData[0][ims_paging_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//No Response
	$value=$cstaData[0][fail_ims_paging_num]+0;
	echo "<td align=\"center\">";
	echo "$value<br>";
	echo "</td>";
	
	//General Error
	$unitValue=$echartUp;
	$value=$cstaData[0][uplink_total_volume];
	echo "<td align=\"center\">";
	echo "$unitValue<br>($value)";
	echo "</td>";
	
	//Total Call Duration
	$unitValue=$echartDown;
	$value=$cstaData[0][downlink_total_volume];
	echo "<td align=\"center\">";
	echo "$unitValue<br>($value)";
	echo "</td>";
	echo "</tr>";
	
	echo "</table>";
}

// //Graphics-1
// {
// 	echo "<br><b>Service(Attempt) & Success/Failure</b><br>";
// 	unset($tmpData);
// 	unset($tmpColor);
// 	$tmpData[]=$cstaData[0][1][Attempt]+0;
// 	$tmpData[]=$cstaData[0][2][Attempt]+0;
	
// 	$tmpColor[]=array(0x99,0x00,0x33);
// 	$tmpColor[]=array(0x99,0x00,0xcc);
	
// 	$image=CreateImage(680,370);
// 	$image=DrawPercentHistogram($image,
// 		$tmpData,
// 		$tmpColor,
// 		array(30,20),
// 		array(160,280),1,0
// 	);
	
// 	unset($tmpData);
// 	$data_index=0;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Success]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Success]+0;
// 	$data_index++;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Cau1]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Cau1]+0;
// 	$data_index++;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Cau2]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Cau2]+0;
// 	$data_index++;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Cau3]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Cau3]+0;
// 	$data_index++;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Cau4]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Cau4]+0;
// 	$data_index++;
// 	$tmpData[$data_index][0]=$cstaData[0][1][Cau5]+0;
// 	$tmpData[$data_index][1]=$cstaData[0][2][Cau5]+0;
// 	$data_index++;
	
// 	$image=DrawStackPercentHistogram($image,
// 		$tmpData,
// 		$tmpColor,
// 		array(220,20),
// 		array(420,280),0,1
// 	);
	
// 	$remarkBlock=array(
// 		"Attempt",
// 		"Success",
// 		"No","Resource",
// 		"Negotiation","Fail",
// 		"Media","Error",
// 		"No","Response",
// 		"General","Error"
// 	);
	
// 	$pointArr=array(
// 		array(90,320),
// 		array(245,310),
// 		array(330,305),array(315,315),
// 		array(365,305),array(387,315),
// 		array(448,305),array(447,315),
// 		array(520,305),array(507,315),
// 		array(572,305),array(577,315)
// 	);
	
// 	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,0,0,2);
	
// 	unset($remarkBlock);
// 	unset($pointArr);
// 	$remarkBlock=array(
// 		"Conference",
// 		"Transcoding"
// 	);
	
// 	$pointArr=array(
// 		array(50,340),
// 		array(340,340),
// 	);
	
// 	$image=AddRemarkToImage($image,$pointArr,$remarkBlock,$tmpColor,0,2);
	
// 	DrawImage($image);
// }
?>
<script src="echarts.js"></script>
<script>
//uplink_total_volume与downlink_total_volume  B --KB 10次方 --MB 20 -GB 30 --TB 40

document.addEventListener("DOMContentLoaded", function() {
            var chart = echarts.init(document.getElementById('chart'));

            // up down 上线行
            var option = {
                grid: {
                    left: '5%',
                    right: '23%',
                    bottom: '3%',
                    containLabel: true
                },
                title: {
                    text: 'Total Volume(MB)'
                },
                tooltip: {
                    trigger: 'axis',
                },
                legend: {
                    data: ['Uplink Total Volume','Downlink Total Volume']
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: <?php echo $upDateArrJson;?>,
                },
                yAxis: {
                    type: 'value'
                },
                //由PHP提供数组
                series: [{
                    name: 'Uplink Total Volume',
                    type: 'line',
					symbol: 'none',
					symbolSize: 6,
					smooth:0.6,
					showSymbol: true,
					data: <?php echo $upDataArrJson;?>
                },
				{
					name:'Downlink Total Volume',
					type: 'line',
					symbol: 'none',
					symbolSize: 6,
					smooth:0.6,
					showSymbol: true,
					data: <?php echo $downDataArrJson;?>
				}
            ]
            };


            chart.setOption(option);

			// 监听窗口大小变化
			window.addEventListener('resize', function() {
			chart.resize();
			});

        })


</script>
	<div class="container">
		<div id="chart" style="width: 100%; height: 100%;"></div>
	</div>
<style>
.container {
	margin-top:20px;
    display: flex; /* 使用 Flexbox 布局 */
    height: 90%;
    width: 100%;
}

#chart {
    flex: 1; /* 使图表容器占据剩余空间 */
    min-height: 0; /* 防止 Flexbox 子元素溢出 */
}
</style>

