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



if (isset($_GET['action'])) {
    if ($_GET['action'] == 'clearall') {
        unset($_SESSION['shop']);
        header('Location: http://localhost/customshop/store.php');
        exit();
    }

    if ($_GET['action'] == 'remove') {
        foreach ($_SESSION['shop'] as $key => $value) {
            if ($value['id'] == $_GET['id']) {
                unset($_SESSION['send'][$key]);
                header('Location: http://localhost/customshop/store.php');
                exit();
            }
        }
    }
}

 

if(isset($_POST['add'])){
    $id    = $_POST['add'];
    $name  = $_POST['name'];
    $company = $_POST['company'];
    $qty   = $_POST['qty'];
    
    // Check if store quantity is available before adding to cart
    $query_check = "SELECT store_quantity FROM products WHERE product_id = ?";
    $stmt_check = mysqli_prepare($conn, $query_check);
    
    if ($stmt_check) {
      
      mysqli_stmt_bind_param($stmt_check, "i", $id);
      mysqli_stmt_execute($stmt_check);
      
      mysqli_stmt_bind_result($stmt_check, $store_quantity);
      mysqli_stmt_fetch($stmt_check);
      
      if ($store_quantity < $qty) {
        // Product is unavailable
        echo "<script>alert('Product with ID $id  the quantity entered is unavailable.');window.location.href='./store.php'; </script>";
        exit();
      }
      
      // Product is available, add to cart
      $session_array = array(
          'id'    =>  $id,
          'name'  =>  $name,
          'company' => $company,
          'qty'   =>  $qty 
      );
          
      if(!empty($_SESSION['send'])){
          $session_array_id = array_column($_SESSION['shop'],'id');
          if(in_array($id, $session_array_id))
          {
              foreach($_SESSION['shop'] as $key => $val){
                  if($_SESSION['shop'][$key]['id']===$id){
                      if(!empty($qty)){
                          $_SESSION['shop'][$key]['qty'] = $_SESSION['shop'][$key]['qty'] + $qty;
                      }
                  }
              }
          }else{
              if(!empty($qty)){
                  $_SESSION['shop'][]= $session_array;
              }
          }
      }else{
          if(!empty($qty)){
              $_SESSION['shop'][]= $session_array;
          }
      }
      
    } else {
      echo "Error: " . mysqli_error($conn);
    }
}

?>
<!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>Rahisi Enterprises</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" href="./quixlab/plugins/tables/css/datatable/dataTables.bootstrap4.min.css">
    <link href="./quixlab/plugins/tables/css/datatable/dataTables.bootstrap4.min.css" rel="stylesheet">
    <link href="./quixlab/css/buttons.bootstrap.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">
                <a href="index.html">
                    <b class="logo-abbr"><img src="./quixlab/images/logo.png" alt=""> </b>
                    <span class="logo-compact"><img src="./quixlab/images/logo-compact.png" alt=""></span>
                    <span class="brand-title">
                        <img src="./quixlab/images/logo-text.png" alt="">
                    </span>
                </a>
            </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="input-group-prepend">
                            <span class="input-group-text bg-transparent border-0 pr-2 pr-sm-3" id="basic-addon1"><i class="mdi mdi-magnify"></i></span>
                        </div>
                        <input type="search" class="form-control" placeholder="Search Dashboard" aria-label="Search Dashboard">
                        <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)" id="storecart">
                                <i class="mdi mdi-cart-outline"></i>
                                <span class="badge badge-pill gradient-12">2</span>
                            </a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->

        <!--**********************************
            Header start
        ***********************************-->
      <div class="nk-sidebar">           
            <div class="nk-nav-scroll">
                <ul class="metismenu" id="menu">
                    <li class="nav-label">Dashboard</li>
                    <li>
                        <a class="has-arrow" href="javascript:void()" aria-expanded="false">
                            <i class="icon-speedometer menu-icon"></i><span class="nav-text">Dashboard</span>
                        </a>
                        <ul aria-expanded="false">
                            <li><a href="index.php">Dashboard</a></li>
                            <!-- <li><a href="./index-2.html">Home 2</a></li> -->
                        </ul>
                    </li>
                    <li class="mega-menu mega-menu-sm">
                        <a href="pos.php" aria-expanded="false">
                            <i class="icon-globe-alt menu-icon"></i><span class="nav-text">Point Of Sales</span>
                        </a>
                      
                    </li>
                    <li>
                        <a href="orders.php" aria-expanded="false">
                             <span class="nav-text">Create Order</span>
                        </a>
                       
                    </li>
                    <li>
                        <a href="payments.php" aria-expanded="false">
                            <i class="icon-screen-tablet menu-icon"></i><span class="nav-text">Payments</span>
                        </a>
                    </li>
                    <li>
                        <a href="reports.php" aria-expanded="false">
                            <i class="icon-graph menu-icon"></i> <span class="nav-text">Reports</span>
                        </a>
                    </li>
                    <li>
                        <a href="reg-customer.php" aria-expanded="false">
                            <i class="icon-grid menu-icon"></i><span class="nav-text">Client Reg</span>
                        </a>
                    
                    </li>
                    <li>
                        <a href="store.php" aria-expanded="false">
                            <i class="icon-badge menu-icon"></i><span class="nav-text">Stores</span>
                        </a>
                    </li>
                    <li>
                        <a href="shop.php" aria-expanded="false">
                            <i class="icon-note menu-icon"></i><span class="nav-text">Shop</span>
                        </a>
                    </li>
                    <li>
                        <a href="employees.php" aria-expanded="false">
                            <i class="icon-menu menu-icon"></i><span class="nav-text"> Employees Info</span>
                        </a>
                        
                    </li>
                    <li>
                        <a href="purchases.php" aria-expanded="false">
                            <i class="icon-notebook menu-icon"></i><span class="nav-text">Purchases</span>
                        </a>
                    </li>
                    <li>
                        <a href="inventory.php" aria-expanded="false">
                            <i class="icon-notebook menu-icon"></i><span class="nav-text">Inventory</span>
                        </a>
                    </li>
                    <li>
                        <a href="expenses.php" aria-expanded="false">
                            <i class="icon-notebook menu-icon"></i><span class="nav-text">Expenses</span>
                        </a>
                    </li>
                    <li>
                        <a href="general.php" aria-expanded="false">
                            <i class="icon-notebook menu-icon"></i><span class="nav-text">General</span>
                        </a>
                    </li>
                    
                </ul>
            </div>
        </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-8 col-sm-6">
                        <div class="card gradient-12" onclick="redirectToPage('inventory.php');">
                            <div class="card-body">
                                <h3 class="card-title text-white" ></h3>
                                <div class="d-inline-block">
                                    <h2 class="text-white">Inventory</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 class="col-lg-4 col-sm-6" id="sendshop">
                        <div class="card gradient-10">
                            <div class="card-body">
                                <h3 class="card-title text-white"></h3>
                                <div class="d-inline-block">
                                    <h2 class="text-white">Send To Store</h2>
                                    <p class="text-white mb-0"></p>
                                </div>
                                <span class="float-right display-5 opacity-5"><i class="fa fa-users"></i></span>
                            </div>
                        </div>
                    </div>
                  </div>
                <div class="row">
                   
                    <div class="col-lg-12 col-sm-6">
                      <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Shop Items</h4>
                                <div class="table-responsive">
                                   <table class="table table-striped table-bordered zero-configuration table-sm" id="product-table">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Product Name</th>
                                                <th>Company</th>
                                                <th>Buying Price</th>
                                                <th>Selling Price</th>
                                                <th>Shop Qty</th>
                                               
                                              
                                            </tr>
                                        </thead>
                                       <tbody></tbody>
                                        <tfoot>
                                            <tr>
                                                <th>#</th>
                                                <th>Product Name</th>
                                                <th>Company</th>
                                                <th>Buying Price</th>
                                                <th>Selling Price</th>
                                                <th>Shop Qty</th>
                                                
                                               
                                            </tr>
                                        </tfoot>
                                    </table>
                               </div>
                            </div>
                        </div>
                    </div>
               </div> 
                 <div class="row">
                   
                    <div class="col-lg-12 col-sm-6">
                      <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Store Items send to shop</h4>
                                <div class="table-responsive">
                                     <table class="table table-striped table-bordered zero-configuration table-sm" id="store-to-store-table">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Product_Code</th>
                                                <th>Product Name</th>
                                                <th>Company</th>
                                                <th>Qty</th>
                                                <th>Date_Send_To_Shop</th>
                                               
                                              
                                            </tr>
                                        </thead>
                                       <tbody></tbody>
                                        
                                    </table>                     
                                 </div>
                            </div>
                        </div>
                    </div>
               </div> 
               

               
                <!-- Send To Shop Modal -->
            <div class="modal fade" id="ShopModal">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">

                        <div class="modal-body">
                        
                              <form class="justify-content-between" method="POST">
                                  <input type="text" class="form-control" id="search_text" name="search_text" placeholder="Search Here">
                                  <button type="submit" class="btn"></button>
                                  <span class="lnr lnr-cross" id="close_search" title="Close Search"></span>
                                  <br/>
                                   <div id="result">
                                     
                                   </div>
				              </form>
                         
                           </div>
                     

                    </div>
                </div>
            </div>
                <!-- End Of Send To Shop Modal -->
                <!-- Start Of Send To shop Items  Modal-->
                <div class="modal fade" id="shopite">
                  <div class="modal-dialog" role="document">
                    <div class="modal-content">
                       <div class="dropdown-content-body-sm">
                                     <div class="card-sm">
                                      <div class="card-body">
                                          <h4 class="card-title">Items Details</h4>
                                          <div class="table-responsive">
                                             <form action="./database/tostore.kts.php" method="post">
                                              <table class="table table-striped table-bordered zero-configuration table-sm" id="bootstrap-data-table-export">
                                                <thead>
                                                    <tr>
                                                        <th scope="col">#</th>
                                                        <th scope="col">Name</th>
                                                        <th scope="col">Company</th>
                                                        <th scope="col">Quantity</th>
                                                        <th scope="col">Action</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                
                                                <?php
                               
                               $total= 0;
                               $amount= 0;   
                               $balance =0;   
                                
                               $output = '';
                               
                               
                               ?>
                                     
                                           
                                       <?php     if (!empty($_SESSION['shop'])){
                                  
                                   foreach($_SESSION['shop'] as $key =>  $value){ ?>
                                            <tr>
                                               
                                              
                                               <td><?php echo $value['id'];?></td>
                                                <td><?php echo $value['name'];?></td>
                                                <td><?php echo $value['company'];?></td>
                                                <td><?php echo $value['qty'];?></td>
                                               
                                                <td>
                                                  <a href="shop.php?action=remove&id=<?php echo $value['id'];?>">
                                                  <button class="btn btn-danger btn-sm" type="button">Remove</button></a>
                                               </td>
                                               
                                            </tr>
                                           <?php 
																		
											} }?>
                                           
                                            </tbody>
                                            <thead>
                                            <tr>
                                                <th scope="col">#</th>
                                                <th scope="col"></th>
                                                <th scope="col"></th>
                                                <th scope="col"></th>
                                                <th scope="col"><a href="shop.php?action=clearall" >
                                                <button type="button" class="btn btn-warning btn-sm" >Clear All</button></a>
                                                </th>
                                            </tr>
                                            <tr>
                                                <th scope="col">#</th>
                                                <th scope="col"></th>
                                                <th scope="col"></th>
                                                <th scope="col"></th>
                                                <th scope="col"><a href="./database/tostore.kts.php" >
                                                <button type="submit" name="tostore" class="btn btn-warning btn-sm" >Send To Store</button></a>
                                                </th>
                                            </tr>
                                           
                                        </thead>
                                            </table>
                                            </form>
                                          </div>
                                      </div>
                                    </div> 
                                </div>
                    </div>
                  </div>
                  
                </div>
                
                <!-- End  of Send to Shop Items Modal -->
                  
            </div>
            <!-- #/ container -->
        </div>
        <!--**********************************
            Content body end
        ***********************************-->
        
        
        <!--**********************************
            Footer start
        ***********************************-->
        <div class="footer">
            <div class="copyright">
                <p>Copyright &copy; Designed & Developed by <a href="https://kakashitech.com">Kakashi Tech</a></p>
            </div>
        </div>
        <!--**********************************
            Footer end
        ***********************************-->
    </div>
    <!--**********************************
        Main wrapper end
    ***********************************-->

    <!--**********************************
        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>

    <!-- Chartjs -->
    <script src="./quixlab/plugins/chart.js/Chart.bundle.min.js"></script>
    <!-- Circle progress -->
    <script src="./quixlab/plugins/circle-progress/circle-progress.min.js"></script>
    <!-- Datamap -->
    <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>
    <!-- Morrisjs -->
    <script src="./quixlab/plugins/raphael/raphael.min.js"></script>
    <script src="./quixlab/plugins/morris/morris.min.js"></script>
    <!-- Pignose Calender -->
    <script src="./quixlab/plugins/moment/moment.min.js"></script>
    <script src="./quixlab/plugins/pg-calendar/js/pignose.calendar.min.js"></script>
    <!-- ChartistJS -->
    <script src="./quixlab/plugins/chartist/js/chartist.min.js"></script>
    <script src="./quixlab/plugins/chartist-plugin-tooltips/js/chartist-plugin-tooltip.min.js"></script>
    <script src="./quixlab/js/dashboard/dashboard-1.js"></script>
    <script src="./quixlab/plugins/chart.js/Chart.bundle.min.js"></script>
    <script src="./quixlab/js/plugins-init/chartjs-init.js"></script>
    <!-- Datatables -->
    <script src="./quixlab/js/dashboard/dashboard-1.js"></script>
    <script src="./quixlab/plugins/tables/js/jquery.dataTables.min.js"></script>
    <script src="./quixlab/plugins/tables/js/datatable/dataTables.bootstrap4.min.js"></script>
    <script src="./quixlab/plugins/tables/js/datatable-init/datatable-basic.min.js"></script>
    
    <script src="./quixlab/js/data-table/dataTables.buttons.min.js"></script>
    <script src="./quixlab/js/data-table/buttons.flash.min.js"></script>
    <script src="./quixlab/js/data-table/jszip.min.js"></script>
    <script src="./quixlab/js/data-table/pdfmake.min.js"></script>
    <script src="./quixlab/js/data-table/vfs_fonts.js"></script>
    <script src="./quixlab/js/data-table/buttons.html5.min.js"></script>
    <script src="./quixlab/js/data-table/buttons.print.min.js"></script>
    <script src="./quixlab/js/data-table/datatables-init.js"></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();
  })    
 </script>
 <script>

    $(document).ready(function(){
        $('#search_text').keyup(function(){
            var txt = $(this).val();
            if (txt != ''){
                $.ajax({
                    url:"./database/fetch.kts.php",
                    method: "post",
                    data:{search:txt},
                    dataType:"text",
                    success:function(data)
                    {
                        $('#result').html(data);
                    }
                });
            }
            else{
                $('#result').html('');
            }
		
		});
    });
		

    
</script>

 <script>
	$(document).ready(function() {
		// Load table data via AJAX
		$.ajax({
			url: './database/get_products.kts.php',
			type: 'GET',
			success: function(response) {
				// Parse the JSON response
				var data = JSON.parse(response);

				// Build the table HTML
				var tableHtml = '';
				for (var i = 0; i < data.length; i++) {
					tableHtml += '<tr>';
					tableHtml += '<td>' + data[i].product_id + '</td>';
					tableHtml += '<td>' + data[i].product_name + '</td>';
					tableHtml += '<td>' + data[i].company + '</td>';
					tableHtml += '<td>' + data[i].buying_price + '</td>';
					tableHtml += '<td>' + data[i].selling_price + '</td>';
					tableHtml += '<td>' + data[i].shop_quantity + '</td>';
					tableHtml += '</tr>';
				}

				// Add the table HTML to the table body
				$('#product-table tbody').html(tableHtml);
			},
			error: function(xhr, status, error) {
				// Handle errors
				console.log(xhr.responseText);
			}
		});
	});
</script>

<script>
	$(document).ready(function() {
		// Load table data via AJAX
		$.ajax({
			url: './database/get_to_store_items.kts.php',
			type: 'GET',
			success: function(response) {
				// Parse the JSON response
				var data = JSON.parse(response);

				// Build the table HTML
				var tableHtml = '';
				for (var i = 0; i < data.length; i++) {
					tableHtml += '<tr>';
					tableHtml += '<td>' + data[i].stid + '</td>';
                    tableHtml += '<td>' + data[i].product_id + '</td>';
					tableHtml += '<td>' + data[i].product_name + '</td>';
					tableHtml += '<td>' + data[i].company + '</td>';
					tableHtml += '<td>' + data[i].quantity + '</td>';
					tableHtml += '<td>' + data[i].sdate + '</td>';
					tableHtml += '</tr>';
				}

				// Add the table HTML to the table body
				$('#store-to-store-table tbody').html(tableHtml);
			},
			error: function(xhr, status, error) {
				// Handle errors
				console.log(xhr.responseText);
			}
		});
	});
</script>
 

<script>
   function redirectToPage(url) {
    window.location.href = url;
    }
  </script>  
      

</body>

</html>