@extends('manage.layout')

@section('content')

    <h1>User: {{ $user->name }}</h1>
    <hr/>

    <h2>Core User Details</h2>
    <form id="core-user-form" class="form" method="POST" action="{{ route('manage.user', ['id' => $user->user_id ]) }}">
        @csrf

        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" value="{{ $user->name }}"/>
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" placeholder="Change password" name="password" id="password"/>
        </div>

        <div class="form-group">
            <label for="role">Role</label>
            <select>
                <option value="admin" {{ $user->role == 'admin' ? 'selected' : '' }}>Admin</option>
            </select>
        </div>

        <div class="form-group">
            <button type="submit">Update</button>
        </div>
    </form>

    <script id="core-user-form-loader">
        // request allowed roles to give from the API (/api/allowedRoles)
        // populate the select with the allowed roles
        // set the selected role to the current role
    </script>

    <h2>User Permissions</h2>
    {{--    for loop, and make sure that only if the user has the DELETE_PERMISSION that they can delete it and if they have the UPDATE_PERMISSION that they can update it--}}
    <div id="permissions">
        <table id="permissions">
            <thead>
            <tr>
                <th>Permission</th>
                <th>Actions</th>
            </tr>
            </thead>
            @foreach(auth()->user()->permissions as $permission)
                <tr>
                    <td>{{ $permission->permission_name }}</td>
                    <td>
                        <form action="{{ route('api.user.permission', ['id' => $user->user_id, 'permission_id' => $permission->permission_id]) }}" method="DELETE">
                            @csrf
                            @method('DELETE')
                            <button type="submit">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach
            <form id="add-permission" method="POST"
                  action="{{ route('api.user.permission', ['id' => $user->user_id ]) }}">
                @csrf
                <tr>
                    <td>
                        <input class="text" type="text" name="permission_name" id="permission"/>
                    </td>
                    <td>
                        <button class="button" type="submit">Add</button>
                    </td>
                </tr>
            </form>
        </table>

        <h2>Cinema Assignments</h2>
        <table id="assignments">
            <thead>
            <tr>
                <th>Assignment</th>
                <th>Actions</th>
            </tr>
            </thead>
            @foreach(auth()->user()->cinemas() as $cinema)
                <tr>
                    <td>{{ $cinema->cinema_name }}</td>
                    <td>
                        @if($user->hasPermission('DELETE_CINEMA_ASSIGNMENT'))
                            <a href="{{ route('api.user.assignment', ['id' => $cinema->cinema_id]) }}">
                                Delete
                            </a>
                        @endif
                    </td>
                </tr>
            @endforeach

            @if ($user->hasPermission('UPDATE_USER'))
                <form id="add-assignment" method="POST"
                      action="{{ route('api.user.assignment', ['id' => $user->user_id ]) }}">
                    @csrf
                    <tr>
                        <td>
                            <select name="cinema" id="cinema">
                                @foreach(auth()->user()->cinemas() as $cinema)
                                    <option value="{{ $cinema->cinema_id }}">{{ $cinema->cinema_name }}</option>
                                @endforeach
                            </select>
                        </td>
                        <td>
                            <button class="button" type="submit">Add</button>
                        </td>
                    </tr>
                </form>
            @endif
        </table>
    </div>
@endsection