<?php
/*********************************************************      
 程序说明：         
    功能说明：vms用户数据维护专用页面
		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;

$statusDef=array(
		//array('Name',		'offset','remark'),
		array('use_flag',	'00.2'	,'Register flag'),
		array('enable_flag','00.1'	,'Active flag'),
		array('locked_flag','00.0'	,'Locked flag')
);

//------------选择语言版本----------
if($language == 'eng'){

}else{

}

//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'=>'msisdn',			'inputName'=>'msisdn'),
	array('fieldName'=>'status',			'inputName'=>'status'),
	array('fieldName'=>'promo_language',	'inputName'=>'promo_language'),
	array('fieldName'=>'greeting',			'inputName'=>'greeting'),
	//array('fieldName'=>'pwdFlag',			'inputName'=>'pwdFlag'),
	array('fieldName'=>'pwdValue',			'inputName'=>'pwdValue')
);

//For operation log use
{
	$used_flag_def=array(
		"00"=>"Disable","0"=>"Disable",
		"01"=>"Enable","1"=>"Enable"
	);
	
	$promo_language_def=array(
		"00"=>"Language 0","0"=>"Language 0",
		"01"=>"Language 1","1"=>"Language 1",
		"02"=>"Language 2","2"=>"Language 2",
		"03"=>"Language 3","3"=>"Language 3",
		"04"=>"Language 4","4"=>"Language 4"
	);
	
	$greeting_def=array(
		"00"=>"System",
		"01"=>"Host name",
		"02"=>"Personal",
		"80"=>"Temporary"
	);
	
	

}

$logFieldDef=array(
	'status',
	'promo_language',
	'greeting',
	'pwdValue',
);


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)){
			//echo "<br>$inputName	= $inputValue";
			if(isset($$ddName))
			{
				$tmpv=$$ppName;
				$val1=${$$ddName}[$tmpv];
				$val2=${$$ddName}[$inputValue];
				if(strlen($$ppName) == 0)
					$val1="NoValue";
				
				if(strcasecmp($val1,$val2) != 0)
				{
					$isChangeFlag=1;
					$preValueInfo.="[{$$nnName},{$val1}->{$val2}]";
					//system("echo '[{$$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 '[{$$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 '[{$$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"){
	//$enableFlag=$enableFlag;
	//$promptLanguage=$promptLanguage;
	//$greetingFlag=$greetingFlag;

	$orgValue=$status;
	$orgValue = sprintf("%02x",$orgValue);  //需要理解
	//echo "<1>orgValue=$orgValue<br>";
	for($i=0;$i<sizeof($statusDef);$i++){
		$inputName	=$statusDef[$i][0];
		$inputValue	=${$inputName};
		if(strlen($inputValue) == 0){
			$inputValue=0;
		}
		$inputOffset=$statusDef[$i][1];
		$orgValue=setBitValue($orgValue,$inputOffset,$inputValue);
	}
	$status=$orgValue;
		
	//格式化每个需要保存的字段，以便数据处理模块处理
	$bgnNo=0;
	$endNo=0;
	for($i=$bgnNo;$i<sizeof($showAttrDef);$i++){
		if($oldFieldName == $showAttrDef[$i][fieldName]){
			continue;
		}
		$oldFieldName = $showAttrDef[$i][fieldName];
		$test=$oldFieldName;
		${'checkBox_'.$endNo} =1;
		${'fieldName_'.$endNo}=$oldFieldName;
		${'editValue_'.$endNo}=$_POST[$oldFieldName];

		if($DEBUG) echo "<br>checkBox1$endNo	= ${'checkBox_'.$endNo}";
		if($DEBUG) echo "<br>fieldName_$endNo	= ${'fieldName_'.$endNo}";
		if($DEBUG) echo "<br>editValue_$endNo	= ${'editValue_'.$endNo}";
		$endNo++;
	}
}

//==========================================================
//数据处理
//==========================================================
if (1 == $isLoadIndex) {
	$fieldNameList		= "msisdn,mailboxID,proo_language,greeting,pwdFlag";
} 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
	if(!isset($isTryCreate)||$isTryCreate <= 0){
		$showkeyValue= $keyValue;
		$keyValueName='MSISDN';
		echo "&nbsp;&nbsp;$keyValueName:$showkeyValue";
	}
	?>
	</nobr></td>
	<?php 
	if($isTryCreate > 0){
		if($isBatchCreate> 0){
		?>
			<TD><NOBR>
				&nbsp;&nbsp;&nbsp;
				Batch amount(<=100):<input type="text" name="batchNum" class="text"  maxlength="3" size="3">
			</TD>
		<?php 
		}
		?>
		<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">
		<input type="hidden" value="<?php echo "$isBatchCreate"; ?>" name="isBatchCreate">
		</td>
		<?php
			$myPrivilege = $userInfo[privilege];
			if($myPrivilege >=4){
		?>
		<td>
		<input type="submit" value="Create in OMC" name="create" title=""  class="button" onClick="javascript:document.update.isCreate.value=2;">
		</td>
		<?php
			}
		?>
	<?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
//msisdn
if($isTryCreate > 0){
	$msisdn=$tmpRow[msisdn];
	echo "\n <tr>";
	echo "<td> $seqNo </td>";
	$seqNo++;
	if($isBatchCreate > 0)
		echo "<td>Start MSISDN</td>";
	else
		echo "<td>MSISDN</td>";
	echo "<td>";
	echo "<input type=\"text\" name=\"msisdn\" maxlength=\"16\" size=\"20\" value=\"$msisdn\" class=\"text\">";
	echo "</td>";
	echo "<td>&nbsp;</td>";
	echo "</tr>";
}else{
	echo "<input type=\"hidden\" name=\"msisdn\" 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\">";


//status
$status =$tmpRow[status];
echo "\n <TR>";
echo "<TD> $seqNo </TD>";
$seqNo++;
echo "<TD>Status</TD>";
echo "<TD>";
$preValue="";
echo "<input type=\"hidden\" name=\"status\" value=\"$status\">";
for($i=0;$i<sizeof($statusDef);$i++){
	$value=getBitValue($status,$statusDef[$i][1]);
	if($value == '1'){
		echo "<input type=\"checkbox\" name=\"{$statusDef[$i][0]}\"	 onClick=selCheckBox(this) value=\"1\" checked  >{$statusDef[$i][2]}<br>";
		$preValue.="{$statusDef[$i][2]},";
	}else{
		echo "<input type=\"checkbox\" name=\"{$statusDef[$i][0]}\"	 onClick=selCheckBox(this) value=\"0\" >{$statusDef[$i][2]}<br>";
	}
}
$preValue=substr($preValue,0,strlen($preValue)-1);
echo "</TD>";
echo "<input type=\"hidden\" name=\"status_name\" value=\"Status\">";
echo "<input type=\"hidden\" name=\"status_ppvalue\" value=\"$preValue\">";
echo "<input type=\"hidden\" name=\"status_cdef\" value=\"statusDef\">";
echo "<TD>-</TD>";
echo "</TR>";

//mailboxNo 
$mailboxID =hexdec($tmpRow[mailboxID]);
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Mailbox no. </td>";
echo "<td>&nbsp;$mailboxID</td>";
echo "<td>Mailbox number (0-9999)</td>";
echo "</tr>";

//promptLanguage  
$languageDefArr=array('Language 0','Language 1','Language 2','Language 3',
						'Language 4');
$promo_language  =$tmpRow[promo_language];
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Language </td>";
echo "<td>";
echo "<select  name=\"promo_language\" size=\"1\">";
for($i=0;$i<sizeof($languageDefArr);$i++){
	$showValue=$languageDefArr[$i];
	$realValue=$i;
	if($realValue == $promo_language){
		echo "<option value=\"$realValue\" selected>$showValue</option>";
	}else{
		echo "<option value=\"$realValue\">$showValue</option>";
	}
}
echo "</select>";
echo "</td>";
echo "<input type=\"hidden\" name=\"promo_language_name\" value=\"Language\">";
echo "<input type=\"hidden\" name=\"promo_language_ppvalue\" value=\"$promo_language\">";
echo "<input type=\"hidden\" name=\"promo_language_ddef\" value=\"promo_language_def\">";
echo "<td>Select a language from five active language for greetings</td>";
echo "</tr>";

//greetingFlag 
$greetingDefArr=array(
					array('00','System'),
					array('01','Host name'),
					array('02','Personal'),
					array('80','Temporary')
				);
$greeting =$tmpRow[greeting];
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Greeting type</td>";
echo "<td>";
echo "<select  name=\"greeting\" size=\"1\">";
for($i=0;$i<sizeof($greetingDefArr);$i++){
	$realValue	=$greetingDefArr[$i][0];
	$showName	=$greetingDefArr[$i][1];
	if($realValue == $greeting){
		echo "<option value=\"$realValue\" selected>$showName</option>";
	}else{
		echo "<option value=\"$realValue\">$showName</option>";
	}
}
echo "</select>";
echo "</td>";
echo "<input type=\"hidden\" name=\"greeting_name\" value=\"Greeting type\">";
echo "<input type=\"hidden\" name=\"greeting_ppvalue\" value=\"$greeting\">";
echo "<input type=\"hidden\" name=\"greeting_ddef\" value=\"greeting_def\">";
echo "<td>Select a kind of greeting to play for dial-in call, selective greetings include system, host name and personal greetings</td>";
/*
//pwdEnable 
$pwdFlag=$tmpRow[pwdFlag];
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Password Enable</td>";
echo "<td><nobr>";
if($pwdFlag > 0){
	echo "<input type=\"radio\" name=\"pwdFlag\" value=\"01\" checked> Enable &nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<input type=\"radio\" name=\"pwdFlag\" value=\"00\"> Disable";
}else{
	echo "<input type=\"radio\" name=\"pwdFlag\" value=\"01\"> Enable &nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<input type=\"radio\" name=\"pwdFlag\" value=\"00\" checked> Disable";
}
echo "</nobr></td>";
echo "<td>VMS service password flag</td>";
echo "</tr>";
*/
//pwdValue 
$pwdValue=$tmpRow[pwdValue];
echo "\n <tr>";
echo "<td> $seqNo </td>";
$seqNo++;
echo "<td>Password</td>";
echo "<td>";
echo "<input type=\"text\" name=\"pwdValue\" maxlength=\"4\" size=\"4\" value=\"$pwdValue\"  class=\"text\">";
echo "</td>";
echo "<input type=\"hidden\" name=\"pwdValue_name\" value=\"Password\">";
echo "<input type=\"hidden\" name=\"pwdValue_ppvalue\" value=\"$pwdValue\">";
echo "<td>4-digit password</td>";
echo "</tr>";

/*
echo "</tr>";
echo "</table> ";
if($isTryCreate < 1){
	//message info list
	$newMessageNum  =$tmpRow[newMessageNum];
	$oldMessageNum  =$tmpRow[oldMessageNum];
	echo "<br>New Message Number:$newMessageNum &nbsp; &nbsp;  Old Message Number:$oldMessageNum ";
	echo "<table border=\"1\" width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"  bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\" bgcolor=\"#FFFFFF\"> ";         
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td>Message no.</td>";
	echo "<td>Caller number</td>";
	echo "<td>New</td>";   
	echo "<td>Time</td>";   
	echo "</tr>";         
	for($i=1;$i<=10;$i++){
		$msgInfo=$tmpRow['messageInfo_'.$i];
		$msgFlag		=subStr($msgInfo,0,2);
		if($msgFlag == 'FF') continue;
		if($msgFlag == '01'){
			$isNew='No';
		}elseif($msgFlag == '02'){
			$isNew='Yes';
		}else{
			continue;
		}
		$msgNo			=$i;
		$timeStr		=subStr($msgInfo,2,8);
		$timeStr		=hexdec($timeStr);
		$timeStr		=date("Y-m-d H:i:s",$timeStr);
		$callerNumber	=subStr($msgInfo,10);
		$callerNumber	=str_replace('F','',$callerNumber);
		echo "<tr>";
		echo "<td>&nbsp;$msgNo</td>";
		echo "<td>&nbsp;$callerNumber</td>"; 
		echo "<td>&nbsp;$isNew</td>";
		echo "<td>&nbsp;$timeStr</td>";
		echo "</tr>";
	}
	echo "</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();
?>
