<?php
     session_start();
      include 'database/shikanisha.kts.php';
        if(!($_SESSION['type'] == 'Cashier' || $_SESSION['type'] == 'Manager')){
            header("location:logout.php");
}


?>
<!DOCTYPE html>
<html 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 id="shopname"></title>
    <!-- Favicon icon -->
    <link rel="icon" type="./quixlab/image/png" sizes="16x16" href="./quixlab/images/favicon.png">
    <!-- Pignose Calender -->
    <link href="./quixlab/plugins/pg-calendar/css/pignose.calendar.min.css" rel="stylesheet">
    <!-- Chartist -->
    <link rel="stylesheet" href="./quixlab/plugins/chartist/css/chartist.min.css">
    <link rel="stylesheet" href="./quixlab/plugins/chartist-plugin-tooltips/css/chartist-plugin-tooltip.css">
    <!-- Custom Stylesheet -->
    <link href="./quixlab/css/design.css" rel="stylesheet">
    <!-- DataTables with Buttons -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css">

   <link href="./quixlab/js/DataTables/datatables.min.css" rel="stylesheet"/>
   <link href="./quixlab/js/datatables.min.css" rel="stylesheet"/>
   <link href="./quixlab/js/DataTables/Buttons-2.3.6/css/buttons.bootstrap.css" rel="stylesheet"/>
   <link href="./quixlab/js/DataTables/Buttons-2.3.6/css/buttons.bootstrap.min.css" rel="stylesheet"/>
   <link href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.13.4/af-2.5.3/b-2.3.6/b-colvis-2.3.6/b-html5-2.3.6/b-print-2.3.6/date-1.4.0/r-2.4.1/sp-2.1.2/datatables.min.css" rel="stylesheet"/>
   <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.25/datatables.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css"/>


   <link href="./quixlab/js/DataTables/datatables.css" rel="stylesheet"/>
   <link href="./quixlab/js/datatables.css" rel="stylesheet"/>
    <link href="./quixlab/css/buttons.bootstrap.min.css" rel="stylesheet">
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" crossorigin="anonymous" />
   <link href="./plugins/bootstrap-material-datetimepicker/css/bootstrap-material-datetimepicker.css" rel="stylesheet">
 
  <link href="./plugins/clockpicker/dist/jquery-clockpicker.min.css" rel="stylesheet">
 
   <link href="./plugins/bootstrap-datepicker/bootstrap-datepicker.min.css" rel="stylesheet">

 <link href="./plugins/timepicker/bootstrap-timepicker.min.css" rel="stylesheet">
    <link href="./plugins/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">

    <script src="./plugins/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>

 <link href="./quixlab/plugins/sweetalert2/dist/sweetalert2.min.css" rel="stylesheet">

</head>

<body>

    <!--*******************
        Preloader start
    ********************-->
    <div id="preloader">
        <div class="loader">
            <svg class="circular" viewBox="25 25 50 50">
                <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="3" stroke-miterlimit="10" />
            </svg>
        </div>
    </div>
    <!--*******************
        Preloader end
    ********************-->

            
    
    <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">

        <!--**********************************
            Nav header start
        ***********************************-->
        <div class="nav-header">
            <div class="brand-logo" style="background-image: url('./quixlab/images/room.png');">
                
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->

        <!--**********************************
            Header start
        ***********************************-->
        <div class="header">    
            <div class="header-content clearfix">
                
                <div class="nav-control">
                    <div class="hamburger">
                        <span class="toggle-icon"><i class="icon-menu"></i></span>
                    </div>
                </div>
                <div class="header-left">
                    <div class="input-group icons">
                        
                       
                        <div class="drop-down animated flipInX d-md-none">
                            <form action="#">
                                <input type="text" class="form-control" placeholder="Search">
                            </form>
                        </div>
                    </div>
                </div>
                <div class="header-right">
                    <ul class="clearfix">
                       <li class="icons dropdown d-none d-md-flex">
    <a href="javascript:void(0)" class="log-user" data-toggle="modal" data-target="#cartModal">
        <span>Cart</span> <i class="fa fa-shopping-cart f-s-14" aria-hidden="true"></i>
        <?php
        // Show the number of item rows in the cart
        $itemCount = !empty($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
        echo '<span class="badge badge-danger">' . $itemCount . '</span>';
        ?>
    </a>
</li>
                                               
                    </ul>
                </div>
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->

        <!--**********************************
            Header start
        ***********************************-->
   



<div class="nk-sidebar">           
    <?php require 'navbar.php'; ?>
</div>
        <!--**********************************
            Header end ti-comment-alt
        ***********************************-->

        <!--**********************************
            Sidebar start
        ***********************************-->
    
        <!--**********************************
            Sidebar end
        ***********************************-->

        <!--**********************************
            Content body start
        ***********************************-->
        <div class="content-body">

            <div class="container-fluid mt-3">
                 <div class="row">
                    <div class="col-lg-12 col-sm-12" id="vendor">
                        <div class="card gradient-9">
                            <div class="card-body">
                                <h3 class="card-title text-white"></h3>
                                <div class="d-inline-block">
                                    <h2 class="text-white">Request/Dispose</h2>
                                    <p class="text-white mb-0"></p>
                                </div>
                                <span class="float-right display-5 opacity-5"><i class="fa fa-shopping-cart"></i></span>
                            </div>
                        </div>
                    </div>
                  
                       
                    
                    
                      
                   
              </div>
              </div>
              <div class="container-fluid mt-3">

              <div class="row">
                <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" id="stockTakeTable">
                                   <thead>
                                            <tr>
                                                <th>Category</th>
                                                <th>Product Name</th>
                                                <th>System Quantity</th>
                                                <th>New Quantity</th>
                                            </tr>
                                        </thead>
                                    </table>

                                </div>
                                <div class="form-group mt-3">
            <label for="stockTakeDate">Date:</label>
            <input type="date" id="stockTakeDate" class="form-control">
        </div>
        <div class="form-group mt-3">
            <label for="takenBy">Taken By:</label>
            <input type="text" id="takenBy" class="form-control">
        </div>
        <button id="submitStockTake" class="btn btn-primary mt-3">Submit</button>

                            </div>
                        </div>
                   
                </div>
              
                </div>
        </div>
          
 <!-- End of cart modal -->
  
        <!--**********************************
            Content body end
        ***********************************-->
        
        
        <!--**********************************
            Footer start
        ***********************************-->
         <div class="container-fluid mt-3">

              <div class="row">
        <div class="footer">
            <div class="copyright">
                <p>Copyright &copy; Designed & Developed by <a href="https://kakashitech.com">Kakashi Tech</a></p>
            </div>
        </div>
           </div>
      </div>
        <!--**********************************
            Footer end
        ***********************************-->
   
    <!--**********************************
        Main wrapper end
    ***********************************-->
  </div>
    <!--**********************************
        Scripts
***********************************-->
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
 <script src="./quixlab/js/jquery/jquery.min.js"></script>
<!-- Bootstrap and other common scripts -->
<script src="./quixlab/plugins/common/common.min.js"></script>
<script src="./quixlab/js/custom.min.js"></script>
<script src="./quixlab/js/settings.js"></script>
<script src="./quixlab/js/gleek.js"></script>
<script src="./quixlab/js/styleSwitcher.js"></script>

<!-- Charting libraries -->
<script src="./quixlab/plugins/chart.js/Chart.bundle.min.js"></script>
<script src="./quixlab/plugins/circle-progress/circle-progress.min.js"></script>
<script src="./quixlab/plugins/d3v3/index.js"></script>
<script src="./quixlab/plugins/topojson/topojson.min.js"></script>
<script src="./quixlab/plugins/datamaps/datamaps.world.min.js"></script>
<script src="./quixlab/plugins/raphael/raphael.min.js"></script>
<script src="./quixlab/plugins/morris/morris.min.js"></script>
<script src="./quixlab/plugins/moment/moment.min.js"></script>
<script src="./quixlab/plugins/pg-calendar/js/pignose.calendar.min.js"></script>
<script src="./quixlab/plugins/chartist/js/chartist.min.js"></script>
<script src="./quixlab/plugins/chartist-plugin-tooltips/js/chartist-plugin-tooltip.min.js"></script>

<!-- DataTables and related plugins -->
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/searchpanes/2.1.2/js/dataTables.searchPanes.min.js"></script>

<!-- PDFMake library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.66/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.66/vfs_fonts.js"></script>

<!-- Other scripts -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js"></script>
      <script src="./quixlab/js/select2.min.js"></script>

<!-- Your custom scripts -->
<script src="./quixlab/js/dashboard/dashboard-1.js"></script>
<script src="./quixlab/js/plugins-init/chartjs-init.js"></script>
<script src="./quixlab/js/datatables.js"></script>
<script src="./quixlab/js/DataTables/init.js"></script>
    <script src="./plugins/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>


    <script src="./plugins/bootstrap-material-datetimepicker/js/bootstrap-material-datetimepicker.js"></script>
    <script src="./plugins/timepicker/bootstrap-timepicker.min.js"></script>

 <script src="./plugins/bootstrap-daterangepicker/daterangepicker.js"></script>

    <script src="./js/plugins-init/form-pickers-init.js"></script>
         <script src="./quixlab/plugins/sweetalert2/dist/sweetalert2.min.js"></script>

</body>
 <script>
        $(document).ready(function() {
            var stockTakeTable = $('#stockTakeTable').DataTable({
                "ajax": "./database/fetch_stock_data.kts.php",
                "columns": [
                    { "data": "category" },
                    { "data": "name" },
                    { "data": "quantity" },
                    {
                        "data": null,
                        "render": function(data, type, row) {
                            return `<input type="number" class="form-control new-quantity" data-id="${row.product_id}" data-name="${row.name}" data-category="${row.category}" value="${row.quantity}">`;
                        }
                    }
                ]
            });

            $('#submitStockTake').on('click', function() {
                var stockData = [];
                $('.new-quantity').each(function() {
                    stockData.push({
                        product_id: $(this).data('id'),
                        name: $(this).data('name'),
                        category: $(this).data('category'),
                        new_quantity: $(this).val()
                    });
                });

                var stockTakeDate = $('#stockTakeDate').val();
                var takenBy = $('#takenBy').val();

                if (!stockTakeDate || !takenBy) {
                    Swal.fire('Error!', 'Please select a date and enter the name of the person taking the stock.', 'error');
                    return;
                }

                $.ajax({
                    url: './database/update_stock.kts.php',
                    method: 'POST',
                    data: {
                        stock_data: stockData,
                        stock_take_date: stockTakeDate,
                        taken_by: takenBy
                    },
                    dataType: 'json',
                    success: function(response) {
                        if (response.status === 'success') {
                            Swal.fire('Success!', 'Stock updated successfully.', 'success');
                            stockTakeTable.ajax.reload();
                        } else {
                            Swal.fire('Error!', response.message, 'error');
                        }
                    },
                    error: function() {
                        Swal.fire('Error!', 'An error occurred while updating stock.', 'error');
                    }
                });
            });
        });
    </script>
    
    
<script>
    // JavaScript code to fetch data and display it
    function fetchBusinessDetails() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', './database/fetch_business.kts.php', true);
        xhr.onload = function() {
            if (this.status === 200) {
                var data = JSON.parse(this.responseText);
                
                console.log(data);  // Check if contact2 is present in the response
                
                // Check if no error
                if (!data.error) {
                    // Update the span elements with the fetched data
                    document.getElementById('shopname').innerText = data.shop_name || '';
                    document.getElementById('shopname1').innerText = data.shop_name || '';
                    document.getElementById('location').innerText = data.location || '';
                    document.getElementById('quote').innerText = data.quote || '';
                    document.getElementById('street').innerText = data.street || '';
                    document.getElementById('address').innerText = data.address || '';
                    document.getElementById('pin').innerText = data.pin || '';
                    document.getElementById('contact1').innerText = data.contact1 || '';
                    document.getElementById('contact2').innerText = data.contact2 || ''; // This should populate contact2
                } else {
                    console.error("Error: " + data.error);
                }
            } else {
                console.error("Failed to fetch data");
            }
        };
        xhr.send();
    }

    // Call the function as soon as the DOM is fully loaded
    document.addEventListener('DOMContentLoaded', fetchBusinessDetails);
</script>

</html>