<?php
/*********************************************************      
 程序说明：         
    功能说明：auc用户数据维护专用页面
		1、将用户数据表的数据导入到临时参数表。
		2、修改的用户数据保存到临时参数表中，
		3、设置用户参数时,返回值将修改用户表和临时表。

    调用关系：调用: header.inc
 
    变量说明：
		输入变量：
			$keyValue	:由用户列表传来
			$sysId		:由用户列表传来或本页面指定
			$dataTypeNo	:数据类型，0=用户数据，1=冲值卡数据
			$language	:语言种类
			$canSetNum	:每次最多可以修改的字段个数

 作者：
      
修改注释：      
NO.1      
姓名：      
时间：  
修改说明： 
*********************************************************/  
require("../../inc/header.inc");
$DEBUG = 0;

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

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

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

$maxLevelNum = 10;

$nInteger	= 2;
$nAscStr	= 3;
$nHexStr	= 4;


//select sum(balance) from user_info
//==========================================================
//输入变量
//==========================================================
$canSetNum=$_REQUEST['canSetNum'];
$keyValue=$_REQUEST['keyValue'];
$subSysNo=$_REQUEST['subSysNo'];
$dataTypeNo=$_REQUEST['dataTypeNo'];
$canSetNum = isset($canSetNum)?$canSetNum:'all';
if ($_REQUEST['sysId'])
{
	$sysId =   $_REQUEST['sysId'];
	if($DEBUG) echo "<br>sysId=$sysId";
	$sysIdArr=explode('_',$sysId);
	$sysTypeNo=$sysIdArr[0];
	$sysNo=$sysIdArr[1];
	//	echo "sysTypeNo=$sysTypeNo,sysNo=$sysNo";
}
$canSetNum = isset($canSetNum)?$canSetNum:'all';

//显示用户数据编辑页面，需要的变量
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';

//操作类型变量
$isLoadIndex=$_REQUEST['isLoadIndex'];
$isTryCreate=$_REQUEST['isTryCreate'];
$isBatchCreate=$_REQUEST['isBatchCreate'];
$isCreate=$_REQUEST['isCreate'];
$isDelete=$_REQUEST['isDelete'];
$isChange=$_REQUEST['isChange'];
$isRead=$_REQUEST['isRead'];
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=$_POST['bgnNo'];
$endNo=$_POST['endNo'];
if($DEBUG) echo "<br>bgnNo=$bgnNo";	
if($DEBUG) echo "<br>endNo=$endNo";	

//---------定义需要编辑保存的字段-------
//$showAttrDef=array(array('fieldName'=>'','inputName'=>'');
$showAttrDef=array(
	array('fieldName'=>'key_imsi',		'inputName'=>'key_imsi'),
	array('fieldName'=>'a3a8_version',	'inputName'=>'a3a8Version'),
	array('fieldName'=>'status',		'inputName'=>'status')
);

//For operation log use
{
	for($i=0;$i<256;$i++)
	{
		$key=strtoupper(dechex($i));
		if($i < 16)
			$key="0".$key;
		$a3a8_version_def[$key]="$i";
	}
	
	$status_def=array(
		"00"=>"Inactive","0"=>"Active",
		"01"=>"Inactive","1"=>"Active"
	);
}



$logFieldDef=array(
	'a3a8_version',
	'status',
);




if($_SERVER['REQUEST_METHOD'] == "POST"){
	//system("echo '-------------------------------------'>>/tmp/test.txt");
	$preValueInfo="Change $pre_keyvalue ";
	$isChangeFlag=0;
	for($i=0;$i<sizeof($logFieldDef);$i++){
		$inputName	=$logFieldDef[$i];
		$inputValue	=${$inputName};
		$ppName=$inputName."_ppvalue";
		$ddName=$inputName."_ddef";
		$cdName=$inputName."_cdef";
		$nnName=$inputName."_name";
		if(strlen($inputValue)){
			//system("echo '{$$ddName} ,$inputName	= $inputValue' >> /tmp/test.txt");
			if(isset($$ddName))
			{
				$tmpv=$$ppName;
				$val1=${$$ddName}[$tmpv];
				$val2=${$$ddName}[$inputValue];
				if(strlen($$ppName) == 0)
					$val1="NoValue";
				//system("echo 'val1=$val1,val2=$val2,tmpv=$tmpv' >>/tmp/test.txt");
				if(strcasecmp($val1,$val2) != 0)
				{
					$isChangeFlag=1;
					$preValueInfo.="[{$$nnName},{$val1}->{$val2}]";
					//system("echo 'c[{$$nnName},{$val1}->{$val2}]' >>/tmp/test.txt");
				}
			}
			else if(isset($$cdName))
			{
				$newValue="";
				for($iii=0;$iii<sizeof(${$$cdName});$iii++)
				{
					$chkname=${$$cdName}[$iii][0];
					if($$chkname == '1')
						$newValue.=${$$cdName}[$iii][2].",";
				}
				$newValue=substr($newValue,0,strlen($newValue)-1);
				if(strcasecmp($$ppName,$newValue) != 0)
				{
					$isChangeFlag=1;
					$preValueInfo.="[{$$nnName},({$$ppName})->({$newValue})]";
					//system("echo 'a[{$$nnName},({$$ppName})->({$newValue})]' >>/tmp/test.txt");
				}
			}
			else
			{
				if(strlen($$ppName) == 0)
					$$ppName="NoValue";
				if(strcasecmp($$ppName,$inputValue) != 0)
				{
					$isChangeFlag=1;
					$preValueInfo.="[{$$nnName},{$$ppName}->$inputValue]";
					//system("echo 'b[{$$nnName},{$$ppName}->$inputValue]'>>/tmp/test.txt");
				}
			}
		}else{
			//echo "<br>Can not find $inputName's value";
		}
	}
	if($isChangeFlag==1)
	{
		$preValueInfo.=" in {$log_sysTypeName}_{$log_sysNo}_{$log_subSysNo}";
		insertLog($preValueInfo);
	}
}

//处理各个的输入单元的值，组成各个需要编辑的字段值
if($_SERVER['REQUEST_METHOD'] == "POST"){

	//格式化每个需要保存的字段，以便数据处理模块处理
	$bgnNo=0;
	$endNo=0;
	for($i=$bgnNo;$i<sizeof($showAttrDef);$i++){
		if($oldFieldName == $showAttrDef[$i][fieldName]){
			continue;
		}
		$oldFieldName = $showAttrDef[$i][fieldName];
		${'checkBox_'.$endNo} =1;
		${'fieldName_'.$endNo}=$oldFieldName;
		${'editValue_'.$endNo}=${$oldFieldName};
		if($DEBUG) echo "<br>checkBox_$endNo	= ${'checkBox_'.$endNo}";
		if($DEBUG) echo "<br>fieldName_$endNo	= ${'fieldName_'.$endNo}";
		if($DEBUG) echo "<br>editValue_$endNo	= ${'editValue_'.$endNo}";
		$endNo++;
	}
}
//==========================================================
//数据处理
//==========================================================
if (1 == $isLoadIndex) {
	$fieldNameList		= "key_imsi,a3a8_version,status";
} else {
	$fieldNameList		= "ALL";
}
if ($DEBUG) echo "fieldNameList=$fieldNameList<br>";
?>
<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
require("../pubSubsMng/sendComm.inc");
require("../pubSubsMng/dataProc.inc");

//==========================================================
//输出并显示数据
//==========================================================
$seqNo=0;
?>

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

adjust_content_head_frame("down");	

echo "<br>";
//**********************************编辑参数时所用javascript*****************************************
?>
<script LANGUAGE="JavaScript">
function selCheckBox(thisCheckBox)
{
	if(thisCheckBox.checked == true){
		thisCheckBox.value=1;
	}else{
		thisCheckBox.value=0;
	}
}

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);
	}
}
</script>
<form method="POST" name="update" action="<?php echo"$PHP_SELF";?>" >
  <?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>$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
	$showkeyValue= substr($keyValue,1);
	$keyValueName='IMSI';
	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>

		<td>
		<?php
		if($canwrite_flag)
			echo "<input type=\"submit\" value=\"Set\" name=\"change\" title=\"\"  class=\"button\" onClick=\"javascript:document.update.isChange.value=1;\">";
		else
			echo "<input type=\"submit\" disabled 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>
		<?php
		if($canwrite_flag)
			echo "<input type=\"button\" value=\"Delete\" name=\"delete\" title=\"Delete this subscriber\"  class=\"button\" onClick=\"askSureToDel('$keyValueName','$keyValue');\">";
		else
			echo "<input type=\"button\" disabled value=\"Delete\" name=\"delete\" title=\"Delete this subscriber\"  class=\"button\" onClick=\"askSureToDel('$keyValueName','$keyValue');\">";
		?>
		<input type="hidden" value="0" name="isDelete">
		</td>
	<?php
	}
	?>
  <td width="100%">&nbsp;</td>
  </tr>  
</table>
<br>
<?php
//将已取出的用户数据显示出来
//$tmpRow[fieldName]

?>
<table border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">    
<tr bgcolor="#E6E6E6"> 
<td width="5%"> No.</td>   
<td width="25%">Name</td>   
<td width="20%">Value</td>     
<td width="50%">Description</td>    
</tr>
<?php
if($isTryCreate > 0){
	//key_imsi
	$key_imsi=$tmpRow[key_imsi];
	echo "\n <tr>";
	echo "<td> $seqNo </td>";
	$seqNo++;
	echo "<td>IMSI</td>";
	echo "<td>";
	echo "<input type=\"text\" name=\"key_imsi\" maxlength=\"16\" size=\"20\" value=\"$key_imsi\" class=\"text\">";
	echo "</td>";
	echo "<td>IMSI=MCC+MNC+MSIN</td>";
	echo "</tr>";
}else{
	echo "<input type=\"hidden\" name=\"key_imsi\" value=\"$keyValue\">";
}

echo "<input type=\"hidden\" name=\"log_sysTypeName\" value=\"$sysTypeName\">";
echo "<input type=\"hidden\" name=\"log_sysNo\" value=\"$sysNo\">";
echo "<input type=\"hidden\" name=\"log_subSysNo\" value=\"$subSysNo\">";
echo "<input type=\"hidden\" name=\"pre_keyvalue\" value=\"{$keyValue}'s\">";


//a3a8_version
$a3a8_version =$tmpRow[a3a8_version];//hex有大小写的区别，故转成dec
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>A3A8 version</td>";
echo "<td>";
echo "<select  name=\"a3a8_version\" size=\"1\">";
for($i=0;$i<256;$i++){
	$showValue=$i;
	$realValue=strtoupper(dechex($i));
	$realValue=(strlen($realValue) == 1)?'0'.$realValue:$realValue;
	if($realValue == $a3a8_version){
		echo "<option value=\"$realValue\" selected>$showValue</option>";
	}else{
		echo "<option value=\"$realValue\">$showValue</option>";
	}
}
echo "</select>";
echo "</td>";
echo "<input type=\"hidden\" name=\"a3a8_version_name\" value=\"A3A8 version\">";
echo "<input type=\"hidden\" name=\"a3a8_version_ppvalue\" value=\"$a3a8_version\">";
echo "<input type=\"hidden\" name=\"a3a8_version_ddef\" value=\"a3a8_version_def\">";
echo "<td>Version of A3/A8 algorithm being used: COMP128-V1, V2 and V3<br>1=version 1, 2=version 2, 3=version 3, 255=pseudo authentication, else (0, 4-254) reserved</td>";
echo "</tr>";

//status
$status=$tmpRow[status];
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Service status</td>";
echo "<td>";
if($status > 0){
	echo "<input type=\"radio\" name=\"status\" value=\"00\"> Inactive &nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<input type=\"radio\" name=\"status\" value=\"01\" checked> Active";
}else{
	echo "<input type=\"radio\" name=\"status\" value=\"00\" checked>Inactive &nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<input type=\"radio\" name=\"status\" value=\"01\">Active";
}
echo "</td>";
echo "<input type=\"hidden\" name=\"status_name\" value=\"Service status\">";
echo "<input type=\"hidden\" name=\"status_ppvalue\" value=\"$status\">";
echo "<input type=\"hidden\" name=\"status_ddef\" value=\"status_def\">";
echo "<td>Inactive=Waiting for authentication. Active=Authentication is successful</td>";
echo "</tr>";
?>
</table> 
<?php
//*****************************************隐藏输入框,用于传递值****************************************
echo "\n<input type=\"hidden\" name=\"dataTypeNo\"		value=\"$dataTypeNo\">";
echo "\n<input type=\"hidden\" name=\"keyFieldName\"	value=\"$indexFieldName\">";
echo "\n<input type=\"hidden\" name=\"keyValue\"		value=\"$keyValue\">";
?>

</form>
&nbsp;
<?php
adjust_content_tail("down");
?>
</body>
</html>
<?php
flush();
?>