<!doctype html>
<?php 

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

?>
<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>Rechnungstexte Vorlagen - {{$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>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/TableDnD/0.9.1/jquery.tablednd.min.js"></script>
    
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://unpkg.com/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>
    <!-- Latest compiled and minified Locales -->
    <script src="https://unpkg.com/bootstrap-table@1.21.1/dist/locale/bootstrap-table-de-DE.min.js"></script>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.21.1/extensions/reorder-rows/bootstrap-table-reorder-rows.min.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.21.1/extensions/reorder-rows/bootstrap-table-reorder-rows.min.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>

    
    
     <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>
   
        .xtext { width:50%;}

        .btn-indigo { background-color:var(--indigo);}
   
   </style>
    
    <script>
   
    
    
  function buttons () {
    return {

    btnReturn: {
            text: 'zurück zur Übersicht',
            icon: 'mdi mdi-home-circle',
            event: function () {
                window.location = "../1faf0aaa6dd4f9bdbde29349c1c829175516baf5";
            },
            attributes: {
            title: 'Zurück zu Startseite erfassen'
            }
      },
      btnAdd: {
        text: 'Neu anlegen',
        icon: 'mdi mdi-plus-circle',
        event: function () {
            BootstrapDialog.show({
                message: $('<div></div>').load('../5fceb377ebdc688d6626c5338cf397fae3ded4b9'),
                title:'Neuer Rechnungstext',
                type:BootstrapDialog.TYPE_LIGHT,
                buttons: [{
                    label: 'Speichern',
                    cssClass:'btn-success',
                    action: function(dialogItself){
                    
                        let postData = $('#new_pattern').serialize();
                    
                        postData._token = '{{csrf_token()}}';
                        
                        $.post("../../api/{{$repository->getConfig()->_service->url}}/84b692d8b9ce096bca28327be5fe9ac581bba7a2/5fceb377ebdc688d6626c5338cf397fae3ded4b9", postData)
                        
                        .done(function(data) {  $('#table').bootstrapTable('append', data); dialogItself.close(); })
                        
                        .fail(function(xhr, status, error) {
                        
                            
                            if(422 == xhr.status) {
                                $('#new_pattern').find('.is-invalid').removeClass('is-invalid');
                                $.each(xhr.responseJSON, function(index, item) {
                                    let array = index.split('.');
                                    $('#new_pattern').find('[name="data['+array[1]+']"]').addClass('is-invalid'); 
                                });
                            
                            
                            }

                            else { 
                            
                                alertException(xhr);
                            }
                        
                        });
                    
                    }
                }, {
                    label: 'Abbrechen',
                    action: function(dialogItself){
                        dialogItself.close();
                    }
                }]
            });
        },
      },
      btnDropDown:{
        text: 'Neu anlegen',
        icon: 'bi bi-file-plus',
        html: '' +
                '<button class="btn btn-indigo text-white dropdown-toggle" type="button" id="menu_button" data-toggle="dropdown" aria-expanded="false" disabled>' +
                '<i class="mdi mdi-cash-clock"></i> gewählter Rechnungstext' +
                '</button>' +
                '<div class="dropdown-menu">' +
                '<a class="dropdown-item" href="#" id="drop_unit_button">Löschen</a>' +
                '</div>' +
                '',
      }
    }
  }
  
  $(function() {
  
 
        $('#drop_unit_button').click(function() { 
        
            BootstrapDialog.confirm('Rechnungstext löschen bestätigen?', function(result){
                if(result) {
                    
                    let postData = {
                        _token:'{{csrf_token()}}',
                        _method: 'DELETE',
                    }
                    
                    let ids = $.map($('#table').bootstrapTable('getSelections'), function (row) {
                        return row.ID
                    })
                    
                          
                    $.post("../../api/{{$repository->getConfig()->_service->url}}/84b692d8b9ce096bca28327be5fe9ac581bba7a2/" + ids[0], postData)
                    
                        .done(function() {
                        
                            $('#menu_button').prop('disabled' ,true);
                        
                            /**$('#table').bootstrapTable('remove', {
                                field: 'ID',
                                values: ids
                            })**/

                            window.location.reload();
                        
                        })
                        
                        .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];
            
            $.post("../../api/{{$repository->getConfig()->_service->url}}/84b692d8b9ce096bca28327be5fe9ac581bba7a2/" + row.ID, postData)
                
                .fail(function(xhr, status, error) {
                          
                    $('#table').bootstrapTable('updateCell', {index: rowIndex, field: field, value: oldValue});
                                
                    alertException(xhr);
                    
                    
        
                });
            
            })
            
            
    
            
        $('#table').on('check.bs.table', function() {
            $('#menu_button').prop("disabled",false);
        });
            
       $('#table').on('uncheck.bs.table', function() {
            $('#menu_button').prop("disabled",true);
        });
        
        
        
        $('#table').on('reorder-row.bs.table', function(evenv, data, row, old) {
                  
            $('#spinner').removeClass('d-none');      
                  
            let postData = {
                _token:'{{csrf_token()}}',
                _method: 'PUT',
                data: {}
            }
                       
            postData.data['order'] = old.order;
            
            $.post("../../api/{{$repository->getConfig()->_service->url}}/84b692d8b9ce096bca28327be5fe9ac581bba7a2/" + row.ID, postData)
            
                .done(function() {
                    //$('#table').bootstrapTable('refresh', {url: "../../api/{{$repository->getConfig()->_service->url}}/be6c4ae56afc682ce85b29e0a3455cd811e92f8d/"});
                    window.location.reload();
                })
                
                .fail(function(xhr, status, error) {
                          
                    //$('#table').bootstrapTable('updateCell', {index: rowIndex, field: field, value: oldValue});
                                
                    alertException(xhr);
                    
        
                });
        
        
        });
        
                 
    });
    
    function toggleDefaultSelected(element, row) {
        
            $('#spinner').removeClass('d-none');      
                  
            let postData = {
                _token:'{{csrf_token()}}',
                _method: 'PUT',
                data: {}
            }
            
            alert($(element).is(':checked'));
                       
            postData.data['flag_default_selected'] = $(element).is(':checked') ? 1 : 0;
            
            $.post("../../api/{{$repository->getConfig()->_service->url}}/84b692d8b9ce096bca28327be5fe9ac581bba7a2/" + row, postData)
            
                .done(function() {
                    window.location.reload();
                })
                
                .fail(function(xhr, status, error) {
                          
                    //$('#table').bootstrapTable('updateCell', {index: rowIndex, field: field, value: oldValue});
                    
                    $('#spinner').removeClass('d-none');      
                                
                    alertException(xhr);
                    
                    
        
                });
        
        
        
        
        }
  
</script>
    
  </head>
  <body class="bg-transparent">

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

        <nav class="navbar navbar-expand-lg navbar-light text-white"  style="background-color:var(--indigo)">
            <a class="navbar-brand text-white" href="#"><i class="mdi mdi-invoice-text"></i> Einstellungen (Rechnungstexte)</a>
        </nav>

        <table id="table"
            data-toggle="table" 
            data-search="true" 
            data-buttons="buttons"
            data-click-to-select="true"
            data-reorderable-rows="true"
            data-use-row-attr-func="true"
            data-single-select="true"
            data-buttons-class="indigo text-white"
            data-show-button-text="true"
            data-buttons-align="left">
            <thead>
                <tr>
                <th data-field="ID" data-visible="false"></th>
                <th data-field="select" data-checkbox="true"></th>
                <th data-field="order" data-visible="true">Sortierung</th>
                <th data-field="descriptor" data-editable="true" data-editable-mode="inline" data-editable-type="text">Bezeichnung</th>
                <th data-field="text" data-editable="true" data-editable-mode="inline" data-editable-type="textarea" data-editable-tpl="<textarea cols=50 rows=10>">Rechnungstext</th>
                <th data-field="flag_default_selected">Standard</th>
                </tr>
            </thead>
            <tbody>
            @foreach(\App\Models\PHPXPress\HwplusInvoiceNotesPatterns::orderBy('order')->get() AS $row)
                <tr>
                    <td>{{$row->ID}}</td>
                    <td></td>
                    <td>{{$row->order}}</td>
                    <td>{{$row->descriptor}}</td>
                    <td>{{$row->text}}</td>
                    <td><input type="checkbox" @if(1 == $row->flag_default_selected) checked @endif onchange="toggleDefaultSelected(this, '{{$row->getKey()}}')"></td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
  </body>
  <script>

  
  </script>
</html>
