<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['rm_id'])):
header('Location:../index.php');
endif;
  $tran_no=0;

if (isset($_GET['trn_no'])) { 
  $tran_no= trim($_GET['trn_no']);
}  //// if is set


if (isset($_POST['trn_no'])) { 
$tran_no= trim($_POST['trn_no']);

}


 //echo $tran_no;
if ($tran_no<>0){

  $sqlsp=mysqli_query($con,"SELECT stock_trans_temp_header.* ,stock_locations.loc_name AS floc,s2.loc_name AS tloc
FROM stock_trans_temp_header
LEFT JOIN stock_locations ON stock_locations.loc_id=stock_trans_temp_header.from_loc_id
LEFT JOIN stock_locations AS s2 ON s2.loc_id=stock_trans_temp_header.to_loc_id WHERE tran_id=$tran_no ")or die(mysqli_error($con));
            $count=mysqli_num_rows($sqlsp); 
            if ($count>0) {
              while($rowsp=mysqli_fetch_array($sqlsp)){     
                 $from_loc_id=$rowsp['from_loc_id']; 
                 $to_loc_id=$rowsp['to_loc_id']; 
                 $floc=$rowsp['floc']; 
                 $tloc=$rowsp['tloc'];  
                 $refnote=$rowsp['refnote'];     
             }  

          
            }// if ($count>0) {


}  ////    if ($tran_no<>0){
?>


	<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub | Item Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>



</head>

<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">
  <div class="container-fluid">
              
                <div class="col-12 grid-margin">       
              
               <?php 
if ($tran_no<>0){

 // $grn_ref=$grnd_no;
echo "Trn Ref:"; echo str_repeat("&nbsp;", 1);  echo $tran_no; echo str_repeat("&nbsp;", 5); echo "From Location : "; echo str_repeat("&nbsp;", 1); echo $floc; 
echo str_repeat("&nbsp;", 5); echo "To Location : ";echo str_repeat("&nbsp;",1); 
echo $tloc;
echo str_repeat("&nbsp;", 5); echo "Note : ";echo str_repeat("&nbsp;",1); echo $refnote;
//echo date("Y-m-d",strtotime($grnd_date));
}
  ?>
                 
                  </div>

          
         <div class="col- grid-margin">
              <div class="card">
                <div class="card-body"> 
            



            <div class="row">
            <div class="col-md-4 grid-margin stretch-card">
              <div class="card">
                <div class="card-body" style="background-color:#DCF7B8">                

                  <form class="forms-sample">
                    <div class="form-group">
                      <label for="exampleInputUsername1">Item</label>
                       <input type="hidden" class="form-control" id="tran_no" name="tran_no" value="<?php echo   $tran_no;  ?>" >
                      <select class="form-control select2" style="width: 100%;" name="item_id" id="item_id" required >
           <option value="0">~~~Select Item ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select * from view_item_master  ")or die(mysqli_error());          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['item_id'];?>"><?php echo $rowd['item_name'];?></option>        
          <?php }?>
          </select> 
          <div class="form-group">
          	<div class="result"></div>  
          <div class="result-unit"></div>
         
  </div>
                    </div>
                    <div class="form-group">
                      <label for="exampleInputEmail1">Qty</label>
                      <input type="number" class="form-control" id="qty" name="qty"placeholder="Transfer Qty">
          <input type="hidden" class="form-control" id="lqty" name="lqty">
           <input type="hidden" class="form-control" id="t_loc" name="t_loc" value="<?php echo $from_loc_id; ?>">



                    </div>

                      <div class="form-group">
                      <label for="exampleInputEmail1"> Note</label>
                      <input type="text" class="form-control" id="note" name="note"placeholder="Note">
                    </div>

                    <button type="button" class="btn btn-primary mr-2">Add Item</button>
                    <button class="btn btn-light">Cancel</button>
                   <div class="form-group"> 
                    <table  class="table" id="stock-listing" >  
                      
                        <th class="bg-primary text-white">         
                        <th>Location</th>
                        <th>QOH</th> 
                        </th>
                                 
                      </table>
                  </div>


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




            <div class="col-md-8 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">       
                 <form class="needs-validation" novalidate method="post" action="stock_trans_final_add.php" enctype="multipart/form-data" > 
                    <div class="form-group row">
                      <div class="table-responsive">
                       <input type="hidden" class="form-control" id="tran_no" name="tran_no" value="<?php  echo $tran_no;  ?>">
                      <table  class="table" id="order-listing" >  
                      
                        <th class="bg-primary text-white">         
                        <th>Item</th>
                        <th>Qty</th>  
                        <th>Action</th>
                        </th>
                                 
                      </table>

                      </div>
                    </div>
                    <button type="submit" class="btn btn-primary mr-2">Create Transfer</button>
                    <button class="btn btn-light">Cancel</button>
                  </form>
                </div>
              </div>
            </div>
            </div>
    
   

        </div>  <!----- <div class="content-wrapper">------>
        </div>
        </div>
      </div>
    </div>
          <?php include('../includes/footer.php');?>
  </div>
  </div>

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
  <!-- container-scroller -->

<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
          page_disp();

function page_disp() {
   //var grnd_no = 101;
   //var grnd_no= $("#grnd_no").val();

    var tran_no= $("#tran_no").val();
       // alert(tran_no);
            $.ajax({
                type: 'POST',
                dataType: "json",
                url: 'item_trans1_disp.php',
                        data: {
                        'tran_no' : tran_no,  
                        
                },
                cache: false,
                success: function(result) {
                 //alert(result.length);
                    document.getElementById("qty").value = '';
                     //document.getElementById("costprice").value = '';
                    $("#order-listing tr").remove();

                            $("#item-disp tr").remove();
                          var row = $('<tr class="bg-success text-black"><td width=30%>' + "Item" + '</td><td width=10%>' + "Qty" + '</td><td width=10%>' + "Note" + '</td><td width=10%>' + "Action" + '</td>');
                          $('#order-listing').append(row);





                    for (var i=0; i<result.length; i++) {
                      //alert(result[i].item_id);
                      var itemid=result[i].item_id;
                     // alert(itemid);
                      var row = $('<tr><td id="test">' + result[i].item_name+ '</td><td>' + result[i].tran_qty +  
                        '<td>' + result[i].note +

                       '</td><td> <form method="post" action="#" enctype="multipart/form-data"> <input type="hidden" class="form-control pull-right" id="itemid" name="itemid"  value=' +  itemid +  ' > <input type="hidden" id="trans_no" name="trans_no" value=' +  tran_no +  '> <button type="submit" formaction="item_trans1_removeitem.php" class="btn btn-outline-secondary btn-rounded btn-icon">                        <i class="fas fa-trash text-danger"></i>                      </button></form>'   );

                        $('#order-listing').append(row);
                    }

                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
}

          
   $('#qty').on('change', function(){
 
// var lqty=document.getElementById("lqty").value;
   var lqty= $("#lqty").val();
   var qty= $("#qty").val();
   if (lqty < qty){
    alert("Transfer qty is larger than the location qoh !!!! ");
     // document.getElementById("qty").value=0;


   }
  
 //document.getElementById("qty").value = '';

 });




          ///////End of page refresh

           $("button").click(function(){
                //alert("Please Selectsssss Orderssss Type");
                //////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 //var category = 100;
                 var category = $("#item_id").val();
                 var issue_qty = $("#qty").val();
                 var tran_no= $("#tran_no").val();
                 var note= $("#note").val();
//alert(category);
                 $.ajax({
                type: 'POST',
                 dataType: "json",
                url: 'stock_trans1_ajax.php',
                        data: {
                        'tran_no' : tran_no,  
                        'category' : category , 
                        'issue_qty' : issue_qty ,  
                        'note' : note ,      

                },
                cache: false,
                success: function(result) {
                	//alert("aa");
                //  page_disp();
                  

                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
page_disp();

              });  //// end of  $("button").click(function(){


              ///  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   $('#item_id').on('change', function(){ 
        var item_id = $('#item_id').val();
        var f_loc = $('#t_loc').val();
        
      //alert(f_loc);
       //item_grn1_itemdisp.php
        $.ajax({
                 type: 'POST',
                 dataType: "json",
                 url: 'item_grn1_itemdisp.php',
                        data: {
                        'item_id' : item_id,  
                         },
                cache: false,
                success: function(result1) {
                  // alert(result1[0].loc_id);

 $("#stock-listing tr").remove();
                   var row = $('<tr class="bg-success text-black"><td width=10%>' + "stock_location" + '</td><td width=10%>' + "QOH" +  '</td><td width=10%>' + "Item" +  '</td>');
                 $('#stock-listing').append(row);


                    for (var i=0; i<result1.length; i++) {
                     // alert(result[i].loc_id);
                      var itemid=result1[i].item_id;
                     //alert(itemid);
                     // $("#myTable tr").remove();
                       // alert(result1[i].loc_id);
                        if (result1[i].loc_id == f_loc) {
                                $('#lqty').val(result1[i].loc_qoh); 
                        }

                   // lqty
              //t_loc




  var row = $('<tr class="table-success text-black"><td width=10%>' + result1[i].loc_name+ '</td><td width=10%>' + result1[i].loc_qoh + '</td><td width=10%>' + result1[i].item_name + '</td>'  );

                        $('#stock-listing').append(row);
                    }
                    
                   

                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
   })



              ///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 //document.getElementById("qty").focus();
 // $("#qty").focus();
     // document.getElementById("qty").focus();
    });////end of $( document ).ready(function() {

function validateForm() { 

    //````````route````````````````````````````````````
    if($('#bname').val().trim().length == 0) {
           alert("Please enter the route you visited !!!");               
            return false;
    } ////``````````````````````````````````````````

    



 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //alert("Please Select Order Type");
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })



     
  })

</script>