<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
	
	
?>
<html>
<head>
	<title>Add Retailers</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:50%;
			height:550px;
			float:left;
			text-align:center;
		}
		.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';
	if(isset($_POST['submit']))
	{
	    $querygetmaxid="select max(Userid) from retailer";
		$resultgetmaxid=mysqli_query($con,$querygetmaxid);
		$row=mysqli_fetch_array($resultgetmaxid);
		$maxid=$row[0];
		$newUsrid=$maxid+1;
				
		$uName = $_POST['uName'];
		$mobile = $_POST['mobile'];
		$email = $_POST['email'];
		$state = $_POST['state'];
		$district = $_POST['district'];
		$pin = $_POST['pin'];
		$address = $_POST['address'];
		
		$quGetDupId="SELECT * FROM retailer WHERE Mobile='$mobile'";
		$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($DupId>0)
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>This Mobile Number 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(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($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;
			$quDis="SELECT * FROM distributer WHERE Userid='$userid'";
			$reDis=mysqli_query($con,$quDis);
			$dataDis=mysqli_fetch_array($reDis);
			$grandparentid=$dataDis['Parentid'];
	
			//Insert into User profile
		 	$query = "INSERT INTO retailer(JoinDate,Userid,Password,Name,Address,State,Mobile,Parentid,Grandparentid,Pincode,City,Email)
			values('$date','$newUsrid','$pwd','$uName','$address','$state','$mobile','$parentId','$grandparentid','$pin','$district','$email')";
			if(mysqli_query($con,$query))
			{   
			    $updateComm="SELECT * FROM operator";
				$reUpdateComm=mysqli_query($con,$updateComm);
				while($dataUpdateComm=mysqli_fetch_array($reUpdateComm))
				{
					$OwnApiCode=$dataUpdateComm['OwnApiCode'];
					$Operator=$dataUpdateComm['Operator'];
					$Serviceid=$dataUpdateComm['Serviceid'];
					$RetComm=0;
									        
					$quUpdateComm="INSERT INTO CommSlab (Userid,OpName,OpCode,ServiceId,Comm) VALUES ('$newUsrid','$Operator','$OwnApiCode','$Serviceid','$RetComm')";
					mysqli_query($con,$quUpdateComm);
				}
				
				$quRetBal="INSERT INTO RetBal(Userid, Walletid, Balance)VALUES('$newUsrid','0','0')";
				if(mysqli_query($con,$quRetBal))
				{
		            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>";
			}
		}
	}
?>
		<div class="content">
			<input type="text" style="display:none;" value="<?php echo $userid; ?>" id="user">
			<div class="head">
				<div class="title"><p>Add Retailers :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
					<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;
						}
					</script>
				<form method="post">
					<div class="div1">
						<input type="text" name="uName" placeholder="Full 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>Userid</th>
							<th>Name</th>
							<th>Mobile</th>
						</tr>
						<?php
							$quGetTxn="SELECT * FROM retailer WHERE Parentid='$userid' ORDER BY Id DESC LIMIT 10";
							$reGetTxn=mysqli_query($con,$quGetTxn);
							while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							{
								echo "<tr>";
									echo "<td>".$dataGetTxn['Userid']."</td>";
									echo "<td>".$dataGetTxn['Name']."</td>";
									echo "<td>".$dataGetTxn['Mobile']."</td>";
								echo "</tr>";
							}
						?>
					</table>
				</div>
				</form>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>