<?php 

@session_start();


	if($_SESSION['SECEXP'] !=''){

	}else{
		header('Location:'.SITEURL);
	}
	$str = '';
	$msg = '';
if(isset($_REQUEST['btnEdit'])){
						@session_start();
						$DB->data = array();
						
					/*	$DB->table = $DB->pre."users";
						$DB->data  = array('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']
						,'createdBy'=>$_REQUEST['id']
						,'dateAdded'=>date('Y-m-d h:i:s')
						);*/
						 $sqlUT = 'UPDATE '.$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="'.$_SESSION['id'].'",dateAdded="'.date('Y-m-d H:i:s').'" WHERE id="'.intval($_REQUEST['hidePKID']).'"';
						
						$pkID = intval($_REQUEST['hidePKID']);
						if($DB->dbQuery($sqlUT)){
								$adminID = $_SESSION['id'];
								$DB->data = array();
								$DB->dbQuery('UPDATE '.$DB->pre.'users SET adminID="'.$adminID.'" WHERE id="'.$pkID.'"');
								 $msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User updated Successfully</p>';
                          
								
																							
						}
}

?>

<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" -->
<!--[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">
<style>
td a{
	font-weight:bold;
	
}
</style>
<script type="text/javascript">

function selectDisabled(){
	var val = document.getElementById('userType').value;
	if(val == 1){
		document.getElementById('underHead').disabled = true;
	}else{
		document.getElementById('underHead').disabled = false;
	}
	
}
</script>

</head>

<body>
	<div class="container" style="background:#fff;" >
		<div class="menus">
      <ul style="">
          <?php echo innerAdminMenu()?>
      </ul>
    </div>


		<div class="row" >
			<div class="a-form" style="width: 450px; float: left; background:#fff;" >
				<?php
				//echo $_REQUEST['tran'].'dddd';
                	$userID = '';
					$name = '';
					$pass = '';
					$mobNo = '';
					$userType = '';
					$underHead = '';
					$winRate = '';
					$commi = '';
					$bonus = '';
					$limit = '';
					$remark = '';
					$act = '';
					

					 $_REQUEST['userID'] = ($_REQUEST['userID']==''?$_REQUEST['hidePKID']:$_REQUEST['userID']);
					if(intval($_REQUEST['userID'])){
					    $DB->data = array();
							$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'];
					}
					
				?>
				<form action="<?php echo SITEURL?>/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 $_SESSION['id'];?>">
                    <input id="hidePKID" name="hidePKID" type="hidden" value="<?php echo intval($_REQUEST['userID'])?>">
                    
                    <input id="u" name="u" type="hidden" value="<?php echo $_REQUEST['u']?>">
                    <input id="id" name="id" value="<?php echo $_SESSION['id']?>" type="hidden">




                    <input id="flag" name="flag" value="1" 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?>" maxlength="10" required></td>
                  </tr>
                  
                  <tr>
                  	<td><b>User Type</b></td>
                    <td>
                    <label for="UserType">:</label>
                        <select name="userType" id="userType" onChange="selectDisabled()">
                        	<?php
                            	if(intval($userType)){
									
									if($userType == '2'){
										echo "<option value='2'>Agent</option>";
									}	
									if($userType == '3'){
										echo "<option value='3'>Retailer</option>";
									}
								}
							?>
                        	<option value="">-Select Type-</option>
                            
                          	<option value="2">Agent</option>
                          	<option value="3">Retailer</option>
                        </select> 
                    </td>
                  </tr>
                  
                                    
                  <tr>
                  	<td><b>Under Head</b></td>
                    <td>
                    <label for="UserType">:</label>
                        <select name="underHead" id="underHead">
                        	<option value="">-Select UnderHead -</option>
                          	<?php echo getTableDD($DB->pre."users","id","userCode",$underHead," status='1' AND isActive='1' AND (userType='1' OR userType='2')");?>
                        </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>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>Winning %</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 id="isActive" name="isActive" value="1" type="checkbox" <?php echo ($act=='1'?'checked="checked"':'');?>>
  								<span class="checkmark"></span> is Active
							</label>
                     	</td>
                  </tr>
                  
                  <tr>
                  <td>					
                    <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="button" onClick="location.href='<?php echo SITEURL?>/agent/?u=<?php echo $_REQUEST['u']?>&flag=1&id=<?php echo $_REQUEST['id']?>'" name="Clear" style="background-color:#FF0">Clear </button>
                  </td>
                  <td>
					<button class="btn" type="submit" name="Delete" style="background-color:#F00">Delete </button>
                    <button class="btn" type="submit" name="DeActive" style="background-color:#F00">De-Active </button>
                  </td>
                  
                  <td>					
                    <button class="btn" type="submit" id="active" name="active" style="background-color:#03C; color:white;">Active </button>
                  </td>
                   
                  </tr>
                  
                  
                  
                  </table>
				    </div>
				</form>
                
                <?php
                	//$msg = '';
					if(isset($_REQUEST['btnAgent'])){
						@session_start();
						$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 = '';
								$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">UserID all ready exits...</p>';
						}else{
						
						
							//$DB = new mxDb('localhost','esssifhw_lottery','esssifhw_lottery','nitin@123');
							date_default_timezone_set('Asia/Kolkata');
							$DB->data = array();
							$sqlU = 'SELECT winrate,bonus,commission,`limit` FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['underHead'].'"';
							$U = $DB->dbRow($sqlU);
							$adminID = $_SESSION['id'];
							$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Please put less values than Underhead</p>';
							if($_REQUEST['userType'] == '1' || $_REQUEST['underHead'] == '1'){
							    $DB->data = array();
							//	$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="1",winrate="'.$_REQUEST['winrate'].'",commission="'.$_REQUEST['commission'].'",bonus="'.$_REQUEST['bonus'].'",`limit`="'.$_REQUEST['limit'].'",remark="'.$_REQUEST['remark'].'",createdBy="'.$_REQUEST['hideUserID'].'",adminID="'.$adminID.'",dateAdded="'.date('Y-m-d H:i:s').'",isActive="'.intval($_REQUEST['isActive']).'"';
									
									if($DB->dbQuery($sql)){
										$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Added Successfully...</p>';
									}
							}else{
								if(($_REQUEST['bonus'] <= $U['bonus']) && ($_REQUEST['limit'] <= $U['limit']) && $_REQUEST['commission'] <= $U['commission']){
								
									$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');	
								$DB->data = array();
								//	$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="'.$_REQUEST['hideUserID'].'",adminID="'.$adminID.'",dateAdded="'.date('Y-m-d H:i:s').'",isActive="'.intval($_REQUEST['isActive']).'"';
									
									if($DB->dbQuery($sql)){
										$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">Added Successfully...</p>';
									}
								}
							}
						}
					}
					
					
					
					if(isset($_REQUEST['active'])){
					    $DB->data = array();
						$sqlE = 'UPDATE '.$DB->pre.'users SET isActive="1" WHERE id="'.$_REQUEST['hidePKID'].'"';
						
						if($DB->dbQuery($sqlE)){
							
							$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User activated successfully...</p>';
						}
					}
					if(isset($_REQUEST['DeActive'])){
						$DB->data = array();
						$sqlS = 'SELECT id FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['hidePKID'].'"';
						$S = $DB->dbRows($sqlS);
						if(count($S)>0){
							foreach($S as $S){
							    $DB->data = array();
								$sqlE = 'UPDATE '.$DB->pre.'users SET isActive="0" WHERE id="'.$S['id'].'"';
								$DB->dbQuery($sqlE);
								$DB->data = array();
								$sqlS1 = 'SELECT id FROM '.$DB->pre.'users WHERE underHead="'.$S['id'].'"';
								$S1 = $DB->dbRows($sqlS1);
								
								if(count($S1) > 0){
									foreach($S1 as $S1){
									    $DB->data = array();
										$sqlE1 = 'UPDATE '.$DB->pre.'users SET isActive="0" WHERE id="'.$S1['id'].'"';
										$DB->dbQuery($sqlE1);
									}
								}
							}
							$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User Deactivated successfully...</p>';
						}	
						$DB->data = array();
							$sqlE2 = 'UPDATE '.$DB->pre.'users SET isActive="0" WHERE id="'.$_REQUEST['hidePKID'].'"';
							$DB->dbQuery($sqlE2);					
							if($DB->dbQuery($sqlE2)){
								$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User Deactivated successfully...</p>';
							}
						
					}
					if(isset($_REQUEST['Delete'])){
						$DB->data = array();
						$sqlDel = 'SELECT id FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['hidePKID'].'"';
						$DE = $DB->dbRows($sqlDel);
						if(count($DE) > 0){
							foreach($DE as $DE){
							    $DB->data = array();
								$sqlD = 'DELETE FROM '.$DB->pre.'users  WHERE id="'.$DE['id'].'"';
								$DB->dbQuery($sqlD);
								$DB->data = array();
								$sqlDel1 = 'SELECT id FROM '.$DB->pre.'users WHERE underHead="'.$DE['id'].'"';
								$DE1 = $DB->dbRows($sqlDel1);
								if(count($DE1) > 0){
									foreach($DE1 as $DE1){
									    $DB->data = array();
										$sqlD1 = 'DELETE FROM '.$DB->pre.'users  WHERE id="'.$DE1['id'].'"';
										$DB->dbQuery($sqlD1);
									}
								}
							}
							$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User Deleted successfully...</p>';
							
						}
						
						
						
						$DB->data = array();
						$sqlE = 'DELETE FROM '.$DB->pre.'users  WHERE id="'.$_REQUEST['hidePKID'].'"';
						
						if($DB->dbQuery($sqlE)){
							$msg = '<p style="background-color: #d92424;height: 20px;padding: 10px;color: #fff;font-weight: bold;">User Deleted successfully...</p>';
						}
					}
					
					
				?>
                <p><?php
                	echo $msg;
				?></p>
			</div>
			<div id="table02" style="width: 871px; float: right; background:#fff;">
				<table border="1" cellspacing="0" cellpadding="5">
				  <tr>

				    <th>User ID </th>
				    <th>Under Head</th>
				    <th>User Type </th>
                    
				    <th>Created </th>
				    <th>Win </th>
				    <th>C % </th>
				    <th>B % </th>
				    <th>Limit </th>
				  </tr>
                  <?php 
                  $DB->data = array();
				   	$sql = 'select U.id,U.userCode,U.`name`,U.userType,U.winrate,U.commission,U.bonus,U.`limit`,U.isActive,U.underHead,U.dateAdded from mx_users as U
WHERE U.adminID="'.$_SESSION['id'].'"';
					$D = $DB->dbRows($sql);
					
					
					foreach($D as $k=>$D){
					    $DB->data = array();
						$sqlN = 'SELECT userCode,id FROM '.$DB->pre.'users WHERE id="'.$D['underHead'].'"';
						$N = $DB->dbRow($sqlN);
						$bgClr = '';
						if(!$D['isActive']){
							$bgClr = 'style="background-color:yellow"';
						}
						?>
                         <tr <?php echo $bgClr;?> >

                            <td><a href="<?php echo SITEURL?>/agent/?userID=<?php echo $D['id']?>"><?php echo $D['userCode']?></a></td>
                           	<td><a href="<?php echo SITEURL?>/agent/?userID=<?php echo $N['id']?>"><?php echo $N['userCode'];?></a></td>

                            </td>                    
                            <td><?php 
									if($D['userType'] == '1'){
										echo "Admin";
									}
									if($D['userType'] == '2'){
										echo "Agent";
									}
									if($D['userType'] == '3'){
										echo "Retailer";
									}
							
							?></td>                    
                            <td><?php
                            	
								echo date('d-m-Y h:i:s',strtotime($D['dateAdded']));
							
							?> </td>                    
                            <td><?php echo $D['winrate']?></td>                    
                            <td><?php echo $D['commission']?></td>                    
                            <td><?php echo $D['bonus']?></td>                    
                            <td><?php echo $D['limit']?></td>
                         </tr>
                        <?php } ?>
				  
				</table>
			
			</div>
		</div>

	</div>

</body>
</html>
