<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>Batch</h1>
                            </div>
                        </div>
                    </div>
                    <!--div class="col-sm-8">
                        <div class="page-header float-right">
                            <div class="page-title">
                                <ol class="breadcrumb text-right">
                                    <li><a href="#">Dashboard</a></li>
                                    <li><a href="#">Forms</a></li>
                                    <li class="active">Basic</li>
                                </ol>
                            </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;
}

</style>  

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/lib/chosen/chosen.min.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/jquery-datepicker.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">   
                                                                
    
  
                <div class="content">
            <div class="animated fadeIn">
       <div class="overlay" style="display:block" id="load"><div class="overlay-content"><img src="<?php echo base_url(); ?>assets/images/loader.gif" alt="Loading..." style="width: 60px;"/></div></div>       

                <div class="row" style="padding-bottom: 30px;">
                           <?php echo validation_errors(); ?>
           
                   <div class="container">
                       
                         <form id="frm1" action="<?php echo site_url('daily_sales_itempdf');?>" method="post" target="_blank">
                        <div class="row">

                                    <div class="col-sm-4">  
                                                        Depot
                                                        <select class="standardSelect form-control" id="depo" data-placeholder="Choose a cat..." name="depo" onchange="get_depot()";>
							<option value="0"> Select Depot </option>
                                                         <?php 
                                                         foreach ($depo as $rf) {            
                                                          ?>
							<option value="<?php echo $rf->dep_id; ?>"><?php echo $rf->dep_name?></option>
                                                          <?php }?>                                                           
                                                        </select>                          
                                                    </div>
                                                    
                                                      <div class="col-sm-4">  
                                                       Item
                                                        <select class="standardSelect form-control" id="item" data-placeholder="Choose a cat..." name="item" onchange="get_batch()";>
							<option value="0"> Select Item</option>
                                                                                                                  
                                                        </select>                          
                                                    </div>
                                                    
                                                   <!--div class="col-sm-4">  
                                                       Catagory
                                                        <select class="standardSelect form-control" id="cat" data-placeholder="Choose a cat..." name="cat">
							<option value="0"> Select Catagory </option>
                                                         <?php 
                                                         foreach ($cat_list as $rf) {            
                                                          ?>
							<option value="<?php echo $rf->cat_id; ?>"><?php echo $rf->cat_name; ?></option>
                                                          <?php }?>                                                           
                                                        </select>                          
                                                    </div-->
                                                    
                                                  
                                                    
                                                   
                                </div> 
                                
                            <!--div class="row">
                                
                                   <div class="col-sm-4">  
                                                       Brand
                                                        <select class="standardSelect form-control" id="brnd" data-placeholder="Choose a Brand..." name="brnd">
							<option value="0"> Select Brand </option>
                                                         <?php 
                                                         foreach ($brnd_list as $rf) {            
                                                          ?>
							<option value="<?php echo $rf->brand_id; ?>"><?php echo $rf->brand_name; ?></option>
                                                          <?php }?>                                                           
                                                        </select>                          
                                                    </div>
                                                    

                                    <div class="col-sm-3">  
                                                       From Date
                                                       <input type="text" style="text-align: right;" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control datepicker" name="frm_date" id="frm_date">                         
                                                    </div>
                                                    
                                                    <div class="col-sm-3">  
                                                       To Date
                                                       <input type="text" style="text-align: right;" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control datepicker" name="to_date" id="to_date">                         
                                                    </div>
                                   
                                                    
                                            <div class="col-sm-2">   
                                                <br>
                                                 <button type="submit" class="btn btn-secondary btn-sm" id="update_data">Print</button>                 
                                        </div>         
                                </div-->     
                    </form> 
            </div>
            
            
            </div>
            
            <div class="row">
                
                <div class="container">
                     
                   
                       <div class="row" style="margin: 0px 0px;  background-color: #D5D6D7; padding: 10px 10px 10px 10px; ">
                    
                       <div class="pull-left col-sm-6">
                     
                     Display <select class=" tableTools-container input-sm" name="state" id="maxRows" style="margin: 4px 4px;"> 
						 <option value="5000">Show ALL Rows</option>
                                                 <option selected="" value="5">5</option>
						 <option value="10">10</option>
						 <option value="15">15</option>
						 <option value="20">20</option>
						 <option value="50">50</option>
						 <option value="70">70</option>
						 <option value="100">100</option>
						</select> records
                 </div>
                
                   <div class="pull-right col-sm-6" style="text-align: end;">
                    Search: <input type="text" class=" input-sm  " id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name" style="margin: 4px 4px;">
                    
                </div>
                 
                
                    
                </div>
                        
                            <table class="table table-bordered" id="table-id">
                                 
                                
                                <thead class="thead-dark" id="thed" ></thead>
                               
                             <tbody id="tb_bdy">
												    
												</tbody>
                        </table>

                   			<nav aria-label="pagination">
  <ul class="pagination">
    <li class="page-item" data-page="prev">
      <a class="page-link" href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
        <span class="sr-only">Previous</span>
      </a>
    </li>
    <li class="page-item" data-page="next" id="prev">
      <a class="page-link" href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
        <span class="sr-only">Next</span>
      </a>
    </li>
  </ul>
</nav>
                
            
            
            </div>
                
                
            </div>
            </div>
            </div>
            
            <div class="clearfix"></div>
            
             <!--MODAL batch-->
          <form id="frm_batch_data">
              <div class="modal fade" id="batchmodel" tabindex="-1" role="dialog" aria-labelledby="scrollmodalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="scrollmodalLabel">Batch Edit</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body" id="modal_batch">
                      
                          <div class='form-group row'>
                              
                              <input type='hidden' name='batchid' id='batchid' class='form-control' readonly='' >
                              <input type='hidden' name='exp' id='exp' class='form-control' readonly='' >
                              <input type='hidden' name='proid' id='proid' class='form-control' readonly='' >
                              <input type='hidden' name='hid_depo' id='hid_depo' class='form-control' readonly='' >
                                <label class='col-md-2 col-form-label'>Buying</label>
                                <div class='col-md-10'>
                                  <input type='text' name='ebuying' id='ebuying' class='form-control' placeholder='' >
                                 </div>
                            </div>
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Selling</label>
                                <div class='col-md-10'>
                                  <input type='text' name='eSelling' id='eSelling' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                            <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Retail</label>
                                <div class='col-md-10'>
                                  <input type='text' name='eRetail' id='eRetail' class='form-control' placeholder=''>
                                 </div>
                            </div>
                            
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Mafd</label>
                                <div class='col-md-10'>
                                  <input type='text' name='eMafd' id='eMafd' class='form-control datepicker' placeholder=''>
                                 </div>
                            </div>
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Exp</label>
                                <div class='col-md-10'>
                                  <input type='text' name='eExp' id='eExp' class='form-control datepicker' placeholder=''>
                                 </div>
                            </div>
                            
                             <div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Comment</label>
                                <div class='col-md-10'>
                                  <textarea id="editcomment" name="editcomment" class='form-control' rows="4" cols="50"></textarea>
                                 </div>
                            </div>
                            
                             
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary" id="update_data">Confirm</button>
                           
                        </div>
                    </div>
                </div>
            </div>
          </form>
            <!--END MODAL edit-->
            
             <!--MODAL batch-->
          <form id="frm_batch_adjsut_data">
              <div class="modal fade" id="batchajustmodel" tabindex="-1" role="dialog" aria-labelledby="scrollmodalLabel" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="scrollmodalLabel">Batch Adjustmentt</h5>
                            <input type='hidden' name='hidd_depo' id='hidd_depo' class='form-control' >
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body" id="modal_Adjust">
                      
                          
                            
                         
                            
                             
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary" id="update_adjust_data">Confirm</button>
                           
                        </div>
                    </div>
                </div>
            </div>
          </form>
            <!--END MODAL edit-->

<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(){
        //show_data(); 
        //getPagination('#table-id');
        document.getElementById("load").style.display = "none";

    });
    
    
    
    

        
              //show edit data    
    function  edit_batch(clickedID){
        //$('#modal_part').html("");
        var rout = clickedID;
          var res = rout.split("/");
            var rt = res[0];
            var id = res[1];
            var g = document.getElementById("depo");
                var depo = g.options[g.selectedIndex].value;
            document.getElementById("load").style.display = "block";
             $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : '<?php echo site_url('')?>'+rt,
                        async : true,
                        dataType : 'json',
                        data : {id:id,depo:depo },
                        success : function(data){
                            
                            document.getElementById("load").style.display = "block";

                            //alert(data);
                            //$('#modal_edit').html(data);
                            $('#batchid').val(data[0]);
                            $('#ebuying').val(data[1]);
                            $('#eSelling').val(data[2]);
                            $('#eRetail').val(data[3]);
                            $('#eMafd').val(data[4]);
                            $('#eExp').val(data[5]);
                            $('#eQty').val(data[6]);
                            $('#exp').val(data[7]);
                            $('#proid').val(data[8]);
                            $('#hid_depo').val(depo);
                            
                            
                            
                            
                            
                            
                            document.getElementById("load").style.display = "none";
                            
                        }
         
                    });
            $('#batchmodel').modal('show');
             document.getElementById("load").style.display = "none";
      
            return false;
     
    }   
    
                 //show edit data    
    function  adjust_batch(clickedID){
        //$('#modal_part').html("");
        var rout = clickedID;
          var res = rout.split("/");
            var rt = res[0];
            var id = res[1];
            var g = document.getElementById("depo");
                var depo = g.options[g.selectedIndex].value;
            
            document.getElementById("load").style.display = "block";
             $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : '<?php echo site_url('')?>'+rt,
                        async : true,
                        dataType : 'json',
                        data : {id:id,hidd_depo:depo},
                        success : function(data){

                           $('#hidd_depo').val(depo);
                            $('#modal_Adjust').html(data);
                      
                            document.getElementById("load").style.display = "none";
                            
                        }
         
                    });
            $('#batchajustmodel').modal('show');
      
            return false;
     
    }  
    
    $('#update_adjust_data').on('click',function(){
            // var rt = document.getElementById('edit_table').value;
            
       
             
              swal({
                  title: "Are you sure?",
                  text: "Update, Your Edited details will be Updated",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                      
                      document.getElementById("load").style.display = "block";
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('update_adjust')?>",
                                    async : true,
                                    dataType : 'json',
                                    data: $("#frm_batch_adjsut_data").serialize(),
                                    success : function(data){
                                        
                                        if(data[2]=="error"){
                                        swal(data[0]);
                                        document.getElementById("load").style.display = "none";
                                            }else{
                                                swal(data[0]);
                                                $('#batchajustmodel').modal('hide');
                                                get_batch()
                                                document.getElementById("load").style.display = "none";
                                                
                                            }
                                         
                                    }
             
                                });
                  } else {
                    swal("No Changes");
                    $('#batchajustmodel').modal('hide');
                  }
                });
            
        });
    
    $('#update_data').on('click',function(){
            // var rt = document.getElementById('edit_table').value;
            
            var sprice = $('#eSelling').val();
            var rprice = $('#eRetail').val();
            var exp = $('#exp').val();
            var Mafd = $('#eMafd').val();
            var Expd = $('#eExp').val();
            
            
            
         if(parseFloat(rprice) < parseFloat(sprice)) {
                swal("Change Selling Price"); 
            }else if((exp=="1") && ((Expd=="") ||(Expd=="0")) ) {
                swal("Please enter EXP Date"); 
            }else if((Date.parse(Expd) <= Date.parse(Mafd) )) {
                swal("Please change EXP Date"); 
            }else{
                
                swal({
                  title: "Are you sure?",
                  text: "Update, Your Edited details will be Updated",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                      
                      document.getElementById("load").style.display = "block";
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('update_batch')?>", 
                                    async : true,
                                    dataType : 'json',
                                    data: $("#frm_batch_data").serialize(),
                                    success : function(data){
                                        
                                        if(data[2]=="error"){
                                        swal(data[0]);
                                        document.getElementById("load").style.display = "none";
                                            }else{
                                                
                                                
                                                swal(data[0]);
                                                $('#batchmodel').modal('hide');
                                                get_batch()
                                                document.getElementById("load").style.display = "none";
                                                
                                            }
                                         
                                    }
             
                                });
                  } else {
                    swal("No Changes");
                    $('#batchmodel').modal('hide');
                  }
                });
            }
            
         
            
           // if(parseFloat(sel) != parseFloat(rtl)) { swal("Price Under Value");}else{swal("no");}
             
              
            
        });
    
    function get_batch(){
        
        document.getElementById("load").style.display = "block";  
        var id =  $('#item').val();
        
        var g = document.getElementById("depo");
                var depo = g.options[g.selectedIndex].value;
       
       // $('#invselect option').remove();
        
       
             $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('batch_list')?>',
                dataType : 'json',
                data : {id:id,depo:depo},
                success : function(data){
                    //console.log(data);
                    
                    // $('#btn').html(data[0]);
                    $('#thed').html(data[1]);
                    $('#tb_bdy').html(data[2]);
                   getPagination('#table-id');
                     document.getElementById("load").style.display = "none";
                 
                }
 
            });
             return false;
        
      
       
    }
    
    function get_depot(){
        
          var id =  $('#repselect').val();
        $('#custselect option').remove();
        $.ajax({
                type  : 'ajax',
                method:'POST',
                url   : '<?php echo site_url('cust_list')?>',
                dataType : 'json',
                data : {id:id},
                success : function(data){
                    console.log(data);
                  $('#custselect').append($('<option>', {
                                                            value: "0",
                                                            text: "select"
                                                        }));
                    if(data){
                        var i;
                        
                    for(i=0; i<data.length; i++){
                        
                     
                                                $('#custselect').append($('<option>', {
                                                            value: data[i].cust_id,
                                                            text: data[i].cust_company
                                                        }));
                    }
                    
                    $("#custselect").trigger("chosen:updated");
                    }                                    
                    document.getElementById("load").style.display = "none";  
                }
 
            }); 
    }
    
    $('#depo').change(function(){ 
        
                $('#item option').remove();
                $('#invselect option').remove();
                $("#item").trigger("chosen:updated");
       
                var g = document.getElementById("depo");
                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('depot_batch')?>',
                dataType : 'json',
                data : {depo:depo},
                success : function(data){
                    
                    get_batch();
                    
                   // console.log(data);
                   // alert(data[0].p_id);
                            $('#item').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                     
                 var i;
                            for (var i = 0; i < data.length; i++) {
                               $('#item').append($('<option>', {
                                                            value: data[i].p_id,
                                                            text: data[i].item
                                                        }));
                            }
                            
                            
                            document.getElementById("load").style.display = "none";
                             $("#item").trigger("chosen:updated");
                }
                
            }); 
      }); 
    
    $( ".datepicker" ).datepicker();
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd");
    
  

        
            jQuery(document).ready(function() {
        jQuery(".standardSelect").chosen({
            disable_search_threshold: 5,
            no_results_text: "Oops, nothing found!",
            width: "100%"
        });
    });  
    
    function getPagination(table) {
  var lastPage = 1;

  $('#maxRows')
    .on('change', function(evt) {
      //$('.paginationprev').html('');						// reset pagination

     lastPage = 1;
      $('.pagination')
        .find('li')
        .slice(1, -1)
        .remove();
      var trnum = 0; // reset tr counter
      var maxRows = parseInt($(this).val()); // get Max Rows from select option

      if (maxRows == 5000) {
        $('.pagination').hide();
      } else {
        $('.pagination').show();
      }

      var totalRows = $(table + ' tbody tr').length; // numbers of rows
      $(table + ' tr:gt(0)').each(function() {
        // each TR in  table and not the header
        trnum++; // Start Counter
        if (trnum > maxRows) {
          // if tr number gt maxRows

          $(this).hide(); // fade it out
        }
        if (trnum <= maxRows) {
          $(this).show();
        } // else fade in Important in case if it ..
      }); //  was fade out to fade it in
      if (totalRows > maxRows) {
        // if tr total rows gt max rows option
        var pagenum = Math.ceil(totalRows / maxRows); // ceil total(rows/maxrows) to get ..
        //	numbers of pages
        for (var i = 1; i <= pagenum; ) {
          // for each page append pagination li
          $('.pagination #prev')
            .before(
              '<li class="page-item" data-page="' +
                i +
                '">\
								  <span class="page-link">' +
                i++ +
                '<span class="sr-only">(current)</span></span>\
								</li>'
            )
            .show();
        } // end for i
      } // end if row count > max rows
      $('.pagination [data-page="1"]').addClass('active'); // add active class to the first li
      $('.pagination li').on('click', function(evt) {
        // on click each page
        evt.stopImmediatePropagation();
        evt.preventDefault();
        var pageNum = $(this).attr('data-page'); // get it's number

        var maxRows = parseInt($('#maxRows').val()); // get Max Rows from select option

        if (pageNum == 'prev') {
          if (lastPage == 1) {
            return;
          }
          pageNum = --lastPage;
        }
        if (pageNum == 'next') {
          if (lastPage == $('.pagination li').length - 2) {
            return;
          }
          pageNum = ++lastPage;
        }

        lastPage = pageNum;
        var trIndex = 0; // reset tr counter
        $('.pagination li').removeClass('active'); // remove active class from all li
        $('.pagination [data-page="' + lastPage + '"]').addClass('active'); // add active class to the clicked
        // $(this).addClass('active');					// add active class to the clicked
	  	limitPagging();
        $(table + ' tr:gt(0)').each(function() {
          // each tr in table not the header
          trIndex++; // tr index counter
          // if tr index gt maxRows*pageNum or lt maxRows*pageNum-maxRows fade if out
          if (
            trIndex > maxRows * pageNum ||
            trIndex <= maxRows * pageNum - maxRows
          ) {
            $(this).hide();
          } else {
            $(this).show();
          } //else fade in
        }); // end of for each tr in table
      }); // end of on click pagination list
	  limitPagging();
    })
    .val(50)
    .change();

  // end of on select change

  // END OF PAGINATION
}

function limitPagging(){
	// alert($('.pagination li').length)

	if($('.pagination li').length > 7 ){
			if( $('.pagination li.active').attr('data-page') <= 3 ){
			$('.pagination li:gt(5)').hide();
			$('.pagination li:lt(5)').show();
			$('.pagination [data-page="next"]').show();
		}if ($('.pagination li.active').attr('data-page') > 3){
			$('.pagination li:gt(0)').hide();
			$('.pagination [data-page="next"]').show();
			for( let i = ( parseInt($('.pagination li.active').attr('data-page'))  -2 )  ; i <= ( parseInt($('.pagination li.active').attr('data-page'))  + 2 ) ; i++ ){
				$('.pagination [data-page="'+i+'"]').show();

			}

		}
	}
}

$(function() {
  // Just to append id number for each row
  $('table tr:eq(0)').prepend('<th> ID </th>');

  var id = 0;

  $('table tr:gt(0)').each(function() {
    id++;
    $(this).prepend('<td>' + id + '</td>');
  });
});


function myFunction() {
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("table-id");
  var rows = table.getElementsByTagName("tr");
  for (i = 1; i < rows.length; i++) {
    var cells = rows[i].getElementsByTagName("td");
    var j;
    var rowContainsFilter = false;
    for (j = 1; j < cells.length; j++) {
      if (cells[j]) {
        if (cells[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
          rowContainsFilter = true;
          
          continue;
        }
      }
    }

    if (! rowContainsFilter) {
      rows[i].style.display = "none";
    } else {
      rows[i].style.display = "";
      
    }
  }
}

</script>    