<?php
session_start();
require("php_con.php");
require("function.php");
?>

<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<form method="post">	
<table class="mb-2">
<tbody>
<tr>
<td><label>From Date</label></td>
<td><label>To Date</label></td>
<td></td>
</tr>
<tr>
<td><input name="from_date" type="date" class="form-control form-control-sm" id="from_date" value="<?php if(isset($_POST['from_date'])){echo $_POST['from_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td><input name="to_date" type="date" class="form-control form-control-sm" id="to_date" value="<?php if(isset($_POST['to_date'])){echo $_POST['to_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td><button type="submit" name="filter" class="btn btn-dark btn-sm ml-2"><i class="fa fa-filter"></i> Filter</button></td>
</tr>
</tbody>
</table>
</form>


<table id="example" class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th>No.</th>
				<th></th>
                <th>Bill ID</th>
                <th style="text-align: right;">Amount</th>
                <th style="text-align: right;">Payment</th>
				<th style="text-align: right;">Discount</th>
				<th>Customer</th>
				<th>Sale Time</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total_amount=0;
			if(isset($_POST['filter'])){
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_date BETWEEN '$_POST[from_date]' AND '$_POST[to_date]' AND sd.sale_details_user='$_COOKIE[user_id]'
			ORDER BY sd.sale_details_id DESC");
			}
			else{
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_date='$current_date' AND sd.sale_details_user='$_COOKIE[user_id]'
			ORDER BY sd.sale_details_id DESC");
			}
			while($resalt=mysqli_fetch_assoc($qury)){
			$count_no++;
			$total_amount+=bill_amount($resalt['sale_details_billid']);
				
			$cus_qury=mysqli_query($connation,"SELECT cs_name FROM customer_details WHERE cs_mobile='$resalt[sale_details_customer]'");
			if(mysqli_num_rows($cus_qury)>0){
			$cus_resalt=mysqli_fetch_assoc($cus_qury);
			$customer=$cus_resalt['cs_name'];
			}
			else{
			$customer="Unknown";
			}
			?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
				<td>
				<a href="view-sale?sale_billid=<?php echo $resalt['sale_details_billid']; ?>" target="_blank" class="badge badge-primary" title="View Bill"><i class="fa fa-eye"></i></a>
				<a href="bill_print.php?print=<?php echo $resalt['sale_details_billid']; ?>" target="re_print_bill" class="badge badge-info" title="Re-print Bill"><i class="fa fa-print"></i></a>
				</td>
                <td style="white-space: nowrap;"><?php echo $resalt['sale_details_billid']; ?></td>
                <td style="text-align: right;"><?php echo number_format((float)bill_amount($resalt['sale_details_billid']),2); ?></td>
                <td style="text-align: right;">Cash <?php echo number_format((float)$resalt['sale_details_cash_payment'],2); ?><br>Card <?php echo number_format((float)$resalt['sale_details_card_payment'],2); ?></td>
				<td style="text-align: right;"><?php echo number_format((float)$resalt['sale_details_discount'],2); ?></td>
				<td class="text-capitalize"><?php echo $customer; ?></td>
				<td><?php echo date("y-m-d h:i:s A",strtotime($resalt['sale_details_date']." ".$resalt['sale_details_time'])); ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	        <tr style="font-weight: bold; background-color: #DDDDDD;">
                <td colspan="3" align="right">Total Amount</td>
				<td align="right"><?php echo number_format($total_amount,2); ?></td>
				<td colspan="5"></td>
            </tr>
	</tfoot>
    </table>

<iframe name="re_print_bill" style="display: none;"></iframe>