@extends('layouts.master')
@section('main-body')
<div class="container-fluid">
    <div class="page-header">
        <div class="row">
            <div class="col-sm-12">
                <h3>Users Management</h3>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="/">Dashboard</a></li>
                    <li class="breadcrumb-item active"><a href="/users">Users</a></li>
                </ol>
                @if($loggedUser->can('create-users'))
                <span class="pull-right">{!! Html::decode(link_to_Route('users.create', '<i class="fa fa-plus"></i> Create User', [], ['class' => 'btn btn-primary'])) !!}</span>
                @endif
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-styling" id="user-list">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Email Address</th>
                                    <th>Role</th>
                                    <th>Verified</th>
                                    <th>Status</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($users as $user)
                                <tr>
                                    <td>
                                        @if(empty($user->userProfile))
                                            {!! $user->username !!}
                                        @else
                                            {!! $user->userProfile->first_name.' '.$user->userProfile->last_name !!}
                                        @endif
                                    </td>
                                    <td>{!! $user->email !!}</td>
                                    <td>{!! $user->role->role !!}</td>
                                    <td>
                                        <h6>
                                        @if($user->verified)
                                            <span class="badge badge-primary">Yes</span>
                                        @else
                                            <span class="badge badge-secondary">No</span>
                                        @endif
                                        </h6>
                                    </td>
                                    <td>
                                        <label class="switch">
                                            @if($loggedUser->can('update-users'))
                                                @if($user->status === 'Active')
                                                <input type="checkbox" checked="" id="{{$user->id}}" onchange="updateStatus(this)"><span class="switch-state"></span>
                                                @else
                                                <input type="checkbox" id="{{$user->id}}" onchange="updateStatus(this)"><span class="switch-state"></span>
                                                @endif
                                            @else
                                                @if($user->status === 'Active')
                                                <input type="checkbox" checked="" id="{{$user->id}}" disabled><span class="switch-state"></span>
                                                @else
                                                <input type="checkbox" id="{{$user->id}}" disabled><span class="switch-state"></span>
                                                @endif
                                            @endif

                                        </label>
                                    </td>
                                    <td class="text-center">
                                        <div class="d-none d-md-block">
                                            {!! Form::open(array('route'=>array('users.destroy',$user->id),'class'=>'d-flex justify-content-around' ,'method'=>'DELETE','id'=>'user-remove-form-'.$user->id)) !!}
                                            @if($loggedUser->can('update-users'))
                                            {!! Html::decode(link_to_Route('users.edit','<i class="fa fa-pencil"></i> Edit', $user->id, array('class' => 'm-auto text-dark ', 'type' => 'button')))!!}
                                            @endif
                                            @if($loggedUser->can('view-users'))
                                            <a href="#" class="m-auto text-dark" type="button" id="{{$user->id}}" onclick="viewDetails(this)"><i class="fa fa-eye"></i> View</a>
                                            @endif
                                            @if($loggedUser->can('delete-users'))
                                            <a href="#" class="m-auto text-dark" type="button" id="{{$user->id}}" onclick="userDelete(this)"><i class="fa fa-trash"></i> Delete</a>
                                            @endif
                                            {!! Form::close() !!}
                                        </div>
                                        <div class="btn-group d-md-none">
                                            <button class="btn bg-transparent text-dark dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false"></button>
                                            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                                {!! Form::open(array('route'=>array('users.destroy',$user->id),'class'=>'d-flex justify-content-around align-items-center','method'=>'DELETE','id'=>'user-remove-form-'.$user->id)) !!}
                                                @if($loggedUser->can('update-users'))
                                                {!! Html::decode(link_to_Route('users.edit','<i class="fa fa-pencil"></i>', $user->id, array('class' => 'm-auto text-dark ', 'type' => 'button')))!!}
                                                @endif
                                                @if($loggedUser->can('view-users'))
                                                <a href="#" class="m-auto text-dark" type="button" id="{{$user->id}}" onclick="viewDetails(this)"><i class="fa fa-eye"></i></a>
                                                @endif
                                                @if($loggedUser->can('delete-users'))
                                                <a href="#" class="m-auto text-dark" type="button" id="{{$user->id}}" onclick="userDelete(this)"><i class="fa fa-trash"></i></a>
                                                @endif
                                                {!! Form::close() !!}
                                            </ul>
                                        </div>    
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@if($loggedUser->can('update-roles'))
<div class="modal fade" id="statusModal" tabindex="-1" role="dialog" aria-labelledby="confirmation" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row-9" style="padding: 20px">
                    <div class="form-group">
                        <h5 id="switch-title"></h5>
                    </div>
                    <div class="form-group">
                    <p id="status-body"></p>
                    <input type="hidden" id="userUpdate" value="">
                    <input type="hidden" id="userStatus" value="">
                    </div>
                    <div class="form-group pull-right">
                        <button class="btn btn-secondary" type="button" onclick="revertSwitch()">Back</button>
                        <button class="btn btn-primary" type="button" id="proceed-btn" onclick="proceedUpdateStatus()">Proceed</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endif
@include('modals.confirmation')
@endsection
@section('page-script')

function updateStatus(elem) {

    let isActive = $('#'+elem.id).prop('checked');
    let message = "";
    let title = "";

    $('#status-body').html('');

    if(isActive) {
        title = "Grant user's access?";
        message = "Are you sure you want this user to have access to the system?";
    } else {
        title = "Disable user's access?";
        message = "This user will no longer be able to use this system";
    }

    let newStatus = isActive == true ? 'Active' : 'Inactive';

    $('#userUpdate').val(elem.id);
    $('#userStatus').val(newStatus);
    $('#status-body').html(message);
    $('#switch-title').html(title);

    $('#statusModal').modal({
        backdrop: 'static',
        keyboard: false
    });

    $('#statusModal').modal('show');
}

function proceedUpdateStatus() {

    $.ajax({
        url: '/users/update-status/'+$('#userUpdate').val(),
        method: 'PATCH',
        data: { status: $('#userStatus').val() },
        success: function(result){

            console.log('updated status')
            $('#statusModal').modal('hide');

            $.notify({
                title:'Success!',
                message: 'User status has been updated'
                },
                {
                    type:'primary',
                    allow_dismiss:false,
                    newest_on_top:false ,
                    mouse_over:false,
                    showProgressbar:true,
                    spacing:10,
                    timer:2000,
                    placement:{
                        from:'bottom',
                        align:'right'
                    },
                    offset:{
                        x:30,
                        y:30
                    },
                    delay:1000 ,
                    z_index:10000,
                    animate:{
                        enter:'animated bounce',
                        exit:'animated bounce'
                    }
                });
        }
    });
}

function revertSwitch() {

    let status = $('#userStatus').val() == 'Active' ? false : true;
    $('#'+$('#userUpdate').val()).prop('checked', status);

    $('#statusModal').modal('hide');
}

function userDelete(elem) {

    let userId = elem.value > 0 ? elem.value : elem.id;

    $('#confirmation-title').html("Delete");
    $('#confirmation-body').html("Are you sure you want to delete this user? This process cannot be undone." );

    $('#submit-btn').html('Delete');
    $('#submit-btn').removeClass('btn-primary');
    $('#submit-btn').addClass('btn-danger');
    $('#submit-btn').val(userId);

    $('#detailsModal').modal('hide');

    $('#confirmationModal').modal('show');
}

function submitForm(elem) {
    let = userId = elem.value;
    $('#user-remove-form-'+userId).submit();
}

function viewDetails(elem) {
    window.location.replace('/users/'+elem.id+'/details');
}




@include('notifications.index')
@endsection
