 <?php

    use Carbon\Carbon;

    if(!request()->year) request()->merge(['year' => date('Y')]);
    if(!request()->month) request()->merge(['month' => date('m')]);

    use Jackiedo\DotenvEditor\Facades\DotenvEditor;
    $editor = DotenvEditor::load(storage_path('app/settings.env'));
?>

<!doctype html>
<html lang="en" class="h-100">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Übersicht Rechnungswesen - {{$editor->getKey('APP_TITLE')['value']}}</title>
    <link rel="icon" type="image/vnd.microsoft.icon" href="/images/bürozeit.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@7.4.47/css/materialdesignicons.min.css">

    <!-- JQuery -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>

   <!-- Bootstrap Table -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.26.0/dist/bootstrap-table.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.26.0/dist/bootstrap-table.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.26.0/dist/extensions/cookie/bootstrap-table-cookie.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.26.0/dist/locale/bootstrap-table-de-DE.js"></script>

    <!-- Bootstrap -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" ></script>

    <!-- Datepicker -->
    <script src="{{url('/js/bootstrap-datepicker.min.js')}}"></script>
    <script src="{{url('/js/day.js')}}"></script>
    <link rel="stylesheet" href="{{url('/css/bootstrap/bootstrap-datepicker.min.css')}}">

     <link rel="stylesheet" href="{{url('/css/xeditable.css')}}">
     <script src="{{url('/js/bootstrap.editable.js')}}"></script>
    <script src="{{url('/js/x-editable.js')}}"></script>
    <script src="{{url('/js/bootstrap-table.editable.js')}}"></script>

    <link rel="stylesheet" href="{{url('/css/bootstrap/bootstrap.dialogs.css')}}">
    <script src="{{url('/js/bootstrap.dialogs.js')}}"></script>

    <script src="{{url('js/hwplus.js')}}"></script>
    <link rel="stylesheet" href="{{url('css/hwplus.css')}}">
    <style>

        .btn-pink {
            background-color:var(--pink)
        }

        div.datepicker-months {
            display:inline;
        }

        .form-control.search-input {
            background-color:var(--pink);
            color:var(--white);
            border-color:var(--pink);
        }

        .form-control.search-input::placeholder {
            color:var(--white);
            opacity: 1; /* Firefox */
        }

        .form-control.search-input::-ms-input-placeholder { /* Edge 12 -18 */
            color:var(--white);
        }

    </style>

    <script>

  function main_buttons () {
    return {

    }
  }

  const queryString = window.location.search;
  const urlParams = new URLSearchParams(queryString);
  const year = urlParams.get('year');
  const month = urlParams.get('month');
  let defaultViewDate = "";


  function set_date_paid(el, row) {

        if ($(el).is(':checked')) {
            $.post("../set_date_paid/"+row+"?paid=1");
        } else {
            $.post("../set_date_paid/"+row+"?paid=0");
        }

  }

  $(function() {

        $('.filter_table').on('click', function() {
            BootstrapDialog.show({
                message: $('<div></div>').load("../d7c52cedbc69872c2582e36e1cae32851b8a66d5"),
                title:'Filtereinstellungen',
                type:BootstrapDialog.TYPE_LIGHT,
                buttons: [{
                    label: 'Filter anwenden',
                    cssClass:'btn-success',
                    action: function(dialogItself) {

                        let sel = $('input[name="filter_option"]:checked').val();
                        switch(sel) {

                            case "all":
                                $('table').bootstrapTable('filterBy', {}, {'filterAlgorithm': (row, filters) => {
                                    return true;
                                }});
                                $('.select_date_filter').html('Alle');
                                dialogItself.close();
                            break;

                            case "in_progress":
                                $('table').bootstrapTable('filterBy', {}, {'filterAlgorithm': (row, filters) => {
                                    return row.invoice_number == "";
                                }});
                                $('.select_date_filter').html('offen');
                                 dialogItself.close();
                            break;

                            case "ready":
                                $('table').bootstrapTable('filterBy', {}, {'filterAlgorithm': (row, filters) => {
                                    return row.invoice_number != "" && row.mailed == "" && row.printed == "";
                                }});
                                $('.select_date_filter').html('fertig');
                                 dialogItself.close();
                            break;

                            case "send":
                                $('table').bootstrapTable('filterBy', {}, {'filterAlgorithm': (row, filters) => {
                                    return row.invoice_number != "" && (row.mailed != "" || row.printed != "");
                                }});
                                $('.select_date_filter').html('versandt');
                                 dialogItself.close();
                            break;

                            case "month":
                                if(!$('#filter_option_date').val()) BootstrapDialog.alert({title:'Fehler', message:'Kein Datum ausgewählt',    type:BootstrapDialog.TYPE_WARNING});
                                let date = $('#filter_option_date').val().split(".");
                                if(date[0] && date[1]) window.location = 'afe4f16263308b092d15e04719f202efe1326813?year=' + date[1] + '&month=' + date[0];
                                dialogItself.close();
                            break;

                        }

                    }
                }, {
                    label: 'Abbrechen',
                    action: function(dialogItself){
                        dialogItself.close();
                    }
                }]
            });
        });

        if(month && year) {
            defaultViewDate = month + '.' + year;
            $('.select_date_filter').html(defaultViewDate);
        } else {
            let today = new Date();
            let year = today.getFullYear();
            let month = today.getMonth();
            defaultViewDate = "today";
            $('.select_date_filter').html(month + '.' + year);
        }



        $('#sub_table').on('check.bs.table', function(event, row) {
            $('#menu_button').prop("disabled",false);

        });

        $('#sub_table').on('uncheck.bs.table', function() {
            $('#menu_button').prop("disabled",true);
        });





    });



</script>

  </head>
  <body class="bg-transparent">



    <div class="container pt-3 bg-white border-left border-right " style="border-color:var(--pink)!important">

        <nav class="navbar navbar-expand-lg navbar-light text-white" style="background-color:var(--pink)">
                <a class="navbar-brand text-white" href="#"><i class="mdi mdi-reminder"></i> Mahnwesen</a>
        </nav>

        <table id="main_table"
            data-toggle="table"
            data-search="true"
            data-show-search-clear-button="true"
            data-buttons="main_buttons"
            data-sort-name="invoice_number"
            data-sort-order="desc"
            data-pagination="true"
            data-cookie="true"
            data-cookie-id-table="frame_invoice_reminders_index"
            data-cookie-storage="localStorage"
            data-page-list="10,25,50,100,all"
            @if(\Browser::isMobile())data-card-view="true"@endif
            data-click-to-select="true"
            data-single-select="true"
            data-buttons-align="left"
            data-buttons-class="pink text-white"
            data-show-button-text="false"
            data-unique-id="ID">
            <thead>
                <tr>
                <th data-field="ID" data-visible="false">ID</th>
                <th data-field="printed" data-visible="false">Gedruckt</th>
                <th data-field="year" data-visible="false">ID</th>
                <th data-field="invoice_number" data-sortable="true" data-align="center" data-style="font-size:large"><b>RG-Nummer</b></th>
                <th data-sortable="true" data-align="center" data-width="10">Typ</th>
                <th data-field="kunde" data-sortable="true">Kunde</th>
                <th data-field="commission_number" data-sortable="true">Kommision</th>
                <th data-align="center" data-field="date" data-sortable="true">RG-Datum</th>
                <th data-field="level" data-sortable="true">Mahnstufe</th>
                <th data-sortable="true"></th>
                </tr>
            </thead>
            <tbody>

                @php $today = Carbon::now(); @endphp


                @foreach(\App\Models\PHPXPress\HwplusInvoices::with('_meta._doctype')
                    ->whereNull('date_adjusted')
                    ->whereNull('date_paid')
                    #->whereNotNull('mailed')
                    #->orWhereNotNull('printed')
                    ->whereNotNull('invoice_number')
                    ->whereHas('_meta._doctype', function($query) {
                        $query->whereIn('identifier', ['invoice', 'standing_order','reverse_invoice', 'adjusted_invoice']);
                    })->get() AS $row)

                    @php

                        $invdate = Carbon::parse($row->date);
                        $validDays = $row->_client->_payment_term->due_date;
                        if($invdate->diff($today)->days <= $validDays) continue;



                        if(!$row->date_payment_reminder) {

                            $days = $row->_client->_payment_term->days_to_remind;

                            if($invdate->diff($today)->days >= $days) {
                                $level = 1;
                            } else continue;

                        } elseif(!$row->date_demand_for_payment ) {

                            $days = $row->_client->_payment_term->days_to_demand;

                            if($invdate->diff($today)->days >= $days) {
                                $level = 2;
                            } else continue;

                        } elseif(!$row->date_dunning_letter) {

                            $days = $row->_client->_payment_term->days_to_dunning_letter;

                            if($invdate->diff($today)->days >= $days) {
                                $level = 3;
                            } else continue;

                        } else {

                            $level = 4;

                        }

                    @endphp


                <tr>
                    <td>{{$row->ID}}</td>
                    <td>{{$row->printed}}</td>
                    <td>{{(new \Carbon\Carbon($row->date))->format('Y')}}</td>
                    <td>{{$row->invoice_number}}</td>
                    <td>{{$row->short_name}}</td>
                    <td>{{$row->client}} {{$row->firstname}} {{$row->surname}}</td>
                    <td>{{$row->commission_number}}</td>
                    <td>{{(new \Carbon\Carbon($row->date))->format('d.m.Y')}}</td>
                    <td>{{$level}}</td>
                    <td><a href="../f7648cbd05e0fe331b0bf690b67ba0be2ee74c1f/27b96af59ee924f9ec98cf9c44b54e06219636c1/{{$row->ID}}"><button class="btn btn-sm" style="background-color:var(--pink); color:white"><i class="bi bi-box-arrow-right"></i></button></a></td>
                </tr>
                @endforeach
            </tbody>
        </table>

        
    </div>
  </body>
  <script>


  </script>
</html>
