<?php

    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>
    
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.21.1/dist/bootstrap-table.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <!--script src="https://unpkg.com/bootstrap-table@1.21.1/dist/bootstrap-table.min.js"></script-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.21.1/dist/bootstrap-table.min.js"></script>
    <!-- Latest compiled and minified Locales -->
    <!--script src="https://unpkg.com/bootstrap-table@1.21.1/dist/locale/bootstrap-table-de-DE.min.js"></script-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.21.1/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" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></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>
       
        div.datepicker-months {
        
            display:inline;
        
        }

        .form-control.search-input {
            background-color:var(--orange);
            color:var(--white);
            border-color:var(--orange);
        }
        
        .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 buttons () {
    return {
       btnIcon: {
            html:'<button type="image" class="btn" style="color:white !important; background-color:var(--orange)" onclick="window.location=\'../854e5746f03aaac88bb0e294185f2f14f168af43\'"><img src="/images/logo_hell.png" widht=20 height=20/> Startseite</button>',
            
      },
        btnHome: {
            text: '<b>Daueraufträge</b>',
            attributes: {
                title: 'Sie befinden sich hier:',
                disabled:'disabled',
                style:"color:white !important; background-color:var(--orange); border-color:var(--orange); opacity:1"
            },
            
      },
      btnDropDown: {
        text: 'Menü',
        icon: 'bi bi-file-plus',
        html: '' +
                '<button class="btn dropdown-toggle text-white rounded-0" type="button" data-toggle="dropdown" id="menu_button" style="background-color:var(--orange)" disabled>' +
                '<i class="mdi mdi-invoice"></i> ausgewählter Dauerauftrag' +
                '</button>' +
                '<div class="dropdown-menu">' +
                '<a class="dropdown-item" href="#" id="show_ticket">Öffnen</a>' +
                '<div class="dropdown-divider"></div>' +
                '<a class="dropdown-item" href="#" id="preview_invoice">Vorschau</a>' +
                '<a class="dropdown-item" href="#" id="pdf_print">Drucken</a>' +
                '<a class="dropdown-item disabled" href="#" id="mail_invoice">per Email senden</a>' +
                '<div class="dropdown-divider"></div>' +
                '<a class="dropdown-item" href="#" id="drop_invoice_button">Löschen</a>' +
                '</div>' +
                '',
        attributes: {
          title: 'Stammdaten erfassen'
        }
      },
      btnFilter: {
        html: '<button class="btn filter_table text-white" style="background-color:var(--orange)"><i class="mdi mdi-filter"></i> Filter</button>'
      },
      btnFilterValue: {
        html: '<button class="btn select_date_filter text-white" style="width:80px; background-color:var(--orange)"></button>'
      },
    }
  }
  
  const queryString = window.location.search;
  const urlParams = new URLSearchParams(queryString);
  const year = urlParams.get('year');
  const month = urlParams.get('month');
  let defaultViewDate = "";


  
  $(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);
        }
  
        $('#show_ticket').click(function() {
               
            let ids = $.map($('#table').bootstrapTable('getSelections'), function (row) {
                return row.ID
            })
        
            window.location = "../137a983681de4ff7f08ec43c875cb00161efc24a/a29fa7371a722844e2c138bdcccdcb06b7fa8285/" + ids[0];
        
        })

  
        $('#drop_standing_order_button').click(function() {
        
       
            return;
       
            BootstrapDialog.confirm({
                message: 'Soll der gewählte Dauerauftrag abschließend gelöscht werden?',
                type:BootstrapDialog.TYPE_DANGER,
                title:'gewählte Rechnung löschen',
                btnCancelLabel: 'Abbrechen',
                btnOKLabel:'Löschen',
                callback: function(result) {
                
                    if(!result) return;
                    
                    let postData = {
                        _token:'{{csrf_token()}}',
                        _method: 'DELETE',
                    }
                    
                    let ids = $.map($('#table').bootstrapTable('getSelections'), function (row) {
                        return row.ID
                    })
                    
                        
                    $.post("../../api/{{$repository->getConfig()->_service->url}}/27b96af59ee924f9ec98cf9c44b54e06219636c1/" + ids[0], postData)
                    
                        .done(function() {
                        
                            $('#menu_button').prop('disabled' ,true);
                        
                            $('#table').bootstrapTable('remove', {
                                field: 'ID',
                                values: ids
                            })
                        
                        })
                        
                        .fail(function(xhr) {
                        
                            alertException(xhr);
                        });
                }
            });
        
        });
  
        
        $('#table').on('editable-save.bs.table', function ( $el, field, row, rowIndex, oldValue) {
            // ...
            
            let postData = {
                _token:'{{csrf_token()}}',
                _method: 'PUT',
                data: {}
            }
            
            postData.data[field] = row[field];
            
            return;
            
            $.post("../../api/{{$repository->getConfig()->_service->url}}/27b96af59ee924f9ec98cf9c44b54e06219636c1/" + row.ID, postData)
                            
                .fail(function(xhr) {
                
                    alertException(xhr);
                    
                });
            
            })
            
        $('#table').on('check.bs.table', function(event, row) {
            $('#menu_button').prop("disabled",false);
            if(row.invoice_number) $('#drop_invoice_button').addClass('disabled');
            else $('#drop_invoice_button').removeClass('disabled');
            if(row.invoice_number) $('#preview_invoice').addClass('disabled');
            else $('#preview_invoice').removeClass('disabled');
            if(row.invoice_number) $('#pdf_print').removeClass('disabled');
            else $('#pdf_print').addClass('disabled');
        });
            
        $('#table').on('uncheck.bs.table', function() {
            $('#menu_button').prop("disabled",true);           
        });
            
            
    });
    


</script>
    
  </head>
  <body class="bg-light">
  
 
    <div class="container bg-white border-left border-right" style="border-color:var(--orange)">
        <table id="table"
            data-toggle="table" 
            data-search="true" 
            __data-pagination="true"
            data-buttons="buttons"
            data-sort-name="invoice_number"
            data-sort-order="desc"
            @if(\Browser::isMobile())data-card-view="true"@endif
            data-click-to-select="true"
            data-single-select="true"
            data-buttons-align="left"
            data-buttons-class="danger text-white"
            data-show-button-text="true"
            data-unique-id="ID">
            <thead>
                <tr>
                <th data-field="select" data-checkbox="true"></th>
                <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"><b>RG-Nummer</b></th>
                <th data-field="kunde" data-sortable="true">Kunde</th>
                <th data-field="commission_number" data-sortable="true">Kommision</th>
                <th data-field="assignment" data-sortable="true">Arbeiten</th>
                <th data-align="center" data-field="date" data-sortable="true">RG-Datum</th>
                <!--th data-field="printed" data-width="20"><i class="bi bi-printer-fill"></i></th -->
                <th data-align="center" data-field="mailed" data-width="20">Status</th>
                <th data-align="center" data-width="20">Öffnen</th>
                </tr>
            </thead>
            <tbody>
                @foreach(\App\Models\PHPXPress\HwplusInvoices::with('_meta._doctype')
                    ->whereHas('_meta._doctype', function($query) {
                        $query->where('identifier', 'invoice_pattern');
                    })->get() AS $row)
                <tr>
                    <td></td>
                    <td>{{$row->ID}}</td>
                    <td>{{$row->printed}}</td>
                    <td>{{(new \Carbon\Carbon($row->date))->format('Y')}}</td>
                    <td>{{$row->invoice_number}}</td>
                    <td>{{$row->client}} {{$row->firstname}} {{$row->surname}}</td>
                    <td>{{$row->commission_number}}</td>
                    <td>{{$row->assignment}}</td>
                    <td>{{(new \Carbon\Carbon($row->date))->format('d.m.Y')}}</td>
                    <!--td>@if($row->printed)<i class="bi bi-check-circle-fill"></i>@endif</td -->
                    <td>@if($row->mailed OR $row->printed)<i class="mdi mdi-check-circle text-success"></i>@elseif(!$row->invoice_number)<i class="mdi mdi-circle text-danger"></i>@else<i class="mdi mdi-circle text-warning"></i>@endif</td>
                    <td><a href="../137a983681de4ff7f08ec43c875cb00161efc24a/27b96af59ee924f9ec98cf9c44b54e06219636c1/{{$row->ID}}"><btn class="btn btn-sm text-white" style="background-color:var(--orange)"><i class="bi bi-box-arrow-right"></i></a></td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
  </body>
  <script>

  
  </script>
</html>
