@extends('admin/layouts/default')

{{-- Page title --}}
@section('title')
    Edit User
    @parent
@stop

{{-- page level styles --}}
@section('header_styles')
    <!--page level css -->
    <link href="{{ asset('assets/vendors/jasny-bootstrap/css/jasny-bootstrap.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendors/select2/css/select2.min.css') }}" type="text/css" rel="stylesheet">
    <link href="{{ asset('assets/vendors/select2/css/select2-bootstrap.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendors/datetimepicker/css/bootstrap-datetimepicker.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendors/iCheck/css/all.css') }}"  rel="stylesheet" type="text/css" />
    <link href="{{ asset('assets/css/pages/wizard.css') }}" rel="stylesheet">
    <link rel="stylesheet" href="{{ asset('assets/vendors/bootstrap-tagsinput/css/bootstrap-tagsinput.css') }}" />
    <link rel="stylesheet" href="{{ asset('assets/vendors/bootstrap-tagsinput/css/app.css') }}" />
    <!--end of page level css-->

@stop


{{-- Page content --}}
@section('content')
    <section class="content-header">
        <h1>Edit user</h1>
        <ol class="breadcrumb">
            <li>
                <a href="{{ route('admin.dashboard') }}">
                    <i class="livicon" data-name="home" data-size="14" data-color="#000"></i>
                    Dashboard
                </a>
            </li>
            <li>Users</li>
            <li class="active">Edit User</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title"> <i class="livicon" data-name="users" data-size="16" data-c="#fff" data-hc="#fff" data-loop="true"></i>Editing user : <p class="user_name_max">{!! $user->first_name!!} {!! $user->last_name!!}</p>
                        </h3>
                    <span class="pull-right clickable">
                        <i class="glyphicon glyphicon-chevron-up"></i>
                    </span>
                    </div>
                    <div class="panel-body">
                        <!--main content-->
                        <div class="row">
                            @if ($message = Session::get('success'))
                                <div class="alert alert-success alert-dismissable">
                                    <p>{{ $message }}</p>
                                </div>
                            @endif
                            <div class="col-md-12">

                                {!! Form::model($user, ['url' => URL::to('admin/users/'. $user->id.''), 'method' => 'put', 'class' => 'form-horizontal','id'=>'commentForm', 'enctype'=>'multipart/form-data','files'=> true]) !!}
                                    {{ csrf_field() }}

                                   
                                        
                                        <div class="tab-content">
                                            
                                                <h2 class="hidden">&nbsp;</h2>
												
												@if(isset($user->organization_name))
											   <div class="form-group ">
														<label class="col-sm-2 control-label">Organization </label>
                                                    <div class="col-sm-10">
                                                        <label class="col-sm-2 control-label">{{$user->organization_name}} </label>
                                                    </div>
                                                    {!! $errors->first('display_name', '<span class="help-block">:message</span>') !!}
                                                </div>
												@endif

                                                <div class="form-group {{ $errors->first('display_name', 'has-error') }}">
                                                    <label for="display_name" class="col-sm-2 control-label">Display Name *</label>
                                                    <div class="col-sm-10">
                                                        <input id="display_name" name="display_name" type="text"
                                                               placeholder="Display Name" class="form-control required"
                                                               value="{!! old('display_name', $user->display_name) !!}" required="required" />
                                                    </div>
                                                    {!! $errors->first('display_name', '<span class="help-block">:message</span>') !!}
                                                </div>


                                                <div class="form-group {{ $errors->first('name', 'has-error') }}">
                                                    <label for="name" class="col-sm-2 control-label">User Name *</label>
                                                    <div class="col-sm-10">
                                                        <input id="name" readonly="readonly" name="name" type="text"
                                                               placeholder="User Name" class="form-control required"
                                                               value="{!! old('name', $user->name) !!}"/>
                                                    </div>
                                                    {!! $errors->first('first_name', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('first_name', 'has-error') }}">
                                                    <label for="first_name" class="col-sm-2 control-label">First Name *</label>
                                                    <div class="col-sm-10">
                                                        <input id="first_name" name="first_name" type="text"
                                                               placeholder="First Name" class="form-control required"
                                                               value="{!! old('first_name', $user->first_name) !!}"/>
                                                    </div>
                                                    {!! $errors->first('first_name', '<span class="help-block">:message</span>') !!}
                                                </div>

                                             <div class="form-group {{ $errors->first('last_name', 'has-error') }}">
                                                    <label for="last_name" class="col-sm-2 control-label">Last Name *</label>
                                                    <div class="col-sm-10">
                                                        <input id="last_name" name="last_name" type="text" placeholder="Last Name"
                                                               class="form-control required"
                                                               value="{!! old('last_name', $user->last_name) !!}"/>
                                                    </div>
                                                    {!! $errors->first('last_name', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('email', 'has-error') }}">
                                                    <label for="email" class="col-sm-2 control-label">Email *</label>
                                                    <div class="col-sm-10">
                                                        <input id="email" readonly="readonly" name="email" placeholder="E-Mail" type="text"
                                                               class="form-control required email"
                                                               value="{!! old('email', $user->email) !!}"/>

                                                    {!! $errors->first('email', '<span class="help-block">:message</span>') !!}
                                                    </div>
                                                </div>

                                                <div class="form-group {{ $errors->first('password', 'has-error') }}">
                                                    <p class="text-warning">If you don't want to change password... please leave them empty</p>
                                                    <label for="password" class="col-sm-2 control-label">Password </label>
                                                    <div class="col-sm-10">
                                                        <input id="password" name="password" type="password" placeholder="Password"
                                                               class="form-control" value="{!! old('password') !!}"/>
                                                    </div>
                                                    {!! $errors->first('password', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('password_confirm', 'has-error') }}">
                                                    <label for="password_confirm" class="col-sm-2 control-label">Confirm Password </label>
                                                    <div class="col-sm-10">
                                                        <input id="password_confirm" name="password_confirm" type="password"
                                                               placeholder="Confirm Password " class="form-control"
                                                               value="{!! old('password_confirm') !!}"/>
                                                        {!! $errors->first('password_confirm', '<span class="help-block">:message</span>') !!}
                                                    </div>

                                                </div>

                                                <div class="form-group">
                                                    <label for="user_domain" class="col-sm-2 control-label">User Domain</label>
                                                    <div class="col-sm-10">
                                                    <input name="user_domain" type="text" data-role="tagsinput" value="{{ old('user_domain', isset($user_domain) ? $user_domain : '') }}"/>
                                                    </div>
                                                </div>

                                                <div class="form-group">
                                                    <label for="proxy_download" class="col-sm-2 control-label">Download Per Article</label>
                                                    <div class="col-sm-10">
                                                    <input name="proxy_download" class="form-control" type="text" value="{!! old('proxy_download', $user->proxy_download) !!}"/>
                                                    </div>
                                                </div>

                                                <h2 class="hidden">&nbsp;</h2>
                                                <div class="form-group {{ $errors->first('dob', 'has-error') }}">
                                                    <label for="dob" class="col-sm-2 control-label">Date of Birth</label>
                                                    <div class="col-sm-10">
                                                        <input id="dob" name="dob" type="text" class="form-control"
                                                               data-date-format="YYYY-MM-DD" value="{!! old('dob', $user->dob) !!}"
                                                               placeholder="yyyy-mm-dd"/>
                                                    </div>
                                                    {!! $errors->first('dob', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('pic_file', 'has-error') }}">
                                                    <label for="pic" class="col-sm-2 control-label">Profile picture</label>
                                                    <div class="col-sm-10">
                                                        <div class="fileinput fileinput-new" data-provides="fileinput">
                                                            <div class="fileinput-new thumbnail" style="width: 200px; height: 200px;">
                                                                @if($user->pic)
                                                                    <img src="{!! url('/').'/uploads/users/'.$user->pic !!}" alt="img"
                                                                         class="img-responsive"/>
                                                                @elseif($user->gender === "male")
                                                                    <img src="{{ asset('assets/images/authors/avatar3.png') }}" alt="..."
                                                                         class="img-responsive"/>
                                                                @elseif($user->gender === "female")
                                                                    <img src="{{ asset('assets/images/authors/avatar5.png') }}" alt="..."
                                                                         class="img-responsive"/>
                                                                @else
                                                                    <img src="{{ asset('assets/images/authors/no_avatar.jpg') }}" alt="..."
                                                                         class="img-responsive"/>
                                                                @endif
                                                            </div>
                                                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 200px;"></div>
                                                            <div>
                                                    <span class="btn btn-default btn-file">
                                                        <span class="fileinput-new">Select image</span>
                                                        <span class="fileinput-exists">Change</span>
                                                        <input id="pic" name="pic_file" type="file"
                                                               class="form-control"/>
                                                    </span>
                                                                <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput" style="color: black !important;">Remove</a>
                                                            </div>
                                                        </div>
                                                        {!! $errors->first('pic_file', '<span class="help-block">:message</span>') !!}
                                                    </div>
                                                </div>
                                                <div class="form-group  {{ $errors->first('pic', 'has-error') }}">
                                                    <label for="bio" class="col-sm-2 control-label">Bio <small>(brief intro)</small></label>
                                                    <div class="col-sm-10">
                                            <textarea name="bio" id="bio" class="form-control resize_vertical"
                                                      rows="4">{!! old('bio', $user->bio) !!}</textarea>
                                                    </div>
                                                    {!! $errors->first('bio', '<span class="help-block">:message</span>') !!}
                                                </div>

                                           
                                                <div class="form-group {{ $errors->first('gender', 'has-error') }}">
                                                    <label for="email" class="col-sm-2 control-label">Gender </label>
                                                    <div class="col-sm-10">
                                                        <select class="form-control" title="Select Gender..." name="gender">
                                                            <option value="">Select</option>
                                                            <option value="male" @if($user->gender === 'male') selected="selected" @endif >Male</option>
                                                            <option value="female" @if($user->gender === 'female') selected="selected" @endif >Female</option>
                                                            <option value="other" @if($user->gender === 'other') selected="selected" @endif >Other</option>

                                                        </select>
                                                    </div>
                                                    {!! $errors->first('gender', '<span class="help-block">:message</span>') !!}
                                                </div>

                                           
                                                <div class="form-group {{ $errors->first('institution_id', 'has-error') }}">
                                                    <label for="email" class="col-sm-2 control-label">Institute </label>
                                                    <div class="col-sm-10">
                                                        <select class="form-control select2" title="Select Institution..." name="institution_id" id="institution_id">
                                                         @if($user_institute)
                                                            <option value="{{$user_institute->institution_id}}" selected>{{$user_institute->name}}</option>
                                                         @else
                                                            <option value="">Select</option>
                                                         @endif
                                                        </select>
                                                    </div>
                                                    {!! $errors->first('gender', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group required {{ $errors->first('country', 'has-error') }}">
                                                    <label for="country" class="col-sm-2 control-label">Country </label>
                                                    <div class="col-sm-10">
                                                        {!! Form::select('country', $countries,old('country',$user->country),array('class' => 'country_field form-control')) !!}

                                                    </div>
                                                    {!! $errors->first('country', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('state', 'has-error') }}">
                                                    <label for="state" class="col-sm-2 control-label">State </label>
                                                    <div class="col-sm-10">
                                                        <input id="state" name="state" type="text" class="form-control"
                                                               value="{!! old('state', $user->state) !!}"/>
                                                    </div>
                                                    {!! $errors->first('state', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('city', 'has-error') }}">
                                                    <label for="city" class="col-sm-2 control-label">City </label>
                                                    <div class="col-sm-10">
                                                        <input id="city" name="city" type="text" class="form-control"
                                                               value="{!! old('city', $user->city) !!}"/>
                                                    </div>
                                                    {!! $errors->first('city', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('address', 'has-error') }}">
                                                    <label for="address" class="col-sm-2 control-label">Address </label>
                                                    <div class="col-sm-10">
                                                        <input id="address" name="address" type="text" class="form-control"
                                                               value="{!! old('address', $user->address) !!}"/>
                                                    </div>
                                                    {!! $errors->first('address', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('postal', 'has-error') }}">
                                                    <label for="postal" class="col-sm-2 control-label">Postal/zip</label>
                                                    <div class="col-sm-10">
                                                        <input id="postal" name="postal" type="text" class="form-control"
                                                               value="{!! old('postal', $user->postal) !!}"/>
                                                    </div>
                                                    {!! $errors->first('postal', '<span class="help-block">:message</span>') !!}
                                                </div>

                                                <div class="form-group {{ $errors->first('postal', 'has-error') }}">
                                                    <label for="postal" class="col-sm-2 control-label">Referral URL</label>
                                                    <div class="col-sm-10">
                                                        <textarea name="referral_url" id="referral_url" class="form-control resize_vertical"
                                                      rows="4">{!! old('referral_url', $user->referral_url) !!}</textarea>      
                                                    </div>
                                                    {!! $errors->first('referral_url', '<span class="help-block">:message</span>') !!}
                                                </div>
                                            @if(isset($user->referral_token))    
                                                <div class="form-group">
                                                    <label for="referral_token" class="col-sm-2 control-label">Referral Token</label>
                                                    <div class="col-sm-10">
                                                        <input id="referral_token" name="referral_token" type="text" class="form-control"
                                                               value="{!! $user->referral_token !!}"/ readonly="readonly">
                                                    </div>
                                                </div>
                                            @endif        
                                                <div class="form-group {{ $errors->first('user_type', 'has-error') }}">
                                                    <label for="email" class="col-sm-2 control-label">Register As </label>
                                                    <div class="col-sm-10">
                                                        <select class="form-control" title="Select..." name="user_type">
                                                            <option value="">Select</option>
                                                            <option value="user" @if($user->user_type === 'user') selected="selected" @endif >New User</option>
                                                            <option value="Org" @if($user->user_type === 'Org') selected="selected" @endif >New Organization</option>
                                                        </select>
                                                    </div>
                                                </div>

                                        <div class="form-group">
                                            <label for="activate" class="col-sm-2 control-label">Change Password</label>
                                            <div class="col-sm-10">
                                                <input id="change_password" name="change_password" type="checkbox"
                                                       class="pos-rel p-l-30 custom-checkbox"
                                                       value="1" @if($user->change_password == 1) checked="checked" @endif >
                                                <span> Allow user to change password, (Uncheck for not allow)</span></div>

                                        </div>

                                        <div class="form-group">
                                            <label for="activate" class="col-sm-2 control-label"> Allow Counter Report</label>
                                            <div class="col-sm-10">
                                                <input id="counter_usage_report" name="counter_usage_report" type="checkbox"
                                                       class="pos-rel p-l-30 custom-checkbox"
                                                       value="1" @if($user->counter_usage_report == 1) checked="checked" @endif >
                                                <span> To activate user counter report, click the check box</span></div>

                                        </div>
                                        <div class="form-group">
										<label for="user_consortia_counter" class="col-sm-2 control-label">Counter Report User</label>
                                            <div class="col-sm-10">
                                            <input name="user_consortia_counter" type="text" data-role="tagsinput" value="{{ old('user_consortia_counter', isset($user_consortia) ? $user_consortia : '') }}"/>
											</div>
                                        </div>

                                        <div class="form-group {{ $errors->first('openathensrealmName', 'has-error') }}">
                                            <label for="openathensrealmName" class="col-sm-2 control-label">openathens realmName *</label>
                                            <div class="col-sm-10">
                                                <input id="openathensrealmName" name="openathensrealmName" type="text" placeholder="Openathens realmName"
                                                       class="form-control"
                                                       value="{!! old('openathensrealmName', $user->openathensrealmName) !!}"/>
                                            </div>
                                            {!! $errors->first('openathensrealmName', '<span class="help-block">:message</span>') !!}
                                        </div>
                                        <div class="form-group {{ $errors->first('openathensderivedEduPersonScope', 'has-error') }}">
                                            <label for="openathensderivedEduPersonScope" class="col-sm-2 control-label">openathens derivedEduPersonScope *</label>
                                            <div class="col-sm-10">
                                                <input id="openathensderivedEduPersonScope" name="openathensderivedEduPersonScope" type="text" placeholder="Openathens derivedEduPersonScope"
                                                       class="form-control"
                                                       value="{!! old('openathensderivedEduPersonScope', $user->openathensderivedEduPersonScope) !!}"/>
                                            </div>
                                            {!! $errors->first('openathensderivedEduPersonScope', '<span class="help-block">:message</span>') !!}
                                        </div>

                                        <div class="form-group">
                                            <label for="ingenta_id" class="col-sm-2 control-label">Ingenta ID </label>
                                            <div class="col-sm-10">
                                                 <input name="ingenta_id" class="form-control" id="ingenta_id" type="text" value="{{ old('ingenta_id',$user->ingenta_id) }}" placeholder="Enter ingenta ID....." /> 
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="autohrs_keywords" class="col-sm-2 control-label">Institute Tags</label>
                                            <div class="col-sm-10">
                                                 <input name="autohrs_keywords" class="form-control" id="autohrs_keywords" type="text" value="{{ old('autohrs_keywords', isset($autohrs_keywords) ? $autohrs_keywords : '') }}" data-role="tagsinput" /> 
                                            </div>
                                        </div>

                                         <div class="form-group {{ $errors->first('openathensrealmName', 'has-error') }}">
                                            <label for="openathensrealmName" class="col-sm-2 control-label">Agent Access Type</label>
                                            <div class="col-sm-10 mt-4">
                                                <label>None <input type="radio" name="agent_access_type" onchange="get_access_form('none')"  value="none" 
                                                    @if(empty($user->agent_access_type))
                                                        checked="true"
                                                    @endif    
                                                    ></label>

                                                <label style="margin-left: 25px;">By Country <input type="radio" name="agent_access_type" onchange="get_access_form('country_field')"  value="by_country"  
                                                    @if($user->agent_access_type == 'C')
                                                        checked="true"
                                                    @endif ></label>
                                                <label style="margin-left: 25px;">By User <input type="radio" name="agent_access_type" onchange="get_access_form('user_field')" value="by_user"
                                                    @if($user->agent_access_type == 'U')
                                                        checked="true"
                                                    @endif
                                                ></label>
                                              </div>
                                        </div>
                                        <div class="form-group" id="user_agent_div" <?php echo ($user->agent_access_type =='U') ? 'style="display:block"': 'style="display:none"';?>>
                                            <label for="user_agent" class="col-sm-2 control-label">Agents *</label>
                                            <div class="col-sm-10">
                                            <input name="user_agent" id="user_agent" type="text" data-role="tagsinput" value="{{ old('user_agent', isset($user_agent) ? $user_agent : '') }}"/>
                                            </div>
                                        </div>
                                        <div class="form-group" id="agent_countries_div" <?php echo ($user->agent_access_type =='C') ? 'style="display:block"': 'style="display:none"';?>>
                                            <label for="agent_countries" class="col-sm-2 control-label">Agent Countries *</label>
                                            <div class="col-sm-10">
                                                 {!! Form::select('agent_countries[]', $countries,$user_agent_country,array('class' => 'country_field form-control','multiple'=>'true','id' => 'agent_countries','autocomplete'=>'off')) !!}
                                            </div>
                                        </div>
                                        
                                            </div>
                                            
                                            <div class="form-actions">
                                        <div class="col-md-offset-11 col-md-12">
                                            <button type="submit" class="btn btn-primary" id="change-password">Submit
                                            </button>
                                           
                                    </div>
                                        </div>
                                   
                                </form>

                            </div>
                        </div>
                        <!--main content end-->
                    </div>
                </div>
            </div>
        </div>
        <!--row end-->
    </section>
@stop

{{-- page level scripts --}}
@section('footer_scripts')
    <script src="{{ asset('assets/vendors/bootstrap-tagsinput/js/bootstrap-tagsinput.js') }}"  type="text/javascript"></script>
     <script src="{{ asset('assets/vendors/iCheck/js/icheck.js') }}"></script>
    <script src="{{ asset('assets/vendors/moment/js/moment.min.js') }}" ></script>
    <script src="{{ asset('assets/vendors/jasny-bootstrap/js/jasny-bootstrap.js') }}"  type="text/javascript"></script>
    <script src="{{ asset('assets/vendors/select2/js/select2.js') }}" type="text/javascript"></script>
    <script src="{{ asset('assets/vendors/bootstrapwizard/jquery.bootstrap.wizard.js') }}" type="text/javascript"></script>
    <script src="{{ asset('assets/vendors/bootstrapvalidator/js/bootstrapValidator.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('assets/vendors/datetimepicker/js/bootstrap-datetimepicker.min.js') }}" type="text/javascript"></script>
    <script>
        function formatState (state) {
            if (!state.id) { return state.text; }
            var $state = $(
                '<span><img src="{{asset('assets/img/countries_flags')}}/'+ state.element.value.toLowerCase() + '.png" class="img-flag" width="20px" height="20px" /> ' + state.text + '</span>'
            );
            return $state;

}
$(".country_field").select2({
    templateResult: formatState,
    templateSelection: formatState,
    placeholder: "select a country",
    theme:"bootstrap"
});
// $("#agent_countries").val([]).trigger('change')


function get_access_form(flag) {
   
    if(flag == 'user_field'){
        $("#user_agent_div").show();
        $("#user_agent").prop("required",true);
        $("#agent_countries").prop("required",false);

        $("#agent_countries_div").hide();
    }else if(flag == 'country_field'){
        $("#user_agent_div").hide();
        $("#agent_countries_div").show();

        $("#agent_countries").prop("required",true);
        $("#user_agent").prop("required",false);
    }else{
        $("#user_agent_div").hide();
        $("#agent_countries_div").hide();
        $("#user_agent").prop("required",false);
        $("#agent_countries").prop("required",false);


    }



}

$("#institution_id").select2({
  theme:"bootstrap",
  width: "100%",
  placeholder: "Select an institution",
  ajax: {
    url: "{{ route('get_institution_with_ajax') }}",
    dataType: "json",
    delay: 250,
    processResults: function (data) {
        console.log(data)
      return {
        results:  $.map(data, function (item) {
              return {
                  text: item.name,
                  id: item.institution_id
              }
          })
      };
    },
    cache: true
  }
});
</script>
@stop
