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

 <input type="hidden" id="table_settings_field" branch_id="" branch_name=""  column_heading="<?= $rows->column_heading;?>" column_width="<?= $rows->column_width;?>" column_alighn="<?= $rows->column_alighn;?>"  column_conversion="<?= $rows->column_conversion;?>" settings_row_id="<?= $id;?>" sp_date_picker="<?= $rows->sp_date_picker;?>"  header_from_date="<?=date('d-m-Y', strtotime('-7 days')); ?>"   header_till_date="<?=date('d-m-Y'); ?>"  sp_sort="<?= $rows->menu_id; ?>"  active_mn="<?= $rows->root;?>"  active_mnt="<?= $rows->menu_type;?>">

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

.form-group {
  margin-bottom: 1rem;
}
#emp_info tr td{
  padding:0px;
}


#pnch_tbl tr td{
padding:4px;
}
#dyn_table_id tbody tr:hover{
  cursor: pointer;
  color:#bda839;
  background:#312311;
}
#dyn_table_id tbody tr td{
  padding:3px;
}
#dyn_table_id thead{
  background:#c0ccab;
}

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

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

<div class="row">


<div class="col-lg-2 col-md-2 col-sm-2 mb-2">

<input type="text" class="form-control form-control-sm" id="serch_tbl" placeholder="Search">

</div>

<div class="col-lg-2 col-md-2 col-sm-2 mb-2">

<input type="text" class="form-control form-control-sm date_cls " readonly id="p_date" value="<?php echo date('d-m-y') ?>">

</div>



</div>
<div class="row filter-row1">

<div class="col-lg-12" id="t_div">
<table class="table   table-nowrap mb-0"  id="dyn_table_id" nbr='0' >
<thead>
<tr>
<th>Sl no</th>
<th style="width:300px;">Employee</th>
<th style="width:150px;">IN</th>
<th style="width:150px;">OUT</th>
<th>Worked Hours</th>
<th>Total Worked Hours</th>
<th style="width: 200px;">Attendance</th>
<th style="width: 100px;">OT</th>
<th>OT Wage</th>
<th>Modify punching</th>

</tr>
</thead>
<tbody >
    
</tbody>
</table>
</div>




<input type="hidden" id="emp_id"  readonly>








</div>


</div>




<input type="hidden" id="hidden_status" value="0">
<input type="hidden" id="row_id" value="0">
<input type="hidden" id="flag" value="0">
<input type="hidden" id="shdl_id" value="0">


<div class="modal" tabindex="-1" role="dialog" id="p_div">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">



      <div class="card">
  <div class="card-body">
  <h4 class="title">Punching Details  <h5 id="emp_name_mdl" style="color:blue;"></h5></h4>

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

<table class="table table-sm" >

<tbody id="pnch_tbl"></tbody>

</table>
</div>





  </div>


</div>


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




@endsection




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





$("#p_date").on('change',function(){

  $("#pnch_tbl").html('');
  $("#emp_name").text('');
  $('#dyn_table_id >tbody').html('');
  fetch_emp();
});


function fetch_emp(){


$.ajax({
            type: 'POST',
             async: false,
              url: '/get_data',
              data: {
                   '_token': "{{ csrf_token() }}",
                  'cid':"{{ Session::get('cid')}}",
                   row_id: $.trim($("#p_date").val()),
                   CID: "{{ Session::get('CID')}}",
            url1:'prl_fetch_mnl_pnch.php'
              },
              error: function (request, error) {
                  alert("Please Wait, Loading...");
              },
              success: function (response) {
data=response['data'];
atn_data=response['atn_typs'];

html='';




if(data.length>0){




$(data).each(function(key,val){
  bg_clr='';
  if(val['flg']==1){
    // bg_clr='style="background:#914444;color:white;"';
    //bg_clr='#914444';
  }


whr='';

  add_grid(atn_data);
  $("#dyn_table_id tbody tr:last-child").css('background',bg_clr)
  $("#dyn_table_id tbody tr:last-child").attr('emp_id',val['emp_id'])
  $("#dyn_table_id tbody tr:last-child").find('.e_name').text(val['emp_name']);
  $("#dyn_table_id tbody tr:last-child").find('.lg_tm').val(val['login']);
  $("#dyn_table_id tbody tr:last-child").find('.lgt_tm').val(val['logut']);

if(!val['whr1']){
  whr=val['whr1'];
}
else{
  whr=val['whr1'];

}

  $("#dyn_table_id tbody tr:last-child").find('.whr').val(whr);
  $("#dyn_table_id tbody tr:last-child").find('.total_whr').val(val['t_whr']);



  $("#dyn_table_id tbody tr:last-child").find('.attn_typ').val(val['atn_type']);
  $("#dyn_table_id tbody tr:last-child").find('.ot').val(val['ot']);
  $("#dyn_table_id tbody tr:last-child").find('.ot_wage').val(val['OTWage']);
  bg_clr='';

//   emp_id="'"+val['emp_id']+"'";
//   emp_name="'"+val['emp_name']+"'";

// html+='<tr '+bg_clr+'  onclick="load_p_details('+emp_id+','+emp_name+')" >';

// html+='<td>'+val['emp_name']+'</td>';
// html+='<td><input type="text" class="from-control form-control-sm lg_tm" value="'+val['login']+'" /></td>';
// html+='<td><input type="text" class="from-control form-control-sm lg_tm" value="'+val['logut']+'" /></td>';
// html+='<td><input type="text" class="from-control form-control-sm lg_tm" value="'+val['whr']+'" /></td>';
// html+='<td>'+slct_dp+'</td>';

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



});
}
else{
  html+='<div>No record found!!</div>'
}
$("#emp_tbl tbody").html(html);


       
   
          
      
      }
        
   });

}
fetch_emp();

function numbering(){
    $('.data-class > td:first-child').each(function(i) {
        $(this).html(i+1);
    });
}


function add_grid(atn_data){ 

  slct_dp='';
slct_dp+='<select class="form-select custom-select form-control attn_typ emp_dtl" style="height: 33px;">';
slct_dp+='<option selected value="">-Select-</option>';
$(atn_data).each(function(key,val){
  slct_dp+='<option value="'+val['atn_val']+'">'+val['atn_type']+'</option>';
})
slct_dp+='</select>';



var count=1;
sd_id=1;
if($('#dyn_table_id tr').length>1){
count=$('#dyn_table_id tr:last-child').attr('nbr');

count++;
}


var tr='<tr class="data-class" nbr="'+count+'"  emp_id=""> ';
  tr+='<td>&nbsp;</td>';
  tr+='<td class="e_name"></td>';
  tr+='<td ><input type="text"   class="form-control input-sm  lg_tm time-picker form-control-sm  emp_dtl"  ></td>'; 
  tr+='<td ><input type="text"   class="form-control input-sm  lgt_tm form-control-sm time-picker emp_dtl" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm whr mtime form-control-sm emp_dtl" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm total_whr mtime form-control-sm emp_dtl" ></td>';
  tr+='<td >'+slct_dp+'</td>';
  tr+='<td ><input type="text"   class="form-control input-sm ot  mtime input_text caps  form-control-sm emp_dtl" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm ot_wage   input_text caps form-control-sm emp_dtl"  ></td>';
  tr+='<td style="text-align:center;"><button type="button" class="btn btn-sm btn-outline-info" onclick="md_time(this);"><i class="las la-wrench"></i></button></td>';

  tr+='</tr>';




  sd_id++;




$('#dyn_table_id >tbody').append(tr);

numbering();
load_time();
}

$(document).on('change', '.emp_dtl', function() {


save_form($(this).closest('tr').attr('emp_id'));




});



$(document).on("focus", ".mtime", function() {
  $(this).mask("99:99", {
    reverse: true
  });
});



function md_time(elm){


emp_id=$(elm).closest('tr').attr('emp_id');
emp_name=$(elm).closest('tr').find('.e_name').text();

load_p_details(emp_id,emp_name);

}




function load_p_details(rt,emp_name){

  
$("#p_div").modal('show');
$("#emp_id").val(rt);
$("#emp_name_mdl").text(emp_name);

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

html='';


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

html+='<tr at_id="'+val['at_id']+'" shdl_id="'+val['id']+'"  flag="'+val['type']+'" >';
html+='<td>'+val['desc_name']+'</td>';
html+='<td><input type="text" class="form-control form-control-sm time-picker p_time" value="'+val['p_time']+'" /></td>';
html+='<td><button type="button" class="btn btn-sm btn-success"  onclick="update_time(this)"><i class="las la-edit"></i></button></td>';
html+='<td><button type="button" class="btn btn-sm btn-danger"  onclick="delet_time(this)"><i class="las la-trash"></i></button></td>';


html+='</tr>';



});
       
   $("#pnch_tbl").html(html);
          
   load_time();


   $('html, body').animate({
    scrollTop: $("#p_div").offset().top
  }, 1000)


      }
        
   });

}


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

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

dropdown: false,
scrollbar: false,
interval: 1


});


}


function update_time(elm){
  shdl_id=$(elm).closest('tr').attr('shdl_id');
  at_id=$(elm).closest('tr').attr('at_id');
  flag=$(elm).closest('tr').attr('flag');
  p_time=$(elm).closest('tr').find('.p_time').val();


  if(p_time!=''){








  arr={
              'cid':"{{ Session::get('cid')}}",
              'hidden_status':0,
              'row_id':0,
              'CID': "{{ Session::get('CID')}}",
              'branch_id': "{{ Session::get('b_id')}}",

              
              'emp_id':$("#emp_id").val(),
              'shdl_id':shdl_id,
              'p_time':p_time,
              'flag':flag,
              'at_id':at_id,
             

              'added_by':"{{ Session::get('user_id')}}",
};


           $.ajax({
          url: "/store_data",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              json_data: JSON.stringify(arr),
              url1:'prl_save_punch_mnl.php'
             
          },
          success:function(response){
            
            // console.log(response);

        

            if (response) {



console.log(response['msg']);


$("#emp_tbl tbody").html('');
$("#pnch_tbl").html('');
  $("#emp_name").text('');

  $("#dyn_table_id tbody").html('');
  fetch_emp();


            }else{

            }
          },
          error: function(response) {

            console.log(response)
          }
         });





        }

        else{

          $(elm).closest('tr').find('.p_time').focus().click();
          alert("Time Must not be empty")
        }
  

}









$(document).ready(function(){
  $("#serch_tbl").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#emp_tbl tbody tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});



function save_form(emp_id){
  var dataArray = {};
$(".data-class").each(function(){
if($(this).attr('emp_id')==emp_id){
  dataArray['emp_id']=emp_id;
  dataArray['in_time']=$(this).find('.lg_tm').val();
  dataArray['out_time']=$(this).find('.lgt_tm').val();
  dataArray['whr']=$(this).find('.whr').val();
  dataArray['t_whr']=$(this).find('.total_whr').val();
  dataArray['ot']=$(this).find('.ot').val();
  dataArray['ot_wage']=$(this).find('.ot_wage').val();
  dataArray['entry_date']=$("#p_date").val();

  dataArray['atn_type']=$(this).find('.attn_typ').val()||0;
  



  
}


});

arr={
              'cid':"{{ Session::get('cid')}}",
              'hidden_status':0,
              'row_id':0,
              'CID': "{{ Session::get('CID')}}",
              'branch_id': "{{ Session::get('b_id')}}",

              
              "dataArray":dataArray,

              'added_by':"{{ Session::get('user_id')}}",
};

$.ajax({
          url: "/store_data",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              json_data: JSON.stringify(arr),
              url1:'prl_daily_attn_save.php'
             
          },
          success:function(response){
            
            // console.log(response);
            if (response) {
              console.log(response['msg']);
              $("#emp_tbl tbody").html('');
$("#pnch_tbl").html('');
  $("#emp_name").text('');
  $("#dyn_table_id tbody").html('');
              fetch_emp();
            
            }else{

            }
          },
          error: function(response) {

            //console.log(response)
          }
         });


}



</script>
@endsection