@extends('admin.layouts.main',array(
    'scripts' => array(
        path_asset('path.backoffice.js_path') . '/view/generic/backend-media-picker.js',
        path_asset('path.backoffice.js_path') . '/view/member/member-form.js'
    )
))

@section('content')

    <div class="row">
        <div class="col-xs-10">
            <h1>Manage Member</h1>
        </div>
        @if($canView)
            <div class="col-xs-2">
                <a href="{{ url('/admin/admin-members') }}" 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_member_form" class="generic_wrapper member-form">
        {!! Form::model($member) !!}
            <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">Company Name</label>
                        {!! Form::text('name', null, array(
                            'id' => 'name_input',
                            'class' => 'form-control',
                            'maxlength' => '250',
                            '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('notes') has-error @enderror">
                        <label class="control-label" for="notes_input">Notes</label>
                        {!! Form::textarea('notes', null, array(
                            'id' => 'notes_input',
                            'class' => 'form-control',
                            'data-toggle' => ($errors->has('notes') ? 'tooltip': null),
                            'title' => ($errors->has('notes') ? $errors->first('notes'): null),
                            'disabled' => $canManage ? null : 'disabled',
                            'rows' => 8 )) !!}
                    </div>

                    <div class="form-group">
                        <label class="control-label">Committees (Com), Taskforce (TF) and Working Group (WG)</label>
                        <div class="accordion" id="accordionCommittees">

                            @foreach ($listCommittees as $committee)
                                @php
                                    if ($committee->type === 'legalCommittee') {
                                        $type = 'committee';
                                        $display = 'none';
                                        if ($member->legal_committee) {
                                            $display = 'block';
                                        }
                                    } else if ($committee->type === 'legalOpinions') {
                                        $type = 'opinions';
                                        $display = 'none';
                                        if ($member->legal_opinions) {
                                            $display = 'block';
                                        }
                                    } else {
                                        $type = 'standard';
                                        $display = 'block';
                                        if ($member->member_type === 'noType') {
                                            $display = 'none';
                                        }
                                    }
                                @endphp
                                <div class="card" data-role="committee-{{ $type }}" style="display: {{ $display }}">
                                    <div class="card-header" id="committee{{ $committee->id }}">
                                        <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}}" {{ in_array($committee->id, $committees) ? '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>

                                    <div id="committee-items{{ $committee->id }}" class="collapse" aria-labelledby="committee{{ $committee->id }}" data-parent="#accordionCommittees">
                                        @if (count($committee->workGroups))
                                            <div class="card-body">
                                                @foreach ($committee->workGroups as $workGroup)
                                                    <div class="form-check">
                                                        <input class="form-check-input" data-role="work-group" data-committee="{{ $committee->slug }}" type="checkbox" name="wg[{{$workGroup->id}}]" id="wg{{$workGroup->id}}" value="{{$workGroup->id}}" {{ in_array($workGroup->id, $workGroups) ? 'checked' : '' }}>
                                                        <label class="form-check-label" for="wg{{$workGroup->id}}">
                                                            {{$workGroup->title}}
                                                        </label>
                                                    </div>
                                                @endforeach
                                            </div>
                                        @endif
                                    </div>
                                </div>
                            @endforeach

                        </div>
                    </div>

                    <div class="form-group" data-role="domains">
                        <div class="margin-bottom-20">
                            <label class="control-label" for="domain_input">Domains</label>
                            <button class="btn btn-default pull-right" type="button" data-action="add-domain">
                                <span class="icon glyphicon glyphicon-plus"></span>
                            </button>
                        </div>

                        @php
                          $indexDomains = 0;
                        @endphp

                        @foreach ($domains as $domain)
                            <div class="form-group group-button margin-bottom-10 @error('domain.' . $indexDomains) has-error @enderror">
                                {!! Form::text('domain[]', $domain, array(
                                    'class' => 'form-control',
                                    'maxlength' => '250',
                                    'title' => ($errors->has('domain.' . $indexDomains) ? $errors->first('domain'): null) )) !!}

                                <button class="btn btn-default pull-right" type="button" data-action="remove-domain">
                                    <span class="icon glyphicon glyphicon-minus"></span>
                                </button>
                            </div>
                            @php
                                $indexDomains++;
                            @endphp
                        @endforeach
                    </div>

                </div>
                <div class="col-xs-12 col-sm-6">

                    <div id="media-picker-member_logo_id" class="form-group media-picker-wrapper @error('logo_id') has-error @enderror">
                        <label id="member_logo_id" class="control-label" for="member_logo_id">Logo</label>
                        {!! Form::hidden('logo_id', '', array('id' => 'member_logo_id', 'class' => 'media-selection')) !!}
                        <button type="button" class="btn btn-xs btn-default command-add-more pull-right">
                            <span class="glyphicon glyphicon-plus"></span>
                        </button>
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <div class="inner-panel">
                                    <a class="select-media" href="#">@lang('backend.select_upload_item')</a>
                                    <ul>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group @error('country_id') has-error @enderror">
                        <label class="control-label" for="country_id">Country</label>

                        <div @error('country') data-toggle="tooltip" title="{{ $errors->has('country') ? $errors->first('country'): null }}" @enderror>
                            {!! Form::select('country_id', $listCountries, null, array(
                                'id' => 'country_id', 'class' => 'form-control'))  !!}
                        </div>
                    </div>

                    <div class="form-group @error('member_type') has-error @enderror">
                        <label class="control-label">Member Type</label>
                        <div class="form-check">
                            <input class="form-check-input" data-role="member-type" type="radio" name="member_type" id="member-type-1" value="noType" {{ $member->member_type === 'noType' ? 'checked' : '' }}>
                            <label class="form-check-label" for="member-type-1">
                                No Type
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" data-role="member-type" type="radio" name="member_type" id="member-type-2" value="regular" {{ $member->member_type === 'regular' ? 'checked' : '' }}>
                            <label class="form-check-label" for="member-type-2">
                                Regular
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" data-role="member-type" type="radio" name="member_type" id="member-type-3" value="associate" {{ $member->member_type === 'associate' ? 'checked' : '' }}>
                            <label class="form-check-label" for="member-type-3">
                                Associate
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label">Legal Options</label>
                        <div class="form-check">
                            @php
                                $optionsArr = ['id' => 'legal-committee', 'class' => 'form-check-input', 'data-role' => 'legal-committee'];
                                if ($member->member_type === 'noType') {
                                    $optionsArr['disabled'] = 'disabled';
                                }
                            @endphp
                            {{ Form::checkbox('legal_committee', '1', $member->legal_committee, $optionsArr) }}
                            <label class="form-check-label" for="legal-committee">
                                Legal Committee
                            </label>
                        </div>
                        <div class="form-check">
                            @php
                                $optionsArr = ['id' => 'legal-opinions', 'class' => 'form-check-input', 'data-role' => 'legal-opinions'];
                                if ($member->legal_committee || $member->member_type === 'noType') {
                                    $optionsArr['disabled'] = 'disabled';
                                }
                            @endphp
                            {{ Form::checkbox('legal_opinions', '1', $member->legal_opinions, $optionsArr) }}
                            <label class="form-check-label" for="legal-opinions">
                                Legal Opinions
                            </label>
                        </div>
                    </div>

                    <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" {{ $member->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" {{ $member->state === 'inactive' ? 'checked' : '' }}>
                            <label class="form-check-label" for="state-2">
                                Inactive
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label">User Permission Level</label>
                        <div class="form-check">
                            @php
                                $optionsArr = ['id' => 'user-permission-level', 'class' => 'form-check-input', 'data-role' => 'user-permission-level'];
                            @endphp
                            {{ Form::checkbox('user_permission_level', '1', $member->user_permission_level, $optionsArr) }}
                            <label class="form-check-label" for="user-permission-level">
                                User Permission Level
                            </label>
                        </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' => 'member_save_btn')) !!}
                        </div>
                    </div>
                </div>
            @endif
        {!! Form::close() !!}
    </div>

    <script>
        $(function() {
            var mediaPicker_member_logo_id = new Eae.View.MediaPicker({
                el: $('#media-picker-member_logo_id'),
                fieldId: 'member_logo_id',
                mediaItems: @json(!empty($member->logo) ? [$member->logo] : []),
                multiple: false,
            });

            var form_member_id = new Eae.View.BackendMemberForm({
                el: $('#edit_member_form'),
                domainContainer: $('[data-role="domains"]'),
                inputLegalCommittee: $('[data-role="legal-committee"]'),
                inputLegalOpinions: $('[data-role="legal-opinions"]'),
                inputCommittees: $('[data-role="committee"'),
                inputWorkGroups: $('[data-role="work-group"'),
                cardTypeCommittee: $('[data-role="committee-committee"'),
                cardTypeOpinions: $('[data-role="committee-opinions"'),
                cardTypeStandard: $('[data-role="committee-standard"'),
            });
        });
    </script>
@stop


