<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>Customer Management</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>
        
  
                <div class="content">
            <div class="animated fadeIn">
              
                  <style>
.content{position: relative;}
.overlay{position: absolute;left: 0; top: 0; right: 0; bottom: 0;z-index: 1060;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="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">   
                                                         
    <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">
                    
                      <!--div class="col-lg-12" style="padding-bottom: 10px;">
                           <?php echo validation_errors(); ?>
                
                <button class="btn btn-secondary" name="tax" id="product_view" onclick ="show_data(this.id);">Product</button>
                <button class="btn btn-secondary" name="curr" id="get_brnd" onclick ="show_data(this.id);">Brand</button>
                <button class="btn btn-secondary" name="unit" id="get_cat" onclick ="show_data(this.id);">Category</button>
                 <button class="btn btn-secondary" name="unit" id="get_model" onclick ="show_data(this.id);">Model</button>
                   <button class="btn btn-secondary" name="unit" id="get_item" onclick ="show_data(this.id);">Item</button>
            </div-->
                     <?php   if(($this->session->userdata['login_data']['userlevel'] < 2)||($this->session->userdata['login_data']['userlevel'] ==15)){
                     
                     ?>
                     <div id="btn" class="col-lg-12" style="padding-bottom: 10px;"></div> 
                                                   <?php }?>  
                    
                   
                       
                       
                       
                     <div class="container">
                           <form id="search_cus_data">
                         
                          <div class="row" style="padding-bottom: 30px;">
                              
                            

                                    <div class="col-sm-4">  
                                                        Sales Rep
                                                        <select class="standardSelect form-control" id="depo" data-placeholder="Choose a cat..." name="depo" ;>
							<option value="0"> Select Rep </option>
                                                         <?php 
                                                         foreach ($ref as $rf) {            
                                                          ?>
							<option value="<?php echo $rf->user_id; ?>"><?php echo $rf->user_fname ." ". $rf->user_lname?></option>
                                                          <?php }?>                                                           
                                                        </select>                          
                                                    </div>
                                                    
                                                      <div class="col-sm-4">  
                                                       Customer Name
                                                       
                                                       <input type="text" name="cust_name" id="cust_name" class="col-xs-12 col-sm-12 input-sm form-control-sm form-control" value="" >
                                                                                
                                                    </div>
                                                    
                                                   <div class="col-sm-4"> 
                                                   <br>
                                                      <button class="btn btn-secondary btn-sm" name="get_cus" id="get_cus" >Search</button>                        
                                                    </div>
                                                    
                                                  
                                              
                                                   
                                </div> 
                                
                                 </form>    
                   
                        <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" style="position: sticky; top: 55px;  background-color: red; z-index:1000;"></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 add-->
          <form id="frm_add_data">
              <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/lib/chosen/chosen.min.css">       
              <div class="modal fade" id="addmodel" 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">Add</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_Add">
                                        <div class="form-group row">
	          <label class="col-md-2 col-form-label">Salse Person</label>
	          <input type="hidden" name="table" id="table" class="form-control" readonly="" value="save_customer">
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_sals" name="sup_sals" onchange="get_route()">
                                  <option value="" selected>Select</option>'; 
                                  
                            <?php      
                                     foreach($dup as $value) {
                                                echo '<option value="'.$value->user_id.'">'.$value->user_fname.' '.$value->user_lname.'</option>';
                                            }     
                            ?>                     
                                        </select>
                                 </div>
                            </div>
                            
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Name </label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_name" id="sup_name" class="form-control" value="" >
                                 </div>
                            </div>
                         
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address</label>
                                <div class="col-md-10">
                                  <textarea id="sup_add" name="sup_add" rows="4" placeholder="" class="form-control"></textarea>
                                 
	        
                                 </div>
                            </div>
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">Province</label>
	          
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_prov" name="sup_prov" onchange="get_prov('add')">
                                  <option value="" selected>Select Province</option> 
                                  
                                  <?php
                                    foreach($province as $value) {
                                                echo '<option value="'.$value->id.'">'.$value->name_en.' </option>';
                                            }     
                                  ?>
                                       
                                        </select>
                                 </div>
                            </div>
                          
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">District</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_dis" name="sup_dis" onchange="get_dis('add')">
                                  <option value="" selected>Select District</option>
                                        </select>
                                 </div>
                            </div>
                          
                          <div class="form-group row">
	          <label class="col-md-2 col-form-label">City</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_city" name="sup_city">
                                  <option value="" selected>Select City</option>
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">Route</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_route" name="sup_route">
                                  <option value="" selected>Select Route</option>
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_con" id="sup_con" class="form-control" value="">
                                 </div>
                            </div>
                         
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_conname" id="sup_conname" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Credit Period</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_cperiod" id="sup_cperiod" class="form-control" value="">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Discount</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_dsico" id="sup_dsico" class="form-control" value="">
                                 </div>
                            </div>
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="button" class="btn btn-primary" id="save_data">Confirm</button>
                        </div>
                    </div>
                </div>
            </div>
          </form>
            <!--END MODAL add-->
            
                         <!--MODAL add address-->
          <form id="frm_add_adddata">
              <div class="modal fade" id="addmodeladd" 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">Add</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_Add">
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line I</label>
                                <div class="col-md-10">
                                  <!--textarea id="sup_add" name="sup_add" rows="4" placeholder="" class="form-control"></textarea-->
                                   <input type="hidden" name="cust_id" id="cust_id" class="form-control" value="">
                                  <input type="text" name="cust_add" id="cust_add" class="form-control" value="">
                                 
	        
                                 </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line II</label>
                                <div class="col-md-10">
                                  <input type="text" name="cust_add1" id="cust_add1" class="form-control" value="">
                                 </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address Line III</label>
                                <div class="col-md-10">
                                  <input type="text" name="cust_add2" id="cust_add2" class="form-control" value="">
                                 </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="button" class="btn btn-primary" id="save_add_data">Confirm</button>
                        </div>
                    </div>
                </div>
            </div>
          </form>
          
         
            <!--END MODAL address-->
            
            
            <!--MODAL edit-->
          <form id="frm_edit_data">
              <link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/lib/chosen/chosen.min.css">  
              <div class="modal fade" id="editmodel" 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">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_edit">
                            
                              <div class="form-group row">
	          <label class="col-md-2 col-form-label">Salse Person</label>
	          <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_customer">
	          <input type="hidden" name="uid" id="uid" class="form-control" readonly="" value="">
                                <div class="col-md-10">
                                  <select class="form-control standardSelect " id="sup_salse" name="sup_salse">
                                      <option value="" selected>Select Salse Person</option>
                                  
                                  
                            <?php      
                                     foreach($dup as $value) {
                                                echo '<option value="'.$value->user_id.'">'.$value->user_fname.' '.$value->user_fname.'</option>';
                                            }     
                            ?>                     
                                        </select>
                                 </div>
                            </div>
                            
	                    <div class="form-group row">
                                <label class="col-md-2 col-form-label">Name </label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_namee" id="sup_namee" class="form-control" value="" >
                                 </div>
                            </div>
                         
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Address</label>
                                <div class="col-md-10">
                                  <textarea id="sup_adde" name="sup_adde" rows="4" placeholder="" class="form-control"></textarea>
                                 
	        
                                 </div>
                            </div>
                            
                             <div class="form-group row">
	          <label class="col-md-2 col-form-label">Province</label>
	          
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_prove" name="sup_prove" onchange="get_prov('edit')">
                                
                                  
                                  <?php
                                    foreach($province as $value) {
                                                echo '<option value="'.$value->id.'">'.$value->name_en.' </option>';
                                            }     
                                  ?>
                                       
                                        </select>
                                 </div>
                            </div>
                          
                            
                               <div class="form-group row">
	          <label class="col-md-2 col-form-label">District</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_dise" name="sup_dise" onchange="get_dis('edit')">
                                  <option value="" selected>Select District</option>
                                        </select>
                                 </div>
                            </div>
                          
                          <div class="form-group row">
	          <label class="col-md-2 col-form-label">City</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_citye" name="sup_citye">
                                  <option value="" selected>Select City</option>
                                        </select>
                                 </div>
                            </div>
                            
                            <div class="form-group row">
	          <label class="col-md-2 col-form-label">Route</label>
	         
                                <div class="col-md-10">
                                  <select class="form-control standardSelect choosen-select" id="sup_routed" name="sup_routed">
                                  <option value="" selected>Select Route</option>
                                        </select>
                                 </div>
                            </div>
                            
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Number</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_cone" id="sup_cone" class="form-control" value="">
                                 </div>
                            </div>
                         
                             <div class="form-group row">
                                <label class="col-md-2 col-form-label">Contact Name</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_connamee" id="sup_connamee" class="form-control" value="">
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Credit Period</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_cperiode" id="sup_cperiode" class="form-control" value="">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Discount</label>
                                <div class="col-md-10">
                                  <input type="text" name="sup_dsicoe" id="sup_dsicoe" class="form-control" value="">
                                 </div>
                            </div> 
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Stetus</label>
                                <div class="col-md-10">
                                  
                                  <select class="form-control choosen-select" id="sup_sts" name="sup_sts" >
                                    
                                    <option value="1" <?php $active ?>>Active</option>
                                    <option value="0" <?php $inactive ?>>Inactive</option>
                                        </select>
                                 </div>
                            </div> 
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="button" class="btn btn-primary" id="update_data">Update</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/popper.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/jquery.matchHeight.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 src="<?php echo base_url(); ?>assets/js/jquery-ui.js"></script>







            
                        <script>

    $(document).ready(function(){
       show_data("customer_view"); 
       getPagination('#table-id');
    });
    
    
        //get data
    function show_data(clickedID){
     var rt = clickedID;
     var rep = document.getElementById('depo').value;
     var cus = $('#cust_name').val();
     
     
      document.getElementById("load").style.display = "block";
     $.ajax({
                type  : 'ajax',
                method : "POST",
                url   : "<?php echo site_url()?>"+rt,
                async : true,
                data : {rep:rep,cus:cus },
                dataType : 'json',
                success : function(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;
     
    }
    
       //show add new
    function add_data(clickedID){
     var rt = clickedID;
     
           
     
    }
    
    function add_adddata(clickedID){
    
     
     var rout = clickedID;
     $('#cust_id').val(rout);
 
    }
    
        // save new data
    $('#save_data').on('click',function(){
     
             //var rt = document.getElementById('table').value;
            
             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  : 'ajax',
                                method : "POST",
                                url   : "<?php echo site_url('save_customer')?>",
                                async : true,
                                dataType : 'json',
                                data: $("#frm_add_data").serialize(),
                                success : function(data){
                                    if(data[2]=="error"){
                                        swal(data[0]);
                                         document.getElementById("load").style.display = "none";
                                            }else{
                                               swal(data[0]);
                                                $("#addmodel").modal('hide');
                                                $('#tb_bdy').html("");
                                                show_data(data[1]);
                                                
                                                 document.getElementById("load").style.display = "none";
                                                
                                                 $(':input','#frm_add_data')
                                                 .not(':button, :submit, :reset, :hidden')
                                                 .val('')
                                                 .removeAttr('checked')
                                                 .removeAttr('selected');
                                            }
                                    
                                }
                 
                            });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
           
            
        });
        
            //show edit data    
    function  edit_view(clickedID){
       var cc = $('#sup_prove option').length;
     // if(cc>9){
     //     $('#sup_prove option:last').remove();
     // }
       //  $('#sup_prove option').filter(function(){
       //  return parseInt(this.value) > 10;
       //  }).remove();
      var dd = $('#sup_sts option').length;
      if(dd>2){
          $('#sup_sts option:last').remove();
      }
       
       var rout = clickedID;
          var res = rout.split("/");
          var rt = res[0];
            var id = res[1];
             $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : '<?php echo site_url('')?>'+rt,
                        async : true,
                        dataType : 'json',
                        data : {id:id },
                        success : function(data){
                            $('#sup_salse option').remove();
                            $('#sup_routed option').remove();
                        
                             $('#sup_salse').append($('<option selected="">').val(data.cusdata[0].cust_salseperson).text(data.cusdata[0].user_fname+' '+data.cusdata[0].user_lname)); 
                             //$('#sup_name').val(data.cusdata[0].cust_company);
                             $('#sup_namee').val(data.cusdata[0].cust_company);
                             $("textarea#sup_adde").val(data.cusdata[0].cust_addres);
                             $('#sup_prove').append($('<option selected>').val(data.cusdata[0].cust_prov).text(data.cusdata[0].pro)); 
                             $('#sup_dise').append($('<option selected="">').val(data.cusdata[0].cust_distc).text(data.cusdata[0].dis));
                             $('#sup_citye').append($('<option selected="">').val(data.cusdata[0].cust_city).text(data.cusdata[0].citynew));
                             $('#sup_routed').append($('<option selected="">').val(data.cusdata[0].routeid).text(data.cusdata[0].routename));
                             $('#sup_cone').val(data.cusdata[0].cust_contact);
                             $('#sup_connamee').val(data.cusdata[0].cust_conperson);
                             $('#sup_cperiode').val(data.cusdata[0].cust_creditper);
                             $('#sup_dsicoe').val(data.cusdata[0].cust_discount);
                             $('#uid').val(data.cusdata[0].ud);
                             $('#sup_sts').append($('<option selected="">').val(data.cusdata[0].cust_status).text(data.cusst));
                             
                             
                             var i;
                                for(i=0; i<data.salseman.length; i++){ 
                                        $('#sup_salse').append($('<option>', {
                                                                        value: data.salseman[i].user_id,
                                                                        text: data.salseman[i].user_fname+' '+data.salseman[i].user_lname
                                                                    }));
                                    
                                                            
                                }
                             $("#sup_salse").trigger("chosen:updated");   
                             $("#sup_prove").trigger("chosen:updated");   
                             $("#sup_dise").trigger("chosen:updated");   
                             $("#sup_citye").trigger("chosen:updated"); 
                             $("#sup_routed").trigger("chosen:updated"); 
                                get_route();
                         
                             $("#editmodel").modal('show');
                                
                        }
         
                    });
            
      
            return false;
     
    }  
    
    function  dadd_view(clickedID){
       
        var rout = clickedID;
          var res = rout.split("/");
          var rt = res[0];
            var id = res[1];
             $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : '<?php echo site_url('')?>'+rt,
                        async : true,
                        dataType : 'json',
                        data : {id:id },
                        success : function(data){
                            
                                $('#thed1').html(data[1]);
                                $('#tb_bdy1').html(data[2]);
                            
                                
                        }
         
                    });
            
      
            return false;
     
    } 
    
         //customer Authorize
   
    
    function auth_cus(clickedID){
        
        var id = clickedID;
       
            document.getElementById("load").style.display = "block";
            // 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) {
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('auth_cus')?>",
                                    async : true,
                                    dataType : 'json',
                                    data : {id:id },
                                    success : function(data){
                                       
                                               swal(data[0]);
                                       
                                        show_data(data[1]);
                                         document.getElementById("load").style.display = "none";
                                           
                                         
                                    }
             
                                });
                  } else {
                    swal("No Changes");
                   // $('#editmodel').modal('hide');
                     document.getElementById("load").style.display = "none";
                  }
                });
            
        }
        
    function cancel_cus(clickedID){
        
        var id = clickedID;
       
            document.getElementById("load").style.display = "block";
            // var rt = document.getElementById('edit_table').value;
             
              swal({
                  title: "Are you sure?",
                  text: "Update, Your Edited details will be Cancel",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('canc_cus')?>",
                                    async : true,
                                    dataType : 'json',
                                    data : {id:id },
                                    success : function(data){
                                       
                                               swal(data[0]);
                                       
                                        show_data(data[1]);
                                         document.getElementById("load").style.display = "none";
                                           
                                         
                                    }
             
                                });
                  } else {
                    swal("No Changes");
                   // $('#editmodel').modal('hide');
                     document.getElementById("load").style.display = "none";
                  }
                });
            
        }
        
    
             //save edited data
    $('#update_data').on('click',function(){
       
            document.getElementById("load").style.display = "block";
             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) {
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('')?>"+rt,
                                    async : true,
                                    dataType : 'json',
                                    data: $("#frm_edit_data").serialize(),
                                    success : function(data){
                                        if(data[2]=="error"){
                                        swal(data[0]);
                                        document.getElementById("load").style.display = "none";
                                            }else{
                                               swal(data[0]);
                                        $('#editmodel').modal('hide');
                                        $('#tb_bdy').html("");
                                        show_data(data[1]);
                                         document.getElementById("load").style.display = "none";
                                            }
                                         
                                    }
             
                                });
                  } else {
                    swal("No Changes");
                   // $('#editmodel').modal('hide');
                     document.getElementById("load").style.display = "none";
                  }
                });
            
        });
        
             //search data
    $('#get_cus').on('click',function(){
       
            document.getElementById("load").style.display = "block";
             //var rt = document.getElementById('edit_table').value;
             
             $("form").on("submit", function (e) {
            e.preventDefault();
        });
                
                                 $.ajax({
                                    type  : 'ajax',
                                    method : "POST",
                                    url   : "<?php echo site_url('search_cus')?>",
                                    async : true,
                                    dataType : 'json',
                                    data: $("#search_cus_data").serialize(),
                                    success : function(data){
                                        $('#btn').html(data[0]);
                                        $('#thed').html(data[1]);
                                        $('#tb_bdy').html(data[2]);
                                        getPagination('#table-id');
                                    document.getElementById("load").style.display = "none"; 
                                         
                                    }
             
                                });
                 
               
            
        });
        
       function  edit_savecus(clickedID){
       var rout = clickedID;
          var res = rout.split("/");
          var rt = res[0];
            var adid = res[1];
            
            var add1txt = "cusad_add"+adid;
            var add1 = document.getElementById(add1txt).value;
          
          var add2txt = "cusad_addone"+adid;
            var add2 = document.getElementById(add2txt).value;
            
            var add3txt = "cusad_addtwo"+adid;
            var add3 = document.getElementById(add3txt).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  : 'ajax',
                                method : "POST",
                                url   : "<?php echo site_url('')?>"+rt,
                                async : true,
                                dataType : 'json',
                                data : {add1:add1, add2:add2,add3:add3,adid:adid },
                                success : function(data){
                                    if(data[2]=="error"){
                                        swal(data[0]);
                                            }else{
                                               swal(data[0]);
                                                $("#showdadd").modal('hide');
                                                //$('#tb_bdy').html("");
                                                dadd_view(adid);
                                                //show_data("customer_view");
                                            }
                                    
                                }
                 
                            });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
          
             
            
      
            return false;
     
    }  
    
     function  edit_delcus(clickedID){
       var rout = clickedID;
          var res = rout.split("/");
          var rt = res[0];
            var adid = res[1];
            
               
            swal({
                  title: "Are you sure?",
                  text: "Delete, Entered Details will be Deleted",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true, 
                })
                .then((willDelete) => {
                  if (willDelete) {
                               $.ajax({
                                type  : 'ajax',
                                method : "POST",
                                url   : "<?php echo site_url('')?>"+rt,
                                async : true,
                                dataType : 'json',
                                data : {adid:adid },
                                success : function(data){
                                    if(data[2]=="error"){
                                        swal(data[0]);
                                            }else{
                                               swal(data[0]);
                                                $("#showdadd").modal('hide');
                                                //$('#tb_bdy').html("");
                                                show_data("customer_view");
                                                //show_data("customer_view");
                                                //document.getElementById("load").style.display = "none";
                                            }
                                    
                                }
                 
                            });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
          
             
            
      
            return false;
     
    }  
        
    //save del add
    
    $('#save_add_data').on('click',function(){
     
          
            
            swal({
                  title: "Are you sure?",
                  text: "Save, Entered Details will be Saved",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true, 
                })
                .then((willDelete) => {
                  if (willDelete) {
                               $.ajax({
                                type  : 'ajax',
                                method : "POST",
                                url   : "<?php echo site_url('')?>save_cusaddress",
                                async : true,
                                dataType : 'json',
                                data: $("#frm_add_adddata").serialize(),
                                success : function(data){
                                    if(data[2]=="error"){
                                        swal(data[0]);
                                            }else{
                                               swal(data[0]);
                                                $("#addmodeladd").modal('hide');
                                                //$('#tb_bdy').html("");
                                                show_data("customer_view");
                                            }
                                    
                                }
                 
                            });
                  } else {
                    swal("No Changes");
                    document.getElementById("load").style.display = "none";
                  }
                });
            
           
            
        });
        
       
       // get city
        
     function get_prov(id){
         $('#sup_citye option').remove();
                $("#sup_citye").trigger("chosen:updated");
        $('#sup_city option').remove();
                $("#sup_city").trigger("chosen:updated");        
         
         if(id!="add"){
             var part_id = $("#sup_prove option:selected").val();
             $('#sup_dise option').remove();
            $("#sup_dise").trigger("chosen:updated");
            
         }else{
             var part_id = $("#sup_prov option:selected").val();
             $('#sup_dis option').remove();
               $("#sup_dis").trigger("chosen:updated");
         }
     
        $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : "<?php echo site_url('dict_list')?>",
                        async : true,
                         data : {part_id:part_id},
                        dataType : 'json',
                        success : function(data){
                            if(id!="add"){
                                $('#sup_dise').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                               var i;
                                for(i=0; i<data.length; i++){
                                $('#sup_dise').append($('<option>', {
                                                                value: data[i].id,
                                                                text: data[i].name_en
                                                            }));
                            
                                                    
                                    }
                            }else{
                                    $('#sup_dis').append($('<option>', {
                                                                value: "",
                                                                text: "select"
                                                            }));
                        
                                    var i;
                                    for(i=0; i<data.length; i++){
                                            $('#sup_dis').append($('<option>', {
                                                                            value: data[i].id,
                                                                            text: data[i].name_en
                                                                        }));
                                        
                                                                
                                    } 
                            }
                            
                              
                    
                     
                                                
                    
                       $("#sup_prov").trigger("chosen:updated");
                       $("#sup_dis").trigger("chosen:updated");
                       
                       $("#sup_prove").trigger("chosen:updated");
                       $("#sup_dise").trigger("chosen:updated");
                        }
         
                    });
                    
                    
    };   
        
        
   // $('#sup_dis').change(function(){
   function get_dis(id){
       
       if(id!="add"){
             var part_id = $("#sup_dise option:selected").val();
             $('#sup_citye option').remove();
                $("#sup_citye").trigger("chosen:updated");
         }else{
             var part_id = $("#sup_dis option:selected").val();
             $('#sup_city option').remove();
        $("#sup_city").trigger("chosen:updated");
         }
       
        
        $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : "<?php echo site_url('city_list')?>",
                        async : true,
                         data : {part_id:part_id},
                        dataType : 'json',
                        success : function(data){
                            
                           
                            
                            if(id!="add"){
                                $('#sup_citye').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                                               
               
                    
                             var i;
                                for(i=0; i<data.length; i++){
                                        $('#sup_citye').append($('<option>', {
                                                                        value: data[i].id,
                                                                        text: data[i].name_en
                                                                    }));
                                    
                                                            
                                }
                            }else{
                                $('#sup_city').append($('<option>', {
                                                            value: "",
                                                            text: "select"
                                                        }));
                                               
               
                    
                             var i;
                                for(i=0; i<data.length; i++){
                                        $('#sup_city').append($('<option>', {
                                                                        value: data[i].id,
                                                                        text: data[i].name_en
                                                                    }));
                                    
                                                            
                                }
                            }
                            
                                
                                                
                    
                       $("#sup_city").trigger("chosen:updated");
                       $("#sup_citye").trigger("chosen:updated");
                        }
         
                    });
                    
      }              
   // });
        
    function get_part(){
        
                if (event.keyCode == 13) {
                 console.log("Enter key is pressed");
                        var part = document.getElementById('add_num').value;
                        var all = $('#part_num').val();
                        if(part!=""){
                            if(all!=""){all = all + "\n" +part;}else{all = part;}
                        }
                        
                        
                        $('#part_num').val(all);
                        $('#add_num').val("");
                 return true;
              } else {
                 return false;
              }
        
    }
    
    function get_route(){
        
            $('#sup_route option').remove();
            var f = document.getElementById("sup_sals");
            var sup_id = f.options[f.selectedIndex].value;
        
    if(sup_id==""){
        var f = document.getElementById("sup_salse");
            var sup_id = f.options[f.selectedIndex].value;  }
            
             document.getElementById("load").style.display = "block";
        
         $.ajax({
                        type  : 'ajax',
                        method : "POST",
                        url   : "<?php echo site_url('route_get')?>",
                        async : true,
                        data : {sup_id:sup_id},
                        dataType : 'json',
                        success : function(data){
                            
                    
                             var i;
                                for(i=0; i<data.length; i++){
                                        $('#sup_route').append($('<option>', {
                                                                        value: data[i].route_id,
                                                                        text: data[i].route_name
                                                                    }));
                                    
                                        $('#sup_routed').append($('<option>', {
                                                                        value: data[i].route_id,
                                                                        text: data[i].route_name
                                                                    }));                    
                                }
                                
                                
                           
                            
                                
                                                
                    
                       $("#sup_route").trigger("chosen:updated");
                       $("#sup_routed").trigger("chosen:updated");
                      // $("#sup_citye").trigger("chosen:updated");
                      
                       document.getElementById("load").style.display = "none";
                        }
         
                    });
        
        
    }
    
                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>

