@extends('components.app')
@section('styles')
@endsection
@section('title')
    Edit Role
@endsection

@section('content')

<!-- Begin Page Content -->
<div class="container">
    
    <!-- Page Heading -->
    {{-- <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Edit User</h1>
        
    </div> --}}
    <div class="d-flex align-items-center p-3 my-3 text-white bg-primary rounded shadow-sm">
        <svg class="bd-placeholder-img flex-shrink-0 me-2 rounded" width="32" height="32" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: 32x32" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="var(--white)"></rect><text x="50%" y="50%" fill="var(--white)" dy=".3em">32x32</text></svg>

        <div class="ml-2">
          <h1 class="h6 mb-0 text-white lh-1">Edit Role</h1>
          <small><a class="text-white" href="{{url('admin/dashboard')}}">dashboard</a> > <a class="text-white" href="{{url($prefix.'manage')}}">manage role</a> > edit role</small>
        </div>
    </div>
    <!-- Content Row -->
    <div class="row">
            <div class="col-sm-12">
                <div class="card mb-4 shadow">
                    <div class="card-body">
                        <form action="{{url($prefix.'update')}}" method="post"> 
                        @csrf
                        <div class="row">
                            <div class="col-sm-6"> 
                                <input type="hidden" name="id" value="{{$role->id}}">
                                <div class="form-group small mb-2">
                                    <label class="small">Guard <span class="text-danger">*</span></label>
                                    <select name="guard_name" id="guard_name" class="form-control @error('guard_name') is-invalid @enderror" >
                                        <option value="web" @if ($role->guard_name == "web") selected @endif>web</option>
                                        <option value="admin" @if ($role->guard_name == "admin") selected @endif>admin</option>
                                    </select>
                                    @error('guard_name')<span class="text-danger small">{{$message}}</span>@enderror 
                                </div>
                                <div class="form-group">
                                    <label class="small">Name <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('name') is-invalid @enderror" value="{{$role->name}}" name="name" aria-describedby="nameHelp" placeholder="Enter Name...">
                                    @error('name')<span class="text-danger small">{{$message}}</span>@enderror 
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <h6>Assign Permissions To Role</h6>
                                <div class="text-right small">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="select-all">
                                        <label class="form-check-label" for="select-all" style="margin-top: 3px;">
                                          Select All
                                        </label>
                                    </div>
                                </div>
                                <div class="row flex-row shadow p-2 rounded small">
                                    @php $permissionIdArr = []  @endphp
                                    @foreach ($role['permissions'] as $item)
                                        @php $permissionIdArr[] = $item['id'] @endphp
                                    @endforeach
                                    @foreach ($permissions as $item)
                                    <div class="col-sm-6">
                                        <div class="form-check">
                                            <input class="form-check-input permissions" type="checkbox" name="permissions[]" value="{{$item->name}}" id="{{$item->id}}" @if(in_array($item->id, $permissionIdArr)) checked @endif>
                                            <label class="form-check-label" for="{{$item->id}}" style="margin-top: 3px;">
                                              {{$item->name}}
                                            </label>
                                        </div>  
                                    </div>
                                    
                                    @endforeach
                                </div>
                            </div>
                        </div>
                        <div class="row mt-4">
                            <div class="col-sm-12">
                                <div class="text-right">
                                    <a class="btn btn-secondary text-uppercase font-weight-bold rounded pl-4 pr-4" href="{{url()->previous()}}">Cancel</a>
                                    <button class="btn btn-primary text-uppercase font-weight-bold rounded pl-4 pr-4" type="submit">Update</button>
                                </div>
                            </div>  
                        </div>
                    </form>
                    </div>
                </div>
                
            </div>
        </div>
        <small class="d-block text-end mt-3">
          
        </small>
      </div>
</div>

@endsection

@section('scripts')
    <script>
        $("#select-all").click(function(){
            $(".permissions").prop('checked', $(this).prop('checked'));
        });

    </script>
@endsection

