<?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'];
?>
<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:470px;}
 .content h3{margin:10 20px; font-size:20px;}
 .content #tb1{margin:10 0 0 250px;}
 .content #tb1 #tbp{float:right; margin:10 20px;}
 .viewstate{background:#996420; width:97%; height:40px; margin:10 0 0 20px;}
 .viewstate h3{line-height:40px; margin-left:20px; color:white;}
  #tb1{width:350px; margin:10 20px; border-collapse:collapse; }
 #td1{background:#fef6ec;}
 #td2{background:#e8edfd; padding:0 0 0 10px;}
 #inp{width:100px;}
 #btn{margin:10 0 0 520px; padding:5px; width:80px; background:green; color:white; cursor:pointer;}
 #actionmsg{border:2px solid #4779b6; width:30%; padding:10 20px;
 font-size:18px; font-weight:bold; color:#4779b6; font-family:sans-serif;
 background:white; margin-left:50px; border-radius:5px;}
 
.foot{background:#996420; width:100%; height:50px; float:left;
text-align:center; margin-top:10px;}
 
 
</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">
	<form method="post">
		<?php
			include'connection.php';
			$uid=$_GET['id'];
			date_default_timezone_set("asia/kolkata");
			$date=date("Y-m-d");
			$time=date("h:i:s A");
			if(isset($_POST['submit']))
			{
				$inputbal=$_POST['addbal'];
				$rmark=$_POST['rmark'];
				if($inputbal>$currentbal)
				{
					echo "<p id='actionmsg'>Insufficiant Balance</p>";
				}
				else
				{	
					$query1="insert into transaction(Fromid,Toid,Credit,Debit) values('$userid','$uid','$inputbal','$inputbal')";
					if(mysqli_query($con,$query1))	
					{
						$query2="select * from transaction order by Id desc";
						$result2=mysqli_query($con,$query2);
						if($data2=mysqli_fetch_array($result2))
						{
							$addbal= $data2['Credit'];
							$deductbal=$data2['Debit'];
							$paymentid=$data2['Id'];
							$query3="select * from wallet where Userid='$userid' order by Id desc";
							$result3=mysqli_query($con,$query3);
							if($data3=mysqli_fetch_array($result3))
							{	
								$oldbalance3= $data3['Balance'];
								$newbal3=$oldbalance3+$deductbal;
								
								 $query4="insert into wallet(Userid,Openingbalance,Debit,Remarks,Balance,Date,Time,Paymentto,Transactiontype,Paynrechid)
								values('$userid','$oldbalance3','$deductbal','$rmark','$newbal3','$date','$time','$uid','Payment_Transfer','$paymentid')";
								if(mysqli_query($con,$query4))
								{
								    
									$query5="select * from wallet where Userid='$uid' order by Id desc";
									$result5=mysqli_query($con,$query5);
									if($data5=mysqli_fetch_array($result5))
									{
										$oldbalance5= $data5['Balance'];
										$newbal5=$oldbalance5-$addbal;
										
										$query6="insert into wallet(Userid,Openingbalance,Credit,Balance,Date,Time,Paymentfrom,Transactiontype,Paynrechid)
										values('$uid','$oldbalance5','$addbal','$newbal5','$date','$time','$userid','Payment_Transfer','$paymentid')";
										if(mysqli_query($con,$query6))
										{
											$query7="update masterdis set Balance='$newbal5' where Userid='$uid'";
											if(mysqli_query($con,$query7))
											{
												$query8="update admin set Balance='$newbal3' where Userid='$userid'";
												if(mysqli_query($con,$query8))
												{
													echo "<p id='actionmsg'>Balance Update Successfully !</p>";
												}
												else
												{
													echo "<error query 8>";
												}
											}
											else
											{
												echo "<error query 7>";
											}
										}
										else
										{
											echo "<error query 6>";
										}
									}
									else
									{
										echo "error in query 5";
									}
								}
								else
								{
									echo "<echo error 5>";
								}
							}
							else
							{
								echo "<error query 3>";
							}
						}
						else
						{
							echo "<error query 2>";
						}
					}
					else
					{
						echo "error query 1";
					}
						
				}
				mysqli_close($con);
			}
		?>

		<h3>Add Balance</h3><hr>
<?php
	include'connection.php';
    $querygetname="select * from masterdis where Userid='$uid'";
	$resultgetname=mysqli_query($con,$querygetname);
	$datagetname=mysqli_fetch_array($resultgetname);
	$username=$datagetname['Name'];
	$address=$datagetname['Address'];
	
	echo"<table border='1' cellpadding='5' id='tb1'>";
		echo "<tr>";
		 echo "<td id='td1'><p id='tbp'>Current Balance :</p></td>";
		
			$query9="select * from wallet where Userid='$uid' order by Id desc";
			$result9=mysqli_query($con,$query9);
			$data9=mysqli_fetch_array($result9);
			$currentbal9= $data9['Balance'];
		
		 echo "<td id='td2'>".$currentbal9."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>User Id : </p></td>";
			echo "<td id='td2'>".$uid."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>User Name : </p></td>";
			echo "<td id='td2'>".$username."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Address : </p></td>";
			echo "<td id='td2'>".$address."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Deduct Amount : </p></td>";
			echo "<td id='td2'><input type='text' name='addbal' id='inp'></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Add Remark : </p></td>";
			echo "<td id='td2'><input type='text' name='rmark'></td>";
		echo "</tr>";
echo "</table>";
		
?>
			<input type="submit" name="submit" id="btn">
		</form>

	</div>
	<div class="foot">
		<p>Copyright . © 2018-19   |   All rights Reserved</p>
	</div>
</body>
</html>