<?php
/*********************************************************      
 程序说明：         
    功能说明：将用户数据表的数据导入到临时参数表。

    调用关系：调用: header.inc,lib.inc     
              被调用:params_adm_down_left.php      
 
    变量说明：
		输入变量：
			$keyValue	:由用户列表传来
			$sysId		:由用户列表传来或本页面指定
			$language:语言种类

     返回值：无

 作者：      
      
修改注释：      
NO.1      
姓名：      
时间：  
修改说明： 
*********************************************************/  
require("../../inc/header.inc");
?>
<base target="_self">
<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>";
$DEBUG = 0;
global $pubConn;

if($DEBUG) echo "<br>正在调试中，给您的工作带来不便，请原谅！！：）";

//==========================================================
//变量定义
//==========================================================
$pubDb				= 'OMC_PUB';
$sysSubsConfTable	='OMC_PUB.sysSubsConf';
$systemConfTable	= 'OMC_PUB.sysConf';

$nReadComm		= 1;
$nChangeComm	= 2;
$nCreateComm	= 3;
$nDeleteComm	= 4;
$nLoadIndexComm	= 5;
$nReplaceIndexComm	= 6;

$maxLevelNum = 10;

$nInteger	= 2;
$nAscStr	= 3;
$nHexStr	= 4;
$dataTypeNo = $_REQUEST['dataTypeNo'];

//------------选择语言版本----------
if($language == 'eng'){
	$objNameField = 'name_2'; 
	$objDescField = 'desc_2';
	$objNoteField = 'note_2';
}else{
	$objNameField = 'name_1';
	$objDescField = 'desc_1';
	$objNoteField = 'note_1';
}

//==========================================================
//输入变量
//==========================================================
$canSetNum = $_REQUEST['canSetNum'];
$keyValue=$_REQUEST['keyValue'];
$canSetNum = isset($canSetNum)?$canSetNum:'all';

$sysId=$_REQUEST['sysId'];
$dataTypeNo = $_REQUEST['dataTypeNo'];
$isLoadIndex=$_REQUEST['isLoadIndex'];
$isTryCreate=$_REQUEST['isTryCreate'];
$isBatchCreate = $_REQUEST['isBatchCreate'];
$isBatchDelete = $_REQUEST['isBatchDelete'];
$Delstartkey = $_REQUEST['Delstartkey'];
$batchDelNum = $_REQUEST['batchDelNum'];
$batchDelete = $_REQUEST['batchDelete'];
$batchNum=$_REQUEST['batchNum'];
$isCreate=$_REQUEST['isCreate'];
$isDelete=$_REQUEST['isDelete'];
$isChange=$_REQUEST['isChange'];
$isRead=$_REQUEST['isRead'];
$sysIdArr=explode('_',$sysId);
$sysTypeNo=$sysIdArr[0];
$sysNo=$sysIdArr[1];

$objConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], 'OBJ_'.$sysTypeNo);


//显示用户数据编辑页面，需要的变量
if($DEBUG) echo "<br>sysId=$sysId";
if($DEBUG) echo "<br>canSetNum=$canSetNum";
if($DEBUG) echo "<br>keyValue=$keyValue";
if($DEBUG) echo "<br>sysTypeNo=$sysTypeNo";
if($DEBUG) echo "<br>sysNo=$sysNo";
if($DEBUG) echo "<br>subSysNo=$subSysNo";
if($DEBUG) echo "<br>dataTypeNo=$dataTypeNo";
//例如：
//$canSetNum =1; // 或= 'all'
//$sysId='330_0_0';
//$keyValue= '0460022157127016';

$isRead=$_REQUEST['isRead'];
$isChange=$_REQUEST['isChange'];
$isCreate=$_REQUEST['isCreate'];
$isDelete=$_REQUEST['isDelete'];
$isTryCreate=$_REQUEST['isTryCreate'];
$isLoadIndex=$_REQUEST['isLoadIndex'];

//操作类型变量
if($DEBUG) echo "<br>isRead=$isRead";
if($DEBUG) echo "<br>isChange=$isChange";
if($DEBUG) echo "<br>isCreate=$isCreate";
if($DEBUG) echo "<br>isDelete=$isDelete";
if($DEBUG) echo "<br>isLoadIndex=$isLoadIndex";


$bgnNo=$_REQUEST['bgnNo'];
$endNo=$_REQUEST['endNo'];
if (!isset($keyValue)) $keyValue=$_POST['keyValue'];

//保存用户数据修改的数据
if($DEBUG) echo "<br>bgnNo=$bgnNo";	
if($DEBUG) echo "<br>endNo=$endNo";	
//显示每个需要保存的字段
for($i=$bgnNo;$i<$endNo;$i++){
	${'checkBox_'.$i} = $_POST['checkBox_'.$i];
	${'fieldName_'.$i} = $_POST['fieldName_'.$i];
	${'editValue_'.$i} = $_POST['editValue_'.$i];
	if($DEBUG) echo "<br>checkBox_$i	= ${'checkBox_'.$i}";
	if($DEBUG) echo "<br>fieldName_$i	= ${'fieldName_'.$i}";
	if($DEBUG) echo "<br>editValue_$i	= ${'editValue_'.$i}";
	if($isCreate > 0){
		if($tmpFieldName == $indexFieldName){
			$indexValueList = $editValue;
			if($DEBUG) echo "<br> indexValueList($indexValueList) be change to $editValue";
		}
	}
}
//if($DEBUG && $endNo > 1) exit();

//==========================================================
//数据处理
//==========================================================
$fieldNameList		= "ALL";
require("./sendComm.inc");
require("./dataProc.inc");
//==========================================================
//输出并显示数据
//==========================================================
?>

<?php
//**********************************编辑参数时所用javascript*****************************************
?>
<script LANGUAGE="JavaScript">         

function changevalue(term)
{
	term.style.color = "BLUE";
	//term.value = term.value.toUpperCase();
    /* 
	if(term.value.length != term.maxlength){  
		var LengthErrorWarning = "<?php echo $strLengthOfTheInputValueIsError; ?>";
		var CorrectItWarning = "<?php echo $strPleaseCorrectIt; ?>";
		alert(LengthErrorWarning + CorrectItWarning + "[= " + term.maxlength + "]");      
		term.focus();
		return (false);
	}
    */
    var checkOK = "-0123456789ABCDEF";
    var checkStr = term.value;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++){
			if (ch == checkOK.charAt(j))
				break;
		}
		if (j == checkOK.length){
			//allValid = false;
			break;
		}
	}

	if (!allValid){  
		var ParaHex="<?php echo $strParaMustBeAHexNumber; ?>";
		var Capital="<?php echo $strCapitalCharacter; ?>";
		alert(ParaHex+"，"+"("+Capital+")！");
		term.focus();
		return (false);
	}
}

function selectToSet(thisCheckBox)
{
	if(thisCheckBox.checked == true)
	{
		thisCheckBox.value=1;
		document.update.setNum.value++;
		if(document.update.setNum.value > (document.update.canSetNum.value - 0)){
			alert("You can not change too much parameter["+document.update.setNum.value+" > "+document.update.canSetNum.value+"] in one times,please try it next time!");
			thisCheckBox.checked = false;
			document.update.setNum.value--;
			return;
		}
	}else{
		thisCheckBox.value=0;
		document.update.setNum.value--;
	}
}

function selectAllToSet(thisCheckBox)
{
	var p;
	var bgnNo=document.update.bgnNo.value;
	var endNo=document.update.endNo.value;
	var tmpCheckBox;
	if(thisCheckBox.checked == true)
	{
		for(p=bgnNo; p < endNo; p++){
			tmpCheckBox=eval("document.update.checkBox_"+p);
			tmpCheckBox.checked = true;
			tmpCheckBox.value=1;
		}
	}else{
		for(p=bgnNo; p < endNo; p++){
			tmpCheckBox=eval("document.update.checkBox_"+p);
			tmpCheckBox.checked = false;
			tmpCheckBox.value=0;
		}
	}
}

function checkSubmit()
{
	var p;
	var bgnNo=document.update.bgnNo.value;
	var endNo=document.update.endNo.value;
	var tmpCheckBox;
	var selectedNum = 0;
	var chgFlag = document.update.isChange.value;
	var crtFlag = document.update.isCreate.value;
	if(chgFlag == 0 && crtFlag == 0){
		return (true);
	}
	for(p=bgnNo; p < endNo; p++){
		tmpCheckBox=eval("document.update.checkBox_"+p);
		if(tmpCheckBox.checked == true){
			if(checkOneInput(p) == true){
				selectedNum++;
			}else{
				return (false);
			}
		}
	}
	if(document.update.isRead.value != 1){
		if(selectedNum > 0){
			return (true);
		}else{
			alert("Please select the check box for edit parametr!");
			return (false);
		}
	}
}

function openSet(sysTypeNo,objKeyId,inputName,dataTypeNo)
{
	var inputValue,checkBoxValue;
	//checkBoxValue = eval("document.update."+checkBoxName+".checked");
	//if(checkBoxValue == false){
	//	alert("Please select the check box first!");
	//	return;
	//}
	inputValue = eval("document.update."+inputName+".value");
	URL = "./paramEdit.php?sysTypeNo="+sysTypeNo;
	URL += "&keyId="+objKeyId+"&setValue="+inputValue
	URL += "&inputName="+inputName+"&dataTypeNo="+dataTypeNo;
	var msgWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=350');
	msgWindow.focus();
}

function checkOneInput(p)
{ 
	var valueType,nInt=2,nAsc=3,nHex=4;
	var i,j;                    
	var checkOK = "-0123456789ABCDEF";                     
	var editValue,readValue;                     
	var allValid = true;         
	var LengthErrorWarning="<?php echo "$strLengthOfTheInputValueIsError"; ?>";
	var CorrectItWarning="<?php echo "$strPleaseCorrectIt"; ?>";                    
	var ValueCannotEmpty="value cannot be empty!";
	var ValueLengthError="value length error!";


	editInput = eval("document.update.editValue_"+p);
	editValue = eval("document.update.editValue_"+p+".value.toUpperCase()");
	valueType = eval("document.update.valueType_"+p+".value");
	editFlag  = eval("document.update.checkBox_"+p+".checked");
	maxLen	  = eval("document.update.maxLen_"+p+".value");

	if((editValue.length < 1) && (editFlag == true)){
		editInput.style.background = "RED";
		alert(ValueCannotEmpty);
		return (false);
	}
		
	switch(valueType){
		case nInt:
			<?php //检测输入值是否为0-f的16进制字符?>
			for (i = 0;  i < editValue.length;  i++){                     
				ch = editValue.charAt(i);                     
				for (j = 0;  j < checkOK.length;  j++){                     
					if (ch == checkOK.charAt(j))                     
					break;
				}
				if (j == checkOK.length){                     
					allValid = false;                     
					break;                     
				}                     
			}
			break;
		case nAsc:
			break;
		case nHex:
			<?php //检测输入值是否超长或超短?>
			if( editFlag == true && (editValue.length  < maxLen || editValue.length  > maxLen)){
				document.update.editInput.style.color = "RED";
				alert(ValueLengthError);
				return (false);
			}
			<?php //检测输入值是否为0-f的16进制字符?>
			for (i = 0;  i < editValue.length;  i++){                     
				ch = editValue.charAt(i);                     
				for (j = 0;  j < checkOK.length;  j++){                     
					if (ch == checkOK.charAt(j))                     
					break;
				}
				if (j == checkOK.length){                     
					allValid = false;                     
					break;                     
				}                     
			}
			break;
		default:
			break;
	}
                                
                     
	//当输入值有非法字符       
	if (!allValid){
		var ParaHex="Input value error!";
		alert(ParaHex);     
		return (false);
	}
	editInput.style.background = "Yellow";
    return (true);
}

function askSureToDel(keyName,keyId)
{
	var answer = confirm("Are you sure to delete this subscriber( "+keyName+"="
                          + keyId + ") from <?php echo"$sysTypeName";?> server?");
    if (answer == true){
		window.location.href = "<?php echo "$PHP_SELF?sysId=$sysId&dataTypeNo=$dataTypeNo&bufferNo=$bufferNo&keyValue=$keyValue&isDelete=1";?>";
		//alert('Try to send delete command');
	}else{
		return(false);
	}
}

function askSureToReplace(keyName,keyId)
{
	var answer = confirm("Are you sure to replace this subscriber's Index( "+keyName+"="
                          + keyId + ")?");
    if (answer == true){
		window.location.href = "<?php echo "$PHP_SELF?sysId=$sysId&dataTypeNo=$dataTypeNo&bufferNo=$bufferNo&keyValue=$keyValue&isReplaceIndex=1";?>";
	}else{
		return(false);
	}
}
</script>
<form method="POST" name="update" action="<?php echo"$PHP_SELF";?>" onSubmit="return checkSubmit() ">
  <?php
	//显示用户参数编辑页面
	?>
  <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
	<td><nobr>
	<?php echo "Server ID "?>
	<select name="sysId" size="1">
	<?php
	$selSysIdSql="SELECT sysNo,subSysNo,sysTypeName 
				FROM sysInfo,sysConf 
				WHERE sysInfo.sysTypeNo='$sysTypeNo' AND sysConf.sysTypeNo='$sysTypeNo'
				ORDER BY baseStat DESC,updateTime DESC
				";
	if($DEBUG) echo "<br>11.$selSysIdSql";
	$sysIdResult=mysqli_query($pubConn,$selSysIdSql);
	echo mysqli_error($pubConn);
	while($sysIdRows=mysqli_fetch_array($sysIdResult)){
		echo "<option value=\"$sysTypeNo"."_$sysIdRows[sysNo]"."_$sysIdRows[subSysNo]\"";
		if(($sysNo == $sysIdRows[sysNo]) && ($subSysNo == $sysIdRows[subSysNo]))
			echo " selected";
		echo " >$sysIdRows[sysTypeName]"."_$sysIdRows[sysNo]"."_$sysIdRows[subSysNo]";
		echo "</option>";
	}
	?>
	</select>
	</nobr>
	</td>  
	<td><nobr>
	<?php 
	if($sysTypeNo == '360' ||$sysTypeNo == '395'|| $sysTypeNo == '350' ||  $sysTypeNo == '375'){ //pps or smsc
		$keyValueName = 'MSISDN';
		if($sysTypeNo == '360')
		{
			if($dataTypeNo == 0)
			{
				$keyValueName = "MSISDN";
			}
    	elseif($dataTypeNo == 1)
    	{
				$keyValueName = "Card No.";
			}
		}
		if($sysTypeNo == '390')
		{
			if($dataTypeNo == 0)
			{
				$keyValueName = "MSISDN";
			}
    	elseif($dataTypeNo == 1)
    	{
				$keyValueName = "Card No.";
			}
		}
	}
	else if($sysTypeNo == '365')
	{
	    $keyValueName = 'SUPI';
	}
	else
	{//hlr or auc or sn
		$keyValueName = 'IMSI';
	}
	
	if(substr($keyValue,0,1) == 0 && strlen($keyValue) > 15){
		$showkeyValue= substr($keyValue,1,15);
	}else{
		$showkeyValue= $keyValue;
	}
	echo "&nbsp;&nbsp;$keyValueName:$showkeyValue";
	?>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	</nobr></td>
	<?php 
	if($isTryCreate > 0){
	?>
		<td >
		<input type="submit" value="Create in <?php echo "$sysTypeName"?>" name="create" title="" class="button" onClick="javascript:document.update.isCreate.value=1;">
		<input type="hidden" value="0" name="isCreate">
		<input type="hidden" value="0" name="isChange">
		</td>
		<td>
		&nbsp;&nbsp;&nbsp;
		<input type="submit" value="Create in OMC" name="create" title=""  class="button" onClick="javascript:document.update.isCreate.value=2;">
		</td>
	<?php
	}else{
	?>
		<td>
		<input type="submit" value="Get" name="read" title=""  class="button" onClick="javascript:document.update.isRead.value=1;">
		<input type="hidden" value="0" name="isRead">
		</td>

		<?php
		if($sysTypeNo!='325')
			{
		?>

		<td>
		<input type="submit" value="Set" name="change" title=""  class="button" onClick="javascript:document.update.isChange.value=1;">
		<input type="hidden" value="0" name="isChange">
		<input type="hidden" value="0" name="isCreate">
		</td>
		
		<td>
		<input type="button" value="Delete" name="delete" title="Delete this subscriber"  class="button" onClick="askSureToDel(<?php echo"'$keyValueName','$keyValue'";?>)">
		<input type="hidden" value="0" name="isDelete">
		</td>

		<td>
		<input type="button" value="Replace index value" name="replace" title="Replace this subscriber's Index Value"  class="button" onClick="askSureToReplace(<?php echo"'$keyValueName','$keyValue'";?>)">
		<input type="hidden" value="0" name="isReplaceIndex">
		</td>
	<?php
			}
	}
	?>
  <td width="100%">&nbsp;</td>
  </tr>  
</table>
<br>
<table border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF">   
  <tr bgcolor="#E6E6E6">
    <td width="5%" align="center" >No.</td>  
    <td width="50%"><?php echo"Parameter"; ?></td>  

	<td width="5%" >
	<?php
	if($canSetNum == 'all'){
		echo "<input type=\"checkbox\" name=\"seleceAll\" title=\"Select all to set\"  value=\"1\" onClick=selectAllToSet(this)>";
	}else{
		echo "&nbsp;";
	}
	?>
	</td> 

    <td width="20%" title="">Value</td>
  </tr>
  
<?php
//************************顺序显示该页每条参数************************

//在参数定义表取出说明和在临时参数表取出相关的值
$paramSql = " SELECT * FROM $confTable  
				WHERE $confTable.operType > '0' 
				ORDER BY level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,level_10 
			";
			
if($DEBUG) echo"<br>objDb =$objDb <br> ParamSql = $paramSql <br> ";
$paramResult = mysqli_query($objConn,$paramSql);
echo mysqli_error($objConn);
$i=0;
while($paramRows = @mysqli_fetch_array($paramResult)){  
	$editName='editValue_'.$i;
	$checkBoxName='checkBox_'.$i;
	$fieldNameName = 'fieldName_'.$i;
	$valueTypeName='valueType_'.$i;
	$maxLen=$paramRows[length]*2;
	$maxLenName='maxLen_'.$i;
	if($paramRows[length] > 4){
		$inputSize = $paramRows[length] * 2;
	}else{
		$inputSize = 8;
	}
	$fieldName =$paramRows[fieldName];
	echo "\n <tr>";
	echo "<td width=\"5%\"> $i</td> ";
	echo "<td width=\"50%\" title=\"$paramRows[$objDescField]\">$paramRows[$objNameField]</td>";
	echo "<td width=\"5%\">";
	echo "<input type=\"checkbox\" name=\"$checkBoxName\" value=\"0\" onClick=selectToSet(this)>";
	echo "</td> ";
	echo "<td width=\"40%\">";
	if($inputSize > 32){
		$rows=ceil($inputSize / 32) + 1;
		$cols=32;
		echo "<textarea  rows=\"$rows\" cols=\"$cols\" name=\"$editName\" size=\"$inputSize\" onchange = changevalue(this)>$tmpRow[$fieldName]</textarea>";
	}else{
		
		echo "<input type=\"text\" name=\"$editName\" maxlength=\"$maxLen\" size=\"$inputSize\" value=\"$tmpRow[$fieldName]\" onchange = changevalue(this)>";
	}

	echo "<a href=javascript:onClick=openSet('$sysTypeNo','$paramRows[keyId]','$editName','$dataTypeNo')>set..</a>";
	
	echo "<input type=\"hidden\" name=\"$fieldNameName\" value=\"$paramRows[fieldName]\">";
	echo "<input type=\"hidden\" name=\"$valueTypeName\" value=\"$paramRows[valueType]\">";
	echo "<input type=\"hidden\" name=\"$maxLenName\" value=\"$paramRows[length]\">";
	echo "</td>";
	echo "</tr>";
	$i++;
}
$bgnNo = 0;
$endNo = $i;
?>
</table>
<?php
//*****************************************隐藏输入框,用于传递值****************************************
echo "\n<input type=\"hidden\" name=\"keyFieldName\"	value=\"$indexFieldName\">";
echo "\n<input type=\"hidden\" name=\"keyValue\"		value=\"$keyValue\">";
echo "\n<input type=\"hidden\" name=\"canSetNum\"		value=\"$canSetNum\">";
echo "\n<input type=\"hidden\" name=\"setNum\"			>";
echo "\n<input type=\"hidden\" name=\"bgnNo\"			value=\"$bgnNo\">";
echo "\n<input type=\"hidden\" name=\"endNo\"			value=\"$endNo\">";
echo "\n<input type=\"hidden\" name=\"dataTypeNo\"		value=\"$dataTypeNo\">";
?>
</form>
&nbsp;
<?php
adjust_content_tail("down");
?>
</body>
</html>
<?php
flush();
?>
