
@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;
}
</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 User Group</a>

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


<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-4">
<div class="form-group">
<label class="col-form-label"> User Group<span class="text-danger">*</span></label>
<input class="form-control focusable" autocomplete="off" type="text" id="ug_name">
</div>
</div>
<div class="col-sm-8">

    <h4 class="card-title text-primary">Menus</h4>
    <div class="form-group row"><label class="col-sm-4 form-label"></label>
      <div class="col-sm-8  ">
        <div class="form-check form-check-success  form-check-inline">
          <label class="form-check-label">
            <input type="checkbox" class="form-check-input" name="" onclick="select_multiple('s',this.checked);" > Save <i class="input-helper"></i>
          </label>
        </div>
        <div class="form-check form-check-success  form-check-inline">
          <label class="form-check-label">
            <input type="checkbox" class="form-check-input" name="" onclick="select_multiple('e',this.checked);" > Edit <i class="input-helper"></i>
          </label>
        </div>
        <div class="form-check form-check-success  form-check-inline">
          <label class="form-check-label">
            <input type="checkbox" class="form-check-input" name="" onclick="select_multiple('d',this.checked);" > Delete <i class="input-helper"></i>
          </label>
        </div>
        <div class="form-check form-check-success  form-check-inline">
          <label class="form-check-label">
            <input type="checkbox" class="form-check-input" name="" onclick="select_multiple('p',this.checked);" > Print <i class="input-helper"></i>
          </label>
        </div>
      </div>
    </div>
    <div class="col-md-12" id="menu_all" style="height:200px;overflow-y:scroll;">
    
    </div>

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

  
   $(document).ready(function() {


            // Select2 Multiple
            /*$('#select2Multiple').select2({
                placeholder: "Select",
                allowClear: false
            });
*/
           $('#select2Multiple').on("select2:unselecting", function(e){

           
      var unselected_value = e.params.args.data.id;
         $('.ug_id'+unselected_value).remove();
    }).trigger('change');

        });
set_mn();

fetch_view_table();





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

    $('#row_id').val(row_id);
    $('#hidden_status').val(1);
    $.ajax({
          url: "/edit_user_group",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              row_id:row_id
          },
          error: function(response) {
          },
          error: function(response) {
          },
          success:function(response){
             $('.add_main_modal').modal('show');
              details=response['details'];
           $('#ug_name').val(response['edit'].ug_name);

$(".form-check-input").prop('checked',false);
           $(details).each(function(key,val){


            
            sedp=val['sedp'].split(',');
            
 
            $('input[name="ugm'+val['menu_id']+'"]').each(function(){

            

if(sedp[0]==1){

  if($(this).hasClass('cs')){
                $(this).prop('checked',true);
              }


              console.log("cs")
}

if(sedp[1]==1){
 
  if($(this).hasClass('ce')){
    
                $(this).prop('checked',true);
              }

}

if(sedp[2]==1){

  if($(this).hasClass('cd')){
                $(this).prop('checked',true);
              }

}

if(sedp[3]==1){

  if($(this).hasClass('cp')){
                $(this).prop('checked',true);
              }


}

            });

            

           });
          }
         });
}

  function Save_form() {
    marr=[];
    menu_parent='';
    c_string='';
     $('.menu_list').each(function(){
      menu_id=$(this).attr('menu_id');
      
     
      if($('input[name="ugm'+menu_id+'"]:checked').length>0){

        if(menu_parent!=$(this).attr('menu_parent') && $(this).attr('menu_parent')!=''){ 
    menu_parent=$(this).attr('menu_parent');
      arr={
          menu_id:menu_parent,
          sedp:',,,'
        }
         marr.push(arr);
      }
     

        c_string='';
       $('input[name="ugm'+menu_id+'"]').each(function(){
         
         if(this.disabled){
          c_string+=',';
         }else{
          if(this.checked){
            c_string+='1,';
          }else{
            c_string+='0,';
          }
          }
        });
        c_string = c_string.slice(0, -1);
        arr={
          menu_id:menu_id,
          sedp:c_string
        }
         marr.push(arr);
      }

     });
     // console.log(marr);
           $.ajax({
          url: "/op_user_group",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
              hidden_status:$.trim($('#hidden_status').val()),
              row_id:$.trim($('#row_id').val()),
              ug_name:$.trim($('#ug_name').val()),
              marr:marr
          },
          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 submit_form() {
    status=0;
      if ($.trim($('#ug_name').val())=='') {
      alert('Enter USer Group ');
       status=1;
      return;
    }
     if ($('input[type="checkbox"]:checked').length=='0') {
      alert('Assign Atleast One Menu To User Group ');
       status=1;
      return;
    }
    if(status==0) Save_form();
  }


  get_menus();
 
  function get_menus() {
           $.ajax({
          url: "/get_menus",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
             ug_id:''
          },
          error: function(response) {
          },
          success:function(response){
            res='';
          var data = JSON.parse(response);
         if(data.length>0){
          mc='';
          $(data).each(function(i,val){
            menu=['Save','Edit','Delete','Print'];
            cls=['s','e','d','p'];
            
            if(val['menu_type']==0){
              mc=val['menu_id'];
            }
             if(val['menu_type']==3){
              mc='';
            }
              if(val['menu_type']!=0){
          res+='<div class="form-group row">';
      res+='<label class="col-sm-4 form-label">'+val['menu_name']+'</label>';
      res+='<div class="col-sm-8  menu_list" menu_id="'+val['menu_id']+'" menu_parent="'+mc+'">'; //alert(val['sedp']);
      arr=val['sedp'].split(',');
     /*  $(arr).each(function(j,val_c){ */
      for (var j = 0; j <4; j++) {
    //  alert(arr[j]);
        chk='disabled="" ';
        if(arr[j]=='0'){
          chk='';
        }else if(arr[j]=='1'){
          chk='checked=""';
        }
        res+='<div class="form-check form-check-primary  form-check-inline">';
          res+='<label class="form-check-label">';
            res+='<input type="checkbox" class="form-check-input c'+cls[j]+'"  name="ugm'+val['menu_id']+'" '+chk+'> '+menu[j]+' <i class="input-helper"></i></label>';
        res+='</div>';
       };
        res+='</div>';
      res+='</div>';
    res+='</div>';
}

            });
         }
         $('#menu_all').html(res);
          }
         });
  }
  function select_multiple(args,chk) { 
    if(chk){
        $('.c'+args).prop('checked',true);
    }else{
      $('.c'+args).prop('checked',false);
    }
    $('input[type="checkbox"]:disabled').prop('checked',false);
    
  }


  $(".form-check-input").prop('checked',false);
</script>
@endsection