<?php
require("../../inc/header.inc");
require("../sub_inc/operWebFunc.inc");
require("param_comm.inc");
?>

<script language="Javascript">
function deleteObject(sysId,oid,instanceNo,tableOid,oldInstance,control)
{
	var reply=confirm("Do you want to delete the selected?");
	if(reply == true)
	{
		window.location.href="param_show_table.php?delete=1&sysId="+sysId+"&deleteOid="+oid+"&deleteInstanceNo="+instanceNo+"&tableOid="+tableOid+"&table_type=0&instanceNo="+oldInstance;
	}
	else
	{
		control.checked=false;
	}
}

function lockTimeSlot(sysId,instanceNo,oidStr,tableOid,opt)
{
	URL="./lockTimeSlot.php?sysId="+sysId+"&oidStr="+oidStr+"&instanceNo="+instanceNo+"&tableOid="+tableOid+"&opt="+opt;
	//window.alert(URL);
	var msgWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=350');
	msgWindow.focus();
}
</script>
<body leftmargin="15" rightmargin="10" onload="javascript:adjust_frame('down');" onresize="javascript:adjust_frame('down');" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";
//*****
//$url_temp=$_SERVER["REQUEST_URI"];
//echo "This page URL:$url_temp<br>";


$sysIdArr=explode('_',$sysId);
$sysTypeNo=$sysIdArr[0];
$sysNo=$sysIdArr[1];
$subSysNo=$sysIdArr[2];
$objDb='OBJ_'.$sysTypeNo;
$paraConfTable=$objDb.'.paramConf';
$pubDb='OMC_PUB';

$sql="select sysTypeName from OMC_PUB.sysConf where sysTypeNo='$sysTypeNo' ";
$res=@mysqli_query($pubConn,$sql);
$row=@mysqli_fetch_array($res);
$module_name=$row[sysTypeName];

if(checkIndependSystem($sysTypeNo))//Plat
	$paraTable=$objDb.".param_".$sysNo;
else
	$paraTable=$objDb.".param_99";


//feake
showTitle("show",$tableOid,$instanceNo,$sysId,$sysTypeNo,$module_name);


echo "<form name=\"myform\" method=\"POST\" action=\"$PHP_SELF\">";
//delete
if($delete == 1)
{
	//echo "Now delete the object,sysId=$sysId,tableOid=$tableOid,deleteOid=$deleteOid,deleteInstanceNo=$deleteInstanceNo";
	$deleteOid=trim($deleteOid);
	$tmpwhere=getLevelWhere($deleteOid);
	$sql="update $paraTable set initValue='6' where $tmpwhere and instanceNo='$deleteInstanceNo' ";
	
	if($sysTypeNo == 260 && $sysNo != 99 && $subSysNo != 99)
		$sql.="and subSysNo='$subSysNo' ";
	
	//echo "$sql<br>";
	mysqli_query($pubConn,$sql);
	$commIdStr=oidSysCommSend($sysId,3,$deleteOid,$selectedSysId=-1,$deleteInstanceNo);
	//echo "oidSysCommSend($sysId,3,$deleteOid,$selectedSysId=-1,$deleteInstanceNo);<br>";
	checkState($commIdStr,"./param_show_table.php?sysId=$sysId&tableOid=$tableOid&table_type=$table_type&instanceNo=$instanceNo");
	
	//update the entries num
	$tlevel=getTableLevel($tableOid,$sysTypeNo);
	if($tlevel > 1)
	{
		$entriesTableName=getTableNameByOid($sysId,$tableOid);
		$entriesTableName=str_replace("Entry","",$entriesTableName);
		$entriesOid=findRelationOid($entriesTableName,TABLE_ENTRIES,$sysTypeNo);
		$entriesInstanceNo=substr($deleteInstanceNo,0,strlen($deleteInstanceNo)-5);
		$commIdStr=oidSysCommSend($sysId,0,$entriesOid,$selectedSysId=-1,$entriesInstanceNo);
		checkState($commIdStr,"./param_show_table.php?sysId=$sysId&tableOid=$tableOid&table_type=$table_type&instanceNo=$instanceNo");
		
		//echo "entriesTableName=$entriesTableName,entriesOid=$entriesOid,entriesInstanceNo=$entriesInstanceNo<br>";
		//echo "deleteOid=$deleteOid,deleteInstanceNo=$deleteInstanceNo,tableOid=$tableOid,tlevel=$tlevel<br>";
	}
	
	//command success,delete the record in the db
	//echo "deleteOid=$deleteOid<br>";
	$etmp=explode(".",$deleteOid);
	$elen=sizeof($etmp);
	$deleteTableOid="";
	for($ti=0;$ti<$elen-1;$ti++)
		$deleteTableOid.=$etmp[$ti].".";
	$deleteTableOid=substr($deleteTableOid,0,strlen($deleteTableOid)-1);
	//echo "deleteTableOid=$deleteTableOid<br>";
	$tmpwhere=getLevelWhere($deleteTableOid);
	$sql="delete from $paraTable where $tmpwhere and instanceNo=$deleteInstanceNo ";
	
	if($sysTypeNo == 260 && $sysNo != 99 && $subSysNo != 99)
		$sql.="and subSysNo='$subSysNo' ";
	
	
	//echo "$sql<br>";
	mysqli_query($pubConn,$sql);
	
	//When delete an object, then get all the table 
	$getArr[0][sysId]=$sysId;
	$getArr[0][oid]=$tableOid;
	//echo "table=$tableOid,instanceNo=$instanceNo<br>";
	$level=getTableLevel($tableOid,$sysTypeNo);
	if($level > 1 && $level < 4)
		$getArr[0][instanceNo]=$instanceNo;
	else
		$getArr[0][instanceNo]="";
	getParam($getArr);
}

//Get all
if($_SERVER['REQUEST_METHOD'] == "POST")
{
	if($get)
	{
		//Get the timeslot parameter from the plat(trunk group id)
		$trunk_table_oid=GetOIDByName(SYSTYPENO_MSC,"timeSlotEntry");
		if($sysTypeNo == SYSTYPENO_MSC && $tableOid == $trunk_table_oid)
		{
			$tmps=explode(".",$instanceNo);
			$grounp_id=GetTrunkGroupID($tmps[0]+0,$tmps[1]+0);
			if($grounp_id == 1)
				$commInfoArr[0][sysId]="320_1_0";
			else
				$commInfoArr[0][sysId]="320_0_0";
			if(!GetSystemStatus(SYSTYPENO_MSC,$grounp_id))
				$commInfoArr[0][sysId]="320_99_99";
		}

		getParam($commInfoArr);
	}
}


$keyId=0;
showTable($tableOid);


for($i=0;$i<$keyId;$i++)
{
	if(!$objects[$i]['type'] == LEAF_TYPE && !strstr($objects[$i]['name_1'],"Entry") && !strstr($objects[$i]['name_1'],"Table"))
	{
		$objects[$i]['table_type']=SCALAR_TABLE_TYPE;
	}
	if($objects[$i]['type'] == UNKNOWN_TYPE)
	{
		$objects[$i]['type']=SCALAR_TABLE_TYPE;
	}
}
$indexObjectNum=0;
$showObjectNum=0;
$entries_count=0;
for($index=0;$index<$keyId;$index++)
{
	
	if($objects[$index]['type'] == ENTRY_TYPE)
	{
		//echo "<b>{$objects[$index]['name_1']}</b><p>";
		echo "<table border=\"1\" width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"  bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\">";
		echo "<tr bgcolor=#cecece>";
	}
	else if($objects[$index]['type'] == LEAF_TYPE)
	{
	
		if(strstr($objects[$index]['desc_1'],"Key Parameter"))
		{
			$displayName=getShowNameOfField($objects[$index]['name_1'],$index);
			
			if(strstr($objects[$index]['name_1'],"RowStatus"))
				echo "<td align=right>Delete</td>";
			else	
			{
				echo "<td><a href=\"param_show_table.php?tableOid=$tableOid&sysId=$sysId&instanceNo=$instanceNo&table_type=$table_type&sort=$showObjectNum\">";
				echo "{$displayName}</a></td>";
			}
			$showObjectList[$showObjectNum]=$index;
			$showObjectNum++;
		}
		
		if(strstr($objects[$index]['name_1'],"Index"))
		{
			$indexObjectList[$indexObjectNum]=$index;
			$indexObjectNum++;
		}
		
		if(strstr($objects[$index]['name_1'],"Entries"))
		{
			//$nextEntriesKeyId=$index;
			$entries_keyId[$entries_count]=$index;
			
			$tb=$objects[$index]['name_1'];
			$tlen=strlen($tb);
			$fg=0;
			$tb1="";
			for($ti=0;$ti<$tlen;$ti++)
			{
				if($tb[$ti] >= 'A' && $tb[$ti] <= 'Z')
					$fg=1;
				if($fg==1)
					$tb1.=$tb[$ti];
			}
			$tb2=explode("Entries",$tb1);
			$tb3=$tb2[0];
			//PrintInfo("table3=".$tb3);
			$entriesOid=findRelationOid($tb3,TABLE_ENTRY,$sysTypeNo);
			$objects[$index]['nextEntryOid']=$entriesOid;
			
			
			$entries_count++;

		}
		
		if(strstr($objects[$index]['name_1'],"RowStatus"))
		{
			$rowstatusKeyId=$index;
			$deleteOid=$objects[$index]['oid'];
		}
		
	}
	
}
echo "</tr>";
//show the object rows
for($i=0;$i<$showObjectNum;$i++)
{
	
	$sKeyId=$showObjectList[$i];
	$valueTypes[$i]=$objects[$sKeyId]['valueType'];
	$objectStyles[$i]=$objects[$sKeyId]['style'];
	if($sKeyId == $indexObjectList[$indexObjectNum-1])
		$showIndexPos=$i;
	//if($sKeyId == $nextEntriesKeyId)
	if($entries_count>0 && in_array($sKeyId,$entries_keyId))
	{
		//$nextEntriesPos=$i;
		$entries_keyId_pos[$i]=$sKeyId;
		$entries_pos[$sKeyId]=$i;
	}
	if($sKeyId == $rowstatusKeyId)
		$rowstatusPos=$i;
	$selectLevel=getLevelWhere($objects[$sKeyId]['oid']);
	$orderBy="order by instanceNo ";
	$tablelevel=getTableLevel($tableOid,$sysTypeNo);
	
	if($sysTypeNo == 260 && $sysNo != 99 && $subSysNo != 99)
		$selectLevel.=" and subSysNo='$subSysNo' ";
	
	if($instanceNo == "0000")
	{
		if($tablelevel>1)
    {
    	$likestr="";
    	for($tl=0;$tl<$tablelevel-1;$tl++)
      	$likestr.="0000.";
      $likestr.="%";
      $sql="select initValue,instanceNo from $paraTable where $selectLevel and instanceNo like '$likestr' $orderBy";
    }
    else
			$sql="select initValue,instanceNo from $paraTable where $selectLevel $orderBy";
	}
	else
	//and instanceNo<>'0000'
		$sql="select initValue,instanceNo from $paraTable where $selectLevel and instanceNo like '$instanceNo.%' $orderBy";
	//echo "$sql<br>";
	$res=@mysqli_query($pubConn,$sql);
	$objectIndex=0;
	while($row=@mysqli_fetch_array($res))
	{
		if(strstr($objects[$sKeyId]['name_1'],"Index"))
			$showValue=$row[initValue]+0;
		else
			$showValue=translateValueToOpt($sKeyId,$row[initValue]);
		$objectValues[$i][$objectIndex]=$showValue;
		$objectKeyId[$i][$objectIndex]=$sKeyId;
		$objectInstanceNo[$i][$objectIndex]=$row[instanceNo];
		$objectIndex++;
	}
}
if(!isset($sort))
	$sort=0;
//echo "style=$objectStyles[$sort],valueType=$valueTypes[$sort]<br>";
if($objectIndex > 0)
{
	if(is_numeric($objectValues[$sort][0]))
		asort($objectValues[$sort],SORT_NUMERIC);
	else
		asort($objectValues[$sort],SORT_STRING);
}
if($objectIndex > 0)
{
	$ki=0;
	foreach ($objectValues[$sort] as $key => $val) 
	{
		$keys[$ki]=$key;
		$ki++;
	  //echo "$key = $val<br>";
	}
}

//for($i=0;$i<$objectIndex;$i++)
if($objectIndex > 0)
{
	foreach($keys as $skey => $i)
	{
		//if($i == $sort)
		//	array_multisort($objectValues[$i], SORT_DESC);
		echo "<tr onMouseOver=\"javascript:MouseOverColor(this);\" onMouseOut=\"javascript:MouseOutColor(this);\">";
		for($j=0;$j<$showObjectNum;$j++)
		{
			//$nowpos=array_search($j,$entries_pos);
			if($showIndexPos == $j)
			{	
				echo "<td>";
				
				echo "<a href=\"param_edit.php?tableOid=$tableOid&sysId=$sysId&table_type=1&instanceNo={$objectInstanceNo[$j][$i]}\">";
				echo "{$objectValues[$j][$i]}&nbsp";
				echo "</a>";
				//echo "index $tableOid ";
			}
			//else if($nextEntriesPos == $j)
			else if($entries_count>0 && in_array($j,$entries_pos))
			{			
				$entriesKeyId=$entries_keyId_pos[$j];
				//PrintInfo("nextEntriesOid=".$objects[$entriesKeyId]['nextEntryOid']);
				$nextEntryOid=$objects[$entriesKeyId]['nextEntryOid'];
				
				echo "<td>";
				echo "<a href=\"param_show_table.php?tableOid=$nextEntryOid&sysId=$sysId&table_type=1&instanceNo={$objectInstanceNo[$j][$i]}\">";
				echo "{$objectValues[$j][$i]}&nbsp</a>";
			}
			else if($rowstatusPos == $j)
			{
				echo "<td align=right>";
				//echo "{$objectInstanceNo[$j][$i]}";
				echo "<input type=\"checkbox\" onClick=\"javascript:deleteObject('$sysId','$deleteOid','{$objectInstanceNo[$j][$i]}','$tableOid','$instanceNo',this);\">";
			}
			else
			{
				echo "<td>";
				$rKeyId=$objectKeyId[$j][$i];
				if(strstr($objects[$rKeyId]['desc_1'],"RelationPoint"))
				{
					$tmp1=strstr($objects[$rKeyId]['desc_1'],"RelationPoint:");
					$tmp2=explode(";",$tmp1);
					$tmp3=explode("RelationPoint:",$tmp2[0]);
					$rTableOid=$tmp3[1];
					
					$showvalue=$objectValues[$j][$i];
					
					if(trim($showvalue) == "--" || trim($showvalue)=="")
					{
						echo "--";
					}
					else
					{
						$tmp1=explode(" ",$showvalue);
						$tmp2=$tmp1[1];
						$tmp3=explode("-",$tmp2);
						$tmp4=omc_keep_length($tmp3[0],'0',4);
						$tmp5=omc_keep_length($tmp3[1],'0',4);
						$rInstanceNo=$tmp4.".".$tmp5;
						$rWhere=getLevelWhere($rTableOid);
						$rsql="select * from $paraTable where instanceNo='$rInstanceNo' and $rWhere ";
						$rres=mysqli_query($pubConn,$rsql);
						$num=mysqli_num_rows($rres);
						//echo "$rsql";
						if($num == 0)
							echo "--";
						else
							echo "<a href=\"./param_edit.php?tableOid=$rTableOid&instanceNo=$rInstanceNo&sysId=$sysId&table_type=1\">$showvalue</a>";
					}
				}
				else
				{
					echo "{$objectValues[$j][$i]}&nbsp";
				}
			}
			echo "</td>";
		}
		echo "</tr>";
	}
}

//check whether this table can add instance
$haveNewLink=0;
$level=getTableLevel($tableOid,$sysTypeNo);
//nest table
if($level > 1)
{
	$nowTableName=getTableNameByOid($sysId,$tableOid);
	$nowTableName=str_replace("Entry","",$nowTableName);
	$createEntryOid=findRelationOid($nowTableName,TABLE_CREATE,$sysTypeNo);
	//PrintInfo("nowTableName=".$nowTableName.",createEntryOid=".$createEntryOid);
	if($createEntryOid != "")
		$haveNewLink=1;
}
else
{
	$checkWhere=getLevelWhere($tableOid);
	$sql="select * from $paraConfTable where $checkWhere and maxAccess='48' and name_1 like '%Index' ";
	//echo "$sql<br>";
	$res=@mysqli_query($pubConn,$sql);
	$num=mysqli_num_rows($res);
	if($num == 1)
		$haveNewLink=1;
}






if($haveNewLink == 1)
{
	echo "<td><a href=\"param_edit.php?createpage=1&sysId=$sysId&tableOid=$tableOid&instanceNo=$instanceNo&table_type=1\">New</a></td>";
		
	for($j=0;$j<$showObjectNum-1;$j++)
		echo "<td>--</td>";
	echo "</tr>";
}
echo "</table>";


$DEBUG=0;
if($DEBUG)
{
	echo "<p>============================test========================<br>";
	echo "keyId=$keyId<br>";
	$test_oid_flag=1;
	$test_name_flag=1;
	$test_nest_flag=1;
	$test_type_flag=1;
	
	for($i=0;$i<$keyId;$i++)
	{
		//if($objects[$i]['type'] != LEAF_TYPE && $objects[$i]['type'] != ENTRY_TYPE)
		{
			echo "$i-";
			if($test_oid_flag)
				echo "oid-{$objects[$i]['oid']}, ";
			if($test_name_flag)
			 	echo "name-{$objects[$i]['name_1']}, ";
			if($test_nest_flag)
				echo "nestNum-{$objects[$i]['nest_num']},  ";
			if($test_type_flag)
				echo "type-{$type_label[$objects[$i]['type']]}, ";
				
			echo "<br>";
		}
	}
}
//echo "<hr>sysId=$sysId,tableOid=$tableOid,instanceNo=$instanceNo<hr>";
echo "<input type=\"hidden\" name=\"commInfoArr[0][sysId]\" value=\"$sysId\">";
echo "<input type=\"hidden\" name=\"commInfoArr[0][oid]\" value=\"$tableOid\">";
//for get the instance 0
if($instanceNo=="0000")
	echo "<input type=\"hidden\" name=\"commInfoArr[0][instanceNo]\" value=\"\">";
else
	echo "<input type=\"hidden\" name=\"commInfoArr[0][instanceNo]\" value=\"$instanceNo\">";


//get all
echo "<input id=\"get_id\" type=\"submit\" name=\"get\" value=\"$strGet\" class=\"button\" style=\"$buttonStyle\">";
echo "<input type=\"hidden\" name=\"tableOid\" value=\"$tableOid\">";
echo "<input type=\"hidden\" name=\"sysId\" value=\"$sysId\">";
echo "<input type=\"hidden\" name=\"table_type\" value=\"$table_type\">";
echo "<input type=\"hidden\" name=\"instanceNo\" value=\"$instanceNo\">";


//lock the time slot
if($sysTypeNo == SYSTYPENO_MSC && $tableOid == '2.4.3.1')
{
	//{$sysTypeNo} {$tableOid} {$objects[$index][oid]}
	echo "<input id=\"Lock\" type=\"button\" name=\"locktimeslot\" value=\"Lock\" class=\"button\" style=\"$buttonStyle\" onClick=\"Javascript:lockTimeSlot('$sysId','$instanceNo','{$objects[$index][oid]}','$tableOid','lock');\">";
	echo "<input id=\"Unlock\" type=\"button\" name=\"unlocktimeslot\" value=\"Unlock\" class=\"button\" style=\"$buttonStyle\" onClick=\"Javascript:lockTimeSlot('$sysId','$instanceNo','{$objects[$index][oid]}','$tableOid','unlock');\">";
	//echo "<a href=\"Javascript:lockTimeSlot('$sysId','$instanceNo','{$objects[$index][oid]}','$tableOid')\" style=\"display:compact;float:right;border-color:black;border:none;\">";
	//echo "<img width=20 height=20 src=\"../../images/lock.png\"></a>";
}	


//if not get timeslot, get the timeslot first
if($sysTypeNo == SYSTYPENO_MSC && $tableOid == "2.4.3.1")
{
	$dualTableLevelWhere="T1.level_1=T2.level_1 AND
						  T1.level_2=T2.level_2 AND
						  T1.level_3=T2.level_3 AND
						  T1.level_4=T2.level_4 AND
						  T1.level_5=T2.level_5 AND
						  T1.level_6=T2.level_6 AND
						  T1.level_7=T2.level_7 AND
						  T1.level_8=T2.level_8 AND
						  T1.level_9=T2.level_9 AND
						  T1.level_10=T2.level_10";
	$count=0;
	$database=$objDb;
	$selSql = "SELECT T1.* FROM $paraTable AS T1,$paraConfTable AS T2 WHERE $dualTableLevelWhere AND T2.name_2='tsIndex' and instanceNo like '$instanceNo.%' ";
	$selRes = @mysqli_query($pubConn,$selSql);
	$count=mysqli_num_rows($selRes);
	if($count==0)
	{
		if($hasEnter!=1)
		{
			$hasEnter=1;
			echo "<input type=\"hidden\" name=\"hasEnter\" value=\"1\">";	
			echo "\n<script language=\"JavaScript\">\n";
			echo "document.getElementById(\"get_id\").click();\n";
			echo "</script>\n";
		
		}
	}
}

echo "</form>";
adjust_content_tail("down");
?>
