<?php

@session_start();
	if($_SESSION['id'] != ''){
		
	}else{
		die;
	}
if(isset($_REQUEST['btnEdit'])){
						
						//echo $_REQUEST['userID'];
		$DB->data = array();
		$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->data = array();
			$DB->table = $DB->pre."users";
			$DB->data  = $_REQUEST;
			$pkID = intval($_REQUEST['hideUID']);


            $DB->data = array();
			$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;
}
if(isset($_REQUEST['btnDelete'])){
	 $sql = 'DELETE FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['hideUserID'].'"';
	
	if($DB->dbQuery($sql)){
		echo   '<p style="width:91%;background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User Deleted Successfully</p>';
	}
	
}


?>
<!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);
        	
			$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'])){
						
						$DB->data = array();
						$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');
							$DB->data = array();
							$sqlW = 'SELECT winrate,bonus,`limit`,commission FROM '.$DB->pre.'users WHERE id="'.$_SESSION['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;
							$DB->data = array();
							$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($_SESSION['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;">Regailer 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['userID'];?>">
                    <?php
                    	$sqlAD = 'SELECT adminID FROM '.$DB->pre.'users WHERE id="'.$_SESSION['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="id" name="id" type="hidden" value="<?php echo $_REQUEST['userID'];?>">
                    
                    </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 == '2'){
										echo "<option value='2'>Agent</option>";
									}	
									if($userType == '3'){
										echo "<option value='3'>Retailer</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>";
									}
									
								}
							?>
                    	<option value="">-Select Rate-</option>
                        <option value="90">90-90</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 $_SESSION['id']?>"><?php echo $_SESSION['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="btnDelete" id="btnDelete" style="background-color:#F00">Delete </button>
                  </td>
                  
                  
                  
                   
                  </tr>
                  <tr>
                  	<td colspan="2"><?php  echo $msg;?></td>
                  </tr>
                  
                  </table>
				    </div>
				</form>
                
                </div>
                

                
			
		</div>
        </div>

        
	</div>

</body>
</html>
