<?php
	session_start();
	if($_SESSION['mid'])
	{
		$userid= $_SESSION['mid'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
		
		$query8="select * from wallet where Userid='$userid' order by Id desc";
		$result8=mysqli_query($con,$query8);
		$data8=mysqli_fetch_array($result8);
		$currentbal= $data8['Balance'];
?>
<?php
	include'connection.php';
	$query="select * from state";
	$result=mysqli_query($con,$query);
	
?>
<script>
function onchange_event()
{
		var sid = document.getElementById("stateid").value;
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET","ajex.php?sid="+sid,false);
			xmlhttp.send(null);
		document.getElementById("city").innerHTML=xmlhttp.responseText;
}
</script>
<html>
<head>
<style>
body{background:url('bg.jpg'); background-size:cover;
width:100%; height:700px; margin:0px;}

.head{background:; width:1350px; height:100px; margin:0px;}
#p1{font-size:34px; float:left; Margin:10 0 0 45px; color:#996420;
	font-weight:bold;}
#p2{font-size:21px; float:left; margin:25 0 0 -10px; color:#eaa040;
	font-family:Courier;}
#p3{font-size:15px; font-family:Comic Sans MS; float:left; color:#996420;
	margin-top:41px; margin-left:-157px;}
#p4{font-family:100px;}
.h2{background:white; width:400px; height:70px; margin:10 200px;}
#link1{float:right; margin:10 30 0 0px; font-size:20px; text-decoration:none;
 color:white; background:red; padding:5px; border-radius:4px;}
 #link2{float:right; margin:10 30 0 0px; font-size:20px; background:white;color:Green; padding:5px; border-radius:4px;}
#link1:hover{ padding:5px; border-radius:4px; color:black;}
	
.menu{background:#996420; height:40px; width:1300px; margin:0 0 0 20px; padding:0;}
.menu ul{padding:0; margin:0; position:relative; display:inline-block;}
.menu ul li{list-style:none; float:left; width:170px; line-height:40px;
	text-align:center; font-size:15px; margin:0; padding:0; color:white;}
.menu ul li ul {display:none;}
.menu ul li:hover{background:#d1994a; cursor:pointer;}
.menu ul li:hover ul {display:block; z-index:2;}
.menu ul li:hover ul li{background:#d1994a; border-top:solid white 0.1px;}
.menu ul li ul li:hover{background:#996420; color:white;}
.menu ul li ul li:hover a{ color:white;}
.menu ul li a{ text-decoration:none; color:white;}
 #a1{border:none; color:white;}
 .menu{letter-spacing:1px;}
 #bal:hover{background:#996420; cursor:text;}
 
.content{background:; height:500px; margin:0; padding:0;}
.content #conp1{margin:10px; font-size:19px; font-family:calibri; font-weight:bold;}
.addagent{background:#996420; height:40px; width:1300px; margin-left:20px;}
.addagent h3{margin-left:20px; line-height:40px; color:white; font-size:20px;}
.form{background:; margin:10 0 0 130px; width:400px; float:left;}
.form td{padding:5px 0 0 20px; height:20px;}
#subbtn{margin-left:10; padding:5 10px; background:#996420; color:white;
 font-size:17px; border-radius:5px; border:1px solid white; float:left;}
#subbtn:hover{background:#d1994a; cursor:pointer;}
.form #tbtdf{float:right; font-weight:none;}
 
.foot{background:#996420; width:100%; height:50px; float:left;text-align:center;
	margin-top:50px;}
 
</style>
</head>
<body>
	<div class="head">
		<p id="p1">LLP</p>
		<p id="p2">Recharge</p>
		<p id="p3">The Complete Solution</p>
		<a id="link1" href="logout.php">Log Out</a>
		<p id="link2"><?php echo"Balance : ". $currentbal;  ?></p>
	</div>
	<div class="menu">
		<ul>
			<li><a href="recharge_home.php" id="a1">Home</a></li>
			<li>Retailer
				<ul>
					<li><a href="add_new_agent.php">Add New Retailer</a></li>
					<li><a href="view_retailer.php">View Total Retailers</a></li>
					<li><a href="low_bal_retailer.php">Low Bal. Retailers</a></li>
					<li><a href="lazy_retailer.php">Lazy Retailers</a></li>
				</ul>
			</li>
			<li>Commission
				<ul>
					<li><a href="earn_commision.php">Earn Commision</a></li>
					<li><a href="withdraw_commision.php">Withdraw Comm.</a></li>
				</ul>
			</li>
			<li>Reports
				<ul>
					<li><a href="recharge_history.php">Recharge History</a></li>
					<li><a href="account_reports.php">Account Reports</a></li>
					<li><a href="check_status.php">Check Status</a></li>
				</ul>
			</li>
			<li>Documents
				<ul>
					<li><a href="sms_format.php">SMS Format</a></li>
					<li><a href="complain.php">Complain</a></li>
				</ul>
			</li>
			<li>Profile
				<ul>
					<li><a href="change_profile.php">Change Profile</a></li>
					<li><a href="change_pwd.php">Change Password</a></li>
				</ul>
			</li>
			<li>Payment Request
				<ul>
					<li><a href="payment_request.php">Update Payment</a></li>
					<li><a href="bank_list.php">Bank List</a></li>
				</ul>
			</li>
		</ul>
	</div>
	<div class="content">
		<div class="addagent">
			<h3>Add Registration</h3>
		</div>
		<p id="conp1">Personal Information</p><hr>
		<div class="form">
		<?php

			include'connection.php';
			date_default_timezone_set("asia/Kolkata");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			
			if(isset($_POST['submit']))
			{	
				$querygetmaxid="select max(Userid) from retailer";
				$resultgetmaxid=mysqli_query($con,$querygetmaxid);
				$row=mysqli_fetch_array($resultgetmaxid);
				$maxid=$row[0];
				$newuserid=$maxid+1;
				
				$uname=$_POST['uname'];
				$address=$_POST['address'];
				$statid=$_POST['statid'];
				
				$querygetstatename="select * from state where Id='$statid'";
				$resultgetstatename=mysqli_query($con,$querygetstatename);
				$row1=mysqli_fetch_array($resultgetstatename);
				$statename1=$row1['Statename'];
				
				$mnum=$_POST['mnum'];
				$pwd=rand(100000,999999);
				$biztype=$_POST['biztype'];
				$pannum=$_POST['pannum'];
				$parent=$_POST['parent'];
				$pincode=$_POST['pincode'];
				$cityname=$_POST['cityname'];
				$email=$_POST['email'];
				$bizname=$_POST['biztype'];
				$opbal=$_POST['$opbal'];
				
				if(empty($opbal))
				{
				    $opbal=0;
				}
				
				$queryinsert="insert into retailer(JoinDate,Userid,Password,Name,Balance,Address,State,Mobile,Businessname,Pan,Parentid,Grandparentid,Pincode,City,Email,Businesstype)
				values('$date','$newuserid','$pwd','$uname','$opbal','$address','$statename1','$mnum','$bizname','$pannum','$parent','$userid','$pincode','$cityname','$email','$biztype')";
				
				if(mysqli_query($con,$queryinsert))
				{
					$quDeductBal="insert into transaction(Fromid, Toid, Credit, Debit) values('$userid','$newuserid','$opbal','$opbal')";
					if(mysqli_query($con,$quDeductBal))
					{
					
						$quGetId="select * from transaction where Fromid='$userid' order by Id desc";
						$rGetId=mysqli_query($con,$quGetId);
						$dataGetId=mysqli_fetch_array($rGetId);
						$paymentId=$dataGetId['Id'];
						
						$quUpdatWallet="select * from wallet where Userid='$userid' order by Id desc";
						$rUpdatWallet=mysqli_query($con,$quUpdatWallet);
						$dataUpdateWallet=mysqli_fetch_array($rUpdatWallet);
						$disBal=$dataUpdateWallet['Balance'];
						$currBal=$disBal-$opbal;
						
						$quInsertBal="insert into wallet(Userid,Openingbalance,Debit,Remarks,Balance,Date,Time,Paymentto,Transactiontype,Paynrechid)
						value('$userid','$disBal','$opbal','Opening_Balance_To_New_Retailer','$currBal','$date','$time','$newuserid','Transfer_Opening_Balance','$paymentId')";
						
						if(mysqli_query($con,$quInsertBal))
						{
							$queryinsertwallet="insert into wallet(Userid,Openingbalance,Credit,Remarks,Balance,Date,Time,Paymentfrom,Transactiontype,Paynrechid)
							value('$newuserid','$opbal','$opbal','Add_Opening_Balance','$opbal','$date','$time','$userid','Recieved_Opening_Balance','$paymentId')";
							if(mysqli_query($con,$queryinsertwallet))
							{
								$queryinsertcomm="insert into commision(Userid)values('$newuserid')";
								if(mysqli_query($con,$queryinsertcomm))
								{
								    $updateComm="select * from operator";
									    $reUpdateComm=mysqli_query($con,$updateComm);
									    while($dataUpdateComm=mysqli_fetch_array($reUpdateComm))
									    {
									        $Columname=$dataUpdateComm['Commision_Column'];
									        $RetComm=$dataUpdateComm['RetComm'];
									        
									        $quUpdateComm="update commision set $Columname='$RetComm' where Userid='$newuserid'";
									        mysqli_query($con,$quUpdateComm);
									    }
									$quUpdate="update masterdis set Balance='$currBal' where Userid='userid'";
									if(mysqli_query($con,$quUpdate))
									{
									    $quUpdate="update reatiler set Balance='$opbal' where Userid='userid'";
									        mysqli_query($con,$quUpdate);
									    	$query="select * from retailer where Userid='$newuserid'";
											$result=mysqli_query($con,$query);
											$data=mysqli_fetch_array($result);
											echo	"<table border='0' cellpadding='10' id='t2'>
													<tr>
														<th>Registration Successfull !</th>
														<td></td>
													</tr><tr>
														<th>User Id : </th>
														<td> ".$data['Userid']."</td>
													</tr>
													<tr>
														<th>Password : </th>
														<td>". $data['Password']."</td>
													</tr>
													<tr>
														<th>Agent Name : </th>
														<td>".$data['Name']."</td>
													</tr>
													<tr>
														<th></th>
														<td><a href='admin.php'>OK</a></td>
													</tr>
												</table><br><br><br>";
									}
									else
									{
										echo "Registration error !";
									}
								}
								
							}
							else
							{
								echo "error 4";
							}
						}
						else
						{
							echo "error 3";
						}
						
						
					}
					
				}
				else
				{
					echo "error";
				}
				
				mysqli_close($con);
			}
		?>
			<form method="post">
				<table border="0" cellpadding="0">
					<tr>
						<td><p id="tbtdf">Agent Name:</p></td>
						<td><input type="text" name="uname"></td>
					</tr>
					<tr>
						<td><p id="tbtdf">Postal Address:</p></td>
						<td><textarea rows="4" cols="22" name="address"></textarea></td>
					</tr>
					<tr>
						<td><p id="tbtdf">State:</p></td>
						<td>
							<select onchange="onchange_event()" id="stateid" name="statid">
								<?php
									echo "<option>--Select State--</option>";
									while($row=mysqli_fetch_array($result))
									{
										$s=$row['Statename'];
										
										echo "<option value='".$row['Id']."'>".$row['Statename']."</option>";
									}
									
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td><p id="tbtdf">Mobile No.:</p></td>
						<td><input type="text" name="mnum"></td>
					</tr>
					<tr>
						<td><p id="tbtdf">Agent Business Type:</p></td>
						<td>
							<select name="biztype">
								<option>--Select Business Type--</option>
								<?php
									include'connection.php';
									$querybt="select * from retailertype";
									$resultbt=mysqli_query($con,$querybt);
									while($row=mysqli_fetch_array($resultbt))
									{
										echo "<option>".$row['Businessname']."</option>";
									}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td><p id="tbtdf">Pan No.:</p></td>
						<td><input type="text" name="pannum"></td>
					</tr>
				</table>
		</div>
		
		<div class="form">
				<table border="0" cellpadding="0">
					<tr>
						<td><p id="tbtdf">Patent Id:</p></td>
						<td>
							<select name="parent">
								<option>--Select Parent--</option>
							<?php
								$querygetparent="select * from distributer";
								$resultgetparent=mysqli_query($con,$querygetparent);
								while($row=mysqli_fetch_array($resultgetparent))
								{
									echo "<option value='".$row['Userid']."'>".$row['Name']."</option>";
								}
							?>
							</select>
							</div>
						</td>
					</tr>
					<tr>
						<td><p id="tbtdf">Pin Code:</p></td>
						<td><input type="text" name="pincode"></td>
					</tr>
					<tr>
						<td><p id="tbtdf">City/District:</p></td>
						<td>
							<div id="city">
							<select name="cityname">
								<option>--Select City--</option>
							</select>
							</div>
						</td>
					</tr>
					<tr>
						<td><p id="tbtdf">E-Mail:</p></td>
						<td><input type="email" name="email" placeholder="example@gmail.com"></td>
					</tr>
					<tr>
						<td><p id="tbtdf">Business name:</p></td>
						<td><input type="text" name="bizname"></td>
					</tr>
					<tr>
						<th>Opening Balance : </th>
						<td><input type="text" name="opbal"></td>
					</tr>
					<tr>
						<td></td>
						<td>
						<input type="submit" name="submit" value="Submit" id="subbtn">
						<input type="submit" name="cancel" value="Cancel" id="subbtn">
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<div class="foot">
		<p>Copyright . © 2018-19   |   All rights Reserved</p>
	</div>
</body>
</html>