
@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;
}

#dyn_table_id tbody tr td{
  padding: 1px !important;
}
#dyn_table_id thead tr th{
  padding: 0px;
}
#dyn_table_id .form-control{
    margin-bottom: 0px;
    height: 25px;

}
.table td{
    padding:0px !important;
}
</style>
   <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;?>">
    @if ($rows->sedp != "")
  @foreach(explode(',', $rows->sedp) as  $z=>$info) 
     @if ($info == "1" && $z==1)
       <input type="hidden" class="additional_btns" btn_icon="fa fa-pencil" btn_text="Edit" btn_click_function="call_edit_modal" btn_class="btn btn-success btn-sm btn-dtbl" col_width="30" >
     @endif 
     @if ($info == "1" && $z==2)
        <input type="hidden" class="additional_btns" btn_icon="fa fa-trash-o " btn_text="Remove" btn_click_function="call_remove_modal" btn_class="btn btn-danger btn-sm btn-dtbl" col_width="40"  >
     @endif 
     @if ($info == "1" && $z==3)
          <input type="hidden" class="additional_btns" btn_icon="fa fa-print" btn_text="Print" btn_click_function="call_print_function2" btn_class="btn btn-info btn-sm btn-dtbl" col_width="40"  >
     @endif
  @endforeach
@endif


<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 Schedule</a>

</div>
</div>
</div>
<!-- <input type="text" class="date_cls" readonly id="" > -->

<div class="row filter-row">




</div>

<div class="row">
<div class="col-md-12">
<div class="table-responsive" id="table_result">
    
</div>
</div>
</div>
<input type="hidden" id="hidden_status" value="0">
<input type="hidden" id="row_id" value="0">





<div id="add_employee" class="modal custom-modal fade add_main_modal" role="dialog">
<div class="modal-dialog modal-dialog-centered modal-lg custom-design-modal" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><?= $rows->menu_name;?></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">
<form>
<div class="row">

<div class="col-sm-3">
<div class="form-group">
<label class="col-form-label">Profile<span class="text-danger">*</span></label>
<input class="form-control form-control-sm focusable" type="text" id="s_name"> 
</div>
</div>

<div class="col-sm-3">
<div class="form-group">
<label class="col-form-label">Start Date<span class="text-danger">*</span></label>
<input class="form-control form-control-sm focusable date_cls" readonly type="text" id="start_date"> 
</div>
</div>

<div class="col-sm-3">
<div class="form-group">
<label class="col-form-label">No of Punchings<span class="text-danger">*</span></label>

<select id="no_of_pnch" class="form-select fomr-control fomr-control-sm" >
<option value="1">2</option>
<option value="2">4</option>
<option value="3">6</option>
<option value="4">8</option>
<option value="5">10</option>
<option value="6">12</option>

</select>

</div>
</div>

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

<table border="1" class='table table-bordered table-striped fixed_header'  id="dyn_table_id" nbr='0'>
    <thead>
      <tr  class="headings">
          <th style="width: 3%;">SL No</th>
          <th style="width: 3%;">&nbsp;</th>
          <th style="width:23%;">type </th>
           
         
          <th  style="width:12%;">Start Time</th>
 
        <th style="width:12%;">End Time</th>
        <th style="width:40%;">Message</th>
          <th style="width: 5%;">Allowed Time(Min) </th>
          <th style="width: 5%;">&nbsp;</th>
      </tr>
      </thead>

      <tbody></tbody>


    </table>

</div>


<div class="submit-section">

<button type="button" id="add_btn" class="btn btn-info btn-lg" onclick="submit_form();">Save</button>
<button type="button" id="edit_btn" class="btn btn-success btn-lg" onclick="submit_form();">Edit</button>
<button type="button" class="btn btn-danger btn-lg">Reset</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection




@section('page-script')
<script src="{{ asset('project-js\vega-front-scripts.js')}}"></script>
<script type="text/javascript">
  /*var table = $('#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
        } ]
    
        
    });



    


$("#dynamic_ajax_table_wrapper").addClass("row");
$('#dynamic_ajax_table_length').addClass("col-lg-2 col-sm-2 col-md-2 col-2");
    $('#dynamic_ajax_table_length').after('<div id="space" class="col-lg-6 col-sm-6 col-md-6 col-6"> <div class="row filter-row"><div class="col-sm-4 col-md-4 col-lg-4  col-4">   <div class="form-group form-focus focused"><div class="cal-icon"><input class="form-control floating datetimepicker fr_date" type="text"></div><label class="focus-label">From</label></div></div><div class="col-sm-4 col-md-4 col-lg-4  col-4">   <div class="form-group form-focus focused"><div class="cal-icon"><input class="form-control floating datetimepicker to_date" type="text"></div><label class="focus-label">To</label></div></div><div class="col-sm-4 col-md-4 col-lg-4  col-4"><a href="#" class="btn btn-success btn-sm "> Load </a></div></div></div>');
$(".dt-buttons").addClass("col-lg-4 col-sm-4 col-md-4 col-4");*/

//$('#dynamic_ajax_table_length').after('<div id="space" class="col-lg-6 col-sm-6 col-md-6 col-6"> <div class="row filter-row"><div class="col-sm-4 col-md-4 col-lg-4  col-4">   <div class="form-group form-focus focused"><div class="cal-icon"><input class="form-control floating datetimepicker fr_date" type="text"></div><label class="focus-label">From</label></div></div><div class="col-sm-4 col-md-4 col-lg-4  col-4">   <div class="form-group form-focus focused"><div class="cal-icon"><input class="form-control floating datetimepicker to_date" type="text"></div><label class="focus-label">To</label></div></div><div class="col-sm-4 col-md-4 col-lg-4  col-4"><a href="#" class="btn btn-success btn-sm "> Load </a></div></div></div>');


set_mn();

fetch_view_table();


function submit_form() {
 status=0;

 
 if($.trim($('#s_name').val())==''){
    alert('Enter Profile Name');
    $('#s_name').focus();
    status==1;
    return;
 }
 if (status==0) {
Save_form();
 }
}
//http://192.168.18.128/prl_api/ij/prl_branch_edit.php

function call_edit_modal(row_id) {
$('#add_btn').hide();
$('#edit_btn').show();

    $('#row_id').val(row_id);
    $('#hidden_status').val(1);
        $.ajax({
              type: 'POST',
               async: false,
                url: '/get_data',
                data: {
                     '_token': "{{ csrf_token() }}",
                    'cid':"{{ Session::get('cid')}}",
                     row_id: row_id,
              url1:'prl_schdl_edit.php'
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {
 master=response.master;
 detail=response.detail;


          
              $('.add_main_modal').modal('show');
            $('#s_name').val(master[0]['s_name']); 
            $('#start_date').val(master[0]['start_date']); 
            $('#no_of_pnch').val(master[0]['no_of_entry']); 
            
            $('#dyn_table_id >tbody').html('');
           // add_grid();


            for(i=0;i<master[0]['no_of_entry'];i++){
                add_grid();
            }

            $(detail).each(function(key,val){
$("#dyn_table_id tbody  tr:eq("+key+")").find('.desc_name').val(val['desc_name']);
$("#dyn_table_id tbody tr:eq("+key+")").find('.desc_name').attr('s_type',val['type']);
$("#dyn_table_id tbody tr:eq("+key+")").find('.f_time').val(val['f_time']);
$("#dyn_table_id tbody tr:eq("+key+")").find('.to_time').val(val['to_time']);
$("#dyn_table_id tbody tr:eq("+key+")").find('.message').val(val['message']);
$("#dyn_table_id tbody tr:eq("+key+")").find('.allowed_time').val(val['allowed_time']);



            });
            
       // start_focus();   
        }
          
     });  
}

  function Save_form() {

    var arr1=[];
 
 $('.data-class').each(function(i) { 
 

  if( $(this).find('.desc_name').val()!=''){


 thisArray = {  


    type: $(this).find('.desc_name').attr('s_type'),
sd_id: $(this).find('.desc_name').attr('sd_id'),
    desc_name: $(this).find('.desc_name').val(),
    f_time: $(this).find('.f_time').val(),
    to_time: $(this).find('.to_time').val(),
    message: $(this).find('.message').val(),
    allowed_time: $(this).find('.allowed_time').val(),



    
   }
  }
arr1.push(thisArray);

});



  arr={
              'cid':"{{ Session::get('cid')}}",
              'hidden_status':$.trim($('#hidden_status').val()),
              'row_id':$.trim($('#row_id').val()),

              's_name':$.trim($('#s_name').val()),
              'start_date':$.trim($('#start_date').val()),
              'no_of_pnch':$.trim($('#no_of_pnch').val()),
              'dataArray':arr1,
              

              'added_by':"{{ Session::get('user_id')}}",
              'CID':"{{ Session::get('CID')}}",
};
           $.ajax({
          url: "/store_data",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              json_data: JSON.stringify(arr),
              url1:'prl_schdl_save.php'
             
          },
          success:function(response){
            
            console.log(response);
            if (response) {
              alert(response['msg']);
            
               $('.add_main_modal').modal('hide');
               fetch_view_table();
            }else{

            }
          },
          error: function(response) {
          }
         });
  }

  $( ".add_main_modal" ).on('hidden.bs.modal', function(){ $('#add_btn').show();
$('#edit_btn').hide();
 $('#hidden_status').val(0);
 $('.add_main_modal .form-control').val('');
});



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

add_grid();
function add_grid(){ 


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


var tr='<tr class="data-class" nbr="'+count+'"> ';
  tr+='<td>&nbsp;</td>';
  tr+='<td >IN</td>';
  tr+='<td ><input type="text"   class="form-control input-sm  desc_name  input_text caps"  s_type="0"  sd_id="'+sd_id+'" ></td>'; 
  tr+='<td ><input type="text"   class="form-control input-sm  f_time  time-picker input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm to_time time-picker input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm message   input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm allowed_time   input_text caps" onkeypress="return isNumber(event,this.value);" ></td>';
tr+='<td rowspan="2" ><button type="button" class="btn btn-sm btn-danger" onclick="remove_grid('+sd_id+');"><i class="fa fa-trash"></i></button></td>';
  tr+='</tr>';
  count+=1;
 tr+='<tr class="data-class" nbr="'+count+'"> ';
  tr+='<td>&nbsp;</td>';
  tr+='<td >OUT</td>';
  tr+='<td ><input type="text"  class="form-control input-sm qty desc_name  input_text caps"  s_type="1" sd_id="'+sd_id+'" ></td>'; 
  tr+='<td ><input type="text"   class="form-control input-sm  f_time  time-picker input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm to_time time-picker input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm message   input_text caps" ></td>';
  tr+='<td ><input type="text"   class="form-control input-sm allowed_time   input_text caps" onkeypress="return isNumber(event,this.value);" ></td>';
tr+='';
  tr+='</tr>';


  sd_id++;




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

numbering();
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());
}







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





    $('#dyn_table_id >tbody').html('');

val=$(this).val();

for(i=0;i<val;i++){
     add_grid();
    
}



});



function remove_grid(sd_id){

if($(".data-class").length>2){

  $(".data-class").each(function(i){



if($(this).find('.desc_name').attr('sd_id')==sd_id){

  $(this).remove();
  
numbering();
}



})
}
else{
  alert("You Cant Delete Last Row");
}


}


</script>
@endsection