<?php
/*************************************************************************      
* 文件说明：         
*    功能说明:列表显示LED灯，并提供修改链接
*
*    调用关系：被调用:applet_display.php        
*
*
* 作者：  
*      
*修改注释：      
*NO.1      
*姓名：            
*时间：      
*修改说明：      
*************************************************************************/
require("../../inc/header.inc");
@set_time_limit(600);
$DEBUG = 0;
$db=$omc_db;
$table_name = sysLedConf; 
$sort_field = 'key_led_num';
$sort_direction = 'ASC';
$number_records_to_display = 160;
$divide_number = 20480;
if($DEBUG) echo "<br> mode = $mode, submode = $submode";
//key_led_num shape key_status key_src_page_type key_src_page_num dst_page_type dst_page_num point_x point_y width height remark update_time 


//===========================================单个LED更新或添加=====================================================
//---------------------------更新-----------------------------
if($mode == 'edit' && isset($selected_led_num)){
	$update_sql = "UPDATE $table_name 
			SET shape ='$shape',
			    key_src_page_type	= '$src_page_type',
			    key_src_page_num	= '$src_page_num',
			    dst_page_type	= '$dst_page_type',
			    dst_page_num	= '$dst_page_num',
			    point_x = '$point_x',
			    point_y = '$point_y',
			    remark = '$remark',
				sync_from_led = '$src_led'
			WHERE	key_led_num ='$selected_led_num'
		      ";
	mysqli_query($pubConn,$update_sql);
	if($DEBUG) echo "<br> update_sql = $update_sql";
	echo mysqli_error($pubConn);
//---------------------------添加-----------------------------
}elseif($mode == 'add'){
	$insert_sql = "INSERT INTO $table_name 
		(key_led_num,shape,key_status,key_src_page_type,key_src_page_num,dst_page_type,dst_page_num,point_x,point_y,width,height,remark)
	      VALUES
		('$add_led_num','$shape','15','$src_page_type','$src_page_num','$dst_page_type','$dst_page_num','$point_x','$point_y','1','1','$remark')
		";
	if($DEBUG) echo "<br> $insert_sql";
	mysqli_query($pubConn,$insert_sql);
	echo mysqli_error($pubConn);
//==================================================批量LED更新============================================================
//--------------------------设置批量更新------------------------------
}elseif($mode == 'batch' && $submode == 'move'){
	$move_x = $move_x -0;
	$move_y = $move_y -0;
	if($DEBUG)  echo "<br>sel_src_page = $sel_src_page,sel_dst_page = $sel_dst_page,sel_shape = $sel_shape,sel_status = $sel_status";
	$src_page_sql = ($sel_src_page == 'on')?" key_src_page_type	= '$src_page_type', key_src_page_num = '$src_page_num', ":'';
	$dst_page_sql = ($sel_dst_page == 'on')?" dst_page_type = '$dst_page_type', dst_page_num = '$dst_page_num', ":'';
	$shape_sql    = ($sel_shape == 'on')?" shape ='$shape', ":'';
	$status_sql   = ($sel_status == 'on')?" key_status ='$status', ":'';
	$point_sql    = "point_x = (point_x + $move_x), point_y = (point_y + $move_y)   ";
	$where_sql    = "WHERE key_led_num >='$bgn_led' AND key_led_num <='$end_led'";
	
	//-------更新原始表的状态数据-------
	$update_sql = "UPDATE $table_name SET ".$src_page_sql.$dst_page_sql.$shape_sql.$status_sql.$point_sql.$where_sql;
	mysqli_query($pubConn,$update_sql);
	if($DEBUG) echo "<br> update_sql = $update_sql";
	echo mysqli_error($pubConn);
	
	//-----------更新内存表的LED状态----------	
	if($sel_status == 'on'){
		//--更新内存表的状态数据---
		$update_status_sql = "UPDATE sysLedStat 
					SET key_status='15151515151515151515151515151515'
					WHERE if(key_led_num < '$divide_number',key_led_num >= FLOOR($bgn_led/16),key_led_num >= '$bgn_led') 
					AND if(key_led_num < '$divide_number',key_led_num <= FLOOR($end_led/16),key_led_num <= '$end_led') 
					";
		mysqli_query($pubConn,$update_status_sql);
		if($DEBUG) echo "<br> update_status_sql = $update_status_sql";
		echo mysqli_error($pubConn);
		//--标识共享内存---
		//删除LED状态命令,如果命令5秒钟无响应，作为超时处理
		//byte0：0/1/2/3=无命令/命令待处理/处理成功/处理失败
		//byte1-4：待清除LED起始编号
		//byte5-8：待清除LED结束编号
		//注：LED灯编号为16进制字符，如果只删除一个LED，LED起止编号一样即可
		//例如：“1000F000F”表示删除LED15
	
		$shm_offset = 1;
		$bgn_led_hex = dechex($bgn_led);
		$bgn_led_hex = omc_keep_length($bgn_led_hex,0,4);
		$end_led_hex = dechex($end_led);
		$end_led_hex = omc_keep_length($end_led_hex,0,4);
		$shm_value = '1'.$bgn_led_hex.$end_led_hex;
		if($DEBUG) echo "<br>  set:$shm_value";
		$shm_result = omc_set_shm($shm_value,$shm_offset);
		if($shm_result < 0){
			echo "<br><font size=\"3\" color =\"#ff0000\">Set LED(from $bgn_led to $end_led) fail,Please try again[can not write share memory]</font>";
		}else{
			sleep(5);
			$shm_count = 9;
			$shm_result = omc_get_shm($shm_offset,$shm_count);
			if($DEBUG) echo "<br> get:$shm_result";
			$shm_result = substr($shm_result,0,1);
			if($shm_result != '2'){
				echo "<br><font size=\"3\" color =\"#ff0000\">Set LED(from $bgn_led to $end_led) fail,Please try again[return error]</font>";
			}
		}
	}

//---------------------------间隔批量更新-----------------------------
}elseif($mode == 'batch' && $submode == 'mid'){
	for($i = $bgn_led; $i < $end_led; $i++){
		$update_sql = "UPDATE $table_name 
				SET point_x = '$bgn_x' ,
				    point_y = '$bgn_y'
				WHERE	key_led_num ='$i'
			      ";
		mysqli_query($pubConn,$update_sql);
		if($DEBUG) echo "<br> update_sql = $update_sql";
		echo mysqli_error($pubConn);
		$bgn_x += $move_x;
		$bgn_y += $move_y;
	}
}

//==================================================LED列表显示============================================================
$offset = array(20480,20489,20577);
function get_page_id($page_type,$page_num,$offset){	
	if($page_type < 50){
		$type_offset = $offset[$page_type];
		switch($page_type){
			case 0:
				$page_type =  "Map"; 
				break;
			case 1:
				$page_type =  "Center"; 
				break;
			case 2:
				$page_type =  "BSS"; 
				break;
			default: 
				$page_type = "unkown";
				return($page_type);
		}
		$page_num  = $page_num - $type_offset;
		$page_id   = $page_type.' - '.$page_num;
	}else{
		$sys_code	= $page_type - 50;
		$sys_type	= omc_sys_code2type($sys_code);
		//echo "<br>sys_type=$sys_type";
		$sys_num	= substr($page_num,0,2);
		$subsys_num	= substr($page_num,2,2);
		$sys_id		= $sys_type.$sys_num.'_'.$subsys_num;
		$page_id	= $sys_id;
	}
	return($page_id);
}

function get_status($status_num)
{
	$status=array('blink gray','blink green','blink yellow','blink red','blink blue','blink umbra','blink white','not use','gray','green','yellow','red','blue','umbra','white','not use');
	for($i=0;$i < sizeOf($status);$i++){
		if($i == $status_num){
			return($status[$i]);
		}elseif($i == sizeOf($status)){
			return('unkown');
		}else{
			continue;
		}

	}
}

//--------------------------- 初始化页面显示变量-----------------------------------------
if (!$number_of_records){     
	$initial_record = 0;//初始纪录号
	$str_sql_all = "SELECT count(*) as number_of_records FROM $table_name";
	if($DEBUG)echo "<br> str_sql_all = $str_sql_all";
	$count_result = @mysqli_query($pubConn,$str_sql_all);
	$count_record = @mysql_fetch_object($count_result);
	$number_of_records = $count_record->number_of_records;
	if(!$number_of_records){ 
		echo "<br><font size =\"3\" color=\"#FF0000\">$strNotFindRecord</font>";  
		exit();
	}
	$all_page_num = ceil($number_of_records / $number_records_to_display);  //总页码  
}

$this_page_num = round($initial_record / $number_records_to_display)+1;  //本页页码  

//------------------------------下面是控制分页显示，传递变量------------------------------

if ($number_of_records >= 1) {  
	//下一页的开始编号
	$next_index = $initial_record + $number_records_to_display;    
	if($next_index > $number_of_records){  
		$next_index = $number_of_records;  
	}
	//上一页的开始编号
	$prev_index = $initial_record - $number_records_to_display;  
	if ($prev_index < 0) {  
		$prev_index = 0;  
	}   
}

//上一页连接图标
if ($initial_record != 0) {  
	$t1 = "all_page_num=$all_page_num&initial_record=$prev_index&number_of_records=$number_of_records";             
	$prev_img ="<a href=\"$PHP_SELF?$t1\">";
	$prev_img .= "<IMG alt=\"$strPreviousPage\" border=\"0\"  src=\"../../images/pre_page.gif\" ></a>&nbsp;";     
}  
   
//下一页连接图标
if ($next_index != $number_of_records && $number_of_records >0) {  
	$t2 = "all_page_num=$all_page_num&initial_record=$next_index&number_of_records=$number_of_records";  
	$next_img ="<a href=\"$PHP_SELF?$t2\">";  
	$next_img .="<IMG alt=\"$strNextPage\" border=\"0\"  src=\"../../images/next_page.gif\" ></a>&nbsp;";    
}
$last_num_this_page = $next_index -1;

//--------------------------------列表显示-----------------------------------------------
echo "<table><tr>";
echo "<td  width=\"30%\">";
echo "Records:<font color=#0000ff>$initial_record - $last_num_this_page</font> of $number_of_records, Page: <font color=#0000ff> $this_page_num </font> ";
echo "</td><td  width=\"15%\">";
echo "$prev_img  $next_img";
//echo "</td><td   width=\"0%\">";
//echo "<a href=\"javascript:select_led('mode=add&all_page_num=$all_page_num&initial_record=$initial_record&number_of_records=$number_of_records')\">$strAddLED</a>";
echo "</td><td   width=\"25%\">";
echo "<a href=\"javascript:select_led('mode=batch&submode=move&all_page_num=$all_page_num&initial_record=$initial_record&number_of_records=$number_of_records')\">$strBatchOfLEDDefineSetting</a>";
echo "</td><td   width=\"25%\">";
echo "<a href=\"javascript:select_led('mode=batch&submode=mid&all_page_num=$all_page_num&initial_record=$initial_record&number_of_records=$number_of_records')\">$strBatchOfLEDClearanceSetting</a>";
echo "</td></tr></table>";
?>

<script LANGUAGE="JavaScript">  
function select_led(link_var){      
	var base_url,url,y; 
	base_url='./led_set_down.php?';
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
	url=base_url+link_var+'&scroll_y='+y;
	window.parent.footnotes.location=url;
}
function select_page(){
	var base_url,url_add,url; 
	base_url='./led_set_up.php?<?php echo "all_page_num=$all_page_num&number_of_records=$number_of_records&initial_record=";?>';
	url_add=document.selectform.page_list.options[document.selectform.page_list.selectedIndex].value;      
	url=base_url+url_add;
	window.location=url;
}

</script>   

<?php
if($scroll_y > 0){
	echo "<body onLoad='self.scrollBy(0,"."$scroll_y".")'>";
}else{
	echo "<body>";
}
$sel_sql = "SELECT * FROM $table_name 
	    ORDER BY $sort_field $sort_direction
	    LIMIT $initial_record, $number_records_to_display 
	   ";
$result = mysqli_query($pubConn,$sel_sql);
echo mysqli_error($pubConn);
if($DEBUG) echo "<br> sel_sql = $sel_sql";
$rows = mysqli_fetch_array($result);

$sel_status_sql = "SELECT key_status
		   FROM sysLedStat
		   WHERE if(key_led_num < '$divide_number',key_led_num >= floor($initial_record/16),key_led_num >= $initial_record) 
		   AND if(key_led_num < '$divide_number',key_led_num <= floor(($initial_record+$number_records_to_display)/16),key_led_num <= ($initial_record+$number_records_to_display))
		  ";
$status_result = mysqli_query($pubConn,$sel_status_sql);
echo mysqli_error($pubConn);
if($DEBUG) echo "<br> sel_status_sql = $sel_status_sql";
$status_rows = mysqli_fetch_array($status_result);
?>
<form name="selectform"  method="POST" action="./led_set_up.php" >
<table  border="1" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">
  <tr>
    <td width="5%">LED_ID</td>
    <td width="10%"><?php echo "$strStatus";?></td>
    <td width="10%"><?php echo "$strShape";?></td>
    <td width="15%"><?php echo "$strSourcePage";?></td>
    <td width="15%"><?php echo "$strTargetPage";?></td>
    <td width="10%"><?php echo "$strPoint";?></td>
    <td width="30%"><?php echo "$strRemark";?></td>
    <td width="5%" >
    <select size="1" name="page_list" onChange="javascript:select_page()">   
<?php
/*
for($i=0;$i < $all_page_num;$i++){
	$show_led_num = $i*$number_records_to_display;
	$isSelected=($initial_record == $show_led_num)?'selected':'';
	echo "<option value=\"$show_led_num\" $isSelected >$show_led_num</option>";
}
*/
$led_name_def  = array('--LED--','OMC','HLR','VLR','PPS','CNF','SMSC','AUC','EIR','GMSC','MSC','LAN','MTP2','CSC','LAPD','LAPDV5','VPS','PCR','PCRK','CNFC','PLAT','BSC','BTS','HBSC','HBTS','SN','VMS','EAN','--Page--','Area_P','Shelf_P','MSS_P','CSU_P','VPS_P','PCR_P','CNFC_P','AUC_P','MSC_P','VMS_P','EIR_P','OMC_P','HLR_P','VLR_P','PPS_P','CNF_P','SMSC_P','BSC_P','BTS_P');
$led_number_def = array('0','0','64','128','192','256','320','384','9792','448','512','576','1600','2624','3648','4672','1600','6720','7744','8256','8768','11648','14272','3648','18112','18048','9856','9920','20480','20481','20489','20493','20497','20505','20521','20525','20537','20541','20545','20549','20553','20557','20561','20565','20569','20573','20577','20601');
if(sizeof($led_name_def) != sizeof($led_number_def)){
	echo sizeof($led_name_def);
	echo sizeof($led_number_def);
	echo "ERROR of LED name <--> number setting!! please check it";
}
for($i=0;$i < sizeof($led_name_def);$i++){
	$show_led_num = $led_number_def[$i];
	$isSelected=($initial_record == $show_led_num)?'selected':'';
	echo "<option value=\"$show_led_num\" $isSelected >{$led_name_def[$i]}</option>";
}
?>
    </select>
  </td>
  </tr>
<?php
$k=0;
do{
	$led_num = $rows[key_led_num];
	//=0,圆形=1,正方形=2,长方形=3,三角
	switch($rows[shape]){
		case '0':
			$shape =  "$strRound"; 
			break;
		case '1':
			$shape =  "$strSquare"; 
			break;
		case '2':
			$shape =  "$strLandscapeRectangle"; 
			break;
		case '3':
			$shape =  "$strTriangle"; 
			break;
		case '4':
			$shape =  "$strPortraitRectangle"; 
			break;
		case '5':
			$shape =  "Cover EAN"; 
			break;
		default: 
			$shape= "unkown";
			break;
	}
	$src_page_id = get_page_id($rows[key_src_page_type],$rows[key_src_page_num],$offset);
	$dst_page_id = get_page_id($rows[dst_page_type],$rows[dst_page_num],$offset);
	$remark	= $rows[remark];
	if($led_num == $selected_led_num){ 
		$bgcolor = "bgcolor=\"#C0C0C0\"";
	}elseif($k % 16 == 0){
		$bgcolor = "bgcolor=\"#CCFFCC\"";
	}else{
		$bgcolor = '';
	}
	if($led_num < $divide_number){
		$status_offset =($led_num%16)*2;
	}else{
		$status_offset = 0;
	}
	$status_substr = substr($status_rows[key_status],$status_offset,2);
	$status = get_status($status_substr);
	echo "<tr $bgcolor>";
	echo "<td width=\"5%\">$led_num</td>";
	echo "<td width=\"10%\">$status</td>";
	echo "<td width=\"10%\">$shape</td>";
	echo "<td width=\"15%\">$src_page_id</td>";
	echo "<td width=\"15%\">$dst_page_id</td>";
	echo "<td width=\"10%\">($rows[point_x],$rows[point_y])</td>";
	echo "<td width=\"30%\">$remark</td>";
	echo "<td width=\"5%\"><a href=javascript:select_led('led_num=$led_num&mode=edit&all_page_num=$all_page_num&initial_record=$initial_record&number_of_records=$number_of_records')>edit</a></td>";
	//echo "<td width=\"5%\"><a href=\"./led_set_down.php?led_num=$led_num&mode=del&all_page_num=$all_page_num&initial_record=$initial_record&number_of_records=$number_of_records\">del</a></td>";
	echo "</tr>";
	$k++;
	if(($led_num+1) >= $divide_number){
		$status_rows = mysqli_fetch_array($status_result);
	}else if((($led_num+1)%16) == 0){
		$status_rows = mysqli_fetch_array($status_result);
	}
}while($rows = mysqli_fetch_array($result));
?>
</table>
</form>
</body>
</html>
