<?php
	//include'session.php';
	include'connection.php';

?>

<?php	
	if(isset($_POST['submit']))
	{
		$packId = $_POST['packId'];
		
		$quPack="SELECT * FROM package WHERE Id='$packId'";
		$rePack=mysqli_query($con,$quPack);
		$dataPack=mysqli_fetch_array($rePack);
		$packName=$dataPack['Name'];
		$packAmount=$dataPack['Amount'];
		$SpoCom=$packAmount*$dataPack['SpoCom']/100;
		
		$quBal="SELECT * FROM main_wallet WHERE Userid='$userid'";
		$reBal=mysqli_query($con,$quBal);
		$OpBal=0;
		while($dataBal=mysqli_fetch_array($reBal))
		{
		    $OpBal=$OpBal+$dataBal['Txn'];
		}
		if($OpBal<$packAmount)
		{ 
			echo"<div class='msg-div'>
			        <form method='post'>
				        <h3>Insufficiant Balance !</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
		{	
		    $ClBal=$OpBal-$packAmount;
			$query = "insert into main_wallet(Date,Time,Userid,TxnType,TxnBy,OpBal,Txn,ClBal,Remarks)
			values('$date','$time','$userid','Upgrade Account','$downline','$OpBal','-$packAmount','$ClBal','Debit For Upgradation')";
			if(mysqli_query($con,$query))
			{
			    $updateUsr="UPDATE user SET package='$packName' WHERE Userid='$userid'";
			    if(mysqli_query($con,$updateUsr))
			    {
			        $quUsr="SELECT * FROM user WHERE Userid='$userid'";
			        $reUsr=mysqli_query($con,$quUsr);
			        $dataUsr=mysqli_fetch_array($reUsr);
			        $sponserId=$dataUsr['SponserId'];
			        
			        if(!empty($SpoCom))
			        {
			            if(!empty($sponserId))
			            { 
			                $insertSpoCom="INSERT INTO income(Date,Time,Userid,TxnType,TxnBy,Txn,Remarks)VALUES('$date','$time','$sponserId','Sponser Income','$userid','$SpoCom','Sponser Commission')";
			                mysqli_query($con,$insertSpoCom);
			            }
			        }
			        
			        for($i=1; $i<=8; $i++)
			        {   
		                $LevelCom=$packAmount*$dataPack['Com'.$i]/100;
			            $ParentId=$dataUsr['Parent'.$i];
			            $level="Level ".$i." Income";
		                if(!empty($LevelCom))
		                {
		                    if(!empty($ParentId))
		                    {
		                        echo $insertLevelCom="INSERT INTO income(Date,Time,Userid,TxnType,TxnBy,Txn,Remarks)VALUES('$date','$time','$ParentId','$level','$userid','$LevelCom','Level Commission')";
			                    mysqli_query($con,$insertLevelCom);
		                    }
		                }
			        }
			        
			  		    echo "<div class='msg-div'>
							    <form method='post'>
							        <h3>Account Upgrade successfully !</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>";
			    }
				
			}
			
		}
	}
?>
<html>
<head>
	<title>Upgrade Account</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:500px;
			float:right;
			text-align:center;
			padding-top:50px;
		}
		.div1 i{
			font-size:300px;
			color:#6b5127;
			margin:0 10px;
			
		}
		.div1 a{
		    padding:5px 25px;
		    background:blue;
			font-size:20px;
			color:white;
			margin:10px 50px 50px 50px;
			transition:1s;
			border-radius:50px;
			float:left;
			border:solid white 1px;
		}
		.div1 a:hover{
		    background:white;
			color:blue;
			border:solid blue 1px;
			transition:1s;
			margin-left:100px;
		}
		.div1 a i{
			font-size:20px;
			margin:10px;
			color:white;
			transition:1s;
		}
		.div1 a:hover i{
			color:blue;
			transition:1s;
		}
		.div1 input,.div1 select{
			width:90%;
			height:55px;
			margin:20px 10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:19px;
		}
		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">
			<div class="head">
				<div class="title"><p>Upgrade Account :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<script>
					function downlinUsr()
					{
						var downlineUsr = document.getElementById("downlineUsr").value;
						var usrid = document.getElementById("usrid").value;
						if(downlineUsr=="")
						{
						    
						}
						else
						{
						    var xmlhttpDownlineUsr = new XMLHttpRequest();
						    xmlhttpDownlineUsr.open("GET","ajax_downline_user.php?downlineUsr="+downlineUsr+"&usrid="+usrid, false)
						    xmlhttpDownlineUsr.send(null);
						    document.getElementById("resultPlace").innerHTML = xmlhttpDownlineUsr.responseText;
						}
					}
				</script>
				<form method="post">
					<div class="div1">
						<i class="fa fa-shopping-cart"></i>
					</div>
					<div class="div1">
					    <a href='package_list.php' target='_blank'>View Package List <i class="fa fa-arrow-right"></i></a>
					    <select name='packId'>
					       <option>--Select Package--</option>
					       <?php
					            $quPack="SELECT * FROM package WHERE Status='Active'";
		                        $rePack=mysqli_query($con,$quPack);
		                        while($dataPack=mysqli_fetch_array($rePack))
		                        {
		                            echo "<option value='".$dataPack=$dataPack['Id']."'>".$dataPack=$dataPack['Name']." - ".$dataPack=$dataPack['Amount']." Rs.</option>";
		                        }
					       ?>
					   </select>
						<input type="submit" name="submit" value="Submit" >
					</div>
				</form>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>