<!-- Extend Layout -->
@extends('layout.app')
<!-- Add Custom CSS -->
@push('css')
<link rel="stylesheet" href="{{asset('/')}}plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
<link rel="stylesheet" href="{{asset('/')}}plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
<link rel="stylesheet" href="{{asset('/')}}plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
@endpush
<!-- Add Content Here -->
@section('content')
<!-- Include Sidebar -->
@include('layout.navbars.sidebar')
<div class="content-wrapper">
    <!-- Main content -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Manajemen User</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Admin</a></li>
              <li class="breadcrumb-item active">Manajemen User</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <section class="content">
      <div class="card">
        <div class="card-header">
          <h3 class="card-title">Title</h3>
  
          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>
        <div class="card-body">
          @if ($errors->any())
            <div class="alert alert-danger">
              <ul>
                @foreach ($errors->all() as $error )
                  <li>{{ $error }}</li>
                @endforeach
              </ul>
            </div>
          @endif
          @if(session('success'))
          <div class="alert alert-success">
            <ul>
                <li>{{ session('success') }}</li>
            </ul>
          </div>
          @endif
          @if(session('error'))
          <div class="alert alert-danger">
            <ul>
                <li>{{ session('error') }}</li>
            </ul>
          </div>
          @endif
          <button class="btn btn-primary" data-toggle="modal" data-target="#modal-default">Tambah Data</button>
          <table id="example1" class="table table-bordered table-striped">
            <thead>
            <tr>
              <th>No</th>
              <th>Nama Role User</th>
              <th>Action</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($data as $datas)
            <tr>
              <td>{{ $loop->iteration }}</td>
              <td>{{ $datas->name }}</td>
              <td class="text-center"><button class="btn btn-success" data-toggle="modal" data-target="#modal{{ $loop->iteration }}">Edit</button> | 
                <button class="btn btn-primary" data-toggle="modal" data-target="#assign{{ $loop->iteration }}">Assign Roles</button> |
                <button class="btn btn-danger" data-toggle="modal" data-target="#deassign{{ $loop->iteration }}">Deassign Roles</button> 
            </tr>
            <div class="modal fade" id="modal{{ $loop->iteration }}">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h4 class="modal-title">Edit Nama Roles</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div class="row">
                      <div class="col">
                        <form action="#" method="post">
                        @csrf
                        <label for="role_name">Nama Role:</label>
                        <input type="text" name="role_name" id="role_name" class="form-control" placeholder="Masukkan nama Role" value="{{ $datas->name }}" required>
                      </div>
                      <!-- /.col -->
                    </div>
                  </div>
                  <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Tambah</button>
                  </form>
                  </div>
                </div>
                <!-- /.modal-content -->
              </div>
              <!-- /.modal-dialog -->
            </div>
            <div class="modal fade" id="assign{{ $loop->iteration }}">
              <div class="modal-dialog modal-lg">
                <div class="modal-content">
                  <div class="modal-header">
                    <h4 class="modal-title">Assign <b>{{ $datas->name }}</b>  Ke:</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div class="row">
                      <div class="col">
                        <form action="{{ route('users-roles.assign')}}" method="post">
                        @csrf
                        <input type="hidden" name="roles_user" value="{{ $datas->name }}">
                        <label class="col-form-label">User: </label>
                        <div class="form-check">
                          <input type="checkbox" class="form-check-input" id="checkAll">
                          <label for="checkAll" class="form-check-label">Check All</label>
                        </div>
                        @foreach ($user as $users)
                        <div class="form-check">
                          <input type="checkbox" class="itemCheckbox form-check-input" name="checkboxes[]" value="{{$users->id}}">
                          <label for="{{$users->id}}" class="form-check-label">{{ $users->nama_gelar }}</label>
                        </div>
                        @endforeach
                        </select>
                      </div>
                      <!-- /.col -->
                    </div>
                  </div>
                  <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Tambah Role</button>
                  </form>
                  </div>
                </div>
                <!-- /.modal-content -->
              </div>
              <!-- /.modal-dialog -->
            </div>
            <div class="modal fade" id="deassign{{ $loop->iteration }}">
              <div class="modal-dialog modal-lg">
                <div class="modal-content">
                  <div class="modal-header">
                    <h4 class="modal-title">DeAssign <b>{{ $datas->name }}</b>  Ke:</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div class="row">
                      <div class="col">
                        <form action="{{ route('users-roles.deassign')}}" method="post">
                        @csrf
                        <input type="hidden" name="roles_user" value="{{ $datas->name }}">
                        <label class="col-form-label">User: </label>
                        <div class="form-check">
                          <input type="checkbox" class="form-check-input" id="checkAll">
                          <label for="checkAll" class="form-check-label">Check All</label>
                        </div>
                        @foreach ($user as $users)
                        <div class="form-check">
                          <input type="checkbox" class="itemCheckbox form-check-input" name="checkboxes[]" value="{{$users->id}}">
                          <label for="{{$users->id}}" class="form-check-label">{{ $users->nama_gelar }}</label>
                        </div>
                        @endforeach
                        </select>
                      </div>
                      <!-- /.col -->
                    </div>
                  </div>
                  <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-danger">Hapus Role</button>
                  </form>
                  </div>
                </div>
                <!-- /.modal-content -->
              </div>
              <!-- /.modal-dialog -->
            </div>
            @endforeach
            </tbody>
            <tfoot>
              <tr>
                <th>ID</th>
                <th>Nama Role User</th>
                <th>Action</th>
              </tr>
            </tfoot>
          </table>
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
          .
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->
      <!-- /.Modal ADD DATA -->
      <div class="modal fade" id="modal-default">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Tambah Data</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <div class="row">
                <div class="col">
                  <form action="{{ route('users-roles.store') }}" method="post">
                  @csrf
                  <label for="role_name">Nama Role:</label>
                  <input type="text" name="role_name" id="role_name" class="form-control" placeholder="Masukkan nama Role" value="{{ old('role_name')}}" required>
                </div>
                <!-- /.col -->
              </div>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="submit" class="btn btn-primary">Tambah</button>
            </form>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
    </section>
    <!-- /.content -->
</div>
@endsection

<!-- Add Custom JS -->
@push('js')
<!-- DataTables  & Plugins -->
<script src="{{asset('/')}}plugins/datatables/jquery.dataTables.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="{{asset('/')}}plugins/jszip/jszip.min.js"></script>
<script src="{{asset('/')}}plugins/pdfmake/pdfmake.min.js"></script>
<script src="{{asset('/')}}plugins/pdfmake/vfs_fonts.js"></script>
<script src="{{asset('/')}}plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="{{asset('/')}}plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
<!-- Page specific script -->
<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true, "lengthChange": false, "autoWidth": false,
    }).container().appendTo('#example1_wrapper .col-md-6:eq(0)');
    //checkAll button
  });
  const checkAll = document.getElementById('checkAll');
  const itemCheckboxes = document.querySelectorAll('.itemCheckbox');

  checkAll.addEventListener('change', function() {
    for (const checkbox of itemCheckboxes) {
      checkbox.checked = this.checked;
    }
  });

  // Optionally, update the "Check All" text based on checked items:
  itemCheckboxes.forEach(checkbox => {
    checkbox.addEventListener('change', function() {
      checkAll.checked = [...itemCheckboxes].every(box => box.checked);
    });
  });
</script>
@endpush

