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

if(isset($_POST['add_cat'])){
	$expenses_details_category=mysqli_real_escape_string($connation,$_POST['expenses_details_category']);
	$expenses_details_amount=mysqli_real_escape_string($connation,$_POST['expenses_details_amount']);
	$expenses_details_date=mysqli_real_escape_string($connation,$_POST['expenses_details_date']);
	mysqli_query($connation,"INSERT INTO expenses_details(expenses_details_category, expenses_details_amount, expenses_details_user, expenses_details_date) VALUES ('$expenses_details_category','$expenses_details_amount','$_COOKIE[user_id]','$expenses_details_date')");
}
?>

<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">

<form method="post" autocomplete="off">
<table class="mb-2">
<tbody>
<tr>
<td><label>Add Date</label></td>
<td><label>Category</label></td>
<td><label>Amount</label></td>
<td></td>
</tr>
<tr>
<td><input name="expenses_details_date" type="date" class="form-control form-control-sm" id="expenses_details_date" value="<?php if(isset($_POST['expenses_details_date'])){echo $_POST['expenses_details_date'];}else{echo $current_date;} ?>" required></td>
<td>
<select name="expenses_details_category" id="expenses_details_category" class="form-control form-control-sm" required>
<option value="" hidden="yes">Select</option>
<?php 
$exp_qury=mysqli_query($connation,"SELECT * FROM expenses_category ORDER BY expenses_text");
while($exp_resalt=mysqli_fetch_array($exp_qury)){	
?>
<option value="<?php echo $exp_resalt['expenses_id']; ?>"><?php echo $exp_resalt['expenses_text']; ?></option>
<?php } ?>
</select>	
</td>
<td><input name="expenses_details_amount" type="number" class="form-control form-control-sm" id="expenses_details_amount" required></td>
<td><button type="submit" name="add_cat" class="btn btn-success btn-sm ml-2"><i class="fa fa-check"></i> Add New Expenses</button></td>
</tr>
</tbody>
</table>
</form>
	
<hr>

<form method="post">	
<table class="mb-2">
<tbody>
<tr>
<td><input name="month_val" type="month" class="form-control form-control-sm" id="month_val" value="<?php if(isset($_POST['from_date'])){echo $_POST['from_date'];}else{echo date("Y-m");} ?>" required></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>Time</th>
				<th>Category</th>
				<th style="text-align: right;">Amount</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total=0;
			
			if(isset($_POST['filter'])){
			$m_start=date("Y-m-a",strtotime($_POST['month_val']));
			$m_end=date("Y-m-t",strtotime($_POST['month_val']));
			$list_qury=mysqli_query($connation,"SELECT * FROM expenses_details INNER JOIN expenses_category ON expenses_details.expenses_details_category=expenses_category.expenses_id WHERE expenses_details_date BETWEEN '$m_start' AND '$m_end' AND expenses_details_user='$_COOKIE[user_id]' ORDER BY expenses_details_id DESC");
			}
			else{
			$list_qury=mysqli_query($connation,"SELECT * FROM expenses_details INNER JOIN expenses_category ON expenses_details.expenses_details_category=expenses_category.expenses_id WHERE expenses_details_user='$_COOKIE[user_id]' ORDER BY expenses_details_id DESC");
			}
				
			while($list_resalt=mysqli_fetch_array($list_qury)){
			$count_no++;
			$total=$total+$list_resalt['expenses_details_amount'];
			?>
            <tr>
                <td><?php echo date_format(date_create($list_resalt['expenses_details_date']),"M d, Y") ?></td>
				<td><?php echo $list_resalt['expenses_text']; ?></td>
				<td style="text-align: right;"><?php echo number_format($list_resalt['expenses_details_amount'],2); ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	        <tr style="font-weight: bold; background-color: #DDDDDD;">
                <td colspan="2" align="right">Total Amount</td>
				<td align="right"><?php echo number_format($total,2); ?></td>
				<td></td>
            </tr>
	</tfoot>
    </table>