<!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>Angebote - {{$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://unpkg.com/bootstrap-table@1.21.1/dist/bootstrap-table.min.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>

    
    <!-- Xeditable -->
    <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>
    
    <!-- Bootstrap Dialogs -->
    <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')}}">
    
    <script>
    
    
  function buttons () {
    return {
       btnHome: {
            text: '<span class="mdi mdi-menu"></span>',
            attributes: {
                title: 'Sie befinden sich hier:',
                disabled:'disabled',
                style:"color:white !important; background-color:var(--primary); opacity:1"
            },
      },
      btnDropDown:{
        text: 'Menü',
        icon: 'mdi mdi-file',
        html: '' +
                '<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="menu_button" disabled>' +
                '<i class="mdi mdi-file-sign"></i> ausgewähltes Angebot' +
                '</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="drop_ticket_button">Löschen</a>' +
                '</div>' +
                '',
        attributes: {
          title: 'Stammdaten erfassen'
        }
      }
    }
  }
  
  $(function() {
  
        $('#show_ticket').click(function() {
        
            let ids = $.map($('#table').bootstrapTable('getSelections'), function (row) {
                return row.ID
            })
        
            window.location = "../d774d83528528c1bb8aa3f8a18d6bd66a9c76e66/afe4f16263308b092d15e04719f202efe1326813/" + ids[0];
        
        })
  
        $('#drop_ticket_button').click(function() { 
        
            BootstrapDialog.confirm('Soll das gewählte Angebot wirklich gelöscht werden?', 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}}/afe4f16263308b092d15e04719f202efe1326813/" + 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];
            
            $.post("../../api/{{$repository->getConfig()->_service->url}}/afe4f16263308b092d15e04719f202efe1326813/" + row.ID, postData)
                
             .fail(function(xhr) {
            
                alertException(xhr);
                
            });
            
        })
            
            
        $('#table').on('check.bs.table', function(evt, row) {
            $('#menu_button').prop("disabled",false);

            if(row.job_number) $('#to_order').removeClass('disabled');
            else $('#to_order').addClass('disabled');
            
            if(row.job_number) $('#to_shipping_order').removeClass('disabled');
            else $('#to_shipping_order').addClass('disabled');
            
            if(row.job_number) $('#drop_ticket_button').addClass('disabled');
            else $('#drop_ticket_button').removeClass('disabled');
            
        });
            
        $('#table').on('uncheck.bs.table', function() {
            $('#menu_button').prop("disabled",true);
        });
            
            
    });
    


</script>
    
  </head>
  <body class="bg-transparent">
  
     <div id="spinner" class="spinner spinner-border d-none" role="status">
        <span class="sr-only">Loading...</span>
    </div>

    <div class="container pt-3 bg-white border-left border-right border-primary">

        <nav class="navbar navbar-expand-lg navbar-light bg-primary text-white">
            <a class="navbar-brand text-white" href="#"><i class="mdi mdi-offer"></i> Angebotswesen</a>
        </nav>


        <table id="table"
            data-toggle="table" 
            data-search="true"
            data-show-search-clear-button="true"
            data-buttons="buttons"
            data-pagination="true"
            data-cookie="true"
            data-cookie-id-table="frame_offers_index"
            data-cookie-storage="localStorage"
            data-page-list="10,25,50,100,all"
            data-click-to-select="true"
            data-single-select="true"
            data-buttons-class="primary text-white"
            data-show-button-text="true"
            data-buttons-align="left"
            data-sortable="true"
            data-sort-name="date_created"
            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="_meta" data-visible="false"></th>
                <th data-field="job_number" data-sortable="true" data-class="font-weight-bold">Nr.</th>
                <th data-field="type" data-sortable="true">Typ</th>
                <th data-field="kunde" data-sortable="true">Kunde</th>
                <th data-field="customer_id" data-sortable="true">Kd.Nr</th>
                <th data-field="commission_number" data-editable="true" data-editable-mode="inline" data-sortable="true">Kommmison</th>
                <th data-field="date_created" data-sortable="true" class="smaller" data-sorter="datesSorter" data-formatter="date_formatter">erstellt am</th>
                <th data-field="date_valid_to" data-sortable="true"  class="smaller" data-sorter="datesSorter" data-formatter="date_formatter">Gültig bis</th>
                <!--th data-field="date_accept_order">angenommen</th-->
                <th data-field="status" data-sortable="true">Status</th>
                <th data-align="right" data-width="20"></th>
                </tr>
            </thead>
            <tbody>
                @foreach(\App\Models\PHPXPress\HwplusTickets::with('_client', '_meta._doctype')
                    ->whereHas('_meta._doctype', function($query) { $query->whereIn('identifier', ['offer','material_offer']);  })
                    #->whereNull('job_number')
                    ->orderBy('job_number', 'DESC')
                    ->get() AS $row)
                <tr>
                    <td></td>
                    <td>{{$row->ID}}</td>
                    <td>{{$row->_meta->getKey()}}</td>
                    <td>{{$row->job_number}} </td>
                    <td>{{$row->short_name}} @if($row->_adjusted_offer)(K)@endif</td>
                    <td>@if($row->_client->descriptor){{$row->_client->descriptor}}@else{{$row->_client->firstname}} {{$row->_client->surname}}@endif</td>
                    <td>{{$row->_client->customer_id}}</td>
                    <td>{{$row->commission_number}}</td>
                    <td>{{$row->date}}</td>
                    <td>{{$row->date_valid_to}}</td>
                    <!-- td>@if($row->date_accept_order) {{(new \Carbon\Carbon($row->date_accept_order))->format('d.m.y')}}  @endif</th -->
                    <td>@if($row->_offer_at_hwplus_tickets->first()) {{$row->_offer_at_hwplus_tickets->first()->descriptor}} @elseif($row->_invoice) {{$row->_invoice->descriptor}} @elseif($row->date_accept_order) angenommen @elseif($row->mailed) per Email gesendet @elseif($row->printed) gedruckt @elseif($row->job_number) abgeschlossen @else in Arbeit @endif</td>
                    <td><a href="../d774d83528528c1bb8aa3f8a18d6bd66a9c76e66/{{$repository->getConfig()->getKey()}}/{{$row->ID}}"><btn class="btn btn-secondary"><i class="bi bi-box-arrow-right"></i></a></td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
  </body>
  <script>

  function datesSorter(a, b) {
        if (new Date(a) < new Date(b)) return 1;
        if (new Date(a) > new Date(b)) return -1;
        return 0;
    }



    let f = new Intl.DateTimeFormat('de', {
        year: "numeric",
        month: "numeric",
        day: "2-digit"
    });

    window.date_formatter = (value, row) => {
        if(!value) return;
        return f.format(new Date(value));
    }

  </script>
</html>
