@extends('layouts.layout-2')

@section('content')

    <div class="card">
        <h6 class="card-header">
            Zahlungen / offene Gutschriften
        </h6>
        <div class="card-datatable table-responsive pt-0">
            <table class="datatables-style table table-striped table-bordered">
                <thead>
                <tr>
                    <th>{{__('Vorname')}}</th>
                    <th>{{__('Nachname') }}</th>
                    <th>{{__('E-Mail') }}</th>
                    <th>{{__('Betrag') }}</th>
                    <th>{{__('Aus Bestellung')}}</th>
                    <th>{{__('#')}}</th>
                </tr>
                </thead>
                <tbody>
                    @foreach ($ShoppingOrderMargins as $ShoppingOrderMargin)
                        <tr>
                            <td>{{ $ShoppingOrderMargin->first_name }}</td>
                            <td>{{ $ShoppingOrderMargin->last_name }}</td>
                            <td>{{ $ShoppingOrderMargin->email }}</td>
                            <td>{!! \App\Services\UserMarign::getMontlyPartnerCommissionOpenByID($ShoppingOrderMargin->user_id, null, true) !!} &euro;</td>
                            <td>
                                @foreach (\App\Services\UserMarign::getOrderFromPartnerCommissionByID($ShoppingOrderMargin->user_id) as $order)
                                    @if($order->shopping_order)
                                    <a href="{{ route('admin_sales_customers_detail', [$order->shopping_order->id]) }}">
                                        {{$order->shopping_order->shopping_user->billing_firstname }}
                                        {{$order->shopping_order->shopping_user->billing_lastname }}
                                        / {{ $order->shopping_order->getLastShoppingPayment('reference') }}
                                        / {{ $order->shopping_order->getFormattedTotalWithoutCredit()." €" }}
                                        / {{ $order->shopping_order->created_at->format("d.m.Y") }}
                                    </a> <br>
                                    @endif
                                @endforeach
                            </td>
                            <td>
                                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-credit"
                                    data-userid="{{ $ShoppingOrderMargin->user_id }}" 
                                    data-email="{{ $ShoppingOrderMargin->email }}" 
                                    data-back="{{url()->current()}}"
                                    data-action="create_credit">
                                <span class="far fa-file-invoice-dollar"></span> <strong>Gutschrift erstellen</strong>
                             </button>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>


    

    <div class="card mt-3">
        <h6 class="card-header">
            Zahlungen / offene Gutschriften pending
        </h6>
        <div class="card-datatable table-responsive pt-0">
            <table class="datatables-style table table-striped table-bordered">
                <thead>
                <tr>
                    <th>{{__('Vorname')}}</th>
                    <th>{{__('Nachname') }}</th>
                    <th>{{__('E-Mail') }}</th>
                    <th>{{__('Betrag') }}</th>
                    <th>{{__('aus Bestellung')}}</th>
                </tr>
                </thead>
                <tbody>
                    @foreach ($ShoppingOrderMarginPendings as $ShoppingOrderMarginPending)
                        <tr>
                            <td>{{ $ShoppingOrderMarginPending->first_name }}</td>
                            <td>{{ $ShoppingOrderMarginPending->last_name }}</td>
                            <td>{{ $ShoppingOrderMarginPending->email }}</td>
                            <td>{!! \App\Services\UserMarign::getMontlyPartnerCommissionPendingByID($ShoppingOrderMarginPending->user_id, null, true) !!} &euro;</td>
                            <td>
                                @foreach (\App\Services\UserMarign::getOrderFromPartnerCommissionPendingByID($ShoppingOrderMarginPending->user_id) as $order)
                                @if($order->shopping_order)
                                <a href="{{ route('admin_sales_customers_detail', [$order->shopping_order->id]) }}">
                                    {{$order->shopping_order->shopping_user->billing_firstname }}
                                    {{$order->shopping_order->shopping_user->billing_lastname }}
                                    / {{ $order->shopping_order->getLastShoppingPayment('reference') }}
                                    / {{$order->shopping_order->getFormattedTotalWithoutCredit()." €" }}
                                    / {{ $order->shopping_order->created_at->format("d.m.Y") }}
                                 
                                </a> <br>
                                @endif
                            @endforeach
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <div class="card mt-3">
        <h6 class="card-header">
            Zahlungen / erstellte Gutschriften
        </h6>
            <div class="col-sm-6 mb-0 mt-2">
                {!! Form::open(['url' => route('admin_payments_credit'), 'class' => 'form-horizontal', 'id'=>'form_filter_sales_year']) !!}

                <label class="form-label" for="filter_sales_year">Filter Jahr</label>
                <select class="custom-select" name="filter_sales_year" id="filter_sales_year">
                    @foreach($years as $year)
                        <option value="{{$year}}" @if($active_year == $year) selected @endif>{{$year}}</option>
                    @endforeach
                </select>
                {!! Form::close() !!}

            </div>
   
        <div class="card-datatable table-responsive pt-0">
            <table class="datatables-style table table-striped table-bordered" id="datatable-credit">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>{{__('Vorname')}}</th>
                    <th>{{__('Nachname') }}</th>
                    <th>{{__('E-Mail') }}</th>
                    <th>{{__('Betrag') }}</th>
                    <th>{{__('Datum') }}</th>
                    <th>{{__('aus Bestellung')}}</th>
                    <th>{{__('Status')}}</th>
                    <th>{{__('Gutschrift')}}</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
    <script>
        $( document ).ready(function() {
            var oTable = $('#datatable-credit').DataTable({
                    "processing": true,
                    "serverSide": true,
                    ajax: {
                        url: '{!! route( 'admin_payments_credit_datatable') !!}',
                        data: function(d) {
                            d.filter_sales_year = $('select[name=filter_sales_year]').val();
                        }
                    },
                    "order": [[0, "desc" ]],
                    "columns": [
                        { data: 'id', searchable: false },
                        { data: 'user.account.first_name', name: 'user.account.first_name', orderable: false },
                        { data: 'user.account.last_name', name: 'user.account.last_name', orderable: false },
                        { data: 'user.email', name: 'user.email', orderable: false },
                        { data: 'total', name: 'total' },
                        { data: 'date', name: 'date' },
                        { data: 'user_margins', name: 'user_margins', orderable: false },
                        { data: 'status', name: 'status', searchable: false  },
                        { data: 'credit', name: 'credit', orderable: false, searchable: false  },
                    ],
                    "bLengthChange": false,
                    "iDisplayLength": 100,
                    "language": {
                        "url": "/js/German.json"
                    }
                });
            $('#filter_sales_year').on('change', function(){
                oTable.draw();
            });
        });



        /*$('#filter_sales_year').on('change', function(){
            $('#form_filter_sales_year').submit();
        });*/


        $( document ).ready(function() {
            $('#modals-credit').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                $(this).find(".modal-content input[name='userid']").val(button.data('userid'));
                $(this).find(".modal-body #set_credit_send_mail").html(button.data('email'));
            });
        });

    
    </script>


    <div class="modal fade" id="modals-credit">
        <div class="modal-dialog">
            <form class="modal-content" action="{{ route('admin_payments_credit_create') }}" method="post">
                @csrf
                <input type="hidden" name="userid" value="">
                <input type="hidden" name="action" value="create_credit">
                <input type="hidden" name="back" value="{{url()->current()}}">
                <div class="modal-header">
                    <h5 class="modal-title">{{__('Gutschrift')}}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>
                </div>
                <div class="modal-body">
                    <div class="form-group col-sm-12">
                        {{ Form::select('credit_option', ['create'=>'Gutschrift erstellen'], false, array('data-live-search'=>'false', 'class'=>'selectpicker')) }}
                    </div>
                    <div class="form-group col-sm-12">
                        <label class="form-label" for="credit_date">{{ __('Gutschriftsdatum') }}</label>
                        {!! Form::text('credit_date', \Carbon::now()->format("d.m.Y"), ['class'=>'form-control datepicker-base']) !!}
                    </div>
                    <div class="form-group col-sm-12">
                        <label class="form-label" for="credit_number">{{ __('Gutschriftsnummer') }}</label>
                        {!! Form::text('credit_number', App\Services\Invoice::getInvoiceNumber(), ['class'=>'form-control']) !!}
                        <em> nächste Rechnungsnummer <a href="{{ route('admin_settings') }}"><i class="fa fa-edit"></i></a></em>
                    </div>
                    <div class="form-group col-sm-12">
                        <label class="custom-control custom-checkbox">
                            {!! Form::checkbox('credit_send_mail', 1, true, ['class'=>'custom-control-input']) !!}
                            <span class="custom-control-label">Rechnung an  <span id="set_credit_send_mail">123</span></span>
                        </label>
                    </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">{{__('save')}}</button>
                </div>
            </form>
        </div>
    </div>

@endsection
