<?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">

<form method="post">	
<table class="mb-2">
<tbody>
<tr>
<td><label>Valid Month</label></td>
<td></td>
</tr>
<tr>
<td><input name="month_val" type="month" class="form-control form-control-sm" id="month_val" value="<?php if(isset($_POST['month_val'])){echo $_POST['month_val'];}else{echo date("Y-m");} ?>" 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>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'])){
			$m_start=date("Y-m-01",strtotime($_POST['month_val']));
			$m_end=date("Y-m-t",strtotime($_POST['month_val']));
			$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 '$m_start' AND '$m_end' AND sd.sale_details_user='$_COOKIE[user_id]'
			ORDER BY sd.sale_details_id DESC");
			}
			else{
			$m_start=date("Y-m-01");
			$m_end=date("Y-m-t");
			$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 '$m_start' AND '$m_end' 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 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>