@extends('layouts.layout-2')

@section('content')

    @if ($errors->any())
        <div class="row">
            <div class="col-sm-12">
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
    @endif

    <h4 class="font-weight-bold py-2 mb-2">
        {{ __('Create/Edit Versandkosten') }}
    </h4>

    <div class="text-left mt-0 mb-2">
        <a href="{{ route('admin_shippings') }}" class="btn btn-default">{{ __('back') }}</a>
    </div>

    <div class="card mb-2">
        {!! Form::open(['url' => route('admin_shipping_store'), 'class' => 'form-horizontal', 'id'=>'']) !!}
        <input type="hidden" name="id" value="@if($value->id>0){{$value->id}}@else new @endif">
        <h5 class="card-header">
            {{ __('Versandkosten') }}
        </h5>
        <div class="card-body">
            <div class="form-row">
                <div class="form-group col-md-6">

                    <label class="form-label" for="name">{{ __('Name') }}*</label>
                    {{ Form::text('name', $value->name,  array('placeholder'=>__('Name'), 'class'=>'form-control'.($errors->has('name') ? ' is-invalid' : ''), 'required')) }}
                    @if ($errors->has('name'))
                        <span class="invalid-feedback" style="display: inline-block;">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                </div>
                <div class="form-group col-md-6">
                    <label class="custom-control custom-checkbox float-right">
                        {!! Form::checkbox('active', 1, $value->active, ['class'=>'custom-control-input']) !!}
                        <span class="custom-control-label">{{__('aktiv')}}</span>
                    </label>
                    <label class="form-label" for="title">{{ __('Versandkosten frei ab') }}</label>
                    {{ Form::text('free', $value->getFormattedFree(),  array('placeholder'=>__('in Euro, leer lassen ohne Freigrenze'), 'class'=>'form-control')) }}
                </div>
            </div>
            <div class="text-left mt-0 mb-2">
                <button type="submit" class="btn btn-secondary" name="action" value="shipping">{{ __('save') }}</button>&nbsp;
            </div>
        </div>
        {!! Form::close() !!}
    </div>

    @if($value->id>0)
    <div class="card mb-2">
        <h6 class="card-header">
            {{__('Preise')}}
        </h6>
        <div class="card-datatable table-responsive">
            <table class="datatables-style table table-striped table-bordered">
                <thead>
                <tr>
                    <th style="max-width: 60px;">&nbsp;</th>
                    <th>{{__('Kunden Preis')}}</th>

                    <th>{{__('Tax')}}</th>
                    <th>{{__('Preis von - bis')}}</th>
                    <th>{{__('Gewicht von - bis')}}</th>

                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach($value->prices as $price)
                    <tr>
                        <td>
                            <button type="button" class="btn icon-btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-price"
                                    data-id="{{ $price->id }}"
                                    data-price="{{ $price->getFormattedPrice() }}"
                                    data-price_comp="{{ $price->getFormattedPriceComp() }}"
                                    data-num_comp="{{ $price->num_comp }}"
                                    data-tax_rate="{{ $price->getFormattedTaxRate() }}"
                                    data-factor="{{ $price->getFormattedFactor() }}"
                                    data-total_from="{{ $price->getFormatTotalFrom() }}"
                                    data-total_to="{{ $price->getFormattedTotalTo() }}"
                                    data-weight_from="{{ $price->weight_from }}"
                                    data-weight_to="{{ $price->weight_to }}">
                                <span class="far fa-edit"></span>
                            </button>
                        </td>
                        <td>{{ $price->getFormattedPrice() }}</td>
                        <td>{{ $price->getFormattedTaxRate() }}</td>
                        <td>{{ $price->getFormatTotalFrom() }} - {{ $price->getFormattedTotalTo() }}</td>
                        <td>{{ $price->weight_from }} - {{ $price->weight_to }}</td>
                        <td><a class="text-danger" href="{{ route('admin_shipping_price_delete', [$price->id]) }}"  onclick="return confirm('{{__('Really delete entry?')}}');"><i class="far fa-trash-alt"></i></a></td>

                    </tr>
                @endforeach
                </tbody>
            </table>
            <div class="mt-4 ml-4">
                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-price"
                        data-id="new"
                        data-price=""
                        data-tax_rate=""
                        data-factor="1"
                        data-total_from=""
                        data-total_to=""
                        data-weight_from=""
                        data-weight_to=""
                >{{__('Neuen Preis erstellen')}}</button>


            </div>
        </div>
    </div>

    <!-- Modal template price -->
    <div class="modal fade" id="modals-price">
        <div class="modal-dialog">
            <form class="modal-content" action="{{ route('admin_shipping_store') }}" method="post">
                @csrf
                <input type="hidden" name="id">
                <input type="hidden" name="shipping_id" value="@if($value->id>0){{$value->id}}@else new @endif">

                <div class="modal-header">
                    <h5 class="modal-title"> {{__('Preis')}} <span class="font-weight-light">{{__('create/edit')}}</span></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>
                </div>
                <div class="modal-body">

                    <div class="form-row">
                        <div class="form-group col-6">
                            <label for="price" class="form-label">{{__('Kunden Preis (brutto)')}}*</label>
                            <input type="text" class="form-control" name="price" placeholder="{{__('Preis in Euro')}}" required>
                        </div>
                        <div class="form-group col-6">
                            <label for="tax_rate" class="form-label">{{__('enthaltene Tax (%)')}}</label>
                            <input type="text" class="form-control" name="tax_rate" placeholder="{{__('Tax in %')}}">
                        </div>
                    </div>


                    <div class="form-row">
                        <div class="form-group col-6">
                            <label for="total_from" class="form-label">{{__('von Preis')}}</label>
                            <input type="text" class="form-control" name="total_from" placeholder="{{__('Preis in Euro')}}">
                        </div>
                        <div class="form-group col-6">
                            <label for="total_to" class="form-label">{{__('bis Preis')}}</label>
                            <input type="text" class="form-control" name="total_to" placeholder="{{__('Preis in Euro')}}">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group col-6">
                            <label for="weight_from" class="form-label">{{__('von Gewicht (g)')}}</label>
                            <input type="text" class="form-control" name="weight_from" placeholder="{{__('in g')}}">
                        </div>
                        <div class="form-group col-6">
                            <label for="weight_to" class="form-label">{{__('bis Gewicht (g)')}}</label>
                            <input type="text" class="form-control" name="weight_to" placeholder="{{__('in g')}}">
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">{{__('close')}}</button>
                    <button type="submit" class="btn btn-primary" name="action" value="price">{{__('save')}}</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card mb-2">
        <h6 class="card-header">
            {{__('Land')}}
        </h6>
        <div class="card-datatable table-responsive">
            <table class="datatables-style table table-striped table-bordered">
                <tbody>
                @foreach($value->countries as $country)
                    <tr>
                        <td>
                            {{ $country->country->de }}
                        </td>
                        <td>
                            @if(!$country->shopping_orders->count())
                            <a class="text-danger" href="{{ route('admin_shipping_country_delete', [$country->id]) }}"  onclick="return confirm('{{__('Really delete entry?')}}');"><i class="far fa-trash-alt"></i></a>
                            @else
                                -
                            @endif
                        </td>

                    </tr>
                @endforeach
                </tbody>
            </table>
            @if(!$value->countries->count())
            <div class="mt-4 ml-4">
                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-country"
                        data-id="new"
                >{{__('Land hinzufügen')}}</button>

            </div>
                @endif
        </div>
    </div>

    <!-- Modal template -->
    <div class="modal fade" id="modals-country">
        <div class="modal-dialog">
            <form class="modal-content" action="{{ route('admin_shipping_store') }}" method="post">
                @csrf
                <input type="hidden" name="id">
                <input type="hidden" name="shipping_id" value="@if($value->id>0){{$value->id}}@else new @endif">

                <div class="modal-header">
                    <h5 class="modal-title"> {{__('Preis')}} <span class="font-weight-light">{{__('create/edit')}}</span></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>
                </div>
                <div class="modal-body">

                    <div class="form-row">
                        <div class="form-group col">
                            <label for="country_ids" class="form-label">{{__('Versandkosten gelten für diese Länder')}}</label>
                            <select class="selectpicker" data-style="btn-default" name="country_ids[]">
                                {!! HTMLHelper::getCountriesWithoutUsedShippings() !!}
                            </select>
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">{{__('close')}}</button>
                    <button type="submit" class="btn btn-primary" name="action" value="country">{{__('save')}}</button>
                </div>
            </form>
        </div>
    </div>



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

            $('#modals-price').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                $(this).find(".modal-content input[name='id']").val(button.data('id'));
                $(this).find(".modal-body input[name='price']").val(button.data('price'));
                $(this).find(".modal-body input[name='tax_rate']").val(button.data('tax_rate'));
                $(this).find(".modal-body input[name='factor']").val(button.data('factor'));
                $(this).find(".modal-body input[name='total_from']").val(button.data('total_from'));
                $(this).find(".modal-body input[name='total_to']").val(button.data('total_to'));
                $(this).find(".modal-body input[name='weight_from']").val(button.data('weight_from'));
                $(this).find(".modal-body input[name='weight_to']").val(button.data('weight_to'));
            });

            $('#modals-country').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                $(this).find(".modal-content input[name='id']").val(button.data('id'));
            });
        });
    </script>
    @endif
    <div class="text-left mt-2 mb-2">
        <a href="{{ route('admin_shippings') }}" class="btn btn-default">{{ __('back') }}</a>
    </div>




@endsection
