<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>Stock Deduction 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>  
                                                                
    <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="space" style="padding-bottom: 10px;"></div>
                
               <div id="po" style="display:block">
                   
                    
                    <div class="row">
                                                    <div class="col-sm-7">  
                                                        <br>
                                                            <select class="form-control standardSelect choosen-select" id="dposelect" data-placeholder="Choose a Supplier..." name="dposelect" onchange="selectitem()" onclick="selectitem()">
    							                                <option value="0"> Select Item </option>
                                                             <?php 
                                                             foreach ($itemdetail as $dbagent) {            
                                                              ?>
    							                           <option value="<?php echo $dbagent->batch_id; ?>"><?php echo $dbagent->batch." - ".$dbagent->itm_name ; ?></option>
                                                              <?php }?>                                                           
                                                            </select> 
                                                        <input type="hidden" name="sup_type" value="" id="sup_type">                          
                                                    </div>
                                                    
                                                    <div class="col-sm-2">
                                                           Stock Qty
                                                           <input type="text" id="stk_qty"  name="stk_qty" placeholder="" value=""class="col-xs-12 col-sm-12 input-sm form-control-sm form-control" readonly/> 
                                                    </div>
                                                    
                                                    <div class="col-sm-2">
                                                           Return Qty 
                                                           <input type="text" id="ret_qty"  name="ret_qty" placeholder="" value=""class="col-xs-12 col-sm-12 form-control input-sm form-control-sm" onkeyup="chk_val()"/> 
                                                    </div>
                                                    
                                                    <div class="col-sm-1">
                                                          <br>
                                                            <button class="btn btn-secondary btn-sm" id="itemadd" onclick="addrow();">Add</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="po_tbl" class="table table-bordered tableFixHead">
                                    											<thead class="thead-dark">
                                    												<tr>
                                    													
                                    													<th>Batch Code</th>
                                    													<th>Item Description</th>
                                    													<th style="text-align:right"><span id="cr_chng">Return Quantity</span> </th>
                                                                                        <th style="text-align:right; width: 130px">Action</th>
                                    
                                    													
                                    												</tr>
                                    											</thead>
                                            
                                    											<tbody id="show_data" >
                                    									
                                    								
                                    											</tbody>
                                    										</table>
                                    									</div><!-- /.span -->
                                                                          
                                                                           <div class="col-sm-6">
                                                           Reason for Return
                                                           <select class="form-control standardSelect choosen-select" id="posup_invoice" data-placeholder="Choose a Supplier..." name="posup_invoice">
    							                                <option value="0"> Select Reason </option>
                                                             <?php 
                                                             foreach ($ret_res as $dbagent) {            
                                                              ?>
    							                            <option value="<?php echo $dbagent->ret_reason_id; ?>"><?php echo $dbagent->ret_reason; ?></option>
                                                              <?php }?>                                                           
                                                            </select>
                                                    </div>                                  
                                                                                                            
                                    								</div><!-- /.row -->
                                    								
                                    								
                                               
                <div class="row">
                    
                     <div class="col-lg-12" style="padding-bottom: 10px;">
                         
                         <table class="table table-bordered">
                                
                                <thead class="thead-dark" id="thed"></thead>
                               
                             <tbody id="tb_bdy">
                            
                        </tbody>
                        </table>
                         
                         </div>
                    
               </div>
               
                    <div class="row">
                                    									<div class="col-lg-12" style="padding-bottom: 10px;">      
                                    
                                                                <div class="row" style="float: right; text-align: right;">
                                                                        
                                        <div class="col-md-12 center-block">
                                            <button class="btn btn-secondary btn-sm" id="btn_posave">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   Save Return Note
                                            </button>
                                           
                                            <button class="btn btn-secondary btn-sm" id="btn_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel Return Note
                                            </button>
                                        </div>
                                        
                                    </div>
                                     </div>
                                    </div>
                   
               </div>
              
            </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-->
        </div>
        
        
<script src="<?php echo base_url(); ?>assets/js/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/popper.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/lib/chosen/chosen.jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> 

<script>
    
    

    $(document).ready(function(){
      show_product();
    });
    
      jQuery(document).ready(function() {
        jQuery(".standardSelect").chosen({
            disable_search_threshold: 5,
            no_results_text: "Oops, nothing found!",
            width: "100%"
        });
    });
    
    function selectitem(){
        
        document.getElementById("load").style.display = "block";
        var item = $( "#dposelect option:selected" ).val();
        $('[name="ret_qty"]').val("");
        
       // var product_code = $('#product_code_delete').val();
            $.ajax({
                type : "POST",
                url  : "<?php echo site_url('gt_itm_stk')?>",
                dataType : "JSON",
                data : {item:item},
                success: function(data){
                    $('#stk_qty').val(data);
                   // show_product();
                   document.getElementById("load").style.display = "none";
                }
            });
            return false;
    }
    
    function chk_val(){
        var stk = $('#stk_qty').val();
        var qty = $('#ret_qty').val();
        
        if(Number(qty) > Number(stk)){
            swal("Incorrect Quantity");
            return;
        }
    }
    
    $('#poselect').change(function(){ 
       
                var g = document.getElementById("poselect");
                var id = g.options[g.selectedIndex].value;
    
             if(id==0) {
                 swal("Select GRN"); 
                 $('#show_data').html("");
                            $('#posup_do').val("");
                            return;} 
         
            document.getElementById("load").style.display = "block";
             $.ajax({
                 
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('ret_product')?>',
                dataType : 'json',
                data : {po_id:id},
                success : function(data){
              
                            $('#show_data').html(data[0]);
                            $('#posup_do').val(data[2]);
                      
                            document.getElementById("load").style.display = "none";
                }
                
            }); 
      }); 
      
      function addrow(){
          
            
            
                var stk = $('#stk_qty').val();
                var qty = $('#ret_qty').val();
            
            if(Number(qty) > Number(stk)){
                swal("Incorrect Quantity");
                return;
            }
            
            var item = $( "#dposelect option:selected" ).val();
            
             
            if((qty=="")||(qty=="0")){
               swal("Please enter Quantity"); 
            }else if(item=="") {
                swal("No Item Selected"); 
            }else{
                        $.ajax({
                        type : "POST",
                        url  : "<?php echo site_url('add_retitm')?>",
                        dataType : "JSON",
                        method:'POST',
                        data : {qty:qty,item:item},
                        success: function(data){
                       
                            if(data!="Added"){
                                swal(data);
                                
                            }
                          
                            show_product();
                            $('[name="stk_qty"]').val("");
                            $('[name="ret_qty"]').val("");
                            //$('[name="grn_exp"]').val("");
                            
                      
                           document.getElementById('dposelect').focus();
                           //document.getElementById("dposelect").select();
                            $("#dposelect").trigger("chosen:updated");
                        }
                    });
                    return false;
                    
                document.getElementById('dposelect').focus();
                $("#dposelect").trigger("chosen:updated");    
                    
            }    
           
        };
      
    //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(); ?>getretprod',
                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>'+data[i][2]+'</td>'+
                                '<td>'+data[i][3]+'</td>'+
                                '<td style="text-align:right">'+data[i][0]+'</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][4]+'" onclick ="del_data(this.id);">X</button>'+
                                '</div>'+
                                '</td>'+
                                '</tr>';
                    }
                    $('#show_data').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);
        };
        
        function po_cancel(clickedID){
            var ret_id = clickedID;
           
            swal({
                  title: "Are you sure?",
                  text: "Cancel, Return Note will be Cancelled",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                            $.ajax({
                            type : "POST",
                            url  : "<?php echo site_url('ret_canc')?>",
                            dataType : "JSON",
                            data : {inv:inv},
                            success: function(data){
                                swal(data);
                                location.href = "<?php echo site_url('view_return')?>/";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
        }; 
        
        
        
            //delete record to database
        $('#btn_delete').on('click',function(){
             document.getElementById("load").style.display = "block";
            var product_code = $('#product_code_delete').val();
            $.ajax({
                type : "POST",
                url  : "<?php echo site_url('retprodelete')?>",
                dataType : "JSON",
                data : {product_code:product_code},
                success: function(data){
                    $('[name="product_code_delete"]').val("");
                    $('#Modal_Delete').modal('hide');
                    document.getElementById("load").style.display = "block";
                    show_product();
                    document.getElementById("load").style.display = "none";
                }
            });
            return false;
        });      
      
    $("#btn_posave").on('click',function() {
        
        document.getElementById("load").style.display = "block";
        
        
            if ($('#show_data tr').length == 0) {
                swal("No data");
                    return;
                    }
                    
             var inv = $( "#posup_invoice option:selected" ).val();
             if(inv==0) {swal("Select Reason for Return"); return;}
                
            
            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_return')?>",
                            dataType : "JSON",
                            data : {inv:inv},
                            success: function(data){
                                swal(data);
                                location.href = "<?php echo site_url('view_return')?>/";
                                document.getElementById("load").style.display = "none";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
                
            
            
    });
    
    $("#btn_cancel").on('click',function() {
        $.ajax({
                        type : "POST",
                        url  : "<?php echo site_url('remove_all_rtn')?>",
                        dataType : "JSON",
                        method:'POST',
                        success: function(data){
                            swal("Done");
                            show_product();
                        }
                    });
                    return false;
    });
    
    
   $("#po_tbl").on('onkeyup','.txt_val',function(){
       var currentRow=$(this).closest("tr");  
         var col1=currentRow.find("td:eq(0)").html();
         alert(crw);
   })
   
  
        
    
</script>