<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	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","ajax.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; margin:0; padding:0; width:1580px;}
.head{background:; width:1450px; height:85px; margin:0px;}
#p1{font-size:34px; float:left; Margin:10 0 0 45px; color:#996420;
	font-weight:bold;}
#p2{font-size:20px; float:left; margin:28 0 0 -8px; color:#eaa040;
	font-family:Courier;}
#p3{font-size:15px; font-family:Comic Sans MS; float:left; color:#996420;
	margin-top:48px; margin-left:-153px;}
.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:#996420; padding:5px; border-radius:4px;}
 #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:1450px; margin-left:10px; padding:0; line-height:40px;}
.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;}
.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:97%; 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;}

#t1{ width:500px; text-align:center; border-collapse:collapse; border-color:black; margin:0px; padding:10px;}
#t1 th{font-family:calibri; background:#f6f0e8; width:170px; text-align:right; padding-right:25px;}
#t1 td{font-family:calibri; background:#e9e7e5;}
#t1 input{padding-left:5px; width:230px; height:25px; font-size:20px;}
#t1 select{padding-left:5px; width:230px; height:25px;}
#t1 textarea{padding-left:5px; width:230px;}
#btntd{background:none; height:60px;}
#subbtn{ background:green; color:white; font-size:17px; border-radius:5px; border:1px solid white; height:40px; width:200px;
margin:10 0 0 300px;}
#subbtn:hover{background:#d1994a; cursor:pointer;}
 #t2{ width:500px; text-align:center; border-collapse:collapse; border-color:black; margin:0px; padding:10px; background:white;}
 .foot{float:left; margin:10 0 0 100px; width:1350px;}
</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">Balance:<?php echo $currentbal; ?> </p>
	</div>
	<div class="menu">
		<ul>
			<li><a href="admin.php" id="a1">Home</a></li>
			<li>Setting
				<ul>
					<li><a href="add_operator.php">Operator</a></li>
					<li><a href="set_commision.php">Set Commision</a></li>
					<li><a href="add_bank_details.php">Add Bank Details</a></li>
					<li><a href="retailer_type.php">Retailer Type</a></li>
					<li><a href="add_state.php">State</a></li>
					<li><a href="add_city.php">City</a></li>
				</ul>
			</li>
			<li>Manage Users
				<ul>
					<li><a href="list_complain.php">Complain</a></li>
					<li><a href="alert_msg.php">Alert</a></li>
					<li><a href="change_pwd.php">Change Password</a></li>
				</ul>
			</li>
			<li>Master Distributers
				<ul>
					<li><a href="add_md.php">Add M. Distributers</a></li>
					<li><a href="view_md.php">View M. Distributers</a></li>
				</ul>
			</li>
			<li>Distributers
				<ul>
					<li><a href="add_distributer.php">Add Distributers</a></li>
					<li><a href="view_distributer.php">View Distributers</a></li>
				</ul>
			</li>
			<li>Retailer
				<ul>
					<li><a href="add_agent.php">Add Retailer</a></li>
					<li><a href="view_retailer.php">View Retailer</a></li>
				</ul>
			</li>
			<li>Reports
				<ul>
					<li><a href="pending_recharge.php">Pending Recharge</a></li>
					<li><a href="admin_recharge_history.php">Recharge History</a></li>
					<li><a href="check_status.php">Check Transaction</a></li>
					<li><a href="account_reports.php">Account Report</a></li>
					<li><a href="view_payment1.php">Payment Request</a></li>
				</ul>
			</li>
		</ul>
	</div>
	<div class="content">
		<div class="addagent">
			<h3>Add New Distributer</h3>
		</div>
		<p id="conp1">Personal Information</p><hr>
		<div class="form">
		<?php

			include'connection.php';
			date_default_timezone_set("asia/Kolkata");
			$datetime= Date("Y-m-d - h:i:s A");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			if(isset($_POST['submit']))
			{	
				$querygetmaxid="select max(Userid) from distributer";
				$resultgetmaxid=mysqli_query($con,$querygetmaxid);
				while($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);
				while($row=mysqli_fetch_array($resultgetstatename))
				{
					$statename1=$row['Statename']; 
				}
				$mnum=$_POST['mnum'];
				$pwd=rand(100000,999999);
				$biztype=$_POST['biztype'];
				$pannum=$_POST['pannum'];
				$parent=$_POST['parentName'];
				$pincode=$_POST['pincode'];
				$cityname=$_POST['cityname'];
				$email=$_POST['email'];
				$bizname=$_POST['bizname'];
				$opbal=$_POST['opbal'];
				if(empty($opbal))
				{
				    $opbal=0;
				}
				if($opbal>$currentbal)
				{
					echo "Insufficiant Balance !";
				}
				else
				{
					$queryinsert="insert into distributer(JoinDate,Userid,Password,Name,Address,State,Mobile,Businessname,Pan,Parentid,Pincode,City,Email,Businesstype,Balance)
					values('$date','$newuserid','$pwd','$uname','$address','$statename1','$mnum','$biztype','$pannum','$parent','$pincode','$cityname','$email','$bizname','$opbal')";
					
					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['DistComm'];
									        
									        $quUpdateComm="update commision set $Columname='$RetComm' where Userid='$newuserid'";
									        mysqli_query($con,$quUpdateComm);
									    }
										$quUpdate="update admin set Balance='$currBal' where Userid='userid'";
										if(mysqli_query($con,$quUpdate))
										{
											$query="select * from distributer 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="1" cellpadding="10" id="t1">
					<tr>
						<th>Agent Name : </th>
						<td><input type="text" name="uname"></td>
					</tr>
					<tr>
						<th>Mobile No. : </th>
						<td><input type="text" name="mnum"></td>
					</tr>
					<tr>
						<th>E-Mail : </th>
						<td><input type="email" name="email" placeholder="example@gmail.com"></td>
					</tr>
					<tr>
						<th>Postal Address : </th>
						<td><textarea rows="4" cols="22" name="address"></textarea></td>
					</tr>
					<tr>
						<th>State : </th>
						<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>
						<th>City/District : </th>
						<td>
							<div id="city">
							<select name="cityname">
								<option>--Select City--</option>
							</select>
							</div>
						</td>
					</tr>
					<tr>
						<th>Pin Code : </th>
						<td><input type="text" name="pincode"></td>
					</tr>
					<tr>
						<th>Select Parent : </th>
						<td>
							<select name="parentName">
								<option>--Select Parent--</option>
								<?php
									include'connection.php';
									$quGetParent="select * from masterdis";
									$reGetParent=mysqli_query($con,$quGetParent);
									while($dataGetparent=mysqli_fetch_array($reGetParent))
									{
										echo "<option value=".$dataGetparent['Userid'].">".$dataGetparent['Name']."</option>";
									}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<th>Agent Business Type : </th>
						<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>
						<th>Pan No. : </th>
						<td><input type="text" name="pannum"></td>
					</tr>
					<tr>
						<th>Business name : </th>
						<td><input type="text" name="bizname"></td>
					</tr>
					<tr>
						<th>Opening Balance : </th>
						<td><input type="text" name="opbal"></td>
					</tr>
				</table>
						
						<input type="submit" name="submit" value="Submit" id="subbtn"><br><br>
						<input type="submit" name="cancel" value="Cancel" id="subbtn">
			</form>
		</div>
	</div>
	<div class="foot">
		<p>Copyright &copy; 2018 Long Life Product Pvt. Ltd.</p>
	</div>
</body>
</html>