<?php
     session_start();
     include_once'database/conn.inc.php';
        if($_SESSION['type'] != 'Manager'){
    
            header('location:page-login.php');
}
?><!DOCTYPE html>
<html class="h-100" lang="en">

<head>
  
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>G-TECH SOLUTIONS</title>
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="images/favicon.png">
    <!-- Pignose Calender -->
    <link href="./plugins/pg-calendar/css/pignose.calendar.min.css" rel="stylesheet">
    <!-- Chartist -->
    <link rel="stylesheet" href="./plugins/chartist/css/chartist.min.css">
    <link rel="stylesheet" href="./plugins/chartist-plugin-tooltips/css/chartist-plugin-tooltip.css">
    <link rel="stylesheet" href="./plugins/tables/css/datatable/dataTables.bootstrap4.min.css">
    <!-- Custom Stylesheet -->
    <link href="css/style.css" rel="stylesheet">
    <link href="./plugins/tables/css/datatable/dataTables.bootstrap4.min.css" rel="stylesheet">
    <link href="./plugins/sweetalert2/dist/sweetalert2.min.css" rel="stylesheet">
    



<style>
    body {
            background-image: url("green-hnd.webp");
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            height: 100vh;
        }
  </style>
    
    
     <style>
        .tabz {
            background-color: rgba(255, 255, 255, 0.8); /* Adjust the alpha (opacity) value as needed */
        }
    </style>
    
</head>

<body class="h-250">
  <div id="main-wrapper">  
    <!--*******************
        Preloader start
    ********************-->
   
    <!--*******************
        Preloader end
    ********************-->

    


          <!--**********************************
                    Header start
          ***********************************-->
<div class="header">    
    <div class="header-content clearfix">           
        <nav class="navbar navbar-expand-lg navbar-light">
            <a class="navbar-brand" href="#"></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="#" onclick="openModal('vendite')">Add Product</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">Exit</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>
</div>


  
                 
                <br>                  
        
        
        <!--**********************************
            Header end 
        ***********************************-->
 
   
    <div class="login-form-bg h-250">
        <div class="container h-250">
            <div class="row justify-content-center h-250">
                <div class="col-md-12">
                    <div class="card tabz">
                            <div class="card-body">
                                <h4 class="card-title">Product Details</h4>
                                <div class="table-responsive">
                                   <table class="table table-striped table-bordered display table-sm items-table" id="myTable">
    <thead>
        <tr>
            <th>#</th>
            <th>Product Name</th>
            <th>Category</th>
            <th>Buying Price</th>
            <th>Retail Price</th>
            <th>Wholesale Price</th>
            <th>Quantity</th>
            <th>Tax Class</th>
            <th>Update Action</th>
            <th>Delete Action</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

                                </div>
                            </div>
                        </div>
                   
                </div>
                    


            </div>
                            
            
                
                
                
                  <!-- Modal -->
            <div class="modal fade show" id="update-product-modal" tabindex="-1" role="dialog" aria-labelledby="updateProductModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="updateProductModalLabel">Update Product</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <form id="update-product-form">
                                <input type="hidden" id="product-id-input" name="product-id-input">
                                <div class="form-row">
                                  <div class="form-group col-md-4">
                                    <label for="product-name-input">Product Name</label>
                                    <input type="text" class="form-control" id="product-name-input" name="product-name-input">
                                  </div>
                                <div class="form-group col-md-4">
                                    <label for="company-input">Category</label>
                                    <input type="text" name="category" id="categorie" class="form-control" required>
                                 </div>
                                <div class="form-group col-md-4">
                                    <label for="buying-price-input">Buying Price</label>
                                    <input type="number" class="form-control" id="buying-price-input" name="buying-price-input">
                                </div>
                              </div>
                               <div class="form-row">
                                <div class="form-group col-md-4">
                                    <label for="selling-price-input">Retail Price</label>
                                    <input type="number" class="form-control" id="selling-price-input" name="selling-price-input">
                                </div>

                                <div class="form-group col-md-4">
                                    <label for="shop-quantity-input">Available Quantity</label>
                                    <input type="number" class="form-control" id="shop-quantity-input" name="shop-quantity-input" disabled>
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="shop-quantity-input">Quantity Received</label>
                                    <input type="number" class="form-control" id="new-quantity-input" name="new-quantity-input">
                                </div>
                              </div> 
                              <div class="form-row">
                              
                                <div class="form-group col-md-6">
                                    <label for="shop-quantity-input">Whole Price</label>
                                    <input type="number" class="form-control" id="new-whole-price" name="new-whole-price">
                                </div>

                             
                                <div class="form-group col-md-6">
                                  <label for="shop-quantity-input">Tax Class</label>
                                   <select class="form-control" id="new-tax" name="new-tax" required>
                                                <option value="">Select Class</option>
                                                <option value="A">A</option>
                                                <option value="B">B</option>
                                                <option value="C">C</option>

                                                <!-- Add more options as needed -->
                                            </select>
                                </div>

                              </div>
                                <button type="submit" class="btn btn-primary">Update Product</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

                
                
                <!-- add new products modal -->
                 <div class="modal fade show" id="vendite">
                  <div class="modal-dialog" role="document">
                    <div class="modal-content">
                       <div class="dropdown-content-body-sm">
                                    <div class="card">
                                       <div class="card-body">
                                                               
                                <p></p>
                                <h4 class="card-title">Add New Drink</h4>
                                <p></p>
                                
                                <div></div>
                                <div class="basic-form">
                                    <form  id="product-form">
                                        <div class="form-row">
                                            <div class="form-group col-md-6">
                                                <label>Drink Name</label>
                                                <input type="text" class="form-control" placeholder="Drink Name" id="product_name" name="product_name" required>
                                            </div>
                                            <div class="form-group col-md-6">
                                                <label>Buying Price</label>
                                                <input type="number" class="form-control" placeholder="Buying Price"   id="buying_price" name="buying_price" required>
                                            </div>
                                           </div>
                                           <div class="form-row">
                                            
                                            <div class="form-group col-md-12">
                                            <label for="category">Category</label>
                                            <select class="form-control" id="category" name="category" required>
                                                <option value="">Select a category</option>
                                                <option value="Wine">Wine</option>
                                                <option value="Spirit">Spirit</option>
                                                <option value="Vodka">Vodka</option>
                                                <option value="Gin">Gin</option>
                                                <option value="Soda">Soda</option>
                                                <option value="Whiskey">Whiskey</option>
                                                <option value="Energy Drink">Energy Drink</option>
                                                <option value="Beer">Beer</option>
                                                
                                                <!-- Add more options as needed -->
                                            </select>
                                        </div>

                                            
                                        </div>
                                            <div class="form-row">
                                            <div class="form-group col-md-6">
                                                <label>Retail Price</label>
                                                <input type="number" class="form-control" placeholder="Retail Price"  id="selling_price" name="selling_price"required>
                                            </div>
                                            <div class="form-group col-md-6">
                                                <label>Whole Price</label>
                                                <input type="number" class="form-control" placeholder="Wholesale Price"   id="whole-price"  name="whole-price" >
                                            </div>
                                            
                                        </div>
                                        <div class="form-row">
                                            
                                                
                                          
                                            <div class="form-group col-md-6">
                                                <label>Quantity</label>
                                                <input type="number" class="form-control" placeholder="Quantity" id="shop_quantity" name="quantity" required>
                                                <input type="hidden" class="form-control" placeholder="Quantity" id="store_quantity" name="store_quantity"  value="0" >
                                            </div>
                                            <div class="form-group col-md-6">
                                            <label for="category">Tax Class</label>
                                            <select class="form-control" id="tax-class" name="tax-class" required>
                                                <option value="">Select Class</option>
                                                <option value="A">A</option>
                                                <option value="B">B</option>
                                                <option value="C">C</option>

                                                <!-- Add more options as needed -->
                                            </select>
                                        </div>
                                            
                                        </div>
                                         
                                        <button type="submit" class="btn btn-dark">Submit</button>
                                         <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                    </form>
                                    <form id="csv-upload-form" method="POST" enctype="multipart/form-data">
    <div>
        <label for="csv-file">Select CSV File:</label>
        <input type="file" id="csv-file" name="csv_file" accept=".csv" required>
    </div>
    <br>
    <button type="submit" class="btn btn-info">Import Data</button>
</form>
                                </div>
                            </div>
                          </div>
                       </div>
                    </div>
                  </div>
                </div>
    </div>
    </div>
       
  </div>

    

    <!--**********************************
        Scripts
    ***********************************-->
     <script src="jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="plugins/common/common.min.js"></script>
<script src="js/custom.min.js"></script>
<script src="js/settings.js"></script>
<script src="js/gleek.js"></script>
<script src="js/styleSwitcher.js"></script>
<!-- Circle progress -->
<script src="./plugins/circle-progress/circle-progress.min.js"></script>

<!-- Datamap -->
<script src="./plugins/d3v3/index.js"></script>
<script src="./plugins/topojson/topojson.min.js"></script>

<!-- Morrisjs -->
<script src="./plugins/raphael/raphael.min.js"></script>
<script src="./plugins/morris/morris.min.js"></script>

<!-- Pignose Calendar -->
<script src="./plugins/moment/moment.min.js"></script>
<script src="./plugins/pg-calendar/js/pignose.calendar.min.js"></script>

<!-- ChartistJS -->
<script src="./plugins/chartist/js/chartist.min.js"></script>
<script src="./plugins/chartist-plugin-tooltips/js/chartist-plugin-tooltip.min.js"></script>

<!-- DataTables -->
<script src="js/data-table/datatables.min.js"></script>
<script src="js/data-table/dataTables.buttons.min.js"></script>
<script src="js/data-table/buttons.flash.min.js"></script>
<script src="js/data-table/jszip.min.js"></script>
<script src="js/data-table/pdfmake.min.js"></script>
<script src="js/data-table/vfs_fonts.js"></script>
<script src="js/data-table/buttons.html5.min.js"></script>
<script src="js/data-table/buttons.print.min.js"></script>
<script src="js/data-table/datatables-init.js"></script>

<!-- SweetAlert2 -->
<script src="plugins/sweetalert2/dist/sweetalert2.min.js"></script>

<!-- Your Custom Scripts -->



    <script>
  function openModal(modalId) {
    $('#' + modalId).modal('show');
  }
</script>
    
     <script>
 //Add Stock Modal 
 $('#addstock').click(function(){
   $('#basicModal').modal();
 });
//Send stock to Shop  
 $('#sendshop').click(function(){
   $('#ShopModal').modal();
 });
// Items To Send
  $('#storecart').click(function(){
    $('#shopite').modal();
  });    
// Add Vendor
  $('#vendor').click(function(){
    $('#vendite').modal();
  });    
 </script>

<script>
    $(document).ready(function() {
    $('#product-form').on('submit', function(event) {
        event.preventDefault();

        var formData = $(this).serialize();

        $.ajax({
            url: 'database/addproducts.inc.php',
            type: 'POST',
            data: formData,
            success: function(response) {
                Swal.fire({
                    type: 'success',
                    title: 'Success!',
                    text: response, // Display the message received from the server
                });

                $('#product-form')[0].reset();
            },
            error: function(xhr, status, error) {
                Swal.fire({
                    type: 'error',
                    title: 'Error!',
                    text: 'An error occurred while processing your request.',
                });
            }
        });
    });
});

</script>
  <script>
    $(document).ready(function() {
        $('#myTable').DataTable({
            ajax: {
                url: 'database/get_products.kts.php', // Replace with the correct path to your PHP script
                type: 'GET',
                dataSrc: 'data' // The property name DataTables expects for the data array
            },
            columns: [
                { data: 'product_id' },
                { data: 'product_name' },
                { data: 'category' },
                { data: 'buying_price' },
                { data: 'selling_price' },
               
                { data: 'whole_price' },
               { data: 'quantity' },
                { data: 'tax_class' },
                // Add more columns as needed
               {
        // Update button column
        render: function(data, type, row) {
          return '<button class="btn btn-primary update-product" data-product-id="' + row.product_id + '">Update</button>';
        }
      },
      {
        // Delete button column
        render: function(data, type, row) {
          return '<button class="btn btn-danger delete-product" data-product-id="' + row.product_id + '">Delete</button>';
        }
      }
            ]
        });
    });
</script>
    <script>
$(document).ready(function() {
    // Load table data via AJAX

    // Update button click handler
    $('.items-table tbody').on('click', '.update-product', function() {
        var productId = $(this).data('product-id');

        // Make an AJAX call to retrieve the current product details
        $.ajax({
            url: './database/get_products.kts.php',
            type: 'POST',
            data: { 'product_id': productId },
            success: function(response) {
                // Check if response is already an object
                var products = (typeof response === 'object') ? response : JSON.parse(response);

                $('#product-id-input').val(products.product_id);
                $('#product-name-input').val(products.product_name);
                $('#categorie').val(products.category);
                $('#buying-price-input').val(products.buying_price);
                $('#selling-price-input').val(products.selling_price);
                $('#shop-quantity-input').val(products.quantity);
                $('#new-whole-price').val(products.whole_price);
                $('#new-distri-price').val(products.distri_price);
                $('#new-tax').val(products.tax_class);
                
                // Display the modal
                $('#update-product-modal').modal('show');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error(errorThrown);
            }
        });
    });

    // Delete button click handler
  $('.items-table tbody').on('click', '.delete-product', function() {
    var productId = $(this).data('product-id');

    // Show a confirmation dialog using SweetAlert2
    Swal.fire({
        title: 'Are you sure?',
        text: 'You won\'t be able to revert this!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            // User confirmed the deletion, make an AJAX call to delete the product
            $.ajax({
                url: './database/delete_product.kts.php',
                type: 'POST',
                data: { 'product_id': productId },
                success: function(response) {
                    // Refresh the table data
                    table.ajax.reload();

                    // Show a success message using SweetAlert2
                    Swal.fire({
                        type: 'success',
                        title: 'Product deleted successfully!',
                        showConfirmButton: false,
                        timer: 1500
                    });
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.error(errorThrown);
                }
            });
        }
    });
});

});


</script>
<script>
    $(document).ready(function() {
        // Add event listener to form submit button
        $('#update-product-form').on('submit', function(event) {
            event.preventDefault(); // Prevent default form submission behavior

            // Serialize form data
            var formData = $(this).serialize();

            // Send AJAX request to server
            $.ajax({
                url: './database/updateproduct.kts.php', // PHP script to handle the request
                type: 'POST',
                data: formData,
                success: function(response) {
                    // Display a SweetAlert2 success message
                    Swal.fire({
                        type: 'success',
                        title: 'Success!',
                        text: response,
                    }).then(function() {
                        // Clear the form input fields and reload the page
                        $('#update-product-form')[0].reset();
                        location.reload();
                    });
                },
                error: function(xhr, status, error) {
                    // Display a SweetAlert2 error message
                    Swal.fire({
                        type: 'error',
                        title: 'Error!',
                        text: 'An error occurred while processing your request.',
                    });
                }
            });
        });
    });
</script>


<script>
        $(document).ready(function() {
            $('#csv-upload-form').on('submit', function(event) {
                event.preventDefault(); // Prevent default form submission behavior

                var formData = new FormData(this); // Create a FormData object

                $.ajax({
                    url: 'database/process_csv.kts.php', // PHP script to handle the request
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(response) {
                        alert(response);
                        $('#csv-upload-form')[0].reset();
                    },
                    error: function(xhr, status, error) {
                        console.log(xhr.responseText);
                    }
                });
            });
        });
    </script>


   
  <script>
    function searchitems(keyword) {
        // Make an AJAX request to search for items by name
        $.ajax({
            url: 'database/search_pr.kts.php',
            method: 'POST',
            data: { keyword: keyword },
            success: function (response) {
                // Show the search results in a dropdown
                $('#itemsDropdown').html(response);
                $('#itemsDropdown').show();
            },
            error: function (xhr, status, error) {
                console.error(xhr);
            }
        });
    }

    // Listen for changes to the "Refil From" input
    $('input[name="start"]').on('input', function () {
        var keyword = $(this).val();
        if (keyword.length > 0) {
            // Search for items
            searchitems(keyword);
        } else {
            // Hide the search results dropdown
            $('#itemsDropdown').hide();
        }
    });

    // Listen for clicks on a search result for "Refil From"
    $(document).on('click', '.item-result', function () {
        // Get the item ID and name
        var itemId = $(this).data('id');
        var itemName = $(this).data('name');

        // Update the "Refil From" input and hide the search results dropdown
        $('input[name="start"]').val(itemName);
        $('#itemsDropdown').hide();

        // Update the hidden item ID input
        $('#item-id').val(itemId);
    });
</script>
    
<script>
    function searchproduct(keyword) {
        // Make an AJAX request to search for products by name
        $.ajax({
            url: 'database/search_it.kts.php',
            method: 'POST',
            data: { keyword: keyword },
            success: function (response) {
                // Show the search results in a dropdown
                $('#productsDropdown').html(response);
                $('#productsDropdown').show();
            },
            error: function (xhr, status, error) {
                console.error(xhr);
            }
        });
    }

    // Listen for changes to the "Refil To" input
    $('input[name="stop"]').on('input', function () {
        var keyword = $(this).val();
        if (keyword.length > 0) {
            // Search for products
            searchproduct(keyword);
        } else {
            // Hide the search results dropdown
            $('#productsDropdown').hide();
        }
    });

    // Listen for clicks on a search result for "Refil To"
    $(document).on('click', '.product-result', function () {
        // Get the product ID and name
        var productId = $(this).data('id');
        var productName = $(this).data('name');

        // Update the "Refil To" input and hide the search results dropdown
        $('input[name="stop"]').val(productName);
        $('#productsDropdown').hide();

        // Update the hidden product ID input
        $('#pr-id').val(productId);
    });
</script>

<script>
$(document).ready(function () {
    // Add a click event listener to the "Refil Now" button
    $('#refilButton').click(function () {
        // Collect the form data
        var formData = {
            start: $('#sp').val(),
            stop: $('#spn').val(),
            weight: $('#weight').val(),
            item_id: $('#item-id').val(),
            pr_id: $('#pr-id').val(),
        };

        // Make an AJAX request to submit the form data
        $.ajax({
            type: 'POST',
            url: 'database/refil.kts.php', // Replace with the URL of your server-side script
            data: formData,
            success: function (response) {
                if (response.success) {
                    // Show a success SweetAlert
                    Swal.fire({
                        icon: 'success',
                        title: 'Success',
                        text: response.message,
                    }).then(function () {
                        // Reload the page after the SweetAlert is closed
                        location.reload();
                    });
                } else {
                    // Show an error SweetAlert
                    Swal.fire({
                        icon: 'error',
                        title: 'Error',
                        text: response.message,
                    });
                }
            },
            error: function (xhr, status, error) {
                // Handle any AJAX errors here
                console.error(xhr);
            }
        });
    });
});
          
</script>
	
</body>
</html>





