<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>Edit Invoice Return</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>
                                                   GRN <?php echo $id  ?>
                                            </button>
                </div>
                </div>
                
                 <div class="space" style="padding-bottom: 10px;"></div>
                
               <div id="po" style="display:none">
                   
                    <div class="row">
                    
                                                     
                                               </div>
                                               <div class="space" style="padding-bottom: 10px;"></div>
                       
               
                    <div class="row">
                                   
                                    </div>
                   
               </div>
               
               <div id="direct" style="display:none">
                   
                    <div class="row">
                                                     
                                                         
                                                     
                                               </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>Item Code</th>
                                    													<th>Item Description</th>
                                    													<th style="text-align:left">Buying Price</th>
                                                                                        <th style="text-align:right; width: 130px;"><span id="cr2_chng" >Recived Quantity</span>  </th>
                                                                                        <th>Expire Date</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-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="direct_po">
                                                    <i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>
                                                   Save GRN
                                            </button>
                                           
                                            <button class="btn btn-secondary btn-sm" id="direct_cancel">
                                                    <i class="ace-icon fa fa-times red2"></i>
                                                    Cancel GRN
                                            </button>
                                        </div>
                                        
                                    </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 class="clearfix"></div>


<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>

 $( 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();
      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("buy_price").focus();
        }
      
      
      $("#buy_price").keyup(function(event) {
            if (event.keyCode === 13) {
                document.getElementById("grn_qty").focus();
            }
        });
        
        $("#grn_qty").keyup(function(event) {
            if (event.keyCode === 13) {
                document.getElementById("grn_exp").focus();
            }
        });
        
        $("#grn_exp").keyup(function(event) {
            if (event.keyCode === 13) {
                document.getElementById("itemadd").focus();
            }
        });

      function selectitem1(){
          
    var code = $( "#dposelect option:selected" ).val();      
    var personName = $( "#dposelect option:selected" ).text();
    const codeonly = personName.split("|", 1);
    const desonly = personName.split("|");
    var newRow = document.createElement("tr");
    
    var id = document.createElement("td");
    var ItemCode = document.createElement("td");
    var ItemDescription = document.createElement("td");
    var buyprice = document.createElement("td");
    var Unit = document.createElement("td");
    var Recived = document.createElement("td");
    var action = document.createElement("td");
    var expdt = document.createElement("td");
    
    ItemDescription.innerHTML = desonly[1];
    ItemCode.innerHTML = codeonly;
    buyprice.innerHTML = '<input type="text" style="text-align: right;" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control" name="dpo_price[]" id="pr'+code+'">';
    Unit.innerHTML = desonly[2] ;
    Recived.innerHTML = '<input type="text" style="text-align: right;" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control" name="dpo_qty[]" id="'+code+'">';
    action.innerHTML = '<a href="javascript:void(0);" class="remCF removeBtn btn btn-outline-danger btn-sm">X</a>'; 
    expdt.innerHTML = '<input type="text" style="text-align: right;" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control datepicker" name="exp_date[]" id="dt'+code+'">';
    
    newRow.append(ItemCode,ItemDescription,buyprice,Unit,Recived,expdt,action);
    document.getElementById("show_datad").appendChild(newRow);
    document.getElementById("dposelect").text = '';
    
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
    
   
      }
      
      //function show all product
    function show_product(){
            document.getElementById("load").style.display = "block";
        
            var id = "<?php echo $id; ?>";
   
            $.ajax({
                type  : 'ajax',
                url   : '<?php echo base_url(); ?>get_invrtn_edit',
                async : true,
                method:'POST',
                data  : {id:id},
                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][6]+"-"+data[i][0]+"-"+data[i][1]+'</td>'+
                                '<td>'+data[i][2]+'</td>'+
                                '<td style="text-align:right">'+data[i][3]+'</td>'+
                                '<td style="text-align:right">'+data[i][4]+'</td>'+
                                '<td style="text-align:right">'+data[i][5]+'</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>'+
                                ' <button class="btn btn-outline-danger btn-sm" name="tax" id="'+data[i][7]+'" onclick ="edit_data(this.id);">Edit</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('grnproduct_delete')?>",
                dataType : "JSON",
                data : {product_code:product_code},
                success: function(data){
                    $('[name="product_code_delete"]').val("");
                    $('#Modal_Delete').modal('hide');
                    show_product();
                }
            });
            return false;
        });  
      
      
      $("#show_datad").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
    
    
    $("#direct_po_old").on('click',function() {
        
        
        
            var values = $('input[name="dpo_qty[]"]').map(function(){
                   return this.value
            }).get()
            var ids = $('input[name="dpo_qty[]"]').map(function(){
                   return this.id
            }).get()
            
             var dpo_price = $('input[name="dpo_price[]"]').map(function(){
                   return this.value
            }).get()
            
            var exp_date = $('input[name="exp_date[]"]').map(function(){
                   return this.value
            }).get()
            
            var inv = $('#dsup_invoice').val();
            var podo = $('#dsup_do').val();
            // if((!inv) && (!podo)){swal("Invoice number or DO number required"); return;}
             
             var x = 0;
                for (let i = 0; i < ids.length; i++) {
                    if(values[i]==""){
                         x++;
                    }
                    
                }
                
                if((ids.length===x) || (dpo_price=="")){
                    swal("No Values");
                        return;
                      
                } 
                
             document.getElementById("load").style.display = "block";
            
            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_dirgrn')?>",
                            dataType : "JSON",
                            data : {values:values, ids:ids,inv:inv,podo:podo,dpo_price:dpo_price,exp_date:exp_date},
                            success: function(data){
                                
                                swal(data);
                                document.getElementById("load").style.display = "none";
                                location.href = "<?php echo site_url('view_grn')?>/";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
                
            
            
    });
    
    $("#direct_po").on('click',function() {
                if ($('#show_datad tr').length == 0) {
                    return;
                }

                
             document.getElementById("load").style.display = "block";
            
            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_grn')?>",
                            dataType : "JSON",
                            success: function(data){
                                
                                swal(data);
                                document.getElementById("load").style.display = "none";
                                location.href = "<?php echo site_url('view_grn')?>/";
                               
                            }
                        });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
                
            
            
    });
    
    $("#direct_cancel").on('click',function() {
     
        
      document.getElementById("load").style.display = "block";
            swal({
                  title: "Are you sure?",
                  text: "Save, Entered Details will be Saved",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                           
                            
                                
                                location.href = "<?php echo site_url('new_grn')?>/";
                                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>