<?php
 session_start();
 require_once('../connect.php');
 require_once('sql_required.php');

 $datetoday = date('Y-m-d'); 
 
$active_tab = "stockist";
if (!isset($_SESSION['unique_id'])) { ?>
    <script language="javascript">
      window.location.href = '../index.php';
    </script>
<?php } 

$stockist_id = $_GET['stockist_id'];
$sqlStockistDetails = "SELECT * FROM tbl_user_account WHERE unique_id = '$stockist_id'";
$resultStockistDetails = $conn->query($sqlStockistDetails);
$rowStockistDetails = mysqli_fetch_assoc($resultStockistDetails);


$success_messages = '';
if (isset($_POST['save'])) {
  $id = $_POST['id'];
  $quantity = $_POST['quantity'];

  $sqlQuantity = "UPDATE tbl_stockist_inventory SET number_stocks = '$quantity' WHERE id = '$id'";
  if (mysqli_query($conn, $sqlQuantity)) {
    $success_messages = '<div class="alert alert-success text-center mb-0">Successfully restock</div>';
  }
}

if (isset($_POST['remove'])) {
  $id = $_POST['id'];

  $sqlRemove = "DELETE FROM tbl_stockist_inventory WHERE id = '$id'";
  if (mysqli_query($conn, $sqlRemove)) {
  	$success_messages = '<div class="alert alert-success text-center mb-0">Successfully Remove</div>';
  }
}

if (isset($_POST['addProduct'])) {
  $count = count($_POST['product']);

  $number_of_stock = $_POST['number_of_stock'];

  for ($i=0; $i < $count; $i++) { 
    $productID = $_POST["product"][$i];

    $sqlAddnew = "INSERT INTO tbl_stockist_inventory(stockist_id, product_id, number_stocks) VALUES ('$stockist_id', '$productID', '$number_of_stock')";
    if (mysqli_query($conn, $sqlAddnew)) {
      $success_messages = '<div class="alert alert-success text-center mb-0">Successfully Added</div>';
    }
  }
}
?> 
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Dashboard</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="icon" href="images/logo.png" type="image/png" sizes="16x16">
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
  <link rel="stylesheet" href="plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
  <link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/select2/css/select2.min.css">
  <link rel="stylesheet" href="plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="css/style1.css">
  <link rel="stylesheet" type="text/css" href="css/skeleton_loading.css">
<style type="text/css">
  .content-wrapper {
    background: #151e27!important;
  }
  .dashboard-icon {
    font-size: 13px;
    margin-bottom: 15px;
  }
  .info-box {
    border-radius: 10px!important;
  }
  .main-div {
    border-radius: 10px;
    background: #1d2532!important;
  }
  .transaction a {
    color: #808080!important;
  }  

  .transaction a.active {
    background-color: transparent!important;
    border-top: transparent;
    border-left: transparent;
    border-right: transparent;
    border-bottom: 2px solid #ffd700!important;
    color: #ffd700!important;
    font-weight: bold;
  }  

  .transaction a:hover{
    border-bottom: 2px solid #ffd700!important;
    border-top: 0px!important;
    border-left: 0px!important;
    border-right: 0px!important;
  }
  legend {
    font-size: 18px;
    margin-bottom: 0px;
    color: #fff;
  }
  .description{
    font-size: 14px;
    margin-bottom: 0px;
  }
  .title-icon { 
    color: #707c8c!important;
    font-size: 25px;
    padding-top: 14px;
  }
  .page-item.active .page-link {
    background-color: #27313d!important;
    border-color: #27313d!important;
    color: #000;
  }
  .page-item .page-link {
    background-color: #151e27!important;
    border-color: grey!important;
    color: grey!important;
  }
  .form-control {
    border-radius: 10px;
  }
  .form-control:focus {
    border: 1px solid #ffd700!important;
  }
  label {
    color: #fff;
  }
  .btn {
  	border-radius: 10px;
  }
  .alert-success {
    color: #3c763d;
    background-color: #dff0d8;
    border-color: #d6e9c6;
    border-radius: 0;
  }
  .alert-danger {
    color: #a94442;
    background-color: #f2dede;
    border-color: #ebccd1;
    border-radius: 0;
  }
</style>
</head>
<body class="hold-transition sidebar-mini layout-fixed" style="background: #151e27;">
<div id="preloader" class="loading" style="display: none"></div>
<div class="wrapper">
  <nav class="main-header navbar navbar-expand navbar-white" style="background: #151e27; border-bottom: 0; box-shadow: none!important;">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link"><span class="brand-text" style="font-size: 20px;"><span style="font-size: 20px; font-weight: bolder; line-height: 20px; color: #fff;">Stockist Product</span></a>
      </li>
    </ul>
    <ul class="navbar-nav ml-auto">
      <a class="nav-link"><i class="fas fa-search"></i></a>
      <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
    </ul>
  </nav>

<?php include ("navbar.php")?>
<div class="content-wrapper">
  <div class="content-header p-0">
    <div class="container-fluid p-0">
      <span id="message">
        <?php echo $success_messages; ?>
      </span>
    </div>
  </div>
  <section class="content">
    <div class="container-fluid">
      <?php
 
   

if ($accnt_type == '3') {

}else { ?> 


 
<H2 style="color:#fff"><?php echo $rowStockistDetails['firstname'].' '.$rowStockistDetails['lastname']; ?>, PRODUCT</H2>
  <?php include('stockistProductList.php'); ?>
  <?php }?>
</div>
</div>

<script src="dist/js/adminlte.min.js"></script>
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="plugins/jquery-validation/additional-methods.min.js"></script>
<script src="dist/js/pages/dashboard.js"></script>
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="plugins/select2/js/select2.full.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
      localStorage.setItem('activeTab', $(e.target).attr('href'));
  });
  var activeTab = localStorage.getItem('activeTab');
  if(activeTab){
      $('#myTab a[href="' + activeTab + '"]').tab('show');
  }

  $('.removeBtn').click(function() {
  	var value = $(this).attr('data-product-id');
  	$("#productID").val(value);
  });
});
 
$(function () {
  $('.select2').select2({
    theme: 'bootstrap4'
  });
  $("#unassign_table").DataTable({
    "responsive": true,
    "autoWidth": false,
  });
  $('#example2').DataTable({
    "responsive": true,
    "autoWidth": false,
  });
  $(".withanimation").click(function(e) {
      e.preventDefault();
      $(".loading").show();
      var url=$(this).attr("href");
      setTimeout(function() {
          window.location=url;
      }, 500);
  });
});
setTimeout(function(){
  $('#message').html('');
}, 5000);

if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</body>
</html>
