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

if(isset($_GET['expenses_details_id'])){
	$expenses_details_id=mysqli_real_escape_string($connation,$_GET['expenses_details_id']);
	if(mysqli_query($connation,"DELETE FROM expenses_details WHERE expenses_details_id='$expenses_details_id'")){
	echo "<script>window.location='expenses';</script>";
	}
}

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')");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">
	

	
<strong>Expenses Manage - Add Expenses and View Summery</strong>
	
<div style="text-align: right"><button type="button" class="btn btn-dark btn-sm" onClick="JavaScript:window.location='expenses-category';"><i class="fa fa-tasks"></i> Category Manager</button></div>

<form method="post">
	
<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>
	
<form method="post">
	
<hr>
	
<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></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></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>
<div class="row">
<div class="table-responsive">
<table id="example" class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th style="width: 100px;">No.</th>
				<th style="width: 50px;">&nbsp;</th>
                <th>Time</th>
				<th>Category</th>
				<th style="text-align: right;">Amount</th>
				<th>User</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total=0;
			
			$join_str="expenses_details INNER JOIN user_details ON expenses_details.expenses_details_user=user_details.user_id INNER JOIN expenses_category ON expenses_details.expenses_details_category=expenses_category.expenses_id";
			
			if(isset($_POST['filter'])){
			$list_qury=mysqli_query($connation,"SELECT * FROM $join_str WHERE expenses_details_date BETWEEN '$_POST[from_date]' and '$_POST[to_date]' ORDER BY expenses_details_id DESC");
			}
			else{
			$list_qury=mysqli_query($connation,"SELECT * FROM $join_str 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 number_format($count_no,0); ?></td>
				<td align="center"><a href="expenses.php?expenses_details_id=<?php echo $list_resalt['expenses_details_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this record?');"><i class="fa fa-trash"></i></a></td>
                <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>
				<td><?php echo $list_resalt['user_fname']." ".$list_resalt['user_lname']; ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	        <tr style="font-weight: bold; background-color: #DDDDDD;">
                <td colspan="4" align="right">Total Amount</td>
				<td align="right"><?php echo number_format($total,2); ?></td>
				<td></td>
            </tr>
	</tfoot>
    </table>
</div>
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
		
	
</div>
</div>	
	

	
</body>
</html>
<?php @mysqli_close($connation); ?>