
@extends('layouts.admin')
@section('pageTitle', 'Vega Soft')
@section('page-content')



<style>
#dynamic_ajax_table_length ,.dt-buttons ,#dynamic_ajax_table_filter{
    //float: left;
}
.custom-design-modal{
max-width:70%;
}
.dt-buttons{
    height: 34px;
}

.fc td:hover{
    cusror:pointer;
}
  
#mg{

background-color: #b3f4eb;
width: 100%;
}
#sg{
background-color: #e2f4af;
    width: 100%;
}
#grand_total{
background-color: #b8ffc9;
    width: 100%;
    font-weight: bold;
}

#rec-table tr td{
    padding: 4px;
}#rec-table tr th{
    padding: 6px;
}
.page-header{
    margin-bottom:6px !important;
}


#rec-table_length{


margin-bottom:-66px;

}



</style>


<div class="page-header">
<div class="row align-items-center">
<div class="col">
<h3 class="page-title"><?= $rows->menu_name;?></h3>
</div>
<div class="col-auto float-end ms-auto">
<!-- <a href="#" class="btn add-btn" data-bs-toggle="modal" data-bs-target="#add_employee"><i class="fa fa-plus"></i>Add Holiday</a> -->

</div>
</div>
</div>


<div class="row filter-row">




</div>

<div class="row">
<div class="col-md-12">


<div class="page-header">
<div class="row">
<div class="col">

<ul class="breadcrumb">

</ul>
</div>
<div class="col-auto">
<!-- <a href="#" class="btn btn-primary">PDF</a> -->
</div>
</div>
</div>

<div class="row">


<div class="col-lg-4">

<div class="row">

<div class="col-lg-5">
<div class="form-group">
<label class="col-form-label">Date</label>
<input class="form-control form-control-sm focusable date_cls" readonly type="text" id="start_date"> 
</div>
</div>

<div class="col-lg-6">
<div class="form-group">
<label class="col-form-label">Employee</label>
<select class="form-control form-select" id="emp_id" style="height:30px;">
<option value="0" >-All-</option>
@foreach($employees as $ep)

<option value="{{$ep->emp_id}}">

{{$ep->emp_name}}
</option>

@endforeach


</select>
</div>
</div>






<div class="col-lg-1">
<label class="col-form-label">&nbsp;</label>
    <button type="button" class="btn btn-sm btn-warning" onclick="load_data();">Load</button>
</div>

</div>

</div>

<div class="col-lg-8">
<div class="row justify-content-center">




<div class="col-md-2 col-sm-6">
<div class="card">
<div class="card-body text-center">
<h3><b id="tot_emp">0</b></h3>
<p style="font-size: 11px;">Total Employees</p>
</div>
</div>
</div>


<div class="col-md-2 col-sm-4">
<div class="card">
<div class="card-body text-center">
<h3 class="text-success"><b id="p_tdy">0</b></h3>
<p style="font-size: 11px;">Punched</p>
</div>
</div>
</div>

<div class="col-md-2 col-sm-4">
<div class="card">
<div class="card-body text-center">
<h3 class="text-danger"><b  id="p_lev">0</b></h3>
<p style="font-size: 11px;">Not Punched</p>
</div>
</div>
</div>


<div class="col-md-2 col-sm-4">
<div class="card">
<div class="card-body text-center">
<h3><b>0</b></h3>
<p style="font-size: 11px;">IN</p>
</div>
</div>
</div>

<div class="col-md-2 col-sm-4">
<div class="card">
<div class="card-body text-center">
<h3><b>0</b></h3>
<p style="font-size: 11px;">Out</p>
</div>
</div>
</div>



</div>




</div>


</div>




<div class="row">

<div class="col-lg-12 ScrollStyle" id="td" >
    
</div>


</div>



</div>
</div>
<input type="hidden" id="hidden_status" value="0">
<input type="hidden" id="row_id" value="0">


<div id="printTable" style=""></div>


@endsection




@section('page-script')
<script src="{{ asset('project-js\vega-front-scripts.js')}}"></script>
<script type="text/javascript">



    


set_mn();

fetch_view_table();

  


	
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+230))

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

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



}



function load_data(){

 $.ajax({
              type: 'POST',
               async: false,
                url: '/get_data_shdl',
                data: {
                     '_token': "{{ csrf_token() }}",
                    'cid':"{{ Session::get('cid')}}",
                     row_id: $("#start_date").val(),
                     emp_id: $("#emp_id").val(),
                     CID: "{{ Session::get('CID')}}",
              url1:'rpt_punch.php'
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {

build_report(response.data)

$("#tot_emp").text(response.emp_cnt)
$("#p_tdy").text(response.p_tdy)
$("#p_lev").text(response.p_lev)
var table = $('#rec-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
        } ]
        
        
    });   
            
        }
          
     });




}





















</script>



@endsection