<?php
@session_start();
	if($_SESSION['id'] != ''){
		
	}else{
		die;
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=11" >

<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/style.css">

</head>

<body style="background-color: rgb(204, 238, 150);">
	<div class="container">
		<div class="row inline">
			<div class="navstyle">
				

				
            </div>
        </div>
        <div class="row inline" style="float: left; width: 100%; background-color: white; min-height: 500px;">
        <?php
			$sql = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.intval($_SESSION['id']).'"';
			$D = $DB->dbrow($sql);
        	$DB->data = array();
			$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.strtoupper($D['userCode']).'" ORDER BY balanceID DESC limit 0,1';
			$B = $DB->dbRow($sqlB);
			
		?>
            <h4 style="text-align: center;">Ajent Balance : <?php echo $B['balance']?> </h4>
			<div class="menus">
                <ul>
                   <?php echo innerAdminMenu();?>
                </ul>
		    </div>

		

		<div class="row">
			<div id="ajent_table02" style="width: 939*px;text-align: center !important;">
            	<div class="a-form" style="width: 30%; float: left; margin-top: 20px;">
				
                    <form id="frmBalance" name="frmBalance" method="GET" action="<?php echo SITEURL?>/lottery-agent/agent-balance">
                      <div class="container">
                      
                      <table>


                          <label for="fname">User ID:</label>
                          <select style="width:160px;" id="userCodeBal" name="userCodeBal">
                    	<option value="">-Select User-</option>
                        <?php
                        $DB->data = array();
                        	$sqlU = 'SELECT userCode,id FROM '.$DB->pre.'users WHERE status="1" AND isActive="1" AND underHead="'.$_SESSION['id'].'"';
							$U = $DB->dbRows($sqlU);
							foreach($U as $U){
								?>
                                <option value="<?php echo $U['userCode']?>"><?php echo $U['userCode']?></option>
                                
                                <?php
								$sql = 'SELECT userCode FROM '.$DB->pre.'users WHERE status="1" AND isActive="1" AND underHead="'.$U['id'].'"';
								$DB->data = array();
								$D = $DB->dbRows($sql);
								if(count($D) > 0){
									foreach($D as $D){
										?>
                                         <option value="<?php echo $D['userCode']?>"><?php echo $D['userCode']?></option>
                                        <?php
									}
								}
							}
						?>
                    </select><br><br>
                          <label for="lname">Balance:</label>
                          <input id="tranBal" name="tranBal" type="text" style="width:150px;" ><br><br>
                          
                          <input type="submit" name="btnBalance" id="btnBalance" style="background-color: greenyellow;" value="+ Add Balance">
                          <input type="submit" name="btnLessBal" id="btnLessBal" style="background-color: rgb(248, 62, 62);" value="- Reduce Balance">
                        </form>

                      </table>
                        </div>
                    </form>
                    <?php
				  $msg = '';
                  	if(isset($_REQUEST['btnBalance'])){
						$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userCode="'.strtolower($_REQUEST['userCodeBal']).'" AND status="1" AND isActive="1"';
						$DB->data = array();
						$data = $DB->dbRow($sql);
						
						$sqlID = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.$_SESSION['id'].'"';
						$DB->data = array();
						$ID = $DB->dbRow($sqlID);
						
						$agentBal = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.$ID['userCode'].'" ORDER BY balanceID DESC limit 0,1';
						$DB->data = array();
						$aBal = $DB->dbRow($agentBal);

						if(count($data) > 0 && $_REQUEST['tranBal'] <= $aBal['balance']){
							date_default_timezone_set('Asia/Kolkata');
	                        $DB->data = array();
							$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'].'")';
							$DB->data = array();
							$B = $DB->dbRow($sqlB);
							$bal = (intval($B['balance'])+$_REQUEST['tranBal']);
							$DB->data = array();
							$DB->table = $DB->pre."balance";
							$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'balance'=>$bal);
							$str = array('str'=>'ERR');
							if($DB->dbInsert()){
								$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Balance Added Successfully...</p>';
							}
							
							$bal1 = (intval($aBal['balance'])-$_REQUEST['tranBal']);
							$DB->data = array();
							$DB->table = $DB->pre."balance";
							$DB->data  = array('userCode'=>$ID['userCode'],'balance'=>$bal1);
							$DB->dbInsert();
							
							
						}else{
							$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Invalid User Or Insufficient Balance</p>';
						}
					
					}
					if(isset($_REQUEST['btnLessBal'])){
						$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userCode="'.strtolower($_REQUEST['userCodeBal']).'" AND status="1" AND isActive="1"';
						$DB->data = array();
						$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'].'")';
							$DB->data = array();
							$B = $DB->dbRow($sqlB);
							$bal = (intval($B['balance'])-$_REQUEST['tranBal']);
							$DB->data = array();
							$DB->table = $DB->pre."balance";
							$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'balance'=>$bal);
							$str = array('str'=>'ERR');
							if($DB->dbInsert()){
								$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Balance Less Successfully...</p>';
							}else{
								$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Invalid User</p>';
							}
						}
					}
					echo $msg;
				  ?>
                </div>

                <div id="ajent_table02" style="width: 70%; float: right; ">
                    <table border="1px">
                      <tr>
                        <th>Sr.No. </th>
                        <th>User ID </th>
                        <th>Balance </th>
                        
                      </tr>
                      <?php
                      $DB->data = array();
                      	$sql = 'SELECT * FROM '.$DB->pre.'users WHERE status="1"  AND underHead="'.$_SESSION['id'].'"';
						$D = $DB->dbRows($sql);
						$DB->data = array();
						$sqlN = 'SELECT userCode,id FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['id'].'"';
						$N = $DB->dbRow($sqlN);
						foreach($D as $k=>$D){
						    $DB->data = array();
					  		$sqlC = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.$D['createdBy'].'"';
							$C = $DB->dbRow($sqlC);
					  ?>
                      <tr>
                        <td><?php echo ($k+1)?></td>
                        <td><a href="<?php echo SITEURL?>/lottery-agent/create-agent/?userID=<?php echo $D['id']?>&u=<?php echo $_REQUEST['u']?>&flag=<?php echo $_REQUEST['flag']?>&id=<?php echo $_REQUEST['id']?>"><?php echo $D['userCode']?></a></td>
                        <td><?php 
							$sqlB1 = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.strtoupper($D['userCode']).'" ORDER BY balanceID DESC limit 0,1';
							$B1 = $DB->dbRow($sqlB1);
							echo $B1['balance']
						?></td>                    
                        
                        
                        
                      </tr>
                      
                                <?php

					  echo getAgentUser($D['id']);
					  } ?>
                      
                      
                      
                      <?php
					  $str = '';
                      	function getAgentUser($ID){
							global $DB;
							$sqlU = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.$ID.'"';
							$U = $DB->dbRow($sqlU);
							$sql = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$ID.'"';
							$D = $DB->dbRows($sql);
							foreach($D as $D){
									
								
								if($D['id'] !=''){

							$sqlB1 = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.strtoupper($D['userCode']).'" ORDER BY balanceID DESC limit 0,1';
							$B1 = $DB->dbRow($sqlB1);
						
								$str .= '
									<tr>
										<td>1</td>
										<td><a href="'.SITEURL.'/lottery-agent/create-agent/?userID='.$D['id'].'&u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">'.$D['userCode'].'</a></td>
										<td>'.$B1['balance'].'</td>                    
										
									  </tr>
								
								';
									getAgentUser($D['id']);
								}
							}
							return $str;
						}
					  
					  ?>
                    </table>
    
                </div>


			</div>
				
			
		</div>
        </div>

        
	</div>

</body>
</html>
