
<?php
 
  $slot=$sel_date_from-$sel_date_to+1;
  $sum_hour=$slot*24;
  $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")));
  $dataTable="CSTA_DB.mscDetailData"; 
  $start=strtotime($startTime);
  $end=strtotime($endTime);
  
function Getdata($tg,$startTime,$endTime,$select_3)
{ 
  $link=mysql_connect("localhost","administrator","*86#ROtartsinim");
  
  for($k=0;$k<256;$k++)
  { 
  	for($i=0;$i<800;$i++)
  	{
  	  $data[$k][$i]=0;
  	}
  }  
  

  for($j=0;$j<256;$j++)
  { 
	  if(strcmp($tg[$j][name]," "))
	  {
      //echo "tg[$j][name]={$tg[$j][name]}<br>";
      	
      if($select_3 == 5)   
      {  
      	//echo "the select_3=$select_3  good <br>";
        $sql="select Duration  csta_datetime FROM CSTA_DB.mscDetailData  where  key_type=3 and key_e1_tg={$tg[$j][type]}  csta_datetime >='{$startTime}' and csta_datetime <='{$endTime}'";
	    }
	    else if($select_3 == 6)
	    {
	    	//echo "the select_3=$select_3  good <br>";
	      $sql="select Duration  csta_datetime FROM CSTA_DB.mscDetailData  where  key_type=4 and key_e1_tg={$tg[$j][type]}  csta_datetime >='{$startTime}' and csta_datetime <='{$endTime}'";
	    }
	    else if($select_3 == 7)
	    {
	    	//echo "the select_3=$select_3  good <br>";
	      $sql="select Duration  csta_datetime FROM CSTA_DB.mscDetailData  where  key_e1_tg={$tg[$j][type]} (key_type=4 or key_type=3) (csta_datetime >='{$startTime}' and csta_datetime <='{$endTime}')";
	    }
      
      	    
	    $res=mysql_query($sql,$link);
	
	    if(!($res))
	    {
	      $num=0;
	    }
	    else
	    {
	      $num=mysqli_num_rows($res);
      }
	
	    $data[$j][count]=$num;
	
	
  	  for($i=0;$i<$num;$i++)
	    {
		    $row=mysqli_fetch_array($res);
	  
	      $data[$j][time]=$row[1];
	   
	      $start=strtotime($startTime);
	      $rowt=strtotime($row[1]);
	      $dif=$rowt-$start;
				$instance=floor($dif/(60*60));
	  
	      $data[$j][$instance] += $row[0]+0;	    
      }
    }
  }  
  return $data;
}

$data1=Getdata($tg,$startTime,$endTime,$select_3);  

//table


	$table_title="Trunk Group Erlang 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 "<br>";
	echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";	
	for($j=0;$j<256;$j++)
  {
    if(strcmp($tg[$j][name]," "))
    { 
    	//echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
      echo "<tr   bgcolor=\"#E6E6E6\" ><td colspan=13 align=\"center\" ><b>{$tg[$j][name]} Erlang(hour)</td></b></tr>";        
   	     
      for($i=0,$k=($slot-1);$i <=($slot-1);$i++,$k--)
      { 
	      $now=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$i,date("Y")));
	      echo "<tr bgcolor=\"#E6E6E6\">";
	      echo "<td rowspan=4 width=\"10%\" bgcolor=\"white\">{$now}</td>";
	      echo "<td align=center>00</td><td align=center>01</td><td align=center>02</td><td align=center>03</td><td align=center>04</td><td align=center>05</td><td align=center>06</td><td align=center>07</td><td align=center>08</td><td align=center>09</td><td align=center>10</td><td align=center>11</td>";
	      echo "</tr>"; 
	      
	      echo "<tr>";	      
	      echo "<td align=center>{$data1[$j][0+(24*$k)]}</td><td align=center>{$data1[$j][1+(24*$k)]}</td><td align=center>{$data1[$j][2+(24*$k)]}</td><td align=center>{$data1[$j][3+(24*$k)]}</td><td align=center>{$data1[$j][4+(24*$k)]}</td><td align=center>{$data1[$j][5+(24*$k)]}</td><td align=center>{$data1[$j][6+(24*$k)]}</td><td align=center>{$data1[$j][7+(24*$k)]}</td><td align=center>{$data1[$j][8+(24*$k)]}</td><td align=center>{$data1[$j][9+(24*$k)]}</td><td align=center>{$data1[$j][10+(24*$k)]}</td><td align=center>{$data1[$j][11+(24*$k)]}</td>";
	      echo "</tr>";
      
        echo "<tr  bgcolor=\"#E6E6E6\">";
	      echo "<td align=center>12</td><td align=center>13</td><td align=center>14</td><td align=center>15</td><td align=center>16</td><td align=center>17</td><td align=center>18</td><td align=center>19</td><td align=center>20</td><td align=center>21</td><td align=center>22</td><td align=center>23</td>";
	      echo "</tr>"; 
	      
	      echo "<tr>";
	      echo "<td align=center>{$data1[$j][12+(24*$k)]}</td><td align=center>{$data1[$j][13+(24*$k)]}</td><td align=center>{$data1[$j][14+(24*$k)]}</td><td align=center>{$data1[$j][15+(24*$k)]}</td><td align=center>{$data1[$j][16+(24*$k)]}</td><td align=center>{$data1[$j][17+(24*$k)]}</td><td align=center>{$data1[$j][18+(24*$k)]}</td><td align=center>{$data1[$j][19+(24*$k)]}</td><td align=center>{$data1[$j][20+(24*$k)]}</td><td align=center>{$data1[$j][21+(24*$k)]}</td><td align=center>{$data1[$j][22+(24*$k)]}</td><td align=center>{$data1[$j][23+(24*$k)]}</td>";
	      echo "</tr>";
      }
      //echo "</table>";       
	  }	
  }
  echo "</table>";


//时间间隔
 $day=$sel_date_from-$sel_date_to+1; 
 $day_num=30; 
  	
	
//画图

//

//=======================================================================================|
//===========                Draw the Graphics                              =============|
//=======================================================================================|
                  
	$sizex=800;
	if($csta_type==0)
	  $sizey=430;
	else
		$sizey=380;
	$bgcolor=F8F8F8;
	$fgcolor=000000;
	
	$im = ImageCreate ($sizex, $sizey);
	
	$background_color = ImageColorAllocate ($im, 
				hexdec(substr($bgcolor, 0, 2)), 
				hexdec(substr($bgcolor, 2, 2)),
				hexdec(substr($bgcolor, 4, 2)));
	
	$text_color = ImageColorAllocate ($im, 
				hexdec(substr($fgcolor, 0, 2)), 
				hexdec(substr($fgcolor, 2, 2)),
				hexdec(substr($fgcolor, 4, 2)));
	
	$black_color=ImageColorAllocate($im,0,0,0);
	$white_color=ImageColorAllocate($im,255,255,255);
  
  
  
	//===background====
	ImageFilledRectangle($im,0,0,$sizex,$sizey,ImageColorAllocate ($im, 198, 198, 198));
	ImageFilledRectangle($im,2,2,$sizex,$sizey,ImageColorAllocate ($im, 130, 130, 130));
	
	$pointArr=array(0,$sizey-1,3,$sizey-4,4,$sizey-1);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	
		
	$pointArr=array($sizex-1,0,$sizex-4,3,$sizex,4);
	Imagefilledpolygon($im, $pointArr, 3, ImageColorAllocate ($im, 130, 130, 130));
	
	ImageFilledRectangle($im,2,2,$sizex-3,$sizey-3,$background_color);
	
	//===border=====
	$xBgn=60;
	$yBgn=25;
	$xEnd=780;
	$yEnd=275;
	
	$unitXPix=30;
	
	
	ImageLine($im, $xBgn, $yBgn, $xBgn, $yEnd, $text_color);
	ImageLine($im, $xBgn, $yBgn, $xEnd, $yBgn, $text_color);
	ImageLine($im, $xBgn, $yEnd, $xEnd, $yEnd, $text_color);
	ImageLine($im, $xEnd, $yBgn, $xEnd, $yEnd, $text_color);
  
  
  //===draw the label block====
		$color[1]=ImageColorAllocate($im,0x99,0x22,0x33);
		$color[2]=ImageColorAllocate($im,0x99,0x00,0xcc);
		$color[3]=ImageColorAllocate($im,0x99,0x99,0x00);
		$color[4]=ImageColorAllocate($im,0x99,0x99,0x99);
		$color[5]=ImageColorAllocate($im,0x99,0xff,0x33);
		$color[6]=ImageColorAllocate($im,0xff,0x99,0x66);
		$color[7]=ImageColorAllocate($im,0xff,0x00,0x00);
		$color[8]=ImageColorAllocate($im,0xff,0xff,0x00);
		$color[9]=ImageColorAllocate($im,0x33,0x3,0x33);
		$color[10]=ImageColorAllocate($im,0x00,0xcc,0xff);
		$color[11]=ImageColorAllocate($im,0x00,0x22,0xcc);
		$color[11]=ImageColorAllocate($im,0xEE,0x00,0xff);
   
///////////////////////表示 X,Y轴坐标 
    
    
    for($k=1;$k < 13;$k++)
    { 
  	  if(strcmp($filelist[$k][name]," "))
      {
    	  for($i=0;$i<($slot)*24;$i++)	
  	    { 
  	    	
  		    $out_put[$k][$i][x]=(60+$i*(30/$slot));
  	      $out_put[$k][$i][y]=(275-$data1[$k][$i]*25);  		
  	    }
  	  }
    }
    	  
	 
	  for($i=$sel_date_to,$k=0;$i<($sel_date_from+1);$i++,$k++)
	  {
		  $datestr[$k]=date("m-d",mktime(0,0,0,date("m"),date("d")-$i,date("Y")));
	  }

//////纵轴		 			
		for($i=0;$i<23;$i++)
		{	
			
			$style = array($black_color, $black_color, $black_color, $black_color, $red, $w, $w, $w, $w, $w);
      imagesetstyle($im, $style);
      imageline($im, ($xEnd - $i*($day_num)-$day_num), $yBgn, ($xEnd - $i*($day_num)-$day_num), $yEnd, IMG_COLOR_STYLED);
			
			//$xstr=$datestr[$i];
			//Imagestringup($im, 2, ($xEnd - $i*($day_num)-$day_num) - 4, $yEnd+60, $xstr, $style);
			
			//if($i==29)
			  //Imagestringup($im, 2, $xBgn + $i*24 - 4, $yEnd+60, $datestr[29], $style);
		}
	  
////纵轴标记
   for($i=0;$i<$slot*24;$i++)
   { 
 	 
 	   if(($i%$slot) == 0)
     { 
   	   $hour_day=$i/24;
   	   
   	   $hour_time=$i/$slot;
   	   
       $datestr[$i]=date("m-d",mktime(0,0,0,date("m"),date("d")-($slot-$hour_day-1),date("Y")));
     
       $hour_hour=$i%24;
       
       if($hour_hour<10)
		   {	
		     $timearray[$i]="0".$hour_hour."h";
		   }
		   else
		   {	
		     $timearray[$i]=$hour_hour."h";
	     }
	   
	     $xstr=$datestr[$i]." ".$timearray[$i];  
	     Imagestringup($im, 2, $xBgn + $hour_time*30 - 4, $yEnd+60, $xstr,  $text_color);         
     }
   }
	  
	  
	  //横轴
	  for($i=0,$k=10;$i<10;$i++,$k--)
    {
    	$displayMaxData=$k;
    	imagesetstyle($im, $style);
    
    	//imageline($im,$xBgn + $i*30, $yBgn, $xBgn + $i*30, $yEnd, IMG_COLOR_STYLED);
    	if($i!=0)
    	  imageline($im,$xBgn , $yBgn+$i*25, $xEnd,  $yBgn+$i*25, IMG_COLOR_STYLED);
		
		  Imagestring($im, 2, $xBgn - 50, $yBgn + $i*25 - 5, "$displayMaxData",  $black_color);	
	  }	
	  Imagestring($im, 2, $xBgn - 50, $yBgn + $i*25 - 5, "0",  $black_color);	
	  ///////////////////////////////////////////////////////////////////////
		
		
		//画线		
		
		for($k=1;$k < 13;$k++)
    { 
  	  if(strcmp($filelist[$k][name]," "))
      {
    	  for($i=0;$i<$slot*24;$i++)	
  	    {
  		    
  		    if($i > 0)
  	      {
  	        imageline($im,$out_put[$k][$i-1][x],$out_put[$k][$i-1][y],$out_put[$k][$i][x],$out_put[$k][$i][y],$color[$k]);	  	      
  	      } 	      
  	    }
  	  }
    }
		
				
		//====graphics title=====
		//$graphics_title="Service erlang statistic graphic";
		//Imagestring($im, 2, 300,5, $graphics_title,  $text_color);
		
		//===draw the label block====
		$color[1]=ImageColorAllocate($im,0x99,0x22,0x33);
		$color[2]=ImageColorAllocate($im,0x99,0x00,0xcc);
		$color[3]=ImageColorAllocate($im,0x99,0x99,0x00);
		$color[4]=ImageColorAllocate($im,0x99,0x99,0x99);
		$color[5]=ImageColorAllocate($im,0x99,0xff,0x33);
		$color[6]=ImageColorAllocate($im,0xff,0x99,0x66);
		$color[7]=ImageColorAllocate($im,0xff,0x00,0x00);
		$color[8]=ImageColorAllocate($im,0xff,0xff,0x00);
		$color[9]=ImageColorAllocate($im,0x33,0x3,0x33);
		$color[10]=ImageColorAllocate($im,0x00,0xcc,0xff);
		$color[11]=ImageColorAllocate($im,0x00,0x22,0xcc);
		$color[12]=ImageColorAllocate($im,0xEE,0x00,0xff);
    	
    
    imagefilledrectangle($im,90,350,100,360,$color[1]);
		imagestring($im,2,105,350,"SUB SUB",$text_color);
		
		imagefilledrectangle($im,260,350,270,360,$color[2]);
		imagestring($im,2,275,350,"SUB TRK",$text_color);
		
		imagefilledrectangle($im,430,350,440,360,$color[3]);
		imagestring($im,2,445,350,"TRK SUB",$text_color);
		
		imagefilledrectangle($im,600,350,610,360,$color[5]);
		imagestring($im,2,615,350,"TRK TRK",$text_color);
		
		imagefilledrectangle($im,90,370,100,380,$color[6]);
		imagestring($im,2,105,370,"INTRA",$text_color);
		
		imagefilledrectangle($im,260,370,270,380,$color[7]);
		imagestring($im,2,275,370,"SUB IP",$text_color);
		
		imagefilledrectangle($im,430,370,440,380,$color[4]);
		imagestring($im,2,445,370,"TRK IP",$text_color);
		
		imagefilledrectangle($im,600,370,610,380,$color[9]);
		imagestring($im,2,615,370,"MO",$text_color);
		
		imagefilledrectangle($im,90,390,100,400,$color[10]);
		imagestring($im,2,105,390,"MT",$text_color);
		
		imagefilledrectangle($im,260,390,270,400,$color[11]);
		imagestring($im,2,275,390,"IN TRK",$text_color);
		
		imagefilledrectangle($im,430,390,440,400,$color[12]);
		imagestring($im,2,445,390,"OUT TRK",$text_color);
		
		imagefilledrectangle($im,600,390,610,400,$color[8]);
		imagestring($im,2,615,390,"IP",$text_color);	
    
	
		
		

//////////////////////////////////////////////////////
////////////////////////////////////////////////////////
	//-----输出图形文件，请勿修改
	//delete *.png
	$handle=opendir('.');
	while($file=readdir($handle))
	{
		$retval="";
		$pt=strrpos($file,".");
		if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
		if($retval=='png') unlink($file);
	}
	
	$tt=time()-4000;
	$filename=$tt.".png";
	
	if(1)
	{
		ImagePng ($im,"$filename");  
		ImageDestroy($im);	
	}
	//-----显示图片
	flush();
	echo "</form>";
	echo "<img src=\"$filename\"></img>";
	
  adjust_content_tail();

?>

