<?php include("includes/db_config.php");
if(isset($_POST['submit']))
{ extract($_POST);
$sql="INSERT into ah_expense_item(item_name,hsn_no,price,tax,gst_rate) values ('$item_name','$hsn_no','$price','$tax_type','$tax_rate')";
$res=mysqli_query($conn,$sql) or die(mysqli_error());
if($res){  
   }
}
if(isset($_POST['update']))
{ extract($_POST);
$id=$_POST["pt_id"];
$sql1 ="UPDATE ah_expense_item SET item_name='$item_name',hsn_no='$hsn_no',price='$price',tax='$tax',gst_rate='$gst_rate' 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 Item</h3>
                                </div>
                                <div class="portlet-body table-responsive">
                                    <table id="datatable-1" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>Expenses Category</th>
                                                <th>Item Name</th>
                                                <th>Quantity</th>
                                                <th>Price/Unit</th>
                                                <th>Amount</th>
                                                <th>
                                                    <center>Action</center>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                             <?php $j=1; 
                                            $sql="select * from ah_expense_item"; 
                                            $result = mysqli_query($conn, $sql); 
                                            while($res= mysqli_fetch_array($result)){
                                             $tid=$res['gst_rate'];   
                                            $sqlts=mysqli_fetch_array(mysqli_query($conn,"select * from tax_rate where id='".$tid."'"));
                                            $tax=$sqlts['tax_gst'];
                                     ?>
                                            <tr>
                                                <td><?= $res['item_name'];?></td>
                                                <td><?= $res['hsn_no'];?></td>
                                                <td><?= $res['price'];?></td>
                                                <td><?= $tax;?></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 Item</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 Category</label>
                                    <select class="form-control" name="tax_type">
                                        <option value="">Select</option>
                                        <option>Petrol</option>
                                        <option>Rent</option>
                                        <option>Mobile</option>
                                    </select>
                                </div>     <div class="mb-3 col-md-12">
                                    <label class="form-label">Expenses Item</label>
                                    <input type="text" placeholder="Enter Expenses Item" name="item_name" class="form-control">
                                </div> 
                               <div class="mb-3 col-md-12">
                                    <label class="form-label">Quantity</label>
                                    <input type="text" placeholder="Enter HSN" name="hsn_no" class="form-control">
                                </div> 
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Price/Unit</label>
                                    <input type="text" placeholder="Enter Price" name="price" class="form-control">
                                </div>  
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Amount</label>
                                    <input type="text" placeholder="Enter Price" name="price" 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 Item</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">
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Expenses Item</label>
                                    <input type="text" placeholder="Enter Expenses Item" id="item_name" name="item_name" class="form-control">
                                </div> 
                               <div class="mb-3 col-md-12">
                                    <label class="form-label">HSN Code</label>
                                    <input type="text" placeholder="Enter HSN" name="hsn_no" id="hsn_no" class="form-control">
                                </div> 
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Price</label>
                                    <input type="text" placeholder="Enter Price" name="price" id="price" class="form-control">
                                </div> 
                                <div class="mb-3 col-md-12">
                                    <label class="form-label">Tax Type</label>
                                    <select class="form-control" name="tax_type" id="tax_type">
                                        <option>With Tax</option>
                                        <option>Without Tax</option>
                                    </select>
                                </div> 
                                <div class="mb-3 col-md-12">
                                  <label class="form-label">Tax Rate</label>   
                                <select class="form-control tax_gsts" name="gst_rate" id="tax_gst">
                                    <?php $sql2="SELECT * from tax_rate";
                                    $exe2=mysqli_query($conn,$sql2);
                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                    <option value="<?= $res2['id'];?>" data-wd="<?= $res2['tax_gst']; ?>"><?= $res2['tax_gst'];?></option>
                                    <?php } ?>
                                </select>
                                </div> 
                           
                        </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_expitm.php",
                method: "POST",
                data: { pt_id: pt_id},
                dataType: "json",
                success: function(data) {
                     $('#item_name').val(data.item_name);
                     $('#hsn_no').val(data.hsn_no);
                     $('#price').val(data.price);
                     $('#tax_type').val(data.tax);
                     $('#tax_gst').val(data.gst_rate);
                     $('#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_expitm.php?id=' + id;
        }
    }
</script>    
</body>

</html>