@extends('layouts.app')

@section('content')
<div class="row">
    <div class="col-sm-3">
        @include('side_menu')
    </div>
    <div class="col-md-9">
        <div class="row">

            <div class="col-sm-12 add-container">
                <form method="POST" action="/user/save">
                    @csrf
                    @if($selectedUser != null)
                    <input type="hidden" id="selected_user" name="selected_user" value="{{$selectedUser->user_id}}" />
                    @endif
                    <div class="container">
                        <div class="row">
                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="name">{{ __('Name') }}</label>
                                    <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="@if($selectedUser != null){{$selectedUser->name}}@else{{ old('name') }}@endif" required autocomplete="name" autofocus>
                                    @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="user_type">{{ __('User Type') }}</label>
                                    <select name="user_type" id="user_type" class="form-control @error('user_type') is-invalid @enderror" required autofocus>
                                        <option value=""></option>
                                        @foreach($user_roles as $role)
                                        @if($selectedUser != null)
                                            <option @if($selectedUser->user_role_id == $role->user_role_id) selected @endif value="{{$role->user_role_id}}">{{$role->role}}</option>
                                        @else
                                            <option value="{{$role->user_role_id}}">{{$role->role}}</option>
                                        @endif
                                        @endforeach
                                    </select>
                                    @error('user_type')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="email">{{ __('E-Mail Address') }}</label>
                                    <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="@if($selectedUser != null){{$selectedUser->email}}@else{{ old('email') }}@endif" required autocomplete="email">
                                    @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="msisdn">{{ __('Phone Number') }}</label>
                                    <input id="msisdn" type="text" class="form-control @error('msisdn') is-invalid @enderror" name="msisdn" value="@if($selectedUser != null){{$selectedUser->msisdn}}@else{{ old('msisdn') }}@endif" required autocomplete="msisdn" autofocus>
                                    @error('msisdn')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="institution">{{ __('Institution') }}</label>
                                    <select name="institution_id" id="institution_id" class="form-control @error('institution_id') is-invalid @enderror" autofocus>
                                        <option value=""></option>
                                        @foreach($institutions as $institution)
                                        @if($selectedUser != null)
                                            <option @if($selectedUser->institution_id == $institution->institution_id) selected @endif value="{{$institution->institution_id}}">{{$institution->institution_name}}</option>
                                        @else
                                            <option value="{{$institution->institution_id}}">{{$institution->institution_name}}</option>
                                        @endif
                                        @endforeach
                                    </select>
                                    @error('institution')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            @if($selectedUser == null)
                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="password">{{ __('Password') }}</label>
                                    <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
                                    @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>
                            @endif
                        </div>

                        @if($selectedUser == null)
                        <div class="row">
                            <div class="col-sm-6 users-f">
                                <div class="form-group">
                                    <label for="password-confirm">{{ __('Confirm Password') }}</label>
                                    <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                                </div>
                            </div>
                        </div>
                        @endif

                        <div class="form-group row mb-0" style="margin-top: 5px;">
                            <div class="col-sm-6">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Save') }}
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <div class="container">
                <div class="row search-mg">
                    <div class="col-md-12 search-col">
                        <button class="btn float-right search-bt" onclick="searchUsers()">Search</button>
                        <input type="text" class="form-control float-right search-txt" id="search-user" name="search-user" placeholder="Search" />
                    </div>
                </div>
                <div class="row t-header">
                    <div class="col-3">Name</div>
                    <div class="col-3">Institution</div>
                    <div class="col-2">Phone Number</div>
                    <div class="col-2">Status</div>
                    <div class="col-2"></div>
                </div>
                <div id="users-data">
                    @foreach ($users as $user)
                    <div class="row t-content">
                        <div class="col-3">
                            {{ $user->name }}
                        </div>
                        <div class="col-3">
                            {{ ($user->institution != null)?$user->institution->institution_name:"" }}
                        </div>
                        <div class="col-2">
                            {{ $user->msisdn }}
                        </div>
                        <div class="col-2">
                            {{ $user->status }}
                        </div>
                        <div class="col-2 edit-butts">
                            <span>
                                <a href="/user/edit/{{$user->user_id}}">
                                    <img src="/images/edit.png" width="15px" alt="Edit" />
                                </a>
                                <button rel="{{$user->user_id}}" class="btn deluser" data-toggle="modal" data-target="#usersModal">
                                    <img src="/images/delete.png" width="15px" alt="Delete" />
                                </button>
                            </span>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>

        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="usersModal" tabindex="-1" role="dialog" aria-labelledby="usersModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="usersModalLabel">Confirm Delete</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Are you sure you would like to delete the selected user?
                </div>
                <div class="modal-footer">
                    <div class="row" style="width:100%">
                        <div class="col-8">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        </div>
                        <div class="col-4">
                            <form action="/user/del" method="post">
                                @csrf
                                <input type="hidden" id="del-user" name="deleteuser" />
                                <button type="submit" class="btn btn-primary">Confirm</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection