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

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


if(isset($_POST['add_cat'])){
	$expenses_text=mysqli_real_escape_string($connation,$_POST['expenses_text']);
	mysqli_query($connation,"INSERT INTO expenses_category(expenses_text) VALUES ('$expenses_text')");
}

if(isset($_POST['update_bt'])){
	$expenses_id=mysqli_real_escape_string($connation,$_GET['expenses_id']);
	$expenses_text=mysqli_real_escape_string($connation,$_POST['expenses_text']);
	mysqli_query($connation,"UPDATE expenses_category SET expenses_text='$expenses_text' WHERE expenses_id='$expenses_id'");
}

if(isset($_GET['expenses_id'])){
$expenses_id=mysqli_real_escape_string($connation,$_GET['expenses_id']);
$edit_qury=mysqli_query($connation,"SELECT * FROM expenses_category WHERE expenses_id='$expenses_id'");
$edit_expresalt=mysqli_fetch_array($edit_qury);
}
?>
<!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 - Category manage</strong>
	
<div style="text-align: right"><button type="button" class="btn btn-dark btn-sm" onClick="JavaScript:window.location='expenses';"><i class="fa fa-backward"></i> Back Expenses</button></div>
	
<form method="post">
	
<table class="mb-2">
<tbody>
<tr>
<td><label>Category</label></td>
<td></td>
</tr>
<tr>
<td><input name="expenses_text" type="text" class="form-control form-control-sm" id="expenses_text" required value="<?php if(isset($_GET['expenses_id'])){echo $edit_expresalt['expenses_text'];} ?>"></td>
<td><button type="submit" name="<?php if(isset($_GET['expenses_id'])){echo "update_bt";}else{echo "add_cat";} ?>" class="btn btn-success btn-sm ml-2"><i class="fa fa-check"></i> <?php if(isset($_GET['expenses_id'])){echo "Edit Category";}else{echo "Add New Category";} ?></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>&nbsp;</th>
				<th>Category</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$exp_qury=mysqli_query($connation,"SELECT * FROM expenses_category ORDER BY expenses_text");
			while($exp_resalt=mysqli_fetch_array($exp_qury)){
			$count_no++;
			?>
            <tr>
                <td style="width: 100px;"><?php echo number_format($count_no,0); ?></td>
				<td style="white-space: nowrap; width: 50px;">
				<a href="expenses-category?expenses_id=<?php echo $exp_resalt['expenses_id']; ?>" class="badge badge-info"><i class="fa fa-edit"></i></a>
				<a href="expenses-category?remove=<?php echo $exp_resalt['expenses_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this expenses?');"><i class="fa fa-trash"></i></a>
				</td>
				<td><?php echo $exp_resalt['expenses_text']; ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
    </table>

	
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
		
</div>	
	

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