<?php
require('../config/dbcon.php');
$output = '';
if (isset($_POST["query"])) {

	$query = "Select noofrow from noofrow_mst where description='noofrow'";
	$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
	while ($row = mysqli_fetch_assoc($rs)) {
		$noofrow     = $row['noofrow'];
	}

	$search = mysqli_escape_string($connect, $_POST["query"]);
	$query = "Select * from bill_mst where Lower(supp_name) LIKE Lower('%" . $search . "%') or Lower(truck_number) LIKE Lower('%" . $search . "%') and payment_status = 'PENDING' limit $noofrow";
} else {

	$query = "Select noofrow from noofrow_mst where description='noofrow'";
	$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
	while ($row = mysqli_fetch_assoc($rs)) {
		$noofrow     = $row['noofrow'];
	}
	$query = "Select * from bill_mst where payment_status = 'PENDING' order by date_one,bill_number limit $noofrow";
}

$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
	$output .= '
	<div class="table-responsive" style="width: 200%; overflow:auto; height: 100%;">
		<table id="tblexportData" style="font-size:18px;" class="table table bordered" >
			<tr>
				<th width="80px" style=" text-align: center">Edit</th>
				<th style = "width:30px; text-align: left">truck_number</th>
				<th style = "width:100px; text-align: left">bill_date</th>
				<th style = "width:100px; text-align: left">bill_number</th>
				<th style = "width:300px; text-align: left">supp_name</th>
				<th style = "width:100px; text-align: left">inv_number</th>
				<th style = "width:100px; text-align: left">placefrom</th>
				<th style = "width:400px; text-align: left">destination</th>
				<th style = "width:100px; text-align: left">weight</th>
				<th style = "width:100px; text-align: left">extra</th>
				<th style = "width:100px; text-align: left">balance</th>
				<th style = "width:100px; text-align: left">total_paid</th>
				<th style = "width:100px; text-align: left">payment_status</th>
				<th style = "width:300px; text-align: left">lrno</th>				
				<th style = "width:200px; text-align: left">lchno</th>
				<th style = "width:500px; text-align: left">rupeesinwords</th>
				<th style = "width:100px; text-align: left">id</th>
				<th style = "width:100px; text-align: left">Loading_date</th>
			</tr>';
	while ($row = mysqli_fetch_array($result)) {
	    $orgDate1 = $row["bill_date"]; 
    	$newDate1 = date("d-m-Y", strtotime($orgDate1));
    	$inv_date1=$newDate1;
    	
    	$orgDate2 = $row["date_one"]; 
    	$newDate2 = date("d-m-Y", strtotime($orgDate2));
    	$inv_date2=$newDate2;
		$output .= '
			<tr>
				<td style="text-align: center"><button type="button" style="width:60px; height:25px;" name="update" id="' . $row["id"] . '" class="update btn btn-success btn-xs">Pay</button></td>  
				<td style="text-align: left">' . $row["truck_number"] . '</td>
				<td style="text-align: left">' . $newDate1 . '</td>
				<td style="text-align: left">' . $row["bill_number"] . '</td>
				<td style="text-align: left">' . $row["supp_name"] . '</td>
				<td style="text-align: left">' . $row["inv_number"] . '</td>
				<td style="text-align: left">' . $row["placefrom"] . '</td>
				<td style="text-align: left">' . $row["destination"] . '</td>
				<td style="text-align: left">' . $row["weight"] . '</td>
				<td style="text-align: left">' . $row["extra"] . '</td>
				<td style="text-align: left">' . $row["balance"] . '</td>
				<td style="text-align: left">' . $row["extra"] . '</td>
				<td style="text-align: left">' . $row["payment_status"] . '</td>
				<td style="text-align: left">' . $row["lrno"] . '</td>				
				<td style="text-align: left">' . $row["lchno"] . '</td>
				<td style="text-align: left">' . $row["rupeesinwords"] . '</td>
				<td style="text-align: left">' . $row["id"] . '</td>
				<td style="text-align: left">' . $inv_date2 . '</td>
			</tr>
		';
	}
	echo $output;
} else {
    echo '<div style="font-size:24px; font-weight:bold;  color: #000066;">Data Not Found</div>';
}