@extends('layouts.layout-2')

@section('content')

<style>
    .td-entry-table-margin {
        padding-bottom: 6px;
        border-bottom: 1px solid rgb(221, 221, 221);
        margin-bottom: 6px;
    }
</style>
    <div class="card">
        <h6 class="card-header">
            Zahlungen / offene Gutschriften

            <div class="float-right">
                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-load-content"
                    data-id="new"
                    data-action="add-user-credit"
                    data-back="{{url()->current()}}"
                    data-route="{{ route('modal_load') }}"><span class="far fa-plus-circle"></span> Gutschrift hinzufügen
                </button>
            </div>
        </h6>

        @if(isset($add_credit_error) && $add_credit_error)
        <div class="col-sm-12">
            <div class="alert alert-danger p-2 mt-2">
                <ul>
                    <li>{{ $add_credit_error }}</li>
                </ul>
            </div>
        </div>
        @endif

        <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  / Gutschrift')}}</th>
                    <th>{{__('#')}}</th>
                </tr>
                </thead>
                <tbody>
                    @foreach ($users_credits as $user_credit)
                        <tr>
                            <td>{{ $user_credit->first_name }}</td>
                            <td>{{ $user_credit->last_name }}</td>
                            <td>{{ $user_credit->email }}</td>
                            <td>{{ formatNumber($user_credit->total) }} €</td>
                            <td>
                                 @foreach($user_credit->items as $entry)
                                        @include('admin.payment.credit.credits-entry', ['entry' => $entry])
                                 @endforeach
                            </td>
                            <td>
                                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modals-credit"
                                    data-userid="{{ $user_credit->user_id }}" 
                                    data-email="{{ $user_credit->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 ($users_credits_pending as $user_credit_pending)
                        <tr>
                            <td>{{ $user_credit_pending->first_name }}</td>
                            <td>{{ $user_credit_pending->last_name }}</td>
                            <td>{{ $user_credit_pending->email }}</td>
                                <td>{{ formatNumber($user_credit_pending->total) }} €</td>
                                <td>  
                                    @foreach($user_credit_pending->items as $entry)
                                        @include('admin.payment.credit.credits-entry', ['entry' => $entry])
                                    @endforeach
                                </td>
                        </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(['action' => 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>{{__('G.Nr.')}}</th>
                    <th>{{__('Gutschrift')}}</th>
                    <th>{{__('Vorname')}}</th>
                    <th>{{__('Nachname') }}</th>
                    <th>{{__('E-Mail') }}</th>
                    <th>{{__('Betrag') }}</th>
                    <th>{{__('Datum') }}</th>
                    <th>{{__('Zahlung')}}</th>
                    <th>{{__('aus Bestellung  / Gutschrift')}}</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <div class="modal fade" id="modals-credit">
        <div class="modal-dialog">
            <form class="modal-content form-prevent-multiple-submits" 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\Credit::getCreditNumber(), ['class'=>'form-control', 'disabled']) !!}
                        <em> nächste Gutschriftsnummer <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">Gutschrift an <span id="set_credit_send_mail">mail</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 button-prevent-multiple-submits"><i class="spinner fa fa-spinner fa-spin"></i> {{ __('save')}}</button>
                </div>
            </form>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            // DataTable-Konfiguration in separate Datei auslagern
            const dataTableConfig = {
                processing: true,
                serverSide: true,
                ajax: {
                    url: '{!! route("admin_payments_credit_datatable") !!}',
                    data: d => ({
                        ...d,
                        filter_sales_year: $('select[name=filter_sales_year]').val()
                    })
                },
                order: [[0, "desc"]],
                columns: [
                    { data: 'id', searchable: false },
                    { data: 'credit_number', name: 'credit_number' },
                    { data: 'credit', name: 'credit', orderable: false, 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: 'status', name: 'status', searchable: false  },
                    { data: 'user_margins', name: 'user_margins', orderable: false },
                ],
                lengthChange: false,
                pageLength: 100,
                language: {
                    url: "/js/German.json"
                }
            };

            const creditTable = $('#datatable-credit').DataTable(dataTableConfig);
            
            // Event Handlers
            $('#filter_sales_year').on('change', () => creditTable.draw());
            
            $('#modals-credit').on('show.bs.modal', function(event) {
                const button = $(event.relatedTarget);
                if (button) {
                    $(this).find("input[name='userid']").val(button.data('userid'));
                    $(this).find("#set_credit_send_mail").html(button.data('email'));
                }
            });
        });
    </script>

@endsection
