<div class="card-body" id="permission-tables">
    <div class="table-responsive">
        <form action="{{ route('signatures.permission-update', $signature->id)}}" method="post" enctype='multipart/form-data'>
            @csrf
            @method('patch')
            <table class="table table-bordered align-middle" style="width:100%">
                <thead class="table-light">
                    <tr>
                        <th class="thbutton">Sl No</th>
                        <th>Departments</th>
                        <th class="thbutton">Permissions</th>
                    </tr>
                </thead>
                <tbody>
                    {{--<div class="parent-checkbox col-md-8 px-3 mt-3 d-sm-flex">
                        <h5 class="pe-2">All Permissions</h5>
                        <input type="checkbox" class='header-checkbox' class="form-control" id="check-all">
                    </div>--}}
                    @php $i=1;@endphp
                    @php $checked=1;@endphp
                    @if(count($data)>0)
                        @foreach ($data as $key => $signature)
                            <tr>
                                <td>{{$key+1}}</td>
                                <td>{{ $signature->name }}</td>
                                <td class="text-center">
                                    {{--<input class="body-checkbox" type="checkbox" {{($signature->permission == 1) ? 'checked' : ''}} name="test_departments_id[{{ $signature->id }}]">--}}

                                    <input class="form-check-input{{$i}} btn-lg changeStatusCustom body-checkbox" type="checkbox" id="inlineCheckbox{{$i}}" data-url="{{ route('signatures.permission-update', $id)}}" data-id="{{$signature->id}}" data-status="{{$signature->permission == 1 ? '0' : '1'}}" {{ $signature->permission == 1 ? 'checked' : ''}}>
                                </td>
                            </tr>
                            @php
                              if ($signature->permission == 0) {
                                $checked=0;
                              }
                            @endphp
                        @endforeach
                    @else
                        <tr>
                            <td colspan="5">No Data Found</td>
                        </tr>
                    @endif
                        <input type="hidden" class="form-control check-all" data-status="{{$checked == 1 ? '0' : '1'}}">
                </tbody>
            </table>
            {{--<div class="col-12 mt-3 px-3">
                <button class="btn btn-primary" type="submit"><i class="bx bx-save"></i> Update Permissions</button>
            </div>--}}
        </form>
    </div>
</div>

<script type="text/javascript">
    
$(document).ready(function(){

    /*********change status**********/
    $("body").on("click",'.changeStatusCustom', function(event){
      dataString       = {"id":$(this).data('id'),"status":$(this).data('status')};
      var UrlValue     = $(this).data('url');
      var status       = $(this).data('status');
      var changeStatus = $(this);
      var btn = $(this);

      $.ajax({
          url     : UrlValue,
          method  : 'post',
          data    :dataString,
          headers:
          {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          },
          beforeSend: function( xhr ) {
              // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
          },
          success : function(response){
            console.log(response.success);
              if(response.success){
                $(this).Toastshow('success',response.message);
              } else {
                  var ErroMsg = $(this).printErrorMsg(response.message);
                  if (ErroMsg === '') {
                      ErroMsg = "Something went wrong!";
                  }
                  $(this).Toastshow('error',ErroMsg);

                  if (status == 1) {
                      changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
                  } else {
                      changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
                  }
              }
          },
          error: function (data) {
              console.log("error ",data);

              if (status == 1) {
                  changeStatus.parent().find('.ui-switcher').attr('aria-checked', false);
              } else {
                  changeStatus.parent().find('.ui-switcher').attr('aria-checked', true);
              }
          }
      });
    });
});

</script>