@extends('layouts.master')

@section('navigation')
    @include('local.managers.admins._navigation')
@stop

@section('main')

<div class="row">
    <div class="col-md-12">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>School</th>
                    <th>Program</th>
                    <th>Primary Email</th>
                    <th>Role</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{ $user->surnames }}, {{ $user->first_name }}</td>
                    <td>
                    @if(count($user->school))
                        {{ $user->school->name }}
                    @endif
                    </td>
                    <td>
                    @if(count($user->program))
                        {{ $user->program->name }}
                    @endif
                    </td>
                    <td class="email">{{ $user->email }}</td>
                    <td>
                        <select name="role" class="role form-control">
                            <option value="1" <?php if($user->role==1) echo 'selected="selected"'?>>Administrator</option>
                            <option value="2" <?php if($user->role==2) echo 'selected="selected"'?>>School Coordinator</option>
                            <option value="3" <?php if($user->role==3) echo 'selected="selected"'?>>Program Coordinator</option>
                            <option value="4" <?php if($user->role==4) echo 'selected="selected"'?>>Professor</option>
                        </select>
                    </td>


                </tr>
            @endforeach
        </table>
        {{ $users->links() }}
        <button id="submit_roles" class="btn btn-primary btn pull-right">Update this page</button>
    </div>
</div>

@stop

@section('javascript')

$(document).ready(function()
{
    // --------------------------------------------------------------------------
    // Page load
    // --------------------------------------------------------------------------

    $('#submit_roles').on('click', function()
    {
        var userBundle = new Array();
        $('tbody tr').each(function()
        {
            var user = new Object();
            user.email= $(this).find('.email').text();
            user.role= $(this).find('.role').find(':selected').val();

            var clone = jQuery.extend({}, user);
            userBundle.push(clone);
        });

        alert(JSON.stringify(userBundle));

        // Save activity to the database
        $.post
        (
            "{{ URL::action('UsersController@update') }}",
            {
                users: JSON.stringify(userBundle),
                submit_roles: true
            },
            function(data)
            {
                location.reload();
            }
        );

    });

    // --------------------------------------------------------------------------
    // Functions
    // --------------------------------------------------------------------------



    // --------------------------------------------------------------------------
    // Events
    // --------------------------------------------------------------------------


});
@stop