@extends('layouts.layout-2')

@section('content')

    <style>
        body {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
        .btn-xs {
            line-height: 1.3em;
        }
        .table tbody + tbody {
            border-top: 1px solid #9c9c9c;
        }
        .table th, .table td {
            border-top: none;
        }
        .table tr.border-none td, .table tr.border-none th  {
            border-top: none;
        }
        .table .thead-dark th {
            color: #4E5155;
            background-color: rgba(24, 28, 33, 0.1);
            border-color: rgba(63, 69, 74, 0.1);
        }

        .input-group-text {
            padding: 0.438rem 0.475rem;
        }


    </style>


    <h4 class="font-weight-bold py-3 mb-1">
        Vorlage verwalten
        <span class="float-right">
            <button class="btn btn-sm btn-warning"  data-toggle="modal" data-target="#exampleNotice"><i class="fa">?</i> </button>
        </span>
    </h4>

    <!-- Modal -->
    <div class="modal fade" id="exampleNotice" tabindex="-1" role="dialog" aria-labelledby="exampleNoticeTitle" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleNoticeTitle">Hinweise</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                  <p>
                      Hinweise zu Platzhaltern im den Vorlagen:
                  </p>
                   <style>
                       ul.exampleModal {
                           padding-left: 15px;
                       }
                       ul.exampleModal li {
                           border-bottom: 1px solid #cfcfcf;
                           margin-bottom: 8px;
                       }
                   </style>
                    <ul class="exampleModal">

                        <li><strong>#Name#</strong> (in jedem Textfeld)<br>
                            Name der Reise - Jordanien ..

                        </li>
                        <li><strong>#Nummer#</strong> (in jedem Textfeld)<br>
                           Reisenummer - JORD-INT..

                        </li>
                        <li><strong>#Zimmer#</strong> (in jedem Textfeld)<br>
                            Anzahl x Beichnung der gebuchten Zimmer mit Komma getrennt - 2 x Doppel..,
                        </li>
                        <li><strong>#Flughafen#</strong> (in jedem Textfeld)<br>
                            Name es Abflughafens - Düssel..
                        </li>
                        <li><strong>#Raumname#</strong> (wird bei Grundpreis Reise ausgewertet)<br>
                            Name des gebuchten Raums  - Doppel..

                        </li>
                        <li><strong>#Kategorie#</strong> (wird bei Aufpreis Kategorie ausgewertet)<br>
                            Name der zusätzlichen Kategorie  - Komfort (4 Sterne)

                        </li>
                        <li><strong>#Option#</strong> (wird bei Aufpreis Option (derzeit nur Verpflegung) ausgewertet)<br>
                            Name der zusätzlichen Optionen  - Halbpension
                        </li>
                    </ul>
                    @if(Auth::user()->isSuperAdmin())
                        <br>
                        <p><strong>Anpassung der Textbausteine für die Preisen <a href="{{ url('admin/translation/file/_vorlagen/de/de/all') }}">Inhalte -> Vorlagen</a></strong></p>
                        @endif
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">schließen</button>
                </div>
            </div>
        </div>
    </div>

    {!! Form::open(['url' => route('draft_detail', [$id]), 'class' => 'form-horizontal', 'id'=>'lead-form-validation']) !!}

    <input type="hidden" name="id" id="id" value="{{$id}}">

    <!-- draft -->
    <div class="card mb-2">
        <div class="card-body">
            <div class="form-group">
                    <label class="custom-control custom-checkbox float-right">
                        {!! Form::checkbox('active', 1, $draft->active, ['class'=>'custom-control-input']) !!}
                        <span class="custom-control-label">{{__('sichtbar')}}</span>
                    </label>

                <label class="form-label" for="draft_name">{{ __('Bezeichnung') }}*</label>
                {{ Form::text('name', $draft->name,  array('placeholder'=>__('Name'), 'class'=>'form-control', 'id'=>'draft_name', 'required'=>true)) }}
            </div>

        </div>
    </div>

    @if(count($draft->draft_items))
        @php($i = 1)
        <div class="text-left mt-3  mb-2">
            <button type="submit" name="action" value="saveAll" class="btn btn-submit btn-sm">{{ __('save changes') }}</button>&nbsp;
            <a href="{{route('drafts')}}" class="btn btn-default btn-sm">{{ __('back') }}</a>
            <div class="float-right">
                <button type="submit" name="action" value="addItem" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> Neue Leistung hinzufügen</button>
            </div>
        </div>
        <div class="table-responsive mb-4">
        <table class="table mb-0" id="table_dragula_tbody" style="min-width: 1080px">
            <thead class="thead-dark">
            <tr>
                <th style="width: 1em"></th>
                <th style="width: 1em">#</th>
                <th>Leistung</th>
                <th style="width: 9em">Datum Start</th>
                <th style="width: 9em">Datum Dauer</th>
                <th style="width: 15em">Teilnehmer/EP</th>
                <th style="width: 15em">Kinder/EP</th>
                <th style="width: 8em">#</th>
            </tr>
            </thead>

        @foreach($draft->draft_items as $draft_item)
        <tbody class="draft_item_tbody">
            <tr @if($draft_item->draft_type) style="background-color: {{ $draft_item->draft_type->color }}" @endif>
                <td>
                    <span class="handle ion ion-ios-move d-inline-block bg-primary text-white p-1"></span>

                </td>
                <td><span class="item_pos_number">{{ $i++ }}</span></td>
                <td>
                    <select class="selectpicker" data-style="btn-light" name="draft_item[{{$draft_item->id}}][draft_type_id]" id="draft_item_{{$draft_item->id }}_draft_type_id" data-live-search="true" required>
                        <option value="" disabled>Vorlagen-Typ*</option>
                        {!!  HTMLHelper::getDraftTypes($draft_item->draft_type_id) !!}
                    </select>
                </td>
                <td>
                    <select class="selectpicker" data-style="btn-light" name="draft_item[{{$draft_item->id}}][days_start]]" id="draft_item_{{$draft_item->id }}_days_start">
                        <option value="">(Datum) Start + Tage</option>
                        {!!  HTMLHelper::getRangeOptions($draft_item->days_start, 30, ' Tag(e)') !!}
                    </select>
                </td>
                <td>
                    <select class="selectpicker" data-style="btn-light" name="draft_item[{{$draft_item->id}}][days_duration]" id="draft_item_{{$draft_item->id }}_days_duration">
                        <option value="">(Datum) Dauer + Tage</option>
                        {!!  HTMLHelper::getRangeOptions($draft_item->days_duration, 30, ' Tag(e)') !!}
                    </select>
                </td>
                <td>
                    <div class="input-group">
                        <select class="custom-select" data-style="btn-light" name="draft_item[{{$draft_item->id}}][adult]" id="draft_item_{{$draft_item->id }}_adult">
                            <option value="">0</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item->adult, 10, ' T.') !!}
                        </select>
                        {{ Form::text('draft_item['.$draft_item->id.'][price_adult]', $draft_item->price_adult,  array('placeholder'=>__('Preis in €'), 'class'=>'form-control', 'id'=>'draft_item_'.$draft_item->id.'_price_adult',)) }}
                        <span class="input-group-append">
                            <span class="input-group-text">€</span>
                        </span>
                    </div>
                </td>
                <td>
                    <div class="input-group">
                        <select class="custom-select" data-style="btn-light" name="draft_item[{{$draft_item->id}}][children]" id="draft_item_{{$draft_item->id}}_children">
                            <option value="">Kinder</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item->children, 10, ' K.') !!}
                        </select>
                        {{ Form::text('draft_item['.$draft_item->id.'][price_children]', $draft_item->price_children,  array('placeholder'=>__('Preis in €'), 'class'=>'form-control', 'id'=>'draft_item_'.$draft_item->id.'_price_children')) }}
                        <span class="input-group-append">
                            <span class="input-group-text">€</span>
                        </span>
                    </div>
                </td>
                <td>
                    <button type="button" name="" value="up_{{$draft_item->id}}" class="btn btn-xs btn-default move-up-btn"><i class="fa fa-arrow-up"></i> </button>
                    <button type="button" name="" value="down_{{$draft_item->id}}" class="btn btn-xs btn-default mr-2 move-down-btn"><i class="fa fa-arrow-down"></i> </button>
                    <a class="text-danger" href="{{ route('draft_item_delete', [$draft_item->id]) }}"  onclick="return confirm('{{__('Wirklich löschen?')}}');"><i class="fa fa-trash-alt"></i></a>
                </td>
            </tr>
            <tr @if($draft_item->draft_type) style="background-color: {{ $draft_item->draft_type->color }}" @endif  class="border-none">
                <td colspan="7">
                    {{ Form::textarea('draft_item['.$draft_item->id.'][service]', $draft_item->service,  array('placeholder'=>__('Leistungen'), 'class'=>'form-control autoExpand', 'id'=>'draft_item_'.$draft_item->id.'_service', 'rows'=>'1', 'data-min-rows'=>'1')) }}
                </td>
                <td>
                    <label class="custom-control custom-checkbox mt-2" style="margin-right: 20px;">
                        {!! Form::checkbox('draft_item['.$draft_item->id.'][in_pdf]', 1, $draft_item->in_pdf, ['class'=>'custom-control-input']) !!}
                        <span class="custom-control-label">{{__('in PDF')}}</span>
                    </label>
                </td>
            </tr>
        </tbody>
        @endforeach
        </table>
        </div>
    @endif

    @if(!empty($draft_items))

        @foreach($draft_items as $key => $draft_item)
            <!-- item -->
            <div class="card mb-2">

                <div class="card-body row">

                    <div class="form-group col-sm-6">
                        <select class="selectpicker" data-style="btn-light" name="draft_item[{{$key}}][draft_type_id]" id="draft_item_{{$key }}_draft_type_id" data-live-search="true" required>
                            <option value="" disabled>Vorlagen-Typ*</option>
                            {!!  HTMLHelper::getDraftTypes($draft_item['draft_type_id']) !!}
                        </select>
                    </div>

                    <div class="form-group col-sm-3 col-6">
                        <select class="selectpicker" data-style="btn-light" name="draft_item[{{$key}}][days_start]]" id="draft_item_{{$key }}_days_start">
                            <option value="">(Datum) Start + Tage</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item['days_start'], 30, ' Tag(e)') !!}
                        </select>
                    </div>

                    <div class="form-group col-sm-3 col-6">
                        <select class="selectpicker" data-style="btn-light" name="draft_item[{{$key}}][days_duration]" id="draft_item_{{$key }}_days_duration">
                            <option value="">(Datum) Dauer + Tage</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item['days_duration'], 30, ' Tag(e)') !!}
                        </select>
                    </div>

                    <div class="form-group col-sm-12">
                        {{ Form::textarea('draft_item['.$key.'][service]', $draft_item['service'],  array('placeholder'=>__('Leistungen'), 'class'=>'form-control', 'id'=>'draft_item_'.$key.'_service', 'rows'=>'1')) }}
                    </div>

                    <div class="form-group col-sm-2 col-3">
                        <select class="selectpicker" data-style="btn-light" name="draft_item[{{$key}}][adult]" id="draft_item_{{$key }}_adult">
                            <option value="">Teilnehmer</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item['adult'], 10, ' T.') !!}
                        </select>
                    </div>

                    <div class="form-group col-sm-2 col-3">
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text">€</span>
                            </div>
                            {{ Form::text('draft_item['.$key.'][price_adult]', $draft_item['price_adult'],  array('placeholder'=>__('Preis in €'), 'class'=>'form-control', 'id'=>'draft_item_'.$key.'_price_adult',)) }}
                        </div>
                    </div>


                    <div class="form-group col-sm-2 col-3">
                        <select class="selectpicker" data-style="btn-light" name="draft_item[{{$key}}][children]" id="draft_item_{{$key}}_children">
                            <option value="">Kinder</option>
                            {!!  HTMLHelper::getRangeOptions($draft_item['children'], 10, ' K.') !!}
                        </select>
                    </div>

                    <div class="form-group col-sm-2 col-3">
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text">€</span>
                            </div>
                            {{ Form::text('draft_item['.$key.'][price_children]', $draft_item['price_children'],  array('placeholder'=>__('Preis in €'), 'class'=>'form-control', 'id'=>'draft_item_'.$key.'_price_children')) }}
                        </div>
                    </div>

                    <div class="form-group col-sm-2 col-3">
                        <label class="custom-control custom-checkbox mt-2">
                            {!! Form::checkbox('draft_item['.$key.'][in_pdf]', 1, $draft_item['in_pdf'], ['class'=>'custom-control-input']) !!}
                            <span class="custom-control-label">{{__('in PDF')}}</span>
                        </label>
                    </div>

                </div>
            </div>

        @endforeach

        <div class="text-left mt-3">
            <button type="submit" name="action" value="saveAllFromOld" class="btn btn-submit">{{ __('aus Vorlage speichern') }}</button>&nbsp;
            <a href="{{route('drafts')}}" class="btn btn-default">{{ __('back') }}</a>
        </div>
    @else
        <div class="text-right">
            <button type="submit" name="action" value="addItem" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> Neue Leistung hinzufügen</button>
        </div>
        <div class="text-left mt-3">
        <button type="submit" name="action" value="saveAll" class="btn btn-submit">{{ __('save changes') }}</button>&nbsp;
        <a href="{{route('drafts')}}" class="btn btn-default">{{ __('back') }}</a>
    </div>
    @endif

    {!! Form::close() !!}


    <script>
        $(document).ready(function() {
            $.dragYScroll();

            var sort_item_pos_number = function(){
                var index = 1;
                $('#table_dragula_tbody .draft_item_tbody').each(function () {
                    $(this).find('.item_pos_number').html(index);
                    $(this).data('rowPosition', index);
                    index++;
                });
            };

            $(function() {
                // Drag handle
                var sortableItems = dragula([$('#table_dragula_tbody')[0]], {
                    moves: function (el, container, handle) {
                        return handle.classList.contains('handle');
                    }
                });
                sortableItems.on('dragend', function() {
                    //sort new
                    sort_item_pos_number();
                });
            });


            $('table .move-up-btn').on('click', function () {
                var thisRow = $(this).closest('tbody');
                var prevRow = thisRow.prev();
                if (prevRow.length) {
                    prevRow.before(thisRow);
                    sort_item_pos_number()

                }
            });

            $('table .move-down-btn').on('click', function () {
                var thisRow = $(this).closest('tbody');
                var nextRow = thisRow.next();
                if (nextRow.length) {
                    nextRow.after(thisRow);
                    sort_item_pos_number()

                }
            });
        });
    </script>






@endsection