<!DOCTYPE html>
<html>
<?php
include('conn.php');
$page='Add individual Items';
?> 
<?php 
if (isset($_GET['re'])) {
$id=$_GET['re'];

}

      ?>

<head>


<?php
include('common/head.php')
?> 
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
<style>
#items {
    height: 100% !important;

}

#unit {
    height: 40px !important;
    max-width: 400px;
}
#panel {
  display: none;
}
#panel1 {
  display: none;
}
</style>

</head>

<?php 


include('conn.php');


if(isset($_POST['submit'])){
 
    $item_name=addslashes($_POST['item_name']);
    $item_names=addslashes($_POST['item_names']);
     $price=$_POST['price'];
    $unit=$_POST['unit'];
    $units=$_POST['units'];
    $unit_value=$_POST['unit_value'];
     $quantity=$_POST['quantity'];
     $brand=$_POST['brand'];
     $datein=$_POST['datein'];
     $location=$_POST['location'];
     $receipt_id=str_replace(' ', '',$_POST['receipt_id']);
     $item_id_new=str_replace(' ', '',$_POST['item_id_new']);
     $item_id=$item_name;
     $by=$_POST['by'];
    
//setting new item when item does not exist
     if ($item_name=='nan') {
        $item_name=$item_names;
        $item_id=$item_id_new;
     }
     if ($unit=='nan') {
        $unit=$units;
     }


 



     $sql10 = "SELECT `item_name` FROM `inventory_cat` WHERE `item_id`='".$item_id."'";
     $result10 = $conn->query($sql10);
     $row10=mysqli_fetch_array($result10);
     
     if($row10 >= 1) {
        $item_name=$row10['item_name'];
     }

  
      





  
     
     $sql = "SELECT `item_id` FROM `inventory_cat` WHERE `item_id`='".$item_id."'";
     $result = $conn->query($sql);
     if($result->num_rows >= 1) {
      
     } else {
		// Image db insert sql
        $sql11 = "INSERT INTO `inventory_cat`(`item_id`, `item_name`,`by`, `date`) VALUES ('$item_id','$item_name','$by','$datein')";
		if(mysqli_query($conn,$sql11)){
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
 
     }

     

     $sql0 = "SELECT `unit` FROM `inventory_unit` WHERE `unit`='".$unit."'";
     $result0 = $conn->query($sql0);
     if($result0->num_rows >= 1) {
      
     } else {
		// Image db insert sql
        $sql1 = "INSERT INTO `inventory_unit`( `unit`, `date`, `by`) VALUES ('$unit','$datein','$by')";
		if(mysqli_query($conn,$sql1)){
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
 
     }

     $sql2 = "INSERT INTO `inventory`( `item_name`,`item_id`,`price`, `unit`, `unit_value`,`brand`, `quantity`, `datein`, `item_location`, `receipt_id`,`by`) VALUES ('$item_name','$item_id','$price','$unit','$unit_value','$brand','$quantity','$datein','$location','$receipt_id','$by')";
     if(mysqli_query($conn,$sql2)){

echo 'Item submitted!!! you can click complete to close the receipt ';
     }
     else{
       echo 'Error: '.mysqli_error($conn);
     }



}

?>



<body>

<?php 
            $today= date("F j, Y ,h:ia");
            $order_id='T'.date('ymdhi').mt_rand(10,100);
            
    

      
            ?>
  <!-- Modal Add users -->
  <div  class="modal-box " >
    <div class="modal-dialog" role="document">
      <div class="modal-content ">
        <div class="modal-header">
          <h5 class="modal-title text-center text-dark" id="exampleModalLabel">Add Item</h5>
          <a href="inventory" type="button" class="close">
            <span aria-hidden="true">&times;</span>
          </a>


        </div>
        <div class="modal-body">


<form method="post"  class="">
    <div class="row">
    <div id="panel" class="form-group col-sm-6 mb-3">
                 <input type="text" class="form-control" name="item_names" id="exampleFormControlInput1" placeholder="Item name" >
               </div>

               <div id="panel1" class="form-group col-sm-6 bg-danger mb-3">
                 <input type="text" class="form-control" name="units" id="exampleFormControlInput1" placeholder="unit"  >
               </div>

            <div  id="select_item" class="input-group col-sm-6  mb-3 ">
        <div class="input-group-prepend">
          <a onclick="myFunction()" class=" btn  btn-primary btn-sm " >+</a>
        </div>
        <?php 
    $query ="SELECT item_name,item_id FROM inventory_cat ";
    $result = $conn->query($query);
    if($result->num_rows> 0){
      $options= mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
?>
                  <select class="form-control items" name="item_name" id="items" style="width: 75%">
                  <option value="nan">Select Item</option>
                  <?php 
  foreach ($options as $option) {
  ?>
    <option value="<?php echo $option['item_id']; ?>"><?php echo $option['item_name']; ?> </option>
    <?php 
    }
   ?>
                  </select>
      </div>



      <div id="select_item1" class="input-group col-sm-6 mb-3">
                <div class="input-group-prepend">
          <a  onclick="myFunctions()" class=" btn  btn-primary  btn-sm" >+</a>
        </div>
        <?php 
    $query1 ="SELECT unit FROM inventory_unit ";
    $result1 = $conn->query($query1);
    if($result1->num_rows> 0){
      $optionse= mysqli_fetch_all($result1, MYSQLI_ASSOC);
    }
?>
                  <select class="form-control unit" name="unit" id="unit">
                  <option value="nan">Select Unit</option>
                  <?php 
  foreach ($optionse as $option1) {
  ?>
    <option><?php echo $option1['unit']; ?> </option>
    <?php 
    }
   ?>
                  </select>
      </div>

      
      <div class="form-group col-sm-4 mb-3">
                 
                 <input type="text" class="form-control" name="unit_value" id="exampleFormControlInput1" placeholder="Unit Quantity"  >
               </div>
                <div class="form-group col-sm-4 mb-3">
                 
                  <input type="number" class="form-control" name="quantity" id="exampleFormControlInput1" placeholder="Quantity" required>
                </div>
                <div class="form-group col-sm-4 mb-3">
                 
                 <input type="text" class="form-control" name="price" id="exampleFormControlInput1" placeholder=" Price per Unit" required>
               </div>
                  <div class="form-group  col-sm-6 mb-3">
                  <input type="text" class="form-control" name="receipt_id" id="exampleFormControlInput1" value="<?php echo $id?> " readonly >
                </div>

                <div class="form-group  col-sm-6 mb-3">
                <select class="form-control" name="location" id="exampleFormControlSelect1">
                  <option value="nan">Select Location</option>
                  <option value="bar">Bar</option>
                  <option value="Restaurant">Restaurant</option>
                </select>
                </div>

                <div class="form-group  col-sm-12 mb-3">
                  <input type="text" class="form-control" name="brand" id="exampleFormControlInput1" placeholder="Product Brand" required >
                </div>
                <input type="hidden" name="item_id_new" value="<?php echo $order_id?>">
                <input type="hidden" name="datein" value="<?php echo $today?>">
                <input type="hidden" name="by" value="<?php echo $by?>">
                  <button class="btn btn-success btn-sm btn-block"  name="submit">Submit</button>
  
                  <a href="inventory" class="btn btn-primary btn-sm btn-block"  name="submit">Completed</a>
</form>



        </div>
        <div class="modal-footer">
        </div>
      </div>
    </div>
  </div>




  </div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
  function matchStart (term, text) {
  if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
    return true;
  }

  return false;
}
 $(function(){
  $(".items").select2({
    theme: "classic", 
  });
 });
 
 $(function(){
  $(".unit").select2({
    theme: "classic",
  });
 }); 
</script>
<script>
function myFunction() {
  document.getElementById("panel").style.display = "block";
  document.getElementById("select_item").style.display = "none";
}

function myFunctions() {
  document.getElementById("panel1").style.display = "block";
  document.getElementById("select_item1").style.display = "none";
}
</script>
</body>
</html>
