<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
?>
<html>
<head>
	<title>Redeem Income</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:;
			width:98%;
			margin:1%;
			padding:10px;
		}
		.div1 input, .div1 p{
			height:40px;
			width:250px;
			font-size:16px;
			padding:5px 10px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:10px 10px 0 5px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			transition:1s;
			cursor:pointer;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			transition:1s;
		}
		.div2{
			background:#f7f7f7;
			width:100%;
			float:left;
			text-align:center;
			padding:0 10px 10px 10px;
			overflow-x:auto;
		}
		.div2 table{
			width:100%;
			text-align:center;
			font-size:15px;
			border-collapse:collapse;
			padding:2px 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
			padding:2px 5px;
		}
		@media(max-width:768px)
		{
			.div1{
				background:;
				width:100%;
				height:auto;
				margin:10px 0 0 0;
				padding:0;
				text-align:center;
			}.div1 input{
				width:90%;
				margin:10px;
			}
			.div2{
				width:100%;
				margin:10px 0 0 0;
				padding:0px;
				text-align:center;
			}
		}
		

	</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>Redeem Income :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<form method="post">
					<div class="div1">
					    <center><h3 align='center' style='color:blue;'>Income Will Be Redeemed Rs.100 or Above only !</h2></center><br>
					    <?php
					        $totalIncome=0;
					        $quIncome="SELECT * FROM MLM_Income WHERE Userid='$userid'";
					        $reIncome=mysqli_query($con,$quIncome);
					        while($dataIncome=mysqli_fetch_array($reIncome))
					        {
					            $totalIncome=$totalIncome+$dataIncome['Txn'];
					        }
					        
					        
					        if(isset($_POST['submit']))
					        {
					            $amount=$_POST['amount'];
					            
					            if($totalIncome<100)
					            {
					                echo "<div class='msg-div'>
		    	                            <form method='post'>
			        	                        <h3>Income Is Less Than 100 !</h3><br>
				                                <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
						                        <a type='submit' class='btnPopup' href='redeem_commission.php'>OK</a><br><br>
			                                </form>
		                                </div>";
					            }
					            elseif($totalIncome<$amount)
					            {
					                echo "<div class='msg-div'>
		    	                            <form method='post'>
			        	                        <h3>Income Is Less Than Redeem Amount !</h3><br>
				                                <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
						                        <a type='submit' class='btnPopup' href='redeem_commission.php'>OK</a><br><br>
			                                </form>
		                                </div>";
					            }
					            elseif(empty($amount))
					            {
					                echo "<div class='msg-div'>
		    	                            <form method='post'>
			        	                        <h3>Enter Redeem Amount Please !</h3><br>
				                                <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
						                        <a type='submit' class='btnPopup' href='redeem_commission.php'>OK</a><br><br>
			                                </form>
		                                </div>";
					            }
					            else
					            {
					                $clIncome=$totalIncome-$amount;
					                $insertIncome="INSERT INTO MLM_Income (Date,Time,Userid,OpBal,Txn,ClBal,TxnType,) VALUES ('$date','$time','$userid','$totalIncome','-$amount','$clIncome','REDEEM')";
					                if(mysqli_query($con,$insertIncome))
					                {
					                    $incomeInsertId=mysqli_insert_id($con);
					                    
					                    $clBal=$usrBal+$amount;
					                    $insertWallet="INSERT INTO MLM_Wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnType,TxnId) VALUES ('$date','$time','$userid','$usrBal','$amount','$clBal','REDEEM','$incomeInsertId')";
					                    if(mysqli_query($con,$insertWallet))
					                    {
					                        $walletInsertId=mysqli_insert_id($con);
					                        
					                        $updateIncome="UPDATE MLM_Income SET TxnId='$walletInsertId' WHERE Id='$incomeInsertId'";
					                        if(mysqli_query($con,$updateIncome))
					                        {
					                            echo "<div class='msg-div'>
						                                 <form method='post'>
						                                    <h3>Income Redeemed successfully !</h3><br>
						                                    <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
						                                    <a type='submit' class='btnPopup' href='redeem_commission.php'>OK</a><br><br>
						                                </form>
					                                </div>";
					                        }
					                        
					                    }
					                }
					            }
					        }
					    
					    ?>
					    <p style="border:none">Income : <i class="fa fa-inr"></i> <?php echo $totalIncome; ?></p>
						<input type="number" name="amount" placeholder="Enter Amount..."><br><br>
						<input type="submit" name="submit" value="Redeem Now"  onclick='showLoader()'>
					</div>
				</form>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>