
<div class="card mb-2 border-primary">
    <h6 class="card-header bg-primary text-white py-2" data-toggle="collapse" data-target="#collapseBookingInfo" aria-expanded="false" aria-controls="collapseBookingInfo">
        <strong style="line-height: 1.6em"> Kunde / Buchung</strong>
    </h6>
    <div class="collapse @if($id === 'new') show @endif" id="collapseBookingInfo">
     <div class="card-body">
        <div class="form-row">
            <div class="form-group col-md-6">
                <label class="form-label">{{ __('Kunde') }}*
                    @if($travel_user_booking_fewo->travel_user_id)
                        {!! '<a href="'.route('travel_user_detail', [$travel_user_booking_fewo->travel_user_id]) .'">'.$travel_user_booking_fewo->travel_user->first_name.' '.$travel_user_booking_fewo->travel_user->last_name.'</a>'  !!}
                    @endif</label>
                <select class="selectpicker" data-style="btn-light" name="travel_user_id" data-live-search="true" required>
                    {!!  HTMLHelper::getTravelUserOptions($travel_user_booking_fewo->travel_user_id, "DESC",  ($id === "new")) !!}
                </select>


            </div>
            <div class="form-group col-md-6">
                <label class="form-label">{{ __('FeWo') }}*</label>
                <select class="selectpicker" data-style="btn-light" name="fewo_lodging_id" required>
                    {!!  HTMLHelper::getFewoLodgingOptions($travel_user_booking_fewo->fewo_lodging_id) !!}
                </select>
            </div>
        </div>

        <hr>

        <div class="form-row">
            <div class="form-group col-sm-6">
                <label class="form-label" for="invoice_number">{{ __('Rechnungsnummer') }} (unique)</label>@if($travel_user_booking_fewo->isInvoice()) <em class="small">Rechnungsnummer ist PDF-Name der Mietbestätigung</em> @endif
                {{ Form::text('invoice_number', $travel_user_booking_fewo->invoice_number,  array('placeholder'=>__('Rechnungsnummer'), 'class'=>'form-control')) }}
            </div>
            <div class="form-group col-sm-6">
                <label class="form-label" for="booking_date">{{ __('Buchungsdatum') }}*</label>
                {{ Form::text('booking_date', $travel_user_booking_fewo->booking_date,  array('placeholder'=>__('Buchungsdatum'), 'class'=>'form-control datepicker-base', 'required'=>true)) }}
            </div>
        </div>

        <hr>

        <div class="form-row">

            <div class="form-group col-sm-4">
                <label class="form-label" for="adults">{{ __('Erwachsene') }}</label>
                <select class="selectpicker" data-style="btn-light" name="adults" required>
                    {!!  HTMLHelper::getRangeOptions($travel_user_booking_fewo->adults, $max_adults, '', 0) !!}
                </select>
            </div>
            <div class="form-group col-sm-4">
                <label class="form-label" for="children">{{ __('Kinder') }}</label>
                <select class="selectpicker" data-style="btn-light" name="children" required>
                    {!!  HTMLHelper::getRangeOptions($travel_user_booking_fewo->children, $max_children, '', 0) !!}
                </select>

            </div>
            <div class="form-group col-sm-4">
                <label class="form-label" for="persons">{{ __('Personen Gesamt') }}*</label>
                {{ Form::text('persons', $travel_user_booking_fewo->persons,  array('placeholder'=>__('Personen'), 'class'=>'form-control', 'readonly')) }}
            </div>
        </div>
        <hr>

        <div class="form-row">
            <div class="form-group col-sm-6">
                <label class="form-label" for="from_date">{{ __('Datum von') }}*</label>
                {{ Form::text('from_date', $travel_user_booking_fewo->from_date,  array('placeholder'=>__('Datum von'), 'class'=>'form-control datepicker-base', 'required'=>true)) }}
            </div>
            <div class="form-group col-sm-6">
                <label class="form-label" for="to_date">{{ __('Datum bis') }}*</label>
                {{ Form::text('to_date', $travel_user_booking_fewo->to_date,  array('placeholder'=>__('Datum bis'), 'class'=>'form-control datepicker-base', 'required'=>true)) }}
            </div>
            <div class="col-sm-12">
                <div class="float-right">
                    <em><small>Bei Datumsänderungen wird die Verfügbarkeit geprüft.</small></em>
                </div>
                    <label class="custom-control custom-checkbox">
                        {!! Form::checkbox('calculate_price_new', 1, 0, ['class'=>'custom-control-input']) !!}
                        <span class="custom-control-label">Neue Berechnung der Preise aus dem System.</span>
                    </label>
            </div>
        </div>
        <hr>
        <div class="form-row">
            @if(isset($travel_user_booking_fewo->daily_prices))
                @foreach($travel_user_booking_fewo->daily_prices as $s => $d)
                    <div class="form-group col-sm-12">
                        {{$s}} | {{$d['fromDay']}} - {!! Carbon::parse($d['toDay'])->modify('+1 day')->format("d.m.Y") !!} | {{$d['numberDays']}} Nächte x {{$d['perNight']}} € / pro Nacht | <strong>{{$d['price']}} € Gesamt</strong>
                    </div>
                @endforeach
            @endif
            <div class="form-group col-sm-4 col-md-4 col-lg-3">

                <div class="row">
                    <div class="col-6">
                        <label class="form-label" for="price_travel">{{ __('Mietpreis alle Tage') }}</label>

                        {{ Form::text('price_travel', $travel_user_booking_fewo->price_travel,  array('placeholder'=>__('Preis Übernachtungen'), 'class'=>'form-control input-auto-calc')) }}
                    </div>
                    <div class="col-6">
                        <label class="form-label" for="price_travel">{{ __('Mietp. zus. P.') }}</label>
                        {{ Form::text('price_extra', $travel_user_booking_fewo->price_extra,  array('placeholder'=>__('zusätzliche Personen'), 'class'=>'form-control input-auto-calc')) }}
                    </div>
                </div>
            </div>

            <div class="form-group col-sm-8 col-md-8 col-lg-3">
                <label class="form-label" for="price_balance">{{ __('Provisionsausgleich durch Vertriebskanal') }}</label>
                <div class="row">
                    <div class="col-6">
                        {{ Form::text('price_balance', $travel_user_booking_fewo->price_balance,  array('placeholder'=>__(' Provisionsausgleich durch Vertriebskanal'), 'class'=>'form-control input-auto-calc')) }}
                    </div>
                    <div class="col-6">
                        {{ Form::text('price_travel_total', $travel_user_booking_fewo->price_travel_total,  array('placeholder'=>__('Gesamt'), 'class'=>'form-control', 'readonly')) }}
                    </div>
                </div>
            </div>
            <div class="form-group col-6 col-sm-4 col-md-4 col-lg-2">
                <label class="form-label" for="price_service">{{ __('Service-Gebühr') }}</label>
                {{ Form::text('price_service', $travel_user_booking_fewo->price_service,  array('placeholder'=>__('Preis Service-Gebühr'), 'class'=>'form-control input-auto-calc')) }}
            </div>
            <div class="form-group col-6 col-sm-4 col-md-4 col-lg-2">
                <label class="form-label" for="price_deposit">{{ __('Kaution') }}</label>
                {{ Form::text('price_deposit', $travel_user_booking_fewo->price_deposit,  array('placeholder'=>__('Preis Kaution'), 'class'=>'form-control input-auto-calc')) }}
            </div>
            <div class="form-group col-sm-4 col-md-4 col-lg-2">
                <label class="form-label" for="price_total">{{ __('Preis Gesamt') }}*</label>
                {{ Form::text('price_total', $travel_user_booking_fewo->price_total,  array('placeholder'=>__('Preis Gesamt'), 'class'=>'form-control', 'readonly')) }}
            </div>
        </div>

        <hr>
        <div class="form-row">
            <div class="col-sm-12">
                <label class="form-label">Eingetragen in</label>
            </div>
            <div class="form-group col-sm-4">
                <label class="custom-control custom-checkbox">
                    {!! Form::checkbox('is_calendar_fewo_direct', 1, $travel_user_booking_fewo->is_calendar_fewo_direct, ['class'=>'custom-control-input']) !!}
                    <span class="custom-control-label">{{__('FEWO Direkt')}}</span>
                </label>
            </div>
            <div class="form-group col-sm-4">
                <label class="custom-control custom-checkbox">
                    {!! Form::checkbox('is_calendar_hrs', 1, $travel_user_booking_fewo->is_calendar_hrs, ['class'=>'custom-control-input']) !!}
                    <span class="custom-control-label">{{__('HRS')}}</span>
                </label>
            </div>
            <div class="form-group col-sm-4">
                <label class="custom-control custom-checkbox">
                    {!! Form::checkbox('is_calendar_stern_tours', 1, $travel_user_booking_fewo->is_calendar_stern_tours, ['class'=>'custom-control-input']) !!}
                    <span class="custom-control-label">{{__('STERN TOURS')}} <br>Im Kalender:
                        @if($travel_user_booking_fewo->fewo_reservation && isset($travel_user_booking_fewo->fewo_reservation->from_date))
                            {{ $travel_user_booking_fewo->fewo_reservation->from_date->format('d.m.Y') }} - {{ $travel_user_booking_fewo->fewo_reservation->to_date->format('d.m.Y') }}
                        @else
                            nicht eingetragen
                        @endif
                    </span>
                </label>
            </div>
        </div>
        <div class="form-row">
            <div class="col-sm-6">
                <input type="hidden" name="status" value="0">
                {{-- <div class="form-group">
                    <label class="form-label">{{ __('Status') }}*</label>
                    <select class="selectpicker" data-style="btn-light" name="status" required>
                        {!!  \App\Models\TravelUserBookingFewo::getStatuesOptions($travel_user_booking_fewo->status) !!}
                    </select>
                </div>
                --}}
                <div class="form-group">
                <label class="form-label">{{ __('Kanal') }}*</label>
                <select class="selectpicker" data-style="btn-light" name="travel_booking_fewo_channel_id" required>
                    {!!  HTMLHelper::getTravelBookingFewoChannelOptions($travel_user_booking_fewo->travel_booking_fewo_channel_id) !!}
                </select>
                </div>
            </div>
            <div class="form-group col-sm-6">
                <label class="form-label" for="status_text">{{ __('Notiz') }}</label>
                {{ Form::textarea('status_text', $travel_user_booking_fewo->status_text, ['class' => 'form-control autoExpand', 'rows'=>1]) }}
            </div>
        </div>
        <hr>
        <div class="form-row">
            <div class="form-group col-sm-12">
                <label class="form-label" for="notice">{{ __('Bemerkung Kunde') }}</label>
                {{ Form::textarea('notice', $travel_user_booking_fewo->notice, ['class' => 'form-control autoExpand', 'rows'=>1]) }}
            </div>
        </div>
        <div class="float-right">
           Letze Änderung: {{ $travel_user_booking_fewo->getLastChangeAt() }}
        </div>
        <button type="submit" name="action" value="saveAll" class="btn btn-submit btn-sm btn-secondary">{{ __('save changes') }}</button>&nbsp;
    </div>
    </div>
</div>



<script>
    function floatNumber(n) {
        'use strict';
        n = n.replace(/\./g, '').replace(',', '.');
        return parseFloat(n);
    }

    function formatNumber(obj) {
        n = obj.val();
        n = n.replace(/\./g, '').replace(',', '.');
        n = parseFloat(n).toFixed(2);
        if(isNaN(n)){
            return obj.val(0);
        }
        return obj.val(n.replace(".", ","));
    }

    function check_price_total(){
        //
        price_total_float = calculate_price_total();

        var val_price_total = floatNumber($("input[name='price_total']").val());

        if(val_price_total === price_total_float){
            $("input[name='price_total']").removeClass('is-invalid');
            $("input[name='price_total']").addClass('is-valid');
        }else{
            $("input[name='price_total']").removeClass('is-valid');
            $("input[name='price_total']").addClass('is-invalid');
        }

    }

    function calculate_price_total(set_in_form = false){

        formatNumber($("input[name='price_travel']"));
        formatNumber($("input[name='price_balance']"));
        formatNumber($("input[name='price_extra']"));
        formatNumber($("input[name='price_travel_total']"));
        formatNumber($("input[name='price_service']"));
        formatNumber($("input[name='price_deposit']"));


        price_travel = floatNumber($("input[name='price_travel']").val());
        price_balance =floatNumber($("input[name='price_balance']").val());
        price_extra =floatNumber($("input[name='price_extra']").val());
        price_service = floatNumber($("input[name='price_service']").val());
        price_deposit = floatNumber($("input[name='price_deposit']").val());

        price_travel_total = parseFloat(price_travel + price_balance + price_extra);
        price_travel_total = parseFloat((Math.round(price_travel_total * 100) / 100)).toFixed(2).replace(".", ",");

        price_total_float = parseFloat(price_travel + price_balance + price_extra + price_service + price_deposit);
        price_total = parseFloat((Math.round(price_total_float * 100) / 100)).toFixed(2).replace(".", ",");

        if(set_in_form){
            $("input[name='price_travel_total']").val(price_travel_total);
            $("input[name='price_total']").val(price_total);
        }
        return price_total_float;

    }
    $( document ).ready(function() {
        @if($travel_user_booking_fewo->isInvoice())
        $("input[name='invoice_number']").on('focus', function () {
            if (!$(this).data('focused')) {
                $(this).data('focused', true);
                alert('Mietbestätigung/Anreiseinfo sind der Rechnungsnummer zugeordnet, bei Änderungen mussen die PDFs neu erstellt werden.');
            }
        });
        $("input[name='invoice_number']").on('onblur', function () {
            $(this).data('focused', false);
        });
        @endif

        $('input.input-auto-calc').on('change', function () {
            calculate_price_total(true);
            check_price_total();
        });

        $("input[name='price_total']").on('change', function () {
            check_price_total();
        });
        check_price_total();

    });
</script>

