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

@section('content')

    <div class="row">
        <div class="col-xs-10">
            <h1>Manage Frontend Users</h1>
        </div>
        @if($canView)
            <div class="col-xs-2">
                <a href="{{ url('/admin/users-frontend') }}" class="back-btn btn btn-default" type="button" data-toggle="tooltip" data-placement="bottom" title="" class="btn btn-default pull-right" data-original-title="{{trans('backend.back_list')}}">
                    <span class="icon glyphicon glyphicon-th-list"></span>
                </a>
            </div>
        @endif
    </div>
    <div id="edit_user_form" class="member-form">
        {!! Form::model($user) !!}
            <div class="row">
                <div class="col-xs-12 col-sm-6">
                    <div class="form-group @error('name') has-error @enderror">
                        <label class="control-label" for="name_input">First Name</label>
                        {!! Form::text('name', $user->name, array(
                            'id' => 'name_input',
                            'class' => 'form-control',
                            'maxlength' => '100',
                            'data-toggle' => ($errors->has('name') ? 'tooltip': null),
                            'title' => ($errors->has('name') ? $errors->first('name'): null),
                            'disabled' => $canManage ? null : 'disabled')) !!}
                    </div>

                    <div class="form-group @error('lastName') has-error @enderror">
                        <label class="control-label" for="lastName_input">Last Name</label>
                        {!! Form::text('lastName', $user->last_name, array(
                            'id' => 'lastName_input',
                            'class' => 'form-control',
                            'maxlength' => '100',
                            'data-toggle' => ($errors->has('lastName') ? 'tooltip': null),
                            'title' => ($errors->has('lastName') ? $errors->first('lastName'): null),
                            'disabled' => $canManage ? null : 'disabled')) !!}
                    </div>

                    <div class="form-group @error('email') has-error @enderror">
                        <label class="control-label" for="email_input">Email</label>
                        {!! Form::text('email', $user->email, array(
                            'id' => 'email_input',
                            'class' => 'form-control',
                            'maxlength' => '100',
                            'data-toggle' => ($errors->has('email') ? 'tooltip': null),
                            'title' => ($errors->has('email') ? $errors->first('email'): null),
                            'readonly' => 'readonly')) !!}
                    </div>

                    <div class="form-group @error('department') has-error @enderror">
                        <label class="control-label" for="department_input">Department</label>
                        {!! Form::text('department', $user->department, array(
                            'id' => 'department_input',
                            'class' => 'form-control',
                            'maxlength' => '20',
                            'data-toggle' => ($errors->has('department') ? 'tooltip': null),
                            'title' => ($errors->has('department') ? $errors->first('department'): null),
                            'disabled' => $canManage ? null : 'disabled')) !!}
                    </div>

                    <div class="form-group @error('function') has-error @enderror">
                        <label class="control-label" for="function_input">Function</label>
                        {!! Form::text('function', $user->function, array(
                            'id' => 'function_input',
                            'class' => 'form-control',
                            'maxlength' => '250',
                            'data-toggle' => ($errors->has('function') ? 'tooltip': null),
                            'title' => ($errors->has('function') ? $errors->first('function'): null),
                            'disabled' => $canManage ? null : 'disabled')) !!}
                    </div>

                    <div class="form-group @error('phoneNumber') has-error @enderror">
                        <label class="control-label" for="phoneNumber_input">Phone Number</label>
                        {!! Form::text('phoneNumber', $user->phone_number, array(
                            'id' => 'phoneNumber_input',
                            'class' => 'form-control',
                            'maxlength' => '20',
                            'data-toggle' => ($errors->has('phoneNumber') ? 'tooltip': null),
                            'title' => ($errors->has('phoneNumber') ? $errors->first('phoneNumber'): null),
                            'disabled' => $canManage ? null : 'disabled')) !!}
                    </div>

                    <div class="form-group @error('password') has-error @enderror">
                        <label class="control-label" for="password_input">{{ trans('backend.password') }}</label>
                        {!! Form::password('password', array(
                            'id' => 'password_input',
                            'class' => 'form-control',
                            'maxlength' => '30',
                            'data-toggle' => ($errors->has('password') ? 'tooltip': null),
                            'title' => ($errors->has('password') ? $errors->first('password'): null))) !!}

                    </div>

                    @if ($user->state !== 'pendingEmailValidation')
                        <div class="form-group @error('state') has-error @enderror">
                            <label class="control-label">State</label>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="state" id="state-1" value="active" {{ $user->state === 'active' ? 'checked' : '' }}>
                                <label class="form-check-label" for="state-1">
                                    Active
                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="state" id="state-2" value="inactive" {{ $user->state === 'inactive' ? 'checked' : '' }}>
                                <label class="form-check-label" for="state-2">
                                    Inactive
                                </label>
                            </div>
                        </div>
                    @endif

                    <div class="form-group">
                        <label class="control-label">User Permissions</label>
                        <div class="form-check">
                            @php
                                $optionsArr = ['id' => 'legal-opinion-permission', 'class' => 'form-check-input', 'data-role' => 'legal-opinion-permission'];
                            @endphp
                            {{ Form::checkbox('legal_opinion_permission', '1', $user->legal_opinion_permission, $optionsArr) }}
                            <label class="form-check-label" for="legal-opinion-permission">
                                Legal Opinion Permission
                            </label>
                        </div>
                        @if ($permissionFinanceSubCommittee)
                            <div class="form-check">
                                @php
                                    $optionsArr = ['id' => 'finance-sub-committee-permission', 'class' => 'form-check-input', 'data-role' => 'finance-sub-committee-permission'];
                                @endphp
                                {{ Form::checkbox('finance_sub_committee_permission', '1', $user->finance_sub_committee_permission, $optionsArr) }}
                                <label class="form-check-label" for="finance-sub-committee-permission">
                                    Finance Sub Committee Permission
                                </label>
                            </div>
                        @endif
                    </div>

                </div>
                <div class="col-xs-12 col-sm-6">
                    @if (!empty($user->image))
                        <div class="form-group media-picker-wrapper">
                            <img src="/storage/profile_images/{{$user->image}}" alt="">
                        </div>
                    @endif

                    <div class="form-group">
                        <label class="control-label">Committees (Com), Taskforce (TF) and Working Group (WG)</label>
                        <div class="accordion" id="accordionCommittees">
                            @php
                                $index = 1;
                            @endphp
                            @foreach ($listCommittees as $committee)
                                <div class="card">
                                    <div class="card-header card-committee" id="committee{{ $committee->id }}">
                                        @php
                                            $isSelectedCommittee = collect($committees)->contains(function ($com) use ($committee) {
                                                return $com['id'] == $committee->id;
                                            });
                                            $selectedCommitteeType = $isSelectedCommittee ? collect($committees)->firstWhere('id', $committee->id)['type'] : null;
                                        @endphp

                                        <div class="header-committees-accordion">
                                            <input class="form-check-input" data-role="committee" data-slug="{{ $committee->slug }}" type="checkbox" name="com[{{$committee->id}}]" id="com{{$committee->id}}" value="{{$committee->id}}" {{ $isSelectedCommittee || old('com.'.$committee->id) ? 'checked' : '' }}>
                                            <button class="btn btn-default collapsed" type="button" data-toggle="collapse" data-target="#committee-items{{ $committee->id }}" aria-expanded="false" aria-controls="committee-items{{ $committee->id }}">
                                                {{ $committee->title }}
                                            </button>
                                        </div>

                                        <div class="form-radio-container">
                                            <div class="form-radio radio-inline @error('com_type.' . $index) has-error @enderror">
                                                <input class="form-check-input radio-inline" data-committee-type="{{ $committee->slug }}" type="radio" name="com_type[{{$committee->id}}]" id="to{{$committee->id}}" value="to" {{ ($selectedCommitteeType == 'to' || old('com_type.'.$committee->id) == 'to') ? 'checked' : '' }}>
                                                <label class="form-check-label" for="to{{$committee->id}}">
                                                    To
                                                </label>
                                            </div>

                                            <div class="form-radio radio-inline @error('com_type.' . $index) has-error @enderror">
                                                <input class="form-check-input radio-inline" data-committee-type="{{ $committee->slug }}" type="radio" name="com_type[{{$committee->id}}]" id="cc{{$committee->id}}" value="cc" {{ ($selectedCommitteeType == 'cc' || old('com_type.'.$committee->id) == 'cc') ? 'checked' : '' }}>
                                                <label class="form-check-label" for="cc{{$committee->id}}">
                                                    Cc
                                                </label>
                                            </div>
                                        </div>
                                    </div>

                                    <div id="committee-items{{ $committee->id }}" class="collapse" aria-labelledby="committee{{ $committee->id }}" data-parent="#accordionCommittees">
                                        <div class="card-body">

                                            @foreach ($committee->workGroups as $workGroup)
                                                <div class="form-check-container">
                                                    <div class="form-check checkbox-inline @error('wg.' . $index) has-error @enderror">
                                                        @php
                                                            $isSelected = collect($workGroups)->contains(function ($wg) use ($workGroup) {
                                                                return $wg['id'] == $workGroup->id;
                                                            });
                                                            $selectedType = $isSelected ? collect($workGroups)->firstWhere('id', $workGroup->id)['type'] : null;
                                                        @endphp
                                                        <input class="form-check-input checkbox-inline" data-role="work-group" data-committee="{{ $committee->slug }}" data-workgroup-slug="{{ $workGroup->slug }}" type="checkbox" name="wg[{{$workGroup->id}}]" id="wg{{$workGroup->id}}" value="{{$workGroup->id}}" {{ $isSelected || old('wg.'.$workGroup->id) ? 'checked' : '' }}>
                                                        <label class="form-check-label" for="wg{{$workGroup->id}}">
                                                            {{$workGroup->title}}
                                                        </label>
                                                    </div>

                                                    <div class="form-radio-container">
                                                        <div class="form-radio radio-inline @error('wg_type.' . $index) has-error @enderror">
                                                            <input class="form-check-input radio-inline" data-workgroup-type="{{ $workGroup->slug }}" data-committee-type="{{ $committee->slug }}" type="radio" name="wg_type[{{$workGroup->id}}]" id="to{{$workGroup->id}}" value="to" {{ ($selectedType == 'to' || old('wg_type.'.$workGroup->id) == 'to') ? 'checked' : '' }}>
                                                            <label class="form-check-label" for="to{{$workGroup->id}}">
                                                                To
                                                            </label>
                                                        </div>

                                                        <div class="form-radio radio-inline @error('wg_type.' . $index) has-error @enderror">
                                                            <input class="form-check-input radio-inline" data-workgroup-type="{{ $workGroup->slug }}" data-committee-type="{{ $committee->slug }}" type="radio" name="wg_type[{{$workGroup->id}}]" id="cc{{$workGroup->id}}" value="cc" {{ ($selectedType == 'cc' || old('wg_type.'.$workGroup->id) == 'cc') ? 'checked' : '' }}>
                                                            <label class="form-check-label" for="cc{{$workGroup->id}}">
                                                                Cc
                                                            </label>
                                                        </div>
                                                    </div>
                                                    @php
                                                        $index++;
                                                    @endphp
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                </div>
                            @endforeach

                        </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', 'id' => 'user_save_btn')) !!}
                        </div>
                    </div>
                </div>
            @endif
        {!! Form::close() !!}
    </div>
    <script>
        $(function() {
            var form_user_id = new Eae.View.BackendUserFrontendForm({
                el: $('#edit_user_form'),
                inputCommittees: $('[data-role="committee"'),
                inputWorkGroups: $('[data-role="work-group"'),
            });
        });
    </script>
@stop


