<?php
	include'session.php';
	include'connection.php';
	include'functions.php';
	include'time.php';
	
?>
<html>
<head>
	<title>Add User</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>
	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1{
			background:#f7f7f7;
			width:48%;
			height:auto;
			float:left;
			text-align:center;
			padding:2%;
		}
		.div1 table{
			width:94%;
			text-align:center;
			font-size:17px;
			border-collapse:collapse;
			margin:10px;
			padding:2px 10px;
		}
		.div1 hr{
			margin:5px 10px;
			padding:0 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
		}
		.div1 hr{width:94%;}
		.div1 input, .div1 select{
			width:94%;
			height:45px;
			margin:8px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:17px;
		}
		input[type=file]
		{
			border:none;
		}
		.div1 textarea{
			width:94%;
			height:100px;
			margin:10px 0;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:19px;
		}
		.divImg{
			background:white;
			width:94%;
			height:100px;
			margin:10 15px;
			padding:8px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:19px;
			text-align:left;
		}
		.divImg img{
			width:19%;
			height:80px;
			float:left;
		}
		.divImg input{
			width:70%;
			float:right;
			font-size:15px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			font-size:30px;
			transition:1s;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
		
		@media(max-width:768px)
		{
			.div1{
				width:100%;
				height:auto;
			}
			.nav,.container{
				height:auto;
			}
		}
	</style>
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
		<?php
			include'menu.php';
	
		?>
		<div class="content">
			<input type="text" style="display:none;" value="<?php echo $userid; ?>" id="user">
			<div class="head">
				<div class="title"><p>Add User :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <?php
			        if(isset($_POST['submit']))
					{
						$uName = $_POST['uName'];
						$mobile = $_POST['mobile'];
						$email = $_POST['email'];
						$state = $_POST['state'];
						$district = $_POST['district'];
						$pin = $_POST['pin'];
						$address = $_POST['address'];
						$sponsor = $_POST['sponsor'];
						$parent = $_POST['parent'];
						
						$newUsrid=$mobile;
						
						$checkSponsor=checkChild($userid,$sponsor);
						$checkChild=checkChild($userid,$parent);
						if($checkChild=="true" || $parent==$userid)
						{
						    $checkParent=checkPlace($parent);
						}
						
						$quGetDupId="SELECT * FROM MLM_Users WHERE Userid='$newUsrid'";
						$reGetDupId=mysqli_query($con,$quGetDupId);
						$DupId=mysqli_num_rows($reGetDupId);
								
						
						if(empty($uName))
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Please Insert User's Name !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($checkSponsor!="true" && $sponsor!=$userid)
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Sponsor ".$checkChild."</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($checkChild!="true" && $parent!=$userid)
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Parent ".$checkChild."</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($checkParent=="PNA")
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Placement Not Available !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif(empty($mobile))
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Please Insert User's Mobile Number !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($DupId>0)
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>This Userid Already Exist !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($state=="--Select State--")
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Select State Please !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						elseif($state=="--Select District--")
						{
							echo "<div class='msg-div'>
										<form method='post'>
											<h3>Select District Please !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
						}
						else
						{   
							$pwd=rand(100000,999999);
							$parentId=$userid;
							
							//Insert into User profile
							$query = "INSERT INTO MLM_Users(Date,Time,Userid,Password,Name,Mobile,Email,City,State,Address,Pin,Parent_Id,SponsorId)
							values('$date','$time','$newUsrid','$pwd','$uName','$mobile','$email','$district','$state','$address','$pin','$parent','$sponsor')";
							if(mysqli_query($con,$query))
							{   
								$insertTree="INSERT INTO MLM_Tree (Userid) VALUES ('$newUsrid')";
								if(mysqli_query($con,$insertTree))
								{
								    $side=$checkParent."Side";
								    $updateTree="UPDATE MLM_Tree SET $side='$newUsrid' WHERE Userid='$parent'";
								    if(mysqli_query($con,$updateTree))
								    {
								        //$countSide=$checkParent."Count";
    									
    									$tmp_Parent=$parent;
    									$tmp_Child=$newUsrid;
    									while(!empty($tmp_Parent))
    									{
    									    $quParentTree="SELECT * FROM MLM_Tree WHERE Userid='$tmp_Parent'";
    									    $reParentTree=mysqli_query($con,$quParentTree);
    									    while($dataParentTree=mysqli_fetch_array($reParentTree))
    									    {
    									        if($dataParentTree['RightSide']==$tmp_Child)
    									        {
    									            $side="Right";
    									        }
    									        elseif($dataParentTree['LeftSide']==$tmp_Child)
    									        {
    									            $side="Left";
    									        }
    									        $countSide=$side."Count";
    									        $sideCount=$dataParentTree[$countSide]+1;
    									    
    									        $updateTree="UPDATE MLM_Tree SET $countSide='$sideCount' WHERE Userid='$tmp_Parent'";
    									        mysqli_query($con,$updateTree);
    									    
    									        $quParents="SELECT * FROM MLM_Users WHERE Userid='$tmp_Parent'";
    									        $reParents=mysqli_query($con,$quParents);
    									        $dataParents=mysqli_fetch_array($reParents);
    									    
    									        $tmp_Child=$tmp_Parent;
    									        $tmp_Parent=$dataParents['Parent_Id'];
    									    }
    									}
    									
    									$updateComm="SELECT * FROM operator";
				                        $reUpdateComm=mysqli_query($con,$updateComm);
				                        while($dataUpdateComm=mysqli_fetch_array($reUpdateComm))
				                        {
					                        $OwnApiCode=$dataUpdateComm['OwnApiCode'];
					                        $Operator=$dataUpdateComm['Operator'];
					                        $RetComm=0;
					                        $Serviceid=$dataUpdateComm['Serviceid'];
									        
					                        $quUpdateComm="INSERT INTO CommSlab (Userid,OpName,OpCode,ServiceId,Comm) VALUES ('$newUsrid','$Operator','$OwnApiCode','$Serviceid','$RetComm')";
					                        mysqli_query($con,$quUpdateComm);
				                        }
		    								echo "<div class='msg-div'>
			    								<form method='post'>
				    								<h3>Registration Successfully !</h3><br>
					    							<h3>Userid : ".$newUsrid."</h3><br>
						    						<h3>Password : ".$pwd."</h3><br>
							    					<i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
								    				<button type='submit' class='btnPopup'>OK</button><br><br>
									    		</form>
										    </div>";
									    
								    }
								}
							}
							else
							{
								echo "<div class='msg-div'>
										<form method='post'>
											<h3>Something Error !</h3><br>
											<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
											<button type='submit' class='btnPopup'>OK</button><br><br>
										</form>
									</div>";
							}
						}
					}
			    
			    ?>
					<script>
						function dist()
						{
							var stateId = document.getElementById("stat").value;
							var xmlhttpState = new XMLHttpRequest();
							xmlhttpState.open("GET","ajax_district.php?stateId="+stateId, false)
							xmlhttpState.send(null);
							document.getElementById("distPlace").innerHTML = xmlhttpState.responseText;
						}
						
						function parentUsr()
						{
						    document.getElementById("parentPlace").innerHTML = "Proccessing...";
						    var parentId = document.getElementById("parentId").value;
						    if(parentId === "")
						    {
						        document.getElementById("parentPlace").innerHTML = "<b style='color:red'>Please Insert Parentid !</b>";
						    }
						    else if(parentId.length == 10)
						    {
							    var xmlhttpState = new XMLHttpRequest();
		    					xmlhttpState.open("GET","ajax.php?parentId="+parentId, false)
			    				xmlhttpState.send(null);
				    			document.getElementById("parentPlace").innerHTML = xmlhttpState.responseText;
						    }
						    else
						    {
						        document.getElementById("parentPlace").innerHTML = "<b style='color:red'>Invalid Userid !</b>";
						    }
						}
						
						function sponsorUsr()
						{
						    document.getElementById("sponsorPlace").innerHTML = "Proccessing...";
						    var sponsorId = document.getElementById("sponsorId").value;
						    if(sponsorId === "")
						    {
						        document.getElementById("sponsorPlace").innerHTML = "<b style='color:red'>Please Insert Sponsorid !</b>";
						    }
						    else if(sponsorId.length == 10)
						    {
	    						var xmlhttpState = new XMLHttpRequest();
		    					xmlhttpState.open("GET","ajax.php?sponsorId="+sponsorId, false)
			    				xmlhttpState.send(null);
				    			document.getElementById("sponsorPlace").innerHTML = xmlhttpState.responseText;
						    }
						    else
						    {
						        document.getElementById("sponsorPlace").innerHTML = "<b style='color:red'>Invalid Userid !</b>";
						    }
						}
					</script>
				<form method="post">
					<div class="div1">
						<input type="number" name="sponsor" id="sponsorId" placeholder="Sponsor Id..." onfocusout="sponsorUsr()"  required>
						<span id="sponsorPlace"></span>
						<input type="number" name="parent" id="parentId" placeholder="Parent Id..." onfocusout="parentUsr()"  required>
						<span id="parentPlace"></span>
						<input type="text" name="uName" placeholder="User's Name ..."  required>
						<input type="number" id="mobile" name="mobile" placeholder="Mobile No ..."  required>
						<input type="email" name="email" placeholder="E-Mail ..." >
						<select name="state" id="stat" onchange="dist()">
							<option>--Select State--</option>
							<?php
								$reState=mysqli_query($con,"SELECT * FROM state ORDER BY Name");
								while($dataState=mysqli_fetch_array($reState))
								{
									echo "<option>".$dataState['Name']."</option>";
								}
							?>
						</select>
						<select name="district" id="distPlace">
							<option>--Select District--</option>
						</select>
						<input type="text" name="pin" placeholder="Pin Code...">
						<textarea name="address" placeholder="Address .."></textarea>
						<input type="submit" name="submit" value="Submit" onclick='showLoader()'>
					</div>
				<div class="div1">
					<center><h1>Recent Added</h1></center><hr>
					<table cellpadding="20" border="2">
						<tr>
							<th>Date</th>
							<th>Userid</th>
							<th>Name</th>
						</tr>
						<?php
						    $sl=0;
							$quGetTxn="SELECT * FROM MLM_Users ORDER BY Id DESC";
							$reGetTxn=mysqli_query($con,$quGetTxn);
							while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							{
							    if(checkChild($userid,$dataGetTxn['Parent_Id'])=="true" && $sl<10)
							    {
							        $sl=$sl+1;
								    echo "<tr>";
									    echo "<td>".$dataGetTxn['Date']."</td>";
									    echo "<td>".$dataGetTxn['Userid']."</td>";
				    					echo "<td>".$dataGetTxn['Name']."</td>";
					    			echo "</tr>";
							    }
							}
						?>
					</table>
				</div>
				</form>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>