<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/jquery-datepicker.css">
<div class="breadcrumbs">
            <div class="breadcrumbs-inner">
                <div class="row m-0">
                    <div class="col-sm-4">
                        <div class="page-header float-left">
                            <div class="page-title">
                                <h1>Transfer Note</h1>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
                <style>
.content{position: relative;}
.overlay{position: absolute;left: 0; top: 0; right: 0; bottom: 0;z-index: 2;background-color: rgba(255,255,255,0.8);position: fixed;   overflow-y: scroll;}
.overlay-content {
    position: absolute;
    transform: translateY(-50%);
     -webkit-transform: translateY(-50%);
     -ms-transform: translateY(-50%);
    top: 50%;
    left: 15%;
    right: 0;
    text-align: center;
    color: #555; 
    position: fixed; 
   overflow-y: scroll;
}

.tableFixHead          { overflow: auto; }
.tableFixHead thead th { position: sticky; top: 0; z-index: 1; }
</style>  
                  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">                                                   
    <div class="overlay" style="display:none" id="load"><div class="overlay-content"><img src="<?php echo base_url(); ?>assets/images/loader.gif" alt="Loading..." style="width: 60px;"/></div></div>
                    
      
          <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/lib/chosen/chosen.min.css">
        
        <div class="content">
            <div class="animated fadeIn">
                
                <!--div class="row">
                    
                    <div class="col-md-12 center-block">
                    
                     <button class="btn btn-secondary btn-sm" id="po_grn" onclick="show_po();" name="po_grn"> 
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   PO GRN
                                            </button>
                                           
                                            <button class="btn btn-secondary btn-sm" id="d_grn" onclick="show_dgrn();">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Direct GRN
                                            </button>
                </div>
                </div-->
                
                 <div class="space" style="padding-bottom: 10px;"></div>
                
               <div id="po" style="display:none"> </div>
               
               <div id="direct" style="display:none">
                   
                    <div class="row">
                                                    
                                                    <div class="col-sm-3">  
                                                       From Depo
                                                            <select class="form-control standardSelect choosen-select" id="fdpotselect" data-placeholder="Choose a Supplier..." name="fdpotselect" >
    							                                <option value="">Please select </option>
    							                                
                                                             <?php 
                                                             foreach ($frmdepo as $dbagent) {            
                                                              ?>
    							                           <option value="<?php echo $dbagent->dep_id; ?>"><?php echo $dbagent->dep_name; ?></option>
                                                              <?php }?>                                                           
                                                            </select> 
                                                        <input type="hidden" name="dpo_type" value="" id="dpo_type">                          
                                                    </div>
                                                    <div class="col-sm-3">  
                                                        To Depo
                                                            <select class="form-control standardSelect choosen-select" id="tdpotselect" data-placeholder="Choose a Supplier..." name="tdpotselect" >
    							                                <option value=""> Select Depot </option>
                                                             <?php 
                                                             foreach ($dpodetail as $dbagent) {            
                                                              ?>
    							                           <option value="<?php echo $dbagent->dep_id; ?>"><?php echo $dbagent->dep_name; ?></option>
                                                              <?php }?>                                                           
                                                            </select> 
                                                        <input type="hidden" name="dpo_type" value="" id="dpo_type">                          
                                                    </div>
                                                   
                                                    <div class="col-sm-6">  
                                                        Product
                                                            <select class="form-control standardSelect choosen-select" id="dposelect" data-placeholder="Choose a Supplier..." name="dposelect" onchange="selectitem()">
    							                                <option value="0"> Select Item </option>
                                                                                                                      
                                                            </select> 
                                                        <input type="hidden" name="sup_type" value="" id="sup_type">                          
                                                    </div>
                                                    
                                                </div>    
                                                <div class="row">    
                                                    <div class="col-sm-3">  
                                                        Batch
                                                            <select class="form-control standardSelect choosen-select" id="batchselect" data-placeholder="Choose a Batch..." name="batchselect" onchange="selectbatch()">
    							                                <option value="0"> Batch </option>
                                                                                                                      
                                                            </select> 
                                                                      
                                                    </div>
                                                    
                                                    <div class="col-sm-3">
                                                           Stock
                                                           <input type="text" id="mainstock"  name="mainstock" placeholder="" value=""class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" readonly="" /> 
                                                    </div>
                                                    
                                                        <div class="col-sm-3">
                                                           Quantity
                                                           <input type="text" id="tr_qty"  name="tr_qty" placeholder="" value=""class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" /> 
                                                    </div>
                                                    
                                                    
                                                   
                                                    
                                                    <div class="col-sm-2">
                                                           <br>
                                                            <button class="btn btn-secondary btn-sm" id="itemadd" onclick="addrow();">Add</button>
                                                           
                                                    </div>
                                                    <!--div class="col-sm-2">
                                                           <br>
                                                            <button class="btn btn-secondary btn-sm" id="all_trn" onclick="all_trns();">All Transfer</button>
                                                           
                                                    </div-->
                                                    
                                                    <!--div class="col-sm-3" style="text-align: center;">Add New<br>
                                                         
                                                            <button class="btn btn-secondary btn-sm" name="tax" id="new_product" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Product</button> 
                                                            <button class="btn btn-secondary btn-sm" name="Ser" id="add_services" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Services</button>
                                                            <button class="btn btn-secondary btn-sm" name="Ser" id="new_supplier" onclick ="add_data(this.id);" data-backdrop='static' data-keyboard='false'>Supplier</button>
                                                    </div-->
                                           
                                                   
                                                         
                                                     
                                               </div>
                                               <div class="space" style="padding-bottom: 10px;"></div>
                                                 <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 10px;overflow:scroll;height:280px;width:100%;overflow:auto">
                                    										<table id="simple-table" class="table table-bordered tableFixHead">
                                    											<thead class="thead-dark">
                                    												<tr >
                                    													
                                    													
                                    													<th>ID</th>
                                    													<th>Item Code</th>
                                    													<th>Item Description</th>
                                    												
                                    												
                                                                                        <th style="text-align:right; width: 130px;"><span id="cr2_chng" >Quantity</span>  </th>
                                                                                       
                                                                                        <th style="text-align:right; width: 50px;"><span id="cr2_chng">Action</span>  </th>
                                    
                                    													
                                    													
                                    
                                    													
                                    												</tr>
                                    											</thead>
                                            
                                    											<tbody id="show_datad" >
                                    									
                                    								
                                    											</tbody>
                                    										</table>
                                    									</div><!-- /.span -->
                                                                                                            
                                                                                                            
                                    								</div><!-- /.row -->
                                    								
                                    								
                                               
                <div class="row">
                    
                     <div class="col-lg-12" style="padding-bottom: 10px;">
                         
                         <table class="table table-bordered" id="grnitm">
                                
                                <thead class="thead-dark" id="thed"></thead>
                               
                             <tbody id="tb_bdyd">
                           
                        </tbody>
                        </table>
                         
                         </div>
                    
               </div>
               
                    <div class="row">
                                    									<div class="col-lg-6" style="padding-bottom: 10px;">      
                                    
                                                                <div class="row" >
                                                                        
                                       <div class="col-sm-12">
                                                           Reference<br>
                                                           <input type="text" id="refid"  name="refid" placeholder="" value=""class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" /> 
                                                    </div>
                                        
                                    </div>
                                     </div>
                                     
                                     				<div class="col-lg-6" style="padding-bottom: 10px;">      
                                    
                                                                <div class="row" style="float: right; text-align: right;">
                                                                        
                                        <div class="col-md-12 center-block"><br>
                                            <button class="btn btn-secondary btn-sm" id="direct_po">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   Save
                                            </button>
                                           
                                            <button class="btn btn-secondary btn-sm" id="btn_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel
                                            </button>
                                        </div>
                                        
                                    </div>
                                     </div>
                                    </div>
               </div>
                
                
            </div>
        </div>
        
        <div class="clearfix"></div>
        
                     <!--MODAL DELETE-->
         <form>
            <div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop='static' data-keyboard='false'>
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete Product</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                       <strong>Are you sure to delete this record?</strong>
                  </div>
                  <div class="modal-footer">
                    <input type="hidden" name="product_code_delete" id="product_code_delete" class="form-control">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                    <button type="button" type="submit" id="btn_delete" class="btn btn-primary">Yes</button>
                  </div>
                </div>
              </div>
            </div>
            </form>
        <!--END MODAL DELETE-->


<script src="<?php echo base_url(); ?>assets/js/popper.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <script src="<?php echo base_url(); ?>assets/js/jquery-ui.js"></script>
  <script src="<?php echo base_url(); ?>assets/js/lib/chosen/chosen.jquery.min.js"></script>

<script>

$(document).ready(function() {
    document.getElementById("load").style.display = "block";
    show_product();
   
document.getElementById("load").style.display = "none";
});

 function all_trns(){
     var dpo = $( "#fdpotselect option:selected" ).val();
     var todpo = $( "#tdpotselect option:selected" ).val();
             if(dpo =="0") { swal("Depot required"); 
             }else if(todpo == dpo){
                 swal("To Depot required");
             }else{
                 
                     
                  swal({
                              title: "Are you sure?",
                              text: "Transfer, All Items will be transfered",
                              icon: "warning",
                              buttons: true,
                              dangerMode: true,
                            })
                            .then((willDelete) => {
                              if (willDelete) {
                                  
                                  document.getElementById("load").style.display = "block";
                                        $.ajax({
                                        type : "POST",
                                        url  : "<?php echo site_url('all_trnf')?>",
                                        dataType : "JSON",
                                        data : {dpo:dpo,todpo:todpo},
                                        success: function(data){
                                            
                                            swal(data);
                                            show_product();
                                                location.href = "<?php echo site_url('transer')?>/";
                                           
                                        }
                                    });
                                    
                                    document.getElementById("load").style.display = "none";
                              } else {
                                swal("No Changes");
                                document.getElementById("load").style.display = "none";
                              }
                            });
                 
                 
             }
             
             
 
                
                show_product();
 }

    function addrow(){
          
            document.getElementById("load").style.display = "block";
            var qty = $('#tr_qty').val();
            var mainstock = $('#mainstock').val();
            
            var f = document.getElementById("tdpotselect");
            var depo_id = f.options[f.selectedIndex].value;
            
            var g = document.getElementById("fdpotselect");
            var frmdepo_id = g.options[g.selectedIndex].value;
            
            var unt = document.getElementById("dposelect");
            var tritm = unt.options[unt.selectedIndex].value;
            
            var bth = document.getElementById("batchselect");
            var batch = bth.options[bth.selectedIndex].value;
         
             
            if((qty=="")||(qty=="0")){
               swal("Please enter Quantity"); 
               document.getElementById("load").style.display = "none";
            }else if(Number(qty) > Number(mainstock)) {
                swal("Exceed Quantity"); 
                document.getElementById("load").style.display = "none";
            }else if(depo_id == frmdepo_id){
                 swal("To Depot required");
                 document.getElementById("load").style.display = "none";
             }else{
                        $.ajax({
                        type : "POST",
                        url  : "<?php echo site_url('add_tritm')?>",
                        dataType : "JSON",
                        method:'POST',
                        data : {frmdepo_id:frmdepo_id,depo_id:depo_id,tritm:tritm,qty:qty,batch:batch},
                        success: function(data){
                           // document.getElementById("curid").disabled = true;,ser:ser,free:free
                            document.getElementById("tdpotselect").disabled = true;
                            if(data!="Added"){
                                swal(data);
                                document.getElementById("load").style.display = "none";
                            }
                          
                            show_product();
                            $('[name="tr_qty"]').val("");
                            $('[name="mainstock"]').val("");
                            
                          
                           document.getElementById('dposelect').focus();
                            $("#dposelect").trigger("chosen:updated");
                        }
                    });
                    return false;
                    
                    document.getElementById("load").style.display = "none";
            }    
           
        };
        
        //function show all product
    function show_product(){
            document.getElementById("load").style.display = "block";
        //var tempid = $('#temp_id').val();
            $.ajax({
                type  : 'ajax',
                url   : '<?php echo base_url(); ?>gettrfprod',
                async : true,
                method:'POST',
                dataType : 'json',
                success : function(data){
                  console.log(data);
                    var html = '';
                    var i;
                    var tot = 0;
                    var tot_item = 0;
                    for(i=0; i<data.length; i++){
                        //var tot = tot + parseFloat(data[i][4] * data[i][5]);
                       // var tot_item = tot_item + parseFloat(data[i][5]);
                        html += '<tr>'+
                                //'<td>'+(i+1)+'</td>'+
                                '<td>'+(i+1)+'</td>'+
                                '<td>'+data[i][8]+'</td>'+
                                '<td>'+data[i][2]+'</td>'+
                                '<td style="text-align:right">'+data[i][4]+'</td>'+
                                
                                '<td style="text-align:right;">'+
                                '<div class="hidden-sm hidden-xs btn-group">'+
                                ' <button class="btn btn-outline-danger btn-sm" name="tax" id="'+data[i][7]+'" onclick ="del_data(this.id);">X</button>'+
                                    //'<a href="javascript:void(0);" class="btn btn-xs btn-success item_edit" data-product_code="'+data[i].inv_pro_id+'" data-product_price="'+data[i].product_name+'" data-product_prc="'+data[i].unit_price+'" data-qty="'+data[i].qty+'"><i class="ace-icon fa fa-pencil bigger-120"></i></a>'+' '+
                                   // '<a href="javascript:void(0);" class="btn btn-xs btn-danger item_delete" data-product_code="'+data[i].po_itm_id+'"><i class="ace-icon fa fa-trash-o bigger-120"></i></a>'+
                               '</div>'+
                                '</td>'+
                                '</tr>';
                    }
                    $('#show_datad').html(html);
                   // $('[name="inv_total"]').val(tot.toFixed(2));
                    //$('[name="net"]').val(tot.toFixed(2));
                   // $('[name="inv_tot_item"]').val(tot_item);
                   // cal_total(); cal_dis_pc();
                    document.getElementById("load").style.display = "none";
               
                 
            }
 
            });
        }
        
                function del_data(clickedID){
            var product_code = clickedID;
            // alert(product_code);
            $('#Modal_Delete').modal('show');
            $('[name="product_code_delete"]').val(product_code);
        };
        
                  //delete record to database
        $('#btn_delete').on('click',function(){
                //var f = document.getElementById("saleselect");
                //var sales = f.options[f.selectedIndex].value;
            var product_code = $('#product_code_delete').val();
            $.ajax({
                type : "POST",
                url  : "<?php echo site_url('trprodelete_data')?>",
                dataType : "JSON",
                data : {product_code:product_code},
                success: function(data){
                    $('[name="product_code_delete"]').val("");
                    $('#Modal_Delete').modal('hide');
                    show_product();
                }
            });
            return false;
        });

 $( function() {
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
  } );
  
  
    function show_po(){
        $("#po").css("display", "block");
        $("#direct").css("display", "none");
        document.getElementById('po_grn').disabled = true;
        document.getElementById('d_grn').disabled = false;
    }
    function show_dgrn(){
        $("#po").css("display", "none");
        $("#direct").css("display", "block");
        //document.getElementById('po_grn').disabled = false;
       // document.getElementById('d_grn').disabled = true;
    }

    $(document).ready(function(){
     // document.getElementById('po_grn').disabled = true;
      show_dgrn();
    });
    
    jQuery(document).ready(function() {
        jQuery(".standardSelect").chosen({
            disable_search_threshold: 5,
            no_results_text: "Oops, nothing found!",
            width: "100%"
        });
    });
    
            $('#fdpotselect').change(function(){ 
                $('#dposelect option').remove();
                $("#dposelect").trigger("chosen:updated");
       
                var g = document.getElementById("fdpotselect");
                var depo = g.options[g.selectedIndex].value;
                
         if(depo==""){
             swal("Please Select the Depot");
             return;
         }
            document.getElementById("load").style.display = "block";
             $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('depo_product')?>',
                dataType : 'json',
                data : {depo:depo},
                success : function(data){
                            $('#dposelect').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                     
                 var i;
                            for (var i = 0; i < data['item'].length; i++) {
                               $('#dposelect').append($('<option>', {
                                                            value: data['item'][i].item_id,
                                                            text: data['item'][i].itm_name
                                                        }));
                            }
                            document.getElementById("load").style.display = "none";
                             $("#dposelect").trigger("chosen:updated");
                }
                
            }); 
      }); 
      
            function selectitem(){
                $('#batchselect option').remove();
                $('#batchselect').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                $("#batchselect").trigger("chosen:updated");
        
    var code = $( "#dposelect option:selected" ).val();  
    var g = document.getElementById("fdpotselect");
                var depo = g.options[g.selectedIndex].value;
    var qty = 0;
        $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('getstock_batch')?>',
                dataType : 'json',
                data : {code:code,depo:depo},
                success : function(data){
                            
                            var i;
                            for (var i = 0; i < data.length; i++) {
                               $('#batchselect').append($('<option>', {
                                                            value: data[i].batch_id,
                                                            text: data[i].batch
                                                        }));
                            }
                            
                          $("#batchselect").trigger("chosen:updated");
                           // document.getElementById("dposelect").text = '';
                           
                }
               
            }); 
    
   
    
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
      }
      
      function selectbatch(){
        
    var batch = $( "#batchselect option:selected" ).val();  
    var g = document.getElementById("fdpotselect");
                var depo = g.options[g.selectedIndex].value;
    var qty = 0;
        $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('getstock_trnf')?>',
                dataType : 'json',
                data : {batch:batch,depo:depo},
                success : function(data){
                        var qty =  data[0].avl_qty;
                        if(qty==0){
                            swal("No Stock Available on this Item");
                        }else{
                            
                            //document.getElementById("dposelect").text = '';
                           $('[name="mainstock"]').val(qty);
                           document.getElementById('tr_qty').focus();
                        }    
                }
               
            }); 
    
   
    
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
      }
      
            $("#show_datad").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
    
           /* $("#btn_posave").on('click',function() {
        
        var inv = $('#posup_invoice').val();
        var podo = $('#posup_do').val();
        if((!inv) && (!podo)){swal("Invoice number or DO number required"); return;}
        
            var values = $('input[name="po_val[]"]').map(function(){
                   return this.value
            }).get()
            var ids = $('input[name="po_val[]"]').map(function(){
                   return this.id
            }).get()
            
            var p_tot = $('input[name="po_tot[]"]').map(function(){
                   return this.value
            }).get()
            
            var x = 0;
                for (let i = 0; i < ids.length; i++) {
                    if(values[i]==""){
                         x++;
                    }else if (values[i] > p_tot[i]){
                        swal("Cannot exceed Po quantity");
                        return;
                    }
                    
                }
                
                if(ids.length===x){
                    swal("No Values");
                        return;
                } 
                
                
                
            var curn = document.getElementById("poselect");
            var poid = curn.options[curn.selectedIndex].text;
             var poidval = curn.options[curn.selectedIndex].value;
            
            
            
            swal({
                  title: "Are you sure?",
                  text: "Save, Entered Details will be Saved",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                            $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('save_pogrn')?>",
                            dataType : "JSON",
                            data : {poid:poid, values:values, ids:ids,inv:inv,podo:podo,poidval:poidval},
                            success: function(data){
                                
                                swal(data);
                                location.href = "<?php echo site_url('view_grn')?>/";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
                
            
            
    });  */
    
    $("#direct_po").on('click',function() {
        
    
             
            var fdpo = $( "#fdpotselect option:selected" ).val();
             
            var dpo = $( "#tdpotselect option:selected" ).val(); 
            
            var refid = $('#refid').val();
            
            if((!fdpo)||(!dpo)||(fdpo===dpo)) {swal("From and To Depot required"); 
             }else{
               
            swal({
                  title: "Are you sure?",
                  text: "Save, Entered Details will be Saved",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                      
                      document.getElementById("load").style.display = "block";
                            $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('save_trnf')?>",
                            dataType : "JSON",
                            data : {dpo:dpo,fdpo:fdpo,refid:refid},
                            success: function(data){
                                
                                swal(data);
                                document.getElementById("load").style.display = "none";
                                location.href = "<?php echo site_url('transer')?>/";
                               
                            }
                        });
                        
                        document.getElementById("load").style.display = "none";
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
                
             }
            
    });
    
            $("#po_tbl").on('onkeyup','.txt_val',function(){
       var currentRow=$(this).closest("tr");  
         var col1=currentRow.find("td:eq(0)").html();
         alert(crw);
   })
        
    
</script>