$(document).ready(function(){
    $("#form-div,#table-div,.content").height($(window).height()-100);

   $("#edit_btn").hide();

$("#table-div").hide();

$(".slide1").click(function(){

$("#form-div").hide();
$("#table-div").show(500);
 fetch_view_table();
});


$(".slide2").click(function(){

$("#form-div").show(500);
$("#table-div").hide();
});




 function  show_form(){
    $("#form-div").show(500);
$("#table-div").hide();
}



/* 
$("#table-div").slimScroll({
    height: '430px', 
    color: 'rgb(176, 180, 185) none repeat scroll 0% 0%'
  });

 */




$('#dynamic_ajax_table').DataTable({
    dom: 'lBfrtip',
    buttons: [
        'copy', 'csv', 'excel', 'pdf', 'colvis'
    ],
    "bLengthChange": true,
    "bSort" : true,
     "lengthMenu": [[ 20, 50,75,100, -1], [ 20, 50,75,100, "All"]],
     "pagingType": "full_numbers",
    columnDefs: [ {
        visible: false
    } ]
    
    
});




});




$('.focusable').each(function(key,val){

    if($(val).attr('disabled')||$(val).attr('readonly')||$(val).attr('type')=='hidden' ){

$(val).removeClass('focusable');
    }



  })




 $('.focusable').keydown(function (e) {
   
     if (e.which === 13) {
       
var index = $('.focusable').index(this) + 1;

         $('.focusable').eq(index).focus();

     }
 });

 $('.caps').on('keyup',function(){

    $(this).val($(this).val().toUpperCase()); 
    
    
    });


function fetch_view_table()
{

	$("#table_result").html("<img src='../images/loading.gif' style='margin-left:35%;height:300px;width:300px;'>");
    

    
    var sp_sort = $('#table_settings_field').attr('sp_sort');
    
    var sp_date_picker = $('#table_settings_field').attr('sp_date_picker');
    var header_from_date = $('#table_settings_field').attr('header_from_date');
    var header_till_date = $('#table_settings_field').attr('header_till_date');
    
    $.ajax({
                type: 'POST',
                   url: '/fetch_table_view_json',
                data: {
                     '_token':  $('meta[name="csrf-token"]').attr('content'),
                   sort_value: sp_sort,
                   sp_date_picker:sp_date_picker,
                   header_from_date:header_from_date,
                   header_till_date: header_till_date
                },
                error: function (request, error) {
                   // alert("Please Wait, Loading...");
                },
                success: function (response) {
                    
            generate_table(response);   

                    
                    }
            });
    
}




function generate_table(response)
{

var data = JSON.parse(response);


if(Object.keys(data).length<=1){
          no_records='<h2>No Records Found</h2>';
          $("#table_result").html(no_records);
          $("#myModaltable > .modal-dialog  > .modal-content > .modal-body > .modal_form").html(no_records);
      }
      else
      {

var column_heading = $('#table_settings_field').attr('column_heading').split('`');
var column_width = $('#table_settings_field').attr('column_width').split('`');
var column_alighn = $('#table_settings_field').attr('column_alighn').split('`');
var column_conversion = $('#table_settings_field').attr('column_conversion').split('`');

    
    t_table='<table   border="1" class="table   table-striped custom-table table-hover datbl"   id="dynamic_ajax_table">';
t_colgroup='';  
t_thead='';
t_tfoot='';
t_tbody='';
t_table_end='';
modal_content='';

t_alihn= {
  L: 'left',
  R : 'right',
  C :  'center'
};



$(data).each(function(i,val)
{

var k =1;   
    
$.each(val,function(key2,val2)
{
    //row code starts here
    
    if(key2==1)
    {
    t_colgroup +='<colgroup> <col width="40px">';   
    t_thead +='<thead><tr> <th>SL</th>';    
    t_tfoot +='<tfoot style="display: table-header-group;"><tr> <th></th>';
    }
    else 
    {
    t_tbody +='<tr><td>'+k+'</td>';     
    }


        $.each(val2,function(key3,val3)
        {
            //td code starts here
            /*alert(key3 + " : " + val3);*/  
            
            if(key3>1)
            {
                key_index = (parseInt(key3) - 2);
            
            if(key2==1)
                {
                
                
                if(typeof column_heading[key_index] === "undefined" || column_heading[key_index]=="")
                {
                default_column_heading=val3;
                }
                else
                {
                default_column_heading=column_heading[key_index];   
                }
                
                
                if(typeof column_width[key_index] === "undefined"  || column_width[key_index]=="")
                {
                default_column_width=100;
                }
                else
                {
                default_column_width=column_width[key_index];   
                }
                
                
                
                alighn_select='<select name="td_alighn[]" class="form-control">';   
                
                left_option_select='';
                right_option_select='';
                center_option_select='';
                                
                    
                    if(column_alighn[key_index]=="L" || column_alighn[key_index]=="")
                    { 
                    left_option_select='selected';
                    }
                    else if(column_alighn[key_index]=="R")
                    { 
                    right_option_select='selected';
                    }
                    else if(column_alighn[key_index]=="C")
                    { 
                    center_option_select='selected';
                    }
                    else
                    {
                    left_option_select='selected';
                    }
                
                alighn_select+='<option value="L" '+left_option_select+' >Left</option>';   
                alighn_select+='<option value="R"  '+right_option_select+' >Right</option>';    
                alighn_select+='<option value="C"  '+center_option_select+' >Center</option>';
                
                alighn_select+='</select>';
                
                
                
                
                convertion_select='<select name="format[]" class="form-control">';  
                
                convertion_option_I='';
                convertion_option_i='';
                convertion_option_d='';
                convertion_option_D='';
                convertion_option_m='';
                convertion_option_M='';
                convertion_option_S='';
                convertion_option_C='';
                convertion_option_c='';
                convertion_option_T='';
                convertion_option_t='';
                convertion_option_R='';
                
                
                
                    if(column_conversion[key_index]=="I")
                    { 
                    convertion_option_I=' selected ';
                    }
                    else if(column_conversion[key_index]=="i")
                    { 
                    convertion_option_i=' selected ';
                    }
                    else if(column_conversion[key_index]=="d")
                    { 
                    convertion_option_d=' selected ';
                    }
                    else if(column_conversion[key_index]=="D")
                    {
                    convertion_option_D=' selected ';
                    }
                    else if(column_conversion[key_index]=="m")
                    { 
                    convertion_option_m=' selected ';
                    }
                    else if(column_conversion[key_index]=="M")
                    { 
                    convertion_option_M=' selected ';
                    }
                    else if(column_conversion[key_index]=="S" || column_conversion[key_index]=="")
                    { 
                    convertion_option_S=' selected ';
                    }
                    else if(column_conversion[key_index]=="C")
                    { 
                    convertion_option_C=' selected ';
                    }
                    else if(column_conversion[key_index]=="c")
                    { 
                    convertion_option_c=' selected ';
                    }
                    else if(column_conversion[key_index]=="T")
                    { 
                    convertion_option_T=' selected ';
                    }
                    else if(column_conversion[key_index]=="t")
                    { 
                    convertion_option_t=' selected ';
                    }
                    else if(column_conversion[key_index]=="R")
                    { 
                    convertion_option_R=' selected ';
                    }
                    else
                    {
                    convertion_option_S=' selected ';
                    }
                    
                convertion_select+='<option value="I" '+convertion_option_I+' >Int</option>';
                convertion_select+='<option value="i" '+convertion_option_i+' >Int(_)</option>';
                convertion_select+='<option value="d" '+convertion_option_d+' >Date(dd/mmm/yy)</option>';
                convertion_select+='<option value="D" '+convertion_option_D+' >Date(dd/mmm/yyyy)</option>';
                convertion_select+='<option value="m" '+convertion_option_m+' >Date(mm-yy)</option>';
                convertion_select+='<option value="M" '+convertion_option_M+' >Date(mmm-yyyy)</option>';
                convertion_select+='<option value="S" '+convertion_option_S+' >String</option>';
                convertion_select+='<option value="C" '+convertion_option_C+' >Cur(0.00)</option>';
                convertion_select+='<option value="c" '+convertion_option_c+' >Cur(0.000)</option>';
                convertion_select+='<option value="T" '+convertion_option_T+' >LongTime</option>';
                convertion_select+='<option value="t" '+convertion_option_t+' >ShortTime</option>';
                convertion_select+='<option value="R" '+convertion_option_R+' >INR Format</option>';
                
                
                convertion_select+='</select>';
                
                
                
                t_colgroup +='<col width="'+default_column_width+'px">';
                
                t_thead +='<th>'+default_column_heading+'</th>';
                
                    
                t_tfoot +='<th><input type="text" placeholder="Search" autocomplete="false"  class="form-control dtbl_frm_cntrl"  /></th>';
                 
 
modal_content+='<div class="form-group row" style="clear:both !important;">                                                                                                                                 <label class="control-label  col-sm-3 col-md-3 col-lg-3 col-xs-12 original_alias">'+val3+'</label>                                                                                                                                  <div class="col-sm-3 col-md-3 col-lg-3 col-xs-12">                                                                                                                                                                                                                                                      <input type="text" name="colname[]"   onfocus="this.select();"  class="form-control input-sm  colname" value="'+default_column_heading+'" >                                                                                                                                                                                                                             </div>                                                                                                                                                                                          <div class=" col-sm-2 col-md-2 col-lg-2 col-xs-12">                                                                                                                                                 <input type="text" name="wid[]"   onfocus="this.select();"  onkeypress="return isNumber(event, this.value);"   class="form-control input-sm" value="'+default_column_width+'" >                                                                                                         </div>                                                                                                                                                                                      <div class=" col-sm-2 col-md-2 col-lg-2 col-xs-12">'+alighn_select+'</div>                                                                                                                              <div class=" col-sm-2 col-md-2 col-lg-2 col-xs-12">'+convertion_select+'</div>                                                                                                                                                                                                                                                                                                                                      </div>';
                
                }
                else
                {
                    if(column_alighn[key_index]=="")
                    {
                    default_column_alighn='left';
                    }
                    else
                    {
                    default_column_alighn=t_alihn[column_alighn[key_index]];    
                    }
                    
                t_tbody +='<td  align="'+default_column_alighn+'">'+val3+'</td>';   
                }
            }
            else
            {
            hiddenid="'"+val3+"'";  
            }
            
        });
        
        
    if(key2==1)
    {     
      $('.additional_btns_td').each(function(index, element) {
         col_width = $(this).attr('col_width');
        t_colgroup +='<col width="50px">';
        t_thead +='<th></th>'; 
        t_tfoot +='<th></th>'; 
        });
      //$('.additional_btns').each(function(index, element) {
        //  col_width = $(this).attr('col_width')
       if( $('.additional_btns').length>0){
        t_colgroup +='<col width="50px">';
        t_thead +='<th></th>'; 
        t_tfoot +='<th></th>'; 
    }
    //});
    
    
    }
    else
    {
    
    
             
       $('.additional_btns_td').each(function(index, element) {
           btn_id = $(this).attr('id');
           btn_text = $(this).attr('btn_text');
            btn_click_function = $(this).attr('btn_click_function');
             btn_class = $(this).attr('btn_class');
             
         /*    t_tbody+='<td class="masked" >                                                                                                                                                                         <input type="button" name="'+btn_text+'" title="'+btn_text+'" onclick="'+btn_click_function+'('+hiddenid+');"  value="'+btn_text+'" hiddenid="'+hiddenid+'" class="'+btn_class+'" />                                                                                                                                                                                                    </td>';*/ 
             hd=hiddenid.replace(/'/g,"");
                 t_tbody+='<td class="masked" >   <button type="button" name="'+btn_text+'" class="'+btn_class+'"  id="'+btn_id+hd+'"  row_id="'+hd+'" onclick="'+btn_click_function+'('+hiddenid+');">'+btn_text+'</button>                                                                                                                                                                                 </td>';
        });
        
        if( $('.additional_btns').length>0){
            t_tbody+=' <td class="text-end">';
       t_tbody+=' <div class="dropdown dropdown-action">';
       t_tbody+=' <a href="#" class="action-icon dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"><i class="material-icons">more_vert</i></a>';
       t_tbody+=' <div class="dropdown-menu dropdown-menu-right">';
           $('.additional_btns').each(function(index, element) {
           btn_text = $(this).attr('btn_text');
            btn_click_function = $(this).attr('btn_click_function');
             btn_class = $(this).attr('btn_class');
             btn_icon = $(this).attr('btn_icon');
      t_tbody+='<a class="dropdown-item" href="javascript:void(0);"  onclick="'+btn_click_function+'('+hiddenid+');" ><i class="'+btn_icon+' m-r-5"></i> '+btn_text+'</a>';

       });
' </div>';
       t_tbody+=' </div>';
       t_tbody+=' </td>';
        }
 
        
        
        
    }
        
        
        
    if(key2==1)
    {
    t_colgroup +='</colgroup>';
    t_thead +='</tr></thead>';  
    t_tfoot +='</tr></tfoot>';  
    }
    else
    {
    t_tbody +='</tr>';  
    }
k++;
});


});

t_table_end +='</table>';
t_tfoot='';
full_table=t_table+t_colgroup+t_thead+t_tfoot+t_tbody+t_table_end;

settings_row_id= $('#table_settings_field').attr('settings_row_id');
csrf=$('meta[name="csrf-token"]').attr('content');
modal_header='<div class="form-group row"> <input type="hidden"  name="_token" value="'+csrf+'" >                                                                                                                                                                 <label class="control-label col-sm-3 col-md-3 col-lg-3 col-xs-12">SP Alias</label>                                                                                                                                  <label class="control-label  col-sm-3 col-md-3 col-lg-3 col-xs-12">Fields   <span onClick="copy_from_alias();" style="color:red; font-size:9px;">Copy Alias</span></label>                                                                                                                              <label class="control-label  col-sm-2 col-md-2 col-lg-2 col-xs-12">Width</label>                                                                                                                                            <label class="control-label  col-sm-2 col-md-2 col-lg-2 col-xs-12">Alighn</label>                                                                                                                       <label class="control-label  col-sm-2 col-md-2 col-lg-2 col-xs-12">Format</label></div>'+
modal_content+'                                                                                                                                                                                              <div class="form-group">                                                                                                                                                                                            <input type="hidden" name="rowid" value="'+settings_row_id+'">                                                                                                                                                                                  <input type="submit" name="tablesettings" class="btn btn-success" style="float:right !important; margin-top: 5px;" value="SAVE">                                                                                                                                                                                                            </div>';


$("#myModaltable > .modal-dialog  > .modal-content > .modal-body > .modal_form").html(modal_header);

$("#table_result").html(full_table);
$('#load_ajax_result').val('1');
$('#dynamic_ajax_table').dataTable().fnDestroy();
load_datatables();  

      }

}






function load_datatables()
{
    
    var header_from_date = $('#table_settings_field').attr('header_from_date');
    var header_till_date = $('#table_settings_field').attr('header_till_date');
    var sp_date_picker = $('#table_settings_field').attr('sp_date_picker');


  var table = $('#dynamic_ajax_table').DataTable({
        dom: 'lBfrtip',
        'autoWidth':false,
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'colvis'
        ],
        "bLengthChange": true,
        "bSort" : true,
         "lengthMenu": [[ 20, 50,75,100, -1], [ 20, 50,75,100, "All"]],
         "pagingType": "full_numbers",
        columnDefs: [ {
            visible: false
        } ]
        
        
    });
    
    
    if(sp_date_picker=="1")
    {   
    /* onChange="sp_header_date_change();" */   
        console.log("inside if");   
     $('#dynamic_ajax_table_filter').append('<label class="formsplabel"> From:</label><input class="formsp_dates form-control  form-control-sm  floating datetimepicker date_cls" readonly type="text" value="'+header_from_date+'" id="formspfrom"  size="8"    > <label class="formsplabel">- To:</label><input class="formsp_dates form-control  form-control-sm  floating datetimepicker date_cls " readonly type="text" value="'+header_till_date+'" id="formspto" size="8"><input type="button" class="btn btn-warning btn-sm" value="Load Records" onClick="sp_header_date_change();" />');  
     
    
$( function() {
    $( ".date_cls" ).datepicker({ dateFormat: 'dd-mm-yy' });
  } );

    

    /*elem=document.querySelector('#formspfrom');
new Datepicker(elem,{
 dateFormat: 'dd-mm-yy' 
});*/    
    
    }
    
    
     $('.dt-buttons').append('<button class="btn btn-primary buttons-collection dropdown-toggle buttons-colvis" data-toggle="modal" data-target="#myModaltable" onclick="load_tb_mdl();" id="wrench" title="Table Settings" type="button" ><i class="fa fa-wrench" style="color:#FFF;"></i></button>');
    
    
    
    
     // Apply the search
    table.columns().every( function () {
        var that = this;
 
        $( 'input', this.footer() ).on( 'keyup change', function () {
            if ( that.search() !== this.value ) {
                
                that.search( this.value ).draw(); 
                
            }
        } );
    } );
    
    
    
     table.on( 'order.dt search.dt', function () {
        table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();
                        
    
}






function sp_header_date_change()
{
$('#table_settings_field').attr('header_from_date',$('#formspfrom').val());
$('#table_settings_field').attr('header_till_date',$('#formspto').val());
fetch_view_table(); 
}


function custom_call_print()
{
    //var oldPage = document.body.innerHTML;
    
    var divElements = document.getElementById('only_table').innerHTML;

    
    document.body.innerHTML = 
          "<html><head><title></title><style>@media screen{body{font-size:12px;}} @media print{body{font-size:12px;}a{display:none;}  } .masked,.cus_print_btn dt-button,.dt-buttons { display:none; } #dynamic_ajax_table_paginate,#dynamic_ajax_table_length,#dynamic_ajax_table_info,#dynamic_ajax_table_filter{ display:none; }  </style></head><body>" + 
          divElements + "</body>";    
          
    window.print() ;
    window.location.reload();
    //document.body.innerHTML = oldPage;
}



function custom_call_dos_print()
{
    
    var divElements = document.getElementById('only_table').innerHTML;
    
    document.body.innerHTML = 
          "<html><head><title></title><style>@media screen{body{font-size:12px;  font-family:Verdana, Geneva, sans-serif; letter-spacing:6px !important; }   .masked,.cus_print_btn dt-button,.dt-buttons { display:none; }    #dynamic_ajax_table_paginate, #dynamic_ajax_table_length, #dynamic_ajax_table_info, #dynamic_ajax_table_filter{ display:none; } #dynamic_ajax_table {  width:760px  !important; } }                               @media print{body{ font-size:12px  !important; font-family:Verdana, Geneva, sans-serif; letter-spacing:6px !important; }     .masked,.cus_print_btn dt-button,.dt-buttons { display:none; } #dynamic_ajax_table_paginate,#dynamic_ajax_table_length,#dynamic_ajax_table_info,#dynamic_ajax_table_filter{ display:none; }  #dynamic_ajax_table { width:760px  !important;    margin: 27mm 16mm 27mm 16mm; }  }  </style></head><body>" + 
          divElements + "</body>";        
    window.print() ;
    window.location.reload();
}


function call_alert_msg_div(msg,color_code)
{
    alert(msg);
    /*if(color_code==0)
    {
    var bgcolor="#5cb85c";
    }
    else
    {
    var bgcolor="#ff6c60";  
    }
    $("#alert_msg_div").css("background-color", bgcolor);
    $("#alert_msg_div").html(msg);
    
     $("#alert_msg_div").fadeIn(1500);
     $("#alert_msg_div").fadeOut(3000);*/
}




function call_edit_modal(row_id)
{


    set_edit_form(row_id);  
/* $('#security_modal').modal('show'); 
$('#action_modal_row_id').val(row_id);
$('#modal_heading').html("Enter Edit Password");
$('#action_modal').val('1'); */



}


function call_remove_modal(row_id)
{


  let text = "Are You Sure Want To Delete";
  if (confirm(text) == true) {
   remove_grid_row(row_id);
  } 

    
/* $('#security_modal').modal('show'); 
$('#action_modal_row_id').val(row_id);
$('#modal_heading').html("Enter Remove Password");
$('#action_modal').val('2'); */
}



    function remove_grid_row(row_id)
{

$( '#hidden_status' ).val("2");
$( '#row_id' ).val(row_id);     
Save_form();
    
}

$('#security_modal').on('shown.bs.modal', function () {
    $('#action_password').focus();
}) 

$(document).on('keypress', '#action_password', function (e) {  
var $action_modal = $('#action_modal').val();

    if (e.which === 13)
    {     
      go_for_action();
    }
    
}); 


function go_for_action()
{
$('#security_modal').modal('hide');
var action_password =  $('#action_password').val();
var row_id = $('#action_modal_row_id').val();
var request_type = $('#action_modal').val();

    if(request_type==1)
    {   
            
        if(action_password == $('#edit_password').val())
        {
            
        set_edit_form(row_id);  
        }
        else
        {
            
        call_alert_msg_div("Edit Password Mismatch",1); 
        }
    }
    else if(request_type==2)
    {
        
        if(action_password == $('#remove_password').val())
        {
            
        remove_grid_row(row_id) 
        }
        else
        {
            
        call_alert_msg_div("Remove Password Mismatch",1);       
        }
    }
    $('#action_password').val('');
}




function set_checkboxes(class_name,str)
{
$('.'+class_name).each(function () {
                var value=','+$(this).val()+',';
                if(str.indexOf(value) != -1){
                    $(this).prop('checked',true);
                }
            }); 
    
}




function set_table_local_storage(form_name,table_name)
{

var str="";

$('#'+table_name).find('thead th').each(function(){
      str += $(this).width()+"px,"; 

     //$(this).css('width','300px'); 
});

str = str.slice(0, -1); 


console.log(str);

localStorage.setItem(form_name, str );

}


function load_table(form_name,table_name)
{
    //alert("form_name"+form_name+"table_name"+table_name);
var s =  localStorage.getItem(form_name);

if(s===null)
{
    
$('#'+table_name).find('thead th').each(function(){ 
     $(this).css('width','170px'); 
}); 
    
}else{
var tabArr = s.split(',');  

$.each(tabArr,function(k, v)
{
    $('#'+table_name+' thead th ').eq(k).css('width',v);
});
}

}


function copy_from_alias()
{
$('.original_alias').each(function(index, element) {
    alias_text=$(this).html();
    $(this).closest('div').find('.colname').val(alias_text);
}); 
}




$(document).ready(function(e) {
   var win= $(window).height();     
   var h = win - 90;
   var panel = h - 35;
   
   
   $('.form-w3layouts').css('height', h+'px');
   $('#table_result').css('height', panel+'px');
    $('#form_body').css('height', panel+'px');
     $('#form_body').css('margin-bottom', '-8px');
     
    /* $("#table_result").slimScroll({
          height: panel+'px',
          axis: 'both', 
          color: 'rgb(176, 180, 185) none repeat scroll 0% 0%'
        });*/
     
});


$(".reset_btn").on('click',function(){

    reset_form()


})

function reset_form(){
    $(".input_text").val('');
}
load_time();
function load_time(){
    $(".time-picker").val('');
$(".time-picker").attr('maxlength','8');

$(".time-picker").timepicker( {

dropdown: false,
scrollbar: false,
interval: 1


});
$('.time-picker').timepicker('setTime', new Date());
}

function set_mn() {

	active_mn=$('#table_settings_field').attr('active_mn');
    active_mnt=$('#table_settings_field').attr('active_mnt');  
 if(active_mnt==3){ 

    $('[p_url="'+active_mn+'"]').parent('.li3').addClass('active');
 }else{
    $('[p_url="'+active_mn+'"]').addClass('active');

	$('[p_url="'+active_mn+'"]').parent('ul').css('display','block');
    $('[p_url="'+active_mn+'"]').parent('ul').siblings('.outa').addClass('active');
    $('[p_url="'+active_mn+'"]').parent('ul').siblings('.outa').addClass('subdrop');
    }
}



function reset_form(){

    $(".input_txt").val('');
}

$( function() {
    $( ".date_cls" ).datepicker({ dateFormat: 'dd-mm-yy' });

    const today = new Date();
    const yyyy = today.getFullYear();
    let mm = today.getMonth() + 1; // Months start at 0!
    let dd = today.getDate();
    
    if (dd < 10) dd = '0' + dd;
    if (mm < 10) mm = '0' + mm;
    
    const formattedToday = dd + '-' + mm + '-' + yyyy;
    
    $( ".date_cls" ).val(formattedToday);

  } );


  
function isNumber(evt, value) {
  
    evt = (evt) ? evt : window.event;
    var k=value.indexOf(".");   
     var charCode = (evt.which) ? evt.which : evt.keyCode;
 
   if(k==-1)
   {
       if (charCode > 31 && (charCode < 46 || charCode > 57 || charCode==47)) {
         
         return false;
         
       }
       return true;
   }
   else if(k!=-1 && charCode==46)
   {
   return false  
   }
   else
   {
      if (charCode > 31 && (charCode < 46 || charCode > 57 || charCode==47)) {
         
         return false;
         
       }
       return true;
   }
 }

 function load_tb_mdl(){
    $("#myModaltable").modal("show");
}


//   $(function() { 
//         $('form').disableAutoFill();
//       });





    
function build_report(response){
    



$("#results").show();
html='';
theading='';
res=response;
var data = Object.values(res[0]);

var sub_total=[];
var mg_total=[];
var new_data=[];
var grand_total=[];
st=0;
mt=0;
var i=0;
var obj={};
var old_sg=0;
var old_mg=0;
var total_col_pos=[];
var g_total=0;
l=data.length;
var flag=false;
var show_total_row="bloc";
var data_len=0;
var col_pos_val=[];
var col_pos_val_same='';
var row_count=0;
var sub_group_count=0;





$.each(res,function(key,value){



if(value['ranks']==0){
        $("#titles").html(value['mg']);
        $("#fltr").html('('+value['sg']+')');
        file_name=value['mg'];

    theading='';
      theading += '<tr class="headings">';
      
            $.each(data.slice(3), function (index, value) { 
             //Heading of table generation


                     tr = value.split("_")[0][2];
                     sc = value.split("_")[0][1];
   lt= value.split("_")[0][3] || 0;
                            
                 if(lt=="" || lt==0)
{
    lt=1;
}


                        col_width=lt*10+"%";
                 if (sc == 'R') {
            theading += '<th style="text-align:right;width:'+col_width+'" >' + value.split("_")[1] + '</th>';


        }
        else if (sc == "L") {
 

           theading += '<th style="text-align:left;width:'+col_width+'" >' + value.split("_")[1] + '</th>';
          
        }
        else {
           
     theading += '<th style="text-align:center;width:'+col_width+'" >' + value.split("_")[1] + '</th>';

        }


sub_total[index]=0;
 mg_total[index]=0;
 grand_total[index]=0;
 col_pos_val.push(tr)


if(tr=="Y"){
    total_col_pos.push(index);
}
else if(tr=="N"){

show_total_row="none";

}




        });
     



    }



})

if(!(res[1])){



}
else{

    if(res[1]['mg']=='' && res[1]['sg']==''){
        
   html+='<table class="table table-bordered rec-table" id="rec-table" ><thead>'+theading+'</thead><tbody>';

    }
}


var result=0;
col_pos_val_same=(col_pos_val.every( (val, i, arr) => val === 'N' ) )
if(col_pos_val_same==true){

    show_total_row="none";
}
else{
    show_total_row="bloc";
}



$.each(res,function(key1,value1){

if(value1['ranks']>0){



  if(old_mg!=value1['mg']){


 


  if(i>1){



    if(row_count>1){



var lg=old_sg+' Total';

html+='<tr style="color:black;background-color:#e1e1e1;display:'+show_total_row+';">';

$.each(sub_total,function (key4,value4) {


if(total_col_pos.includes(key4)){

if(isNaN(value4))

{
    value4='';
}

    html+='<td >'+value4+'</td>';
}
else{

html+='<td>'+lg+'</td>' ;
lg='';

}

 
   
})
}
row_count=0;
for(i=0;i<sub_total.length;i++){
sub_total[i]=0;

}

html+='</tr>';



var mg=old_mg+' Total';
html+='<tr style="color:black;background-color:#f0b4b4;">';

$.each(mg_total,function(key6,value6){


if(total_col_pos.includes(key6)){
if(isNaN(value6)){

    value6='';
}


html+='<td>'+value6+'</td>';

}
else{
    html+='<td>'+mg+'</td>';
    mg='';
}


})


   for(i=0;i<mg_total.length;i++){
mg_total[i]=0;

}
html+='</tr>';

           
                
    }       



 html+='</table></div>';


//main group ,main total,sub total generation
           
        html+='<div style="padding:5px;border:2px solid #a7becb;border-radius:6px;" class="shadow mb-1 bg-white rounded"><div class="col-lg-12" id="mg" ><div class="col-lg-6"><div class="row"><div class="col-lg-6" >' +value1['mg']+ '</div></div></div></div> ';
        old_mg=value1['mg']


   
    if(old_sg!=value1['sg'] && old_mg==value1['mg']){
row_count=0;
   html+='</table>' ;

  //sub group generation 
 html+='<div class="col-lg-12"  id="sg"><div class="col-lg-6"><div class="row"><div class="col-lg-6" >' +value1['sg']+ '</div></div></div></div> ';
    

 old_sg=value1['sg'];

 sub_group_count++;

   html+='<table class="table table-bordered rec-table" id="rec-table"><thead>'+theading+'</thead><tbody>';



    }
mt=0;

    }



    if(old_sg!=value1['sg']){

//sub group generation multiple
   


  if(old_sg!=0){

if(row_count>1){


var lg=old_sg+' Total ';




html+='<tr style="color:black;background-color:#e1e1e1;display:'+show_total_row+';">';

$.each(sub_total,function (key4,value4) {


if(total_col_pos.includes(key4)){


    if(isNaN(value4)){
        value4='';
    }

    html+='<td>'+value4+'</td>';
}
else{

html+='<td>'+lg+' </td>' ;
lg='';

}

 
    
})
html+='</tr>';
}
row_count=0;

html+='</table>';
for(i=0;i<sub_total.length;i++){
sub_total[i]=0;

}


  }


 html+='<div class="col-lg-12" id="sg"><div class="col-lg-6"><div class="row"><div class="col-lg-6" >' +value1['sg']+ ' </div></div></div></div> ';
     

 old_sg=value1['sg'];
 sub_group_count++;

   html+='<table class="table table-bordered rec-table" id="rec-table" ><thead>'+theading+'</thead><tbody>';



    }







   new_data.push(Object.values(value1).slice(3));


row_count++;
html+='<tr>';

$.each(new_data[0],function (key3,value3) {

           
              if(total_col_pos.includes(key3)){
                sub_total[key3]+=parseFloat(value3);
         grand_total[key3]+=parseFloat(value3);
                mg_total[key3]+=parseFloat(value3);
              }

             
html+='<td>'+value3+'</td>';

})


html+='</tr>';


flag=true;


}






 new_data.splice(0, new_data.length);

i++;




})




if(i>2){


    //sub total and main total for main group which have more than one sub group

if(old_mg==0){
    if(row_count>1 && sub_group_count>1 ){



var lg=old_sg+' Total';

html+='<tr style="color:black;background-color:#e1e1e1;display:'+show_total_row+';">';

$.each(sub_total,function (key4,value4) {


if(total_col_pos.includes(key4)){

if(isNaN(value4)){
    value4='';
}


    html+='<td >'+value4+'</td>';
}
else{

html+='<td>'+lg+' </td>'; 
lg='';

}

 
    
})
html+='</tr>';
 }
 row_count=0;

 g_total="Grand Total";
html+='<tr style="background-color: #7dea98;color:black;display:'+show_total_row+';">';
    $.each(grand_total,function (key7,value7) {
       
if(total_col_pos.includes(key7)){

if(isNaN(value7)){
    value7='';
}

    html+='<td>'+value7+'</td>';
}
else{
    html+='<td style="font-weight:bold;">'+g_total+'</td>';
    g_total='';
}


    });

html+='</tr>';

   
    html+='</table>' ;

}
else if(old_sg==0){

     //html+='<table style="width:100%;background-color:#f0b4b4;"><tr style="border-bottom:1pt solid #b5e7ec;"> <td colspan="' + l + '" >'+old_mg+' Total</td><td style="text-align:right;" class="rt">' + mt.toFixed(decimal_place)+ '</td></tr></table><br>';
}
else{


if(row_count>1){


var lg=old_sg+' Total';

html+='<tr style="color:black;background-color:#e1e1e1;display:'+show_total_row+';">';

$.each(sub_total,function (key4,value4) {


if(total_col_pos.includes(key4)){

    if(isNaN(value4)){
        value4='';
    }

    html+='<td >'+value4+'</td>';
}
else{

html+='<td>'+lg+' </td>' ;
lg='';

}

 
    
})
html+='</tr>';


};
row_count=0;


var mg=old_mg+' Total';
html+='<tr style="color:black;background-color:#f0b4b4;">';

$.each(mg_total,function(key6,value6){


if(total_col_pos.includes(key6)){

if(isNaN(value6)){
    value6='';
}

html+='<td>'+value6+'</td>';

}
else{
    html+='<td>'+mg+'</td>';
    mg='';
}


})


   for(i=0;i<mg_total.length;i++){
mg_total[i]=0;

}
html+='</tr>';

 g_total="Grand Total ";
html+='<tr style="background-color: #7dea98;color:black;display:'+show_total_row+';">';
    $.each(grand_total,function (key7,value7) {
       
if(total_col_pos.includes(key7)){

    if(isNaN(value7)){
        value7='';
    }

    html+='<td>'+value7+'</td>';
}
else{
    html+='<td style="font-weight:bold;">'+g_total+'</td>';
    g_total='';
}


    });

html+='</tr>';
      
}
mt=0;

}

  


       if(flag==false){
        html+='<div class="col-lg-12 text-center p-5"><div><img src="{{ asset("new_theme/img/icons-folder.gif") }}"></div><h4 class="text-danger fw-bolder">No Records Found!!</h4></div>';
       }
    
$("#td").html(html);








$.each(data.slice(3), function (index, value) {


        fr = value.split("_")[0][0];
        sc = value.split("_")[0][1];
        tr = value.split("_")[0][2];
        i = index;

        i++;

if (fr == "T") {
          




            $("#rec-table tr td:nth-child(" + i + ")").each(function () {

                var value = $(this).text();
              



                    $(this).text(String(value));
                


            })

        }
        else if (fr == "C") {

           


            $("#rec-table tr td:nth-child(" + i + ")").each(function () {

                var value = $(this).text();
           if(!isNaN(value) && value.length!=0){
            
  

                    $(this).text(parseFloat(value).toFixed(decimal_place));
                             }

                


            })
        }

 else if (fr == "I") {   

           


            $("#rec-table tr td:nth-child(" + i + ")").each(function () {

                var value = $(this).text();
           

                    $(this).text(parseInt(value));
                    

                


            })
        }





        if (sc == 'R') {
            $("#rec-table tr td:nth-child(" + i + ")").css("text-align","right");



        }
        else if (sc == "L") {

            $("#rec-table tr td:nth-child(" + i + ")").css("text-align","left");

        }
        else {
            $("#rec-table tr td:nth-child(" + i + ")").css("text-align","center");

        }

          
   
    
    });



var nav_bar_h=$('.topbar').height();
var sub=$("#row1").height();
var filter_id_h=$("#filter_div").height();
//$("#td").css("height", $(window).height()-(nav_bar_h+sub+filter_id_h+300))

console.log( $(window).height()-(nav_bar_h+sub+filter_id_h+230))

// $('.card-body').css("height",$("#td").height()+50);



}
