<?php
	session_start();
	if($_SESSION['kbuid'])
	{
		$userid= $_SESSION['kbuid'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?PHP
	include'connection.php';
	$incomeBal=0;
	$quIncomeBal="SELECT * FROM income WHERE Userid='$userid'";
	$reIncomeBal=mysqli_query($con,$quIncomeBal);
	while($dataIncomeBal=mysqli_fetch_array($reIncomeBal))
	{
	    $incomeBal=$incomeBal+$dataIncomeBal['Txn'];
	}
	
	
	if(isset($_POST['submit']))
	{
		$amount = $_POST['amount'];
		
		if(empty($amount))
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Fill All Fields Please !</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>";
		}
		elseif(date("l")!=="Tuesday")
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Withdrawal Allowed Only On Monday !</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>";
		}
		elseif($amount<1000)
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Amount Is Less Than 1000 !</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>";
		}
		elseif($incomeBal<$amount)
		{
		    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>Insufficiant Income You Have !</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
		{	
			$quInsertRequest="INSERT INTO income (Date,Time,Userid,TxnType,Txn,Remarks)VALUES('$date','$time','$userid','WITHDRAWAL','-$amount','Withdrawal Income By User')";
			if(mysqli_query($con,$quInsertRequest))
			{
			    $incomeWalletId=mysqli_insert_id($con);
			    
				$quInsertRequest="INSERT INTO withdrawal (Date,Time,Userid,Amount,IncomeWalletId)VALUES('$date','$time','$userid','$amount','$incomeWalletId')";
				if(mysqli_query($con,$quInsertRequest))
				{
				    $withdrawalId=mysqli_insert_id($con);
				    
				    $updateIncome="UPDATE income SET TxnBy='$withdrawalId' WHERE Id='$incomeWalletId'";
				    if(mysqli_query($con,$updateIncome))
				    {
					    echo "<div class='msg-div'>
						    	<form method='post'>
							        <h3>Withdrawal Request Submited 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>Withdrawal 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:#f7f7f7;
			width:50%;
			height:500px;
			float:left;
			text-align:center;
			padding:10px;
		}
		.div1 table{
			width:94%;
			text-align:center;
			font-size:17px;
			border-collapse:collapse;
			margin:10px;
			padding:2px 10px;
		}
		.div1 hr{
			margin:5px 10px;
			padding:0 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
		}
		.div1 hr{width:94%;}
		.div1 input,select, textarea{
			width:90%;
			height:45px;
			margin:5px 10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:18px;
		}
		textarea{
			height:100px;
			padding:5px 10px;
		}
		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>Withdrawal Income :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <center><h3 style="color:blue; margin:10px;">-: Withdrawal Income Only On Monday And Amount Of 1000 Or Above Only ! :-</h3><hr></center>
				<form method="post">
					<div class="div1">
					    <h4>Total Remaining Income : <?php echo $incomeBal; ?> Rs.</h4><br>
						<input type="number" name="amount" placeholder="Minimum Amount 1000..."><br><br>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div1">
					<center><h1>Recent Withdrawal</h1></center><hr>
					<table cellpadding="20" border="2">
						<tr>
							<th>Date</th>
							<th>Amount</th>
							<th>Txn No.</th>
							<th>Status</th>
						</tr>
						<?php
							$quGetTxn="SELECT * FROM withdrawal WHERE Userid='$userid' ORDER BY Id DESC LIMIT 10";
							$reGetTxn=mysqli_query($con,$quGetTxn);
							while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							{
								if($dataGetTxn['Status']=="PENDING")
								{
									$color="blue";
								}
								elseif($dataGetTxn['Status']=="APPROVED")
								{
									$color="green";
								}
								else
								{
									$color="red";
								}
								echo "<tr>";
									echo "<td>".$dataGetTxn['Date']."</td>";
									echo "<td>".$dataGetTxn['Amount']."</td>";
									echo "<td>".$dataGetTxn['TxnNo']."</td>";
									echo "<td style='color:".$color."'>".$dataGetTxn['Status']."</td>";
								echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>