<?php include("includes/db_config.php");
if(isset($_POST['submit']))
{ extract($_POST);
$sql="INSERT into expenses_category(ename) values ('$ename')";
$res=mysqli_query($conn,$sql) or die(mysqli_error());
if($res){  
   }
}
if(isset($_POST['update']))
{ extract($_POST);
$id=$_POST["pt_id"];
$sql1 ="UPDATE expenses_category SET ename='$ename' WHERE id='$id'"; 
$res=mysqli_query($conn,$sql1) or die(mysqli_error());
 if($res){
   }
} ?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?> 
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">

            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5"> 
                    <div class="row">
                        <div class="col-md-8">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                    <h3 class="portlet-title">All Expense Category</h3>
                                </div>
                                <div class="portlet-body table-responsive">
                                    <table id="datatable-1" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>Expense Name</th>
                                                <th>
                                                    <center>Action</center>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php $j=1; 
                                            $sql="select * from expenses_category"; 
                                            $result = mysqli_query($conn, $sql); 
                                            while($res= mysqli_fetch_array($result)){ ?>
                                            <tr>
                                                <td><?= $res['ename'];?></td>
                                                <td>
                                                    <center> 
                                                        <button name="edit" id="<?= $res['id'];?>" class="btn btn-success edit_data"> <i class="far fa-edit"></i></button>
                                                        <a href="javascript:delete_ptype_by_ID('<?php echo $res['id'] ?>');" class="btn btn-danger shadow btn-xs sharp"><i class="fa fa-trash"></i></a>
                                                    </center>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                    <h3 class="portlet-title">Add Expense Category</h3>
                                </div>
                                <div class="portlet-body">
                                     <form class="row g-3" action="" method="post">
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Expenses Name</label>
                                    <input type="text" placeholder="Enter Expenses Name" name="ename" class="form-control">
                                </div> 
                        <div class="col-md-12 mb-3"><button class="btn btn-primary" type="submit" name="submit">Submit</button></div>  
                            </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade" id="modal3" data-bs-backdrop="static">
                <div class="modal-dialog dialog-lg">
                    <form action="" method="post">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">Edit Expenses Details</h5><button type="button" class="btn btn-label-danger btn-icon" data-bs-dismiss="modal"><i class="fa fa-times"></i></button>
                        </div>
                        <div class="modal-body">
                            <form class="row g-3" action="#">
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Expenses Name</label>
                                    <input type="text" placeholder="Enter Expenses Name" name="ename" id="ename" class="form-control">
                                </div> 
                                 
                            </form>
                        </div>
                       <input type="hidden" name="pt_id" id="pt_id" />
                            <div class="modal-footer"><button type="submit" name="update" id="insert" class="btn btn-primary">Update</button></div>
                        
                    </div>
                   </form>
                </div>
            </div> 
            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
<script type="text/javascript">   
 $(document).ready(function() {
        $(document).on('click', '.edit_data', function() {
            var pt_id = $(this).attr("id");
            $.ajax({
                url: "edit_expct.php",
                method: "POST",
                data: { pt_id: pt_id},
                dataType: "json",
                success: function(data) {
                     $('#ename').val(data.ename);
                     $('#pt_id').val(data.id);
                     $('#insert').val("Update");
                    $('#modal3').modal('show');
                }
            });
        });
    });   
     function delete_ptype_by_ID(id) {
        if (confirm('Do You Want to Deleting This \nContinue anyway?')) {
            window.location.href = 'delete_expct.php?id=' + id;
        }
    }
</script>    
</body>

</html>