<h2 class="mt-3">Rechnungsdetails</h2>
<div class="row">
    <div class="col-12">
        <div class="form-group">
            <label class="switcher switcher-success">
                {!! Form::checkbox('is_invoice_details_private', 1, true, ['id' => 'switch_invoice_details_private', 'class' => 'switcher-input']) !!}
                <span class="switcher-indicator">
                    <span class="switcher-yes">
                        <span class="ion ion-md-checkmark"></span>
                    </span>
                    <span class="switcher-no">
                    </span>
                </span>
                <span class="switcher-label">Privat (keine Firma)</span>
            </label>
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <label class="switcher switcher-success">
                {!! Form::checkbox('is_invoice_details_germany', 1, (Yard::instance('shopping')->getShippingCountryId() == 1 ? true : false), ['id' => 'switch_invoice_details_germany', 'class' => 'switcher-input']) !!}
                <span class="switcher-indicator">
                    <span class="switcher-yes">
                        <span class="ion ion-md-checkmark"></span>
                    </span>
                    <span class="switcher-no">
                    </span>
                </span>
                <span class="switcher-label">aus Deutschland</span>
            </label>
        </div>
    </div>
    <div class="col-12">
        <div class="form-group {{ $errors->has('billing_salutation') ? 'error' : '' }}">
            <label for="billing_salutation">Anrede <span class="required">*</span></label>
            <select id="billing_salutation" name="billing_salutation"
                class="form-control selectpicker revalidat" data-style="btn-default" data-error="#error-billing_salutation" required>
                {!! HTMLHelper::getSalutation(old('billing_salutation')) !!}
            </select>
            <div id="error-billing_salutation" class="text-left"></div>
            @if ($errors->has('billing_salutation'))
                <label for="billing_salutation" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_salutation') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="billing_firstname">Vorname <span class="required">*</span></label>
            {!! Form::text('billing_firstname', '', ['class' => 'form-control ' . ($errors->has('billing_firstname') ? 'error' : ''), 'id' => 'billing_firstname', 'required' => true]) !!}
            @if ($errors->has('billing_firstname'))
                <label for="billing_firstname" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_firstname') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="billing_lastname">Nachname <span class="required">*</span></label>
            {!! Form::text('billing_lastname', '', ['class' => 'form-control ' . ($errors->has('billing_lastname') ? 'error' : ''), 'id' => 'billing_lastname', 'required' => true]) !!}
            @if ($errors->has('billing_lastname'))
                <label for="billing_lastname" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_lastname') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12 not-show show_invoice_details_private">
        <div class="form-group">
            <label for="billing_company">Firmenname (optional)</label>
            {!! Form::text('billing_company', '', ['class' => 'form-control', 'id' => 'billing_company']) !!}
        </div>
    </div>
    <div class="col-12 not-show show_invoice_details_germany">
        <div class="form-group">
            <label for="billing_state">Land / Region <span class="required">*</span></label>
            <select id="billing_state" name="billing_state" class="form-control selectpicker"
                data-style="btn-default">
                {!! HTMLHelper::getCountriesForShipping(Yard::instance('shopping')->getShippingCountryId(), false, 2) !!}
            </select>
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <label for="billing_address">Straße + Hausnummer <span
                    class="required">*</span></label>
            {!! Form::text('billing_address', '', ['class' => 'form-control ' . ($errors->has('billing_address') ? 'error' : ''), 'id' => 'billing_address', 'required' => true]) !!}
            @if ($errors->has('billing_address'))
                <label for="billing_address" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_address') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            {!! Form::text('billing_address_2', '', ['placeholder' => 'Zusätzliche Angaben / Hinweise (optional)', 'class' => 'form-control ' . ($errors->has('billing_address_2') ? 'error' : ''), 'id' => 'billing_address_2']) !!}
            @if ($errors->has('billing_address_2'))
                <label for="billing_address_2" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_address_2') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="billing_zipcode">PLZ <span class="required">*</span></label>
            {!! Form::text('billing_zipcode', '', ['class' => 'form-control ' . ($errors->has('billing_zipcode') ? 'error' : ''), 'id' => 'billing_zipcode', 'required' => true]) !!}
            @if ($errors->has('billing_zipcode'))
                <label for="billing_zipcode" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_zipcode') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="billing_city">Stadt <span class="required">*</span></label>
            {!! Form::text('billing_city', '', ['class' => 'form-control ' . ($errors->has('billing_city') ? 'error' : ''), 'id' => 'billing_city', 'required' => true]) !!}
            @if ($errors->has('billing_city'))
                <label for="billing_city" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_city') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <label for="billing_email">E-Mail <span class="required">*</span></label>
            {!! Form::email('billing_email', '', ['class' => 'form-control ' . ($errors->has('billing_email') ? 'error' : ''), 'id' => 'billing_email', 'required' => true]) !!}
            @if ($errors->has('billing_email'))
                <label for="billing_email" class="error text-danger small"
                    style="display: block;">{{ $errors->first('billing_email') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <label class="switcher switcher-success">
                {!! Form::checkbox('same_as_billing', 1, '', ['id' => 'shipping_address_switch', 'class' => 'switcher-input']) !!}
                <span class="switcher-indicator">
                    <span class="switcher-yes">
                        <span class="ion ion-md-checkmark"></span>
                    </span>
                    <span class="switcher-no">
                    </span>
                </span>
                <span class="switcher-label">Lieferung an eine andere Adresse senden?</span>
            </label>
        </div>
    </div>
</div>

<div class="row" id="shipping_address">
    <div class="col-12">
        <hr class="">
    </div>
    <div class="col-12">
        <h2 class="mt-3">Lieferadresse</h2>
    </div>
    <div class="col-12">
        <div class="form-group {{ $errors->has('shipping_salutation') ? 'error' : '' }}">
            <label for="shipping_salutation">Anrede <span class="required">*</span></label>
            <select id="shipping_salutation" name="shipping_salutation"
                class="form-control selectpicker revalidat" data-style="btn-default" data-error="#error-shipping_salutation" required>
                {!! HTMLHelper::getSalutation(old('shipping_salutation')) !!}
            </select>
            <div id="error-shipping_salutation" class="text-left"></div>
            @if ($errors->has('shipping_salutation'))
                <label for="shipping_salutation" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_salutation') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="shipping_firstname">Vorname <span class="required">*</span></label>
            {!! Form::text('shipping_firstname', '', ['class' => 'form-control ' . ($errors->has('shipping_firstname') ? 'error' : ''), 'id' => 'shipping_firstname', 'required' => true]) !!}
            @if ($errors->has('shipping_firstname'))
                <label for="shipping_firstname" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_firstname') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="shipping_lastname">Nachname <span class="required">*</span></label>
            {!! Form::text('shipping_lastname', '', ['class' => 'form-control ' . ($errors->has('shipping_lastname') ? 'error' : ''), 'id' => 'shipping_lastname', 'required' => true]) !!}
            @if ($errors->has('shipping_lastname'))
                <label for="shipping_lastname" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_lastname') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12 not-show show_invoice_details_private">
        <div class="form-group">
            <label for="shipping_company">Firmenname (optional)</label>
            {!! Form::text('shipping_company', '', ['class' => 'form-control', 'id' => 'shipping_company']) !!}
        </div>
    </div>
    <div class="col-12 not-show show_invoice_details_germany">
        <div class="form-group">
            <label for="shipping_state">Land / Region <span class="required">*</span></label>
            <select id="shipping_state" name="shipping_state" class="form-control selectpicker"
                data-style="btn-default">
                {!! HTMLHelper::getCountriesForShipping(Yard::instance('shopping')->getShippingCountryId(), false, 2) !!}
            </select>
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            <label for="shipping_address">Straße + Hausnummer <span
                    class="required">*</span></label>
            {!! Form::text('shipping_address', '', ['class' => 'form-control ' . ($errors->has('shipping_address') ? 'error' : ''), 'id' => 'shipping_address', 'required' => true]) !!}
            @if ($errors->has('shipping_address'))
                <label for="shipping_address" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_address') }}</label>
            @endif
        </div>
    </div>
    <div class="col-12">
        <div class="form-group">
            {!! Form::text('shipping_address_2', '', ['placeholder' => 'Zusätzliche Angaben / Hinweise (optional)', 'class' => 'form-control ' . ($errors->has('shipping_address_2') ? 'error' : ''), 'id' => 'shipping_address_2']) !!}
            @if ($errors->has('shipping_address_2'))
                <label for="shipping_address_2" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_address_2') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="shipping_zipcode">PLZ <span class="required">*</span></label>
            {!! Form::text('shipping_zipcode', '', ['class' => 'form-control ' . ($errors->has('shipping_zipcode') ? 'error' : ''), 'id' => 'shipping_zipcode', 'required' => true]) !!}
            @if ($errors->has('shipping_zipcode'))
                <label for="shipping_zipcode" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_zipcode') }}</label>
            @endif
        </div>
    </div>
    <div class="col-sm-6">
        <div class="form-group">
            <label for="shipping_city">Stadt <span class="required">*</span></label>
            {!! Form::text('shipping_city', '', ['class' => 'form-control ' . ($errors->has('shipping_city') ? 'error' : ''), 'id' => 'shipping_city', 'required' => true]) !!}
            @if ($errors->has('shipping_city'))
                <label for="shipping_city" class="error text-danger small"
                    style="display: block;">{{ $errors->first('shipping_city') }}</label>
            @endif
        </div>
    </div>
</div>

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

        $('#invoice_details_holder select').selectpicker();
        
        var validator = $("#shop-form-validations").validate({
            submitHandler: function(form) {
                $('.button-prevent-multiple-submits').attr('disabled', true);
                $('.button-prevent-multiple-submits').find('.spinner').show();
                form.submit();
            },
            errorPlacement: function errorPlacement(error, element) {
                var placement = $(element).data('error');
                if (placement) {
                    $(placement).append(error.addClass('invalid-feedback d-block'));
                } else {
                    error.insertAfter(element).addClass('invalid-feedback small d-block');
                }
            },
            highlight: function( element, errorClass, validClass ) {
                if($(element).hasClass('selectpicker')){
                    $(element).parent('.form-control').find('.dropdown-toggle').addClass( errorClass ).removeClass( validClass );
                }else if ( element.type === "radio" ) {
                    this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
                } else {
                    $( element ).addClass( errorClass ).removeClass( validClass );
                }
            },
            unhighlight: function( element, errorClass, validClass ) {
                if($(element).hasClass('selectpicker')){
                    $(element).parent('.form-control').find('.dropdown-toggle').removeClass( errorClass ).addClass( validClass );
                } else if ( element.type === "radio" ) {
                    this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
                } else {
                    $( element ).removeClass( errorClass ).addClass( validClass );
                }
            },
            messages: {
                user_free_product_id: {
                  required: "Bitte ein Produkt auswählen.",
                },
                switchers_shipping: {
                    required: "Bitte eine Auswahl treffen.",
                },
                payment_method: {
                    required: "Bitte eine Zahlungsart auswählen.",
                }
              }
        });

        $('select.revalidat').on('change', function () {
            validator.element($(this));
        });

        function switch_invoice_details_germany(){
            if($('input#switch_invoice_details_germany').is(':checked')){
                $('.show_invoice_details_germany').hide('slow');
            }else{
                $('.show_invoice_details_germany').show('slow');
            }   
        }
        switch_invoice_details_germany();
        $('input#switch_invoice_details_private').on('change', function () {
            if($(this).is(':checked')){
                $('.show_invoice_details_private').hide('slow');
            }else{
                $('.show_invoice_details_private').show('slow');
            }
        });
        $('input#switch_invoice_details_germany').on('change', function () {
            switch_invoice_details_germany();
        });

        // Shipping Address show|hide
        $("#shipping_address_switch").bind("change", function() {
            $('#shipping_address').slideToggle(200, function() {
                if ($('#shipping_address').is(":visible")) {
                    _scrollTo('#shipping_address', 60);
                }
            });
        });
        if ($('#shipping_address_switch').is(':checked')) {
            $('#shipping_address').show();
        } else {
            $('#shipping_address').hide();
        }
    });
</script>