<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Wallet Ledger</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:96%;
			padding:10px;
			margin:2%;
		}
		.div1 input, .div1 select{
			height:30px;
			width:180px;
			font-size:16px;
			padding:5px 8px;
			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:96%;
			text-align:center;
			font-size:14px;
			border-collapse:collapse;
			padding:2px 10px;
			margin:2%;
		}
		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>Wallet Ledger :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<form method="post">
					<div class="div1">
					    <?php
					        $option1 = "selected";
					        $option2 = "";
					        if(isset($_POST['submit']))
					        {
					            $date1Value = $_POST['fdate'];
					            $date2Value = $_POST['ldate'];
					            $OrderBy    = $_POST['OrderBy'];
					            if($OrderBy=="ASC")
					            {
					                $option1 = "";
					                $option2 = "selected";
					            }
					              
					        }
					        else
					        {
					            $date1Value=$date;
					            $date2Value=$date;
					        }
					    
					    ?>
						<input type="date" name="fdate" value="<?php echo $date1Value; ?>">
						<input type="date" name="ldate" value="<?php echo $date2Value; ?>">
						<select name="OrderBy">
						    <option <?php echo $option1; ?>>DESC</option>
						    <option <?php echo $option2; ?>>ASC</option>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Wallet Id</th>
							<th>Txn Id</th>
							<th>Date Time</th>
							<th>Name</th>
							<th>Op.Bal.</th>
							<th>Txn.</th>
							<th>Cl. Bal.</th>
							<th>Txn Type</th>
							<th>Remarks</th>
						</tr>
						<?php
						    $data1   = "w.Id, w.Date, w.Time, w.OpBal, w.Txn, w.ClBal, w.TxnId, w.Txntype, w.TxnWith, w.Remarks,";
							$data2   = "u.Name,";
							$data3   = "s.Name";
							$LJwNu   = "LEFT JOIN user AS u ON w.TxnWith=u.Userid";
							$LJwNs   = "LEFT JOIN Seller AS s ON w.TxnWith=s.Id";
							$where1  = "w.Userid='$userid'";	
						    $where2  = "";
						    $orderBy = "";
						    
							if(isset($_POST['submit']))
							{
								$fdate   = $_POST['fdate'];
								$ldate   = $_POST['ldate'];
								$OrderBy = $_POST['OrderBy'];
								
								 $where2 = "AND w.Date>='$fdate' AND w.Date<='$ldate'";
								 $orderBy = "ORDER BY w.Date $OrderBy";
							}
							else
							{
							    $orderBy = "ORDER BY w.Id DESC LIMIT 40";
							    $sql = "START";
							}
							
							$quLedger    = "SELECT $data1 $data2 $data3 FROM wallet AS w $LJwNu $LJwNs WHERE $where1 $where2 $orderBy";
							$reLedger    = mysqli_query($con,$quLedger);
							$countLedger = mysqli_num_rows($reLedger);
							$totalTxn    = 0;
							if($countLedger==0)
							{
								    echo "<tr><td colspan='10'><center><h3 style='color:red'>Data Not Found !</h3></center></td></tr>";
							}	
							elseif(isset($_POST['submit']) || $sql== "START")
							{
								while($dataLedger=mysqli_fetch_array($reLedger))
								{
								    $Id      = $dataLedger[0];
								    $Date    = $dataLedger[1];
								    $Time    = $dataLedger[2];
								    $OpBal   = $dataLedger[3];
								    $Txn     = $dataLedger[4];
								    $ClBal   = $dataLedger[5];
								    $TxnId   = $dataLedger[6];
								    $TxnType = $dataLedger[7];
								    $TxnWith = $dataLedger[8];
								    $Remarks = $dataLedger[9];
								    $UsrName = $dataLedger[10];
								    $Seller  = $dataLedger[11];
								    
								    $color="#17811C";
								    if($Txn<0)
								    {
										$color="red";
									}
									echo "<tr>";
										echo "<td>".$Id."</td>";
										echo "<td>".$TxnId."</td>";
										echo "<td>".$Date."<br>".$Time."</td>";
										echo "<td>".$UsrName." ".$Seller."</td>";
										echo "<td>".getRA($OpBal)."</td>";
										echo "<td style='color:".$color."'><b>".getRA($Txn)."</b></td>";
										echo "<td>".getRA($ClBal)."</td>";
										echo "<td><textarea style='padding:5px;'>".$TxnType."</textarea></td>";
										echo "<td><textarea style='padding:5px;'>".$TxnWith." ".$Remarks."</textarea></td>";
					        		echo "</tr>";
								}
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>