@extends('admin.layouts.main',array(
    'scripts' => array(
        Config::get('path.backoffice.js_path') . '/view/user/backend-user-form.js'
    )
))

@section('content')
    <div class="row">
        <div class="col-xs-10">
            <h1>Manage User</h1>
        </div> 
        @if($canView)
            <div class="col-xs-2">
                <a href="{{ url('/admin/admin_users'); }}" class="back-btn btn btn-default" type="button" data-toggle="tooltip" data-placement="bottom" title="" class="btn btn-default pull-right" data-original-title="Go back to list">
                    <span class="icon glyphicon glyphicon-th-list"></span>
                </a>
            </div>
        @endif
    </div>
    <div id="edit_user_form" class="user-form">
        {{ Form::model($user) }}
            <div class="row">
                <div class="col-xs-6">
                    <div class="form-group <?=$errors->first('username', 'has-error')?>">
                        <label class="control-label" for="username_input">Username</label>
                        {{ Form::text('username', null, array( 
                            'id' => 'username_input',
                            'class' => 'form-control', 
                            'maxlength' => '20',
                            'data-toggle' => ($errors->has('username') ? 'tooltip': null),
                            'title' => ($errors->has('username') ? $errors->first('username'): null),
                            'disabled' => $canEdit ? null : 'disabled')) }}
                    </div>
                    <div class="form-group <?=$errors->first('email', 'has-error')?>">
                        <label class="control-label" for="email_input">Email</label>
                        {{ Form::text('email', null, array(
                            'id' => 'email_input',
                            'class' => 'form-control', 
                            'maxlength' => '100',
                            'data-toggle' => ($errors->has('email') ? 'tooltip': null),
                            'title' => ($errors->has('email') ? $errors->first('email'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    <div class="form-group <?=$errors->first('first_name', 'has-error')?>">
                        <label class="control-label" for="first_name_input">First Name</label>
                        {{ Form::text('first_name', null, array( 
                            'id' => 'first_name_input',
                            'class' => 'form-control', 
                            'maxlength' => '50',
                            'data-toggle' => ($errors->has('first_name') ? 'tooltip': null),
                            'title' => ($errors->has('first_name') ? $errors->first('first_name'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    
                    <div class="form-group <?=$errors->first('last_name', 'has-error')?>">
                        <label class="control-label" for="last_name_input">Last Name</label>
                        {{ Form::text('last_name', null, array( 
                            'id' => 'last_name_input',
                            'class' => 'form-control', 
                            'maxlength' => '50',
                            'data-toggle' => ($errors->has('last_name') ? 'tooltip': null),
                            'title' => ($errors->has('last_name') ? $errors->first('last_name'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    @if($canEdit)
                    <div class="form-group <?=$errors->first('password', 'has-error')?>">
                        <label class="control-label" for="password_input">{{ Lang::get('backend.password') }}</label>
                        <span class="icon glyphicon glyphicon-question-sign pull-right" data-toggle="tooltip" title="{{ $user->exists ? Lang::get('backend.password_leave_empty') : '6 to 12 alpha-numeric characters' }}" class="btn btn-default pull-right"></span>
                        {{ Form::password('password', array( 
                            'id' => 'password_input', 
                            'class' => 'form-control', 
                            'maxlength' => '20',
                            'data-toggle' => ($errors->has('password') ? 'tooltip': null),
                            'title' => ($errors->has('password') ? $errors->first('password'): null))) }}
                       
                    </div>
                    
                    <div class="form-group <?=$errors->first('password_confirmation', 'has-error')?>">
                        <label class="control-label" for="password_conf_input">{{ Lang::get('backend.password_confirm') }}</label>
                        {{ Form::password('password_confirmation', array( 
                            'id' => 'password_conf_input', 
                            'class' => 'form-control', 
                            'maxlength' => '20')) }}
                    </div>
                    @endif
                </div>
                <div class="col-xs-6">
                    <div class="form-group <?=$errors->first('location', 'has-error')?>">
                        <label class="control-label" for="location_input">Location</label>
                        {{ Form::text('location', null, array( 
                            'id' => 'location_input',
                            'class' => 'form-control', 
                            'maxlength' => '100',
                            'data-toggle' => ($errors->has('location') ? 'tooltip': null),
                            'title' => ($errors->has('location') ? $errors->first('location'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    <div class="form-group <?=$errors->first('contact_1', 'has-error')?>">
                        <label class="control-label" for="contact_1_input">Contact 1</label>
                        {{ Form::text('contact_1', null, array( 
                            'id' => 'contact_1_input',
                            'class' => 'form-control', 
                            'maxlength' => '40',
                            'data-toggle' => ($errors->has('contact_1') ? 'tooltip': null),
                            'title' => ($errors->has('contact_1') ? $errors->first('contact_1'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    <div class="form-group <?=$errors->first('contact_2', 'has-error')?>">
                        <label class="control-label" for="contact_2_input">Contact 2</label>
                        {{ Form::text('contact_2', null, array( 
                            'id' => 'contact_2_input',
                            'class' => 'form-control', 
                            'maxlength' => '40',
                            'data-toggle' => ($errors->has('contact_2') ? 'tooltip': null),
                            'title' => ($errors->has('contact_2') ? $errors->first('contact_2'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div>
                    <div class="form-group <?=$errors->first('role', 'has-error')?>">
                        <label class="control-label" for="role_input">Role</label>
                        {{ Form::text('role', null, array( 
                            'id' => 'role_input',
                            'class' => 'form-control', 
                            'maxlength' => '50',
                            'data-toggle' => ($errors->has('role') ? 'tooltip': null),
                            'title' => ($errors->has('role') ? $errors->first('role'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div> 
                    
                    <div class="form-group <?=$errors->first('type_id', 'has-error')?>">
                        <label class="control-label" for="type_id_input">Type</label>
                        {{ Form::select('type_id', $userTypes, null, array( 
                            'id' => 'type_id_input',
                            'class' => 'form-control',
                            'data-toggle' => ($errors->has('type_id') ? 'tooltip': null),
                            'title' => ($errors->has('type_id') ? $errors->first('type_id'): null),
                            'disabled' => $canManage ? null : 'disabled')) }}
                    </div> 
                </div>
            </div>
            <div class="row user-permissions-wrapper">
                <div class="col-sm-12">
                    <div class="well">
                        <h2>User Permissions</h2>
                        @foreach($permissions as $permissionGroup)
                            <div class="col-sm-6 permission-group-wrapper">
                                <h3>{{$permissionGroup->name}}</h3>
                                @if($canManage)
                                    <span class="select-all-permissions">
                                        <a href="#" data-permission-group-id="{{$permissionGroup->id}}">
                                            select/unselect all
                                        </a>
                                    </span>    
                                @endif
                                <div class="clear"></div>
                                <div class="permission-wrapper">
                                    @foreach($permissionGroup->permissions as $permission)
                                        <div class="permission-picker">
                                            <label class="checkbox-inline">
                                                {{ Form::checkbox(
                                                    "user_permissions[]", 
                                                    $permission->id,
                                                    $user->permissions->contains($permission->id),
                                                    array(
                                                        "id" => "user_permission_{$permission->id}",
                                                        'disabled' => $canManage ? null : 'disabled',
                                                        'data-permission-group-id' => $permissionGroup->id
                                                    )) }}
                                                <b>{{ ucfirst($permission->display_name) }}</b>
                                            </label>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                        <div class="clear"></div>
                    </div>
                </div>
            </div>
            @if($canEdit)
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group pull-right">
                            {{ Form::submit(Lang::get('backend.save'), array('class' => 'btn btn-primary')) }}
                        </div>
                    </div>
                </div>
            @endif
        {{ Form::close() }}
    </div>
    
    <script>
        $(function() {
            var manageUsersView = new Eae.View.BackendUserForm({
                el : $('#edit_user_form')
            });
        });  
    </script>
@stop


