<?php
@session_start();
if($_SESSION['SECEXP'] !=''){

	}else{
		echo "Go And Fuck Your Self";die;
	}

?>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=11" >
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie7.css">
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie7.css">


</head>

<body>
	<div class="container">
		<div class="menus">
      <ul style="">
          <?php echo innerAdminMenu()?>
      </ul>
    </div>

		<div class="row">
			<div class="a-form" style="width: 450px; float: left;">
				
				<form id="frmBalance" name="frmBalance" method="GET" action="<?php echo SITEURL?>/balance" >
				  <div class="container" style="padding:7px; background-color: #CFF;">
                  <table>
                  <tr>
                  	<td>User ID</td>
                    <td>
                    <select style="width:281px;" id="userCodeBal" name="userCodeBal">
                    	<option value="">-Select User-</option>
                        <?php
                        	if($_REQUEST['id'] == 1){
                        		$sqlU = 'SELECT userCode FROM '.$DB->pre.'users WHERE status="1" AND isActive="1" AND status="1"';
							}else{
								$sqlU = 'SELECT userCode FROM '.$DB->pre.'users WHERE status="1" AND isActive="1" AND status="1" AND adminID="'.$_REQUEST['id'].'"';
							}
							$U = $DB->dbRows($sqlU);
							foreach($U as $U){
								?>
                                <option value="<?php echo $U['userCode']?>"><?php echo $U['userCode']?></option>
                                <?php
							}
						?>
                    </select>
                    
                  </tr>
                  <tr>
                  	<td>Add Balance</td>
                    <td><input id="tranBal" name="tranBal" type="text" style="width:21em;" ></td>
                  </tr>
                  <input id="u" name="u" value="<?php echo $_REQUEST['u']?>" type="hidden">
                  <input id="flag" name="flag" value="<?php echo $_REQUEST['flag']?>" type="hidden">
                  <input id="id" name="id" value="<?php echo $_REQUEST['id']?>" type="hidden">
                  <tr>
                  <td>					
                    </td>
                  <td>
					<button class="btn" type="submit" name="btnBalance" id="btnBalance" style="background-color:#0F0;">Add Balance </button>
                    <button class="btn" type="submit" name="btnChkBal" style="background-color:#0CF">Check Balance </button>
                    <button class="btn" type="submit" name="btnLessBal" style="background-color:#F00">less Balance </button>
                  </td>
                  
                   
                  </tr>
                  
                  
                  
                  </table>
                  
                  <?php
				  $msg = '';
                  	if(isset($_REQUEST['btnBalance'])){
						$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userCode="'.strtolower($_REQUEST['userCodeBal']).'" AND status="1" AND isActive="1"';
						$data = $DB->dbRow($sql);
						if(count($data) > 0){
							date_default_timezone_set('Asia/Kolkata');
	
							$DB->table = $DB->pre."wallet";	
							$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'tranBal'=>$_REQUEST['tranBal'],'dateAdded'=>date('Y-m-d H:i:s'));
							$DB->dbInsert();
							
							$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE userCode="'.$_REQUEST['userCodeBal'].'")';
							$B = $DB->dbRow($sqlB);
							$bal = (intval($B['balance'])+$_REQUEST['tranBal']);
							
							$DB->table = $DB->pre."balance";
							$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'balance'=>$bal);
							$str = array('str'=>'ERR');
							if($DB->dbInsert()){
								$msg = 'Balance Added Successfully...';
							}
							
						}else{
							$msg = 'Invalid User';
						}
					
					}
					if(isset($_REQUEST['btnLessBal'])){
						$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userCode="'.strtolower($_REQUEST['userCodeBal']).'" AND status="1" AND isActive="1"';
						$data = $DB->dbRow($sql);
						if(count($data) > 0){
							date_default_timezone_set('Asia/Kolkata');
							$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE userCode="'.$_REQUEST['userCodeBal'].'")';
							$B = $DB->dbRow($sqlB);
							$bal = (intval($B['balance'])-$_REQUEST['tranBal']);
							
							$DB->table = $DB->pre."balance";
							$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'balance'=>$bal);
							$str = array('str'=>'ERR');
							if($DB->dbInsert()){
								$msg = 'Balance Less Successfully...';
							}else{
								$msg = 'Invalid User';
							}
						}
					}
					$str = '';
					if(isset($_REQUEST['btnChkBal'])){
						$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE userCode="'.$_REQUEST['userCodeBal'].'")';
						$B = $DB->dbRow($sqlB);
						$str .= '<b>Current Balance :</b>';
						$str .= '</br><b>User ID :</b>'.$_REQUEST['userCodeBal'];
						$str .= '</br><b>Balance :</b>'.$B['balance'];
						
					}
				  ?>
                  <p><?php echo $str;?></p>
                  <p><?php echo $msg;?></p>
				    </div>
				</form>
			</div>
		</div>
        
        <div id="table02" style="width:861px; float: right; background-color: #FFC;">  
        	<table> 
            	<tr>
                	<th style="text-align:center;">Show Agent Balance</th>                	
                    <th style="text-align:center;">Show User Balance</th>
                </tr>
                <tr>
                	<td>
                    
                        <table border="1" cellspacing="2" cellpadding="7">
                            <tr>
                                <th>Name</th>
                                <th>Balance</th>
                            </tr>
                            <?php
                            	$sqlA = 'SELECT userCode FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['id'].'" AND userType="2" AND status="1" AND isActive="1"';
								$A = $DB->dbRows($sqlA);
								foreach($A as $A){
									?>
                                    <tr>
                                        <td><?php echo $A['userCode']?></td>
                                        <td>
                                        	<?php
                                            	$sqlAB = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.$A['userCode'].'" ORDER BY balanceID DESC limit 0,1';
												$AB = $DB->dbRow($sqlAB);
												echo $AB['balance'];
											?>
                                        </td>
                                    </tr>
                                    <?php
								}
							?>
                            
                            
                            
                        </table>
                    
                    
                    </td>
                    <td>
                    
                    	<table border="1" cellspacing="2" cellpadding="7">
                    	<tr>
                        	<th>Sr.No.</th>
                            <th>Balance</th>
                        </tr>
                        <?php
								$sqlAg = 'SELECT id FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['id'].'" AND userType="2" AND status="1" AND isActive="1"';
								$AG = $DB->dbRows($sqlAg);
								foreach($AG as $AG){
                            	$sqlA1 = 'SELECT userCode FROM '.$DB->pre.'users WHERE underHead="'.$AG['id'].'"  AND userType="3" AND status="1" AND isActive="1"';
								$A1 = $DB->dbRows($sqlA1);
								foreach($A1 as $A1){
									?>
                                    <tr>
                                        <td><?php echo $A1['userCode']?></td>
                                        <td>
                                        	<?php
                                            	$sqlAB1 = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.$A1['userCode'].'" ORDER BY balanceID DESC limit 0,1';
												$AB1 = $DB->dbRow($sqlAB1);
												echo $AB1['balance'];
											?>
                                        </td>
                                    </tr>
                                    <?php
								}
								}
							?>
                    </table>
                    
                    </td>
                    
                </tr>
                    
                    
                    
                	
                	
               </table>
        </div>
        </div>

	

</body>
</html>
 