<?php


if(isset($_REQUEST['btnEdit'])){
						
						//echo $_REQUEST['userID'];
		$sqlW = 'SELECT winrate,bonus,`limit`,commission FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['id'].'"';
		$W = $DB->dbRow($sqlW);
		$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Please put less values than Underhead</p>';
		if(($_REQUEST['winrate'] <= $W['winrate']) && ($_REQUEST['bonus'] <= $W['bonus']) && ($_REQUEST['limit'] <= $W['limit']) && $_REQUEST['commission'] <= $W['commission']){	
			$DB->table = $DB->pre."users";
			$DB->data  = $_REQUEST;
			$pkID = intval($_REQUEST['hideUID']);



			$sqlU = "UPDATE `mx_users` SET `userCode`='".$_REQUEST['userCode']."',`name`='".$_REQUEST['name']."',`password`='".$_REQUEST['password']."',`mobile`='".$_REQUEST['mobile']."',`userType`='".$_REQUEST['userType']."',`underHead`='".$_REQUEST['underHead']."',`winrate`='".$_REQUEST['winrate']."',`commission`='".$_REQUEST['commission']."',`bonus`='".$_REQUEST['bonus']."',`limit`='".$_REQUEST['limit']."',`remark`='".$_REQUEST['remark']."',`isActive`='".$_REQUEST['isActive']."' WHERE id='".$pkID."'";
			if($DB->dbQuery($sqlU)){
					$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User updated Successfully</p>';
			}
		}
		echo $msg;
}


?>
<!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($_REQUEST['id']).'"';
			$D = $DB->dbrow($sql);
        	
			$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>

		<?php
				
                	$userID = '';
					$name = '';
					$pass = '';
					$mobNo = '';
					$userType = '';
					$underHead = '';
					$winRate = '';
					$commi = '';
					$bonus = '';
					$limit = '';
					$remark = '';
					$act = '';

					//echo $_REQUEST['userID'];die;
					$_REQUEST['userID'] = ($_REQUEST['userID']==''?$_REQUEST['hideUID']:$_REQUEST['userID']);
					if(intval($_REQUEST['userID'])){
							$sqlU = 'SELECT * FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['userID'].'"';
							$U = $DB->dbRow($sqlU);
							$userID = $U['userCode'];
							$name = $U['name'];
							$pass = $U['password'];
							$mobNo = $U['mobile'];
							$userType = $U['userType'];
							$underHead = $U['underHead'];
							$winRate = $U['winrate'];
							$commi = $U['commission'];
							$bonus = $U['bonus'];
							$limit = $U['limit'];
							$remark = $U['remark'];
							$act = $U['isActive'];
					}
					
					
					
					
                
				$msg = '';
					if(isset($_REQUEST['btnAgent'])){
						
						
						$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userCode="'.strtolower($_REQUEST['userCode']).'"';
						$data = $DB->dbRow($sql);
						$str = array('str'=>'OK');
						if(count($data) > 0){
								$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">UserID all ready exits...</p>';
						}else{
						
							date_default_timezone_set('Asia/Kolkata');
							
							$sqlW = 'SELECT winrate,bonus,`limit`,commission FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['id'].'"';
							$W = $DB->dbRow($sqlW);
							$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Please put less values than Underhead</p>';
							if(($_REQUEST['winrate'] <= $W['winrate']) && ($_REQUEST['bonus'] <= $W['bonus']) && ($_REQUEST['limit'] <= $W['limit']) && $_REQUEST['commission'] <= $W['commission']){
						
							$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');	
						
							$DB->table = $DB->pre."users";
							$DB->data  = $_REQUEST;
							$sql = 'INSERT INTO '.$DB->pre.'users SET userCode="'.$_REQUEST['userCode'].'",name="'.$_REQUEST['name'].'",`password`="'.$_REQUEST['password'].'",mobile="'.$_REQUEST['mobile'].'",userType="'.$_REQUEST['userType'].'",underHead="'.$_REQUEST['underHead'].'",winrate="'.$_REQUEST['winrate'].'",commission="'.$_REQUEST['commission'].'",bonus="'.$_REQUEST['bonus'].'",`limit`="'.$_REQUEST['limit'].'",remark="'.$_REQUEST['remark'].'",createdBy="'.intval($_REQUEST['id']).'",dateAdded="'.date('Y-m-d H:i:s').'",isActive="'.$_REQUEST['isActive'].'",adminID="'.$_REQUEST['hideAID'].'"';
							
							if($DB->dbQuery($sql)){
								$msg = '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Agent Added Successfully...</p>';
							}
							}
						}
					}
				
				
				
					
				?>

		<div class="row">
			<div id="ajent_table02" style="width: 939*px;text-align: center !important;">
            	<div class="a-form" style="width: 0%; float: left;">
				
                    <form action="<?php echo SITEURL?>/lottery-agent/create-agent" id="frmAgent" name="frmAgent" method="GET"  autocomplete="off">
				  <div class="container">
                  <table>
                  <tr>
                  	<td>User ID</td>
                    <td><input id="userCode" name="userCode" type="text" value="<?php echo $userID?>" required>
                    <input id="hideUserID" name="hideUserID" type="hidden" value="<?php echo $_REQUEST['id'];?>">
                    <?php
                    	$sqlAD = 'SELECT adminID FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['id'].'"';
						$AD = $DB->dbRow($sqlAD);
					?>
                   <input id="hideAID" name="hideAID" type="hidden" value="<?php echo $AD['adminID'];?>">
                   <input id="hideUID" name="hideUID" type="hidden" value="<?php echo $_REQUEST['userID'];?>">
                    <input id="u" name="u" value="<?php echo $_REQUEST['u']?>" type="hidden">
                    <input id="flag" name="flag" value="1" type="hidden">
					<input id="u" name="u" value="<?php echo $_REQUEST['u']?>" type="hidden">
                    <input id="id" name="id" value="<?php echo $_REQUEST['id']?>" type="hidden">
                    </td>
                  </tr>
                  <tr>
                  	<td>Name</td>
                    <td><input id="name" name="name" type="text" value="<?php echo $name;?>" required></td>
                  </tr>
                  <tr>
                  	<td>Password</td>
                    <td><input id="password" name="password" type="text" value="<?php echo $pass?>" required></td>
                  </tr>
                  <tr>
                  	<td>Mobile No</td>
                    <td><input id="mobile" name="mobile" type="text" value="<?php echo $mobNo?>" required></td>
                  </tr>
                  
                  <tr>
                  	<td><b>User Type</b></td>
                    <td>
                    <label for="UserType">:</label>
                        <select name="userType" id="userType">
                        	<?php
                            	if(intval($userType)){
									if($userType == '1'){
										echo "<option value='1'>Admin</option>";
									}
									if($userType == '2'){
										echo "<option value='2'>Agent</option>";
									}	
									if($userType == '3'){
										echo "<option value='3'>Retailer</option>";
									}
								}
							?>
                        	
                          	<option value="2">Agent</option>
                          	<option value="3">Retailer</option>
                        </select> 
                    </td>
                  </tr>
                  
                                    
                  
                  
                  
                  
                  <tr>
                  	<td>Win Rate</td>
                    <td>
                    <select id="winrate" name="winrate" required>
                    		<?php
                            	if(intval($winRate)){
									if($winRate == '90'){
										echo "<option value='90'>90</option>";
									}
									if($winRate == '80'){
										echo "<option value='80'>80</option>";
									}	
									if($winRate == '70'){
										echo "<option value='70'>70</option>";
									}
								}
							?>
                    	<option value="">-Select Rate-</option>
                        <option value="90">90-90</option>
                        <option value="80">80-80</option>
                        <option value="70">70-70</option>
                    </select>
                    </td>
                  </tr>
                  <tr>
                  	<td><b>Under Head</b></td>
                    <td>
                    <label for="UserType">:</label>
                        <select name="underHead" id="underHead">
                        	<?php

                            	if($underHead !=''){
								?>
                                <?php echo getTableDD($DB->pre."users","id","userCode",$underHead," status='1' AND isActive='1'");?>
                                <?php
								}
							?>
                            <option value="">-Select UnderHead -</option>
                            
                            <option value="<?php echo $_REQUEST['id']?>"><?php echo $_REQUEST['u']?></option>
                          	
                        </select> 
                    </td>
                  </tr>
                  <tr>
                  	<td>Commition%</td>
                    <td><input id="commission" name="commission" type="text" value="<?php echo $commi?>" required></td>
                  </tr>
                  
                  <tr>

                  	<td>Bonus%</td>
                    <td><input id="bonus" name="bonus" type="text" value="<?php echo $bonus?>" required></td>
                  </tr>
                  
                  <tr>
                  	<td>Limit</td>
                    <td><input id="limit" name="limit" type="text" value="<?php echo $limit?>" required></td>
                  </tr>
                  
                  <tr>
                  	<td><b>Remark</b></td>
                    <td><input id="remark" name="remark" type="text" value="<?php echo $remark?>" required></td>
                  </tr>
                  
                  <tr>
                    	<td>
                    		<label class="container" id="idactive">
  							<input value="1" id="isActive" name="isActive" type="checkbox" <?php echo ($act=='1'?'checked="checked"':'');?>>
  								<span class="checkmark"></span> is Active
							</label>
                     	</td>
                  </tr>
                  
                  <tr>
                  <td colspan="2">					
                    <button class="btn" type="submit" id="btnAgent" name="btnAgent" style="background-color:#6F0">Save </button>
                    <button class="btn" type="submit" id="btnEdit" name="btnEdit" style="background-color:#6F0">Update </button>
					<button class="btn" type="reset" name="Clear" style="background-color:#FF0">Clear </button>
                    <button class="btn" type="submit" name="Delete" style="background-color:#F00">Delete </button>
                  </td>
                  
                  
                  
                   
                  </tr>
                  <tr>
                  	<td colspan="2"><?php  echo $msg;?></td>
                  </tr>
                  
                  </table>
				    </div>
				</form>
                
                </div>
                

                <div id="ajent_table02" style="width: 70%; float: right; ">
                    <table border="1px">
                      <tr>
                        <th>Sr.No. </th>
                        <th>User ID </th>
                        <th>Agent </th>
                        <th>Active </th>
                        <th>Created </th>
                        <th>Play </th>
                        <th>Win </th>
                        <th>Comm </th>
                      </tr>
                      <?php
                      	$sql = 'SELECT * FROM '.$DB->pre.'users WHERE status="1"  AND underHead="'.$_REQUEST['id'].'"';
						$D = $DB->dbRows($sql);
						$sqlN = 'SELECT userCode,id FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['id'].'"';
						$N = $DB->dbRow($sqlN);
						foreach($D as $k=>$D){
					  		$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 echo $_REQUEST['u']?></td>                    
                        <td><?php echo ($D['isActive']=='1'?'Active':'Deactive');?></td>                    
                        <td><?php echo $C['userCode']?> </td>                    
                        <td>
                        <?php
							$tot = 0;
							if($D['userType'] == 3){
								$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$D['id'].'" AND lotDate="'.date('Y-m-d').'" GROUP BY tranID';
								$L = $DB->dbRow($sqlL);
								
								$tot += getSaleCommon($L);
								
							}else{
								$GLOBALS['retailer'] = array();
								$allRet = getAgentRetailerCommon($D['id']);
								foreach($allRet as $RS){
									$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$RS.'" AND lotDate="'.date('Y-m-d').'" GROUP BY tranID';
									$L = $DB->dbRow($sqlL);
									$tot += getSaleCommon($RS);
								}
								

							}
							echo $tot;
						?>
                        
                        
                        </td>                    
                        <td>
                        <?php
                        	
							
							$win = 0;
							
							
							if($D['userType'] == 3){
								if($D['winrate'] == 90){
									$win = winClaimeAdminCommon($D['id'],$D['winrate']);
								}else if($D['winrate'] == 80){
									$win8 = winClaimeAdminCommon($D['id'],$D['winrate']);
								}else if($D['winrate'] == 80){
									$win = winClaimeAdminCommon($D['id'],$D['winrate']);
								}
							}else{
								$GLOBALS['retailer'] = array();
								$allRet = getAgentRetailerCommon($D['id']);
	
								$totS = 0;
								$sqlU = 'SELECT * FROM '.$DB->pre.'users WHERE id="'.$D['id'].'"';
								$U = $DB->dbRow($sqlU);
								foreach($allRet as $RS){
									$totS += winClaimeAdminCommon($RS,$U['winrate']);
								}
								if($D['winrate'] == 90){
									$win = $totS;
								}else if($D['winrate'] == 80){
									$win = $totS;
								}else if($D['winrate'] == 80){
									$win = $totS;
								}
	
							}
							echo $win;
						
						?>
                        
                        </td>                    
                        <td><?php echo ($tot/$D['commission'])?></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){
								
								$win = 0;
							
							
							if($D['userType'] == 3){
								if($D['winrate'] == 90){
									$win = winClaimeAdminCommon($D['id'],$D['winrate']);
								}else if($D['winrate'] == 80){
									$win8 = winClaimeAdminCommon($D['id'],$D['winrate']);
								}else if($D['winrate'] == 80){
									$win = winClaimeAdminCommon($D['id'],$D['winrate']);
								}
							}else{
								$GLOBALS['retailer'] = array();
								$allRet = getAgentRetailerCommon($D['id']);
	
								$totS = 0;
								$sqlU = 'SELECT * FROM '.$DB->pre.'users WHERE id="'.$D['id'].'"';
								$U = $DB->dbRow($sqlU);
								foreach($allRet as $RS){
									$totS += winClaimeAdminCommon($RS,$U['winrate']);
								}
								if($D['winrate'] == 90){
									$win = $totS;
								}else if($D['winrate'] == 80){
									$win = $totS;
								}else if($D['winrate'] == 80){
									$win = $totS;
								}
	
							}
								
								
								$sqlC = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.$D['createdBy'].'"';
								$C = $DB->dbRow($sqlC);
								if($D['id'] !=''){
									
									if($D['userType'] == 3){
										$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$D['id'].'" AND lotDate="'.date('Y-m-d').'" GROUP BY tranID';
										$L = $DB->dbRow($sqlL);
										
										$tot += getSaleCommon($L);
										
									}else{
										$GLOBALS['retailer'] = array();
										$allRet = getAgentRetailerCommon($D['id']);
										foreach($allRet as $RS){
											$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$RS.'" AND lotDate="'.date('Y-m-d').'" GROUP BY tranID';
											$L = $DB->dbRow($sqlL);
											$tot += getSaleCommon($RS);
										}
										
						
									}
									
									
									
									
								$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>'.$U['userCode'].'</td>                    
										<td>'.($D['isActive']=='1'?'Active':'Deactive').'</td>                    
										<td>'.$C['userCode'].'</td>                    
										<td>'.$tot.'</td>                    
										<td>'.$win.'</td>                    
										<td>'.($tot/$D['commission']).'</td>                   
									  </tr>
								
								';
									getAgentUser($D['id']);
								}
							}
							return $str;
						}
					  
					  ?>
                    </table>
    
                </div>


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

        
	</div>

</body>
</html>
