<?php use Fgits\CarbonGermanHolidays\CarbonGermanHolidays; ?>
<?php setlocale(LC_TIME, 'German'); ?>
@php
    $users = [];
    $gesamt = 0;
    $whSumMonth = 0;
    $sumOvertime = 0;
    $staff = \App\Models\PHPXPress\HwplusStaff::get();
    $defaultStaff = \App\Models\PHPXPress\HwplusStaff::first()->getKey();
    
    $selStaff = \App\Models\PHPXPress\HwplusStaff::find(request('staff') ? request('staff') : $defaultStaff);
       
@endphp
<!doctype html>
<html lang="en" class="h-100">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Zeitenauswertung</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <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">
    <!-- JQuery -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.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/bootstrap/bootstrap.dialogs.css')}}">
    <script src="{{url('/js/bootstrap.dialogs.js')}}"></script>
    
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    
    <script src="{{url('/js/x-editable.js')}}"></script>
    
    <script src="{{url('js/hwplus.js?1')}}"></script>
    
    <style type="text/css">
    
   
        td { 
        
            vertical-align:top;
            padding:0;
            margin:0;
        
        }
        
    </style>
    
    <script>
    
    
        $(function() { 
        
        
        addEvent = function(y,m,d) {

        
            dialog = BootstrapDialog.show({
                message: $('<div></div>').load('../9d0ac7ab68df81554dfe7ce08b3fbc335133778f?staff={{$selStaff->getKey()}}&start=' + y + '-' + m + '-' + d),
                title:'Kalendereintrag anlegen',
                buttons: [{
                    label: 'Speichern',
                    action: function(dialogItself) {
                    
                        $('#spinner').removeClass('d-none');
                        
                   
                        let postData = $('#new_event').serialize();
                        
                        $.post("../../api/{{$repository->getConfig()->_service->url}}/b901231a330d256e30a16ceb5474bf2ae4962bc3/" + $('#new_event [name="form"]').val(), postData)
                            
                            .done(function(data) { 

                                $('#spinner').addClass('d-none');
                                dialogItself.close();
                                window.location.reload();
                                
                            })
                            
                            .fail(function(xhr) {
                            
                                $('#spinner').addClass('d-none');
                            
                            
                                if(422 == xhr.status) {
                                
                                    $('#new_event').find('.is-invalid').removeClass('is-invalid');
                                    $.each(xhr.responseJSON, function(index, item) {
                                        let array = index.split('.');
                                        $('#new_event').find('[name="data['+array[1]+']"]').addClass('is-invalid');
                                        alert(item[0]);
                                    });
                                
                                    return;
                                
                                }
                            
                                alertException(xhr);     
                            
                            });
                    
                    }
                }, {
                    label: 'Abbrechen',
                    action: function(dialogItself){
                        dialogItself.close();
                    }
                }]
            });
        
        
        };
        
        editEvent = function(id) {

        
            dialog = BootstrapDialog.show({
                message: $('<div></div>').load('../edit/b901231a330d256e30a16ceb5474bf2ae4962bc3/' + id + '/4a9ec1e275e86546713245103ab298cf197e594f/'),
                title:'Kalendereintrag bearbeiten',
                buttons: [{
                    label: 'Löschen',
                    cssClass: 'btn-danger',
                    action: function(dialogItself) {
                   
                   
                   
                   
                   
                   
                    dialog = BootstrapDialog.confirm('Kalendereintrag löschen bestätigen?', function(result){
                    if(result) {
                    
                        $('#spinner').removeClass('d-none');
                        
                        let postData = {
                            _token:'{{csrf_token()}}',
                            _method: 'DELETE',
                        }
                        
                                                
                        $.post("../../../api/{{$repository->getConfig()->_service->url}}/b901231a330d256e30a16ceb5474bf2ae4962bc3/" + id, postData)
                        
                            .done(function() {
                                                    
                                $('#spinner').addClass('d-none');
                                
                                window.location.reload();
                            
                            })
                            
                            .fail(function(xhr) {
                            
                                $('#spinner').addClass('d-none');
                            
                                alertException(xhr);
                            });
                        

                    }
                });
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                    }
                }, {
                    label: 'Speichern',
                    cssClass: 'btn-success',
                    action: function(dialogItself) {
                    
                        $('#spinner').removeClass('d-none');
                        return;
                   
                        let postData = $('#new_event').serialize();
                        
                        $.post("../../api/{{$repository->getConfig()->_service->url}}/b901231a330d256e30a16ceb5474bf2ae4962bc3/" + $('#new_event [name="form"]').val(), postData)
                            
                            .done(function(data) { 

                                $('#spinner').addClass('d-none');
                                dialogItself.close();
                                window.location.reload();
                                
                            })
                            
                            .fail(function(xhr) {
                            
                                $('#spinner').addClass('d-none');
                            
                            
                                if(422 == xhr.status) {
                                
                                    $('#new_event').find('.is-invalid').removeClass('is-invalid');
                                    $.each(xhr.responseJSON, function(index, item) {
                                        let array = index.split('.');
                                        $('#new_event').find('[name="data['+array[1]+']"]').addClass('is-invalid'); 
                                    });
                                
                                    return;
                                
                                }
                            
                                alertException(xhr);      
                            
                            });
                    
                    }
                }, {
                    label: 'Abbrechen',
                    action: function(dialogItself){
                        dialogItself.close();
                    }
                }]
            });
        
        
        };
        
        })
    </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" style="border-color:var(--rose)">
        
            <nav class="navbar navbar-expand-lg navbar-light text-white" style="background-color:var(--rose)">
                <a class="navbar-brand text-white" href="#"><i class="mdi mdi-timelapse"></i> Stundenauswertung</a>
            </nav>

            <div class="btn-group mt-2 mb-2" role="group">
                <button type="button" class="btn" style="background-color:var(--rose)" onclick="window.location='../854e5746f03aaac88bb0e294185f2f14f168af43'"><i class="bi bi-files"></i></button>
                <button type="button" class="btn" style="background-color:var(--rose)" onclick="addEvent('{{request('year')}}', '{{request('month')}}' , '01')"><i class="bi bi-plus"></i></button>
                <button type="button" class="btn" style="background-color:var(--rose)" onclick="window.location='../6ca633a39a30db63099945dda453f32ed976b44a'"><i class="bi bi-bar-chart"></i></button>
            </div>
        
        
            @php 
                if(!request('month')) request()->merge(['month'=> date('m'), 'year' => date('Y')]); 
            @endphp
            
            Mitarbeiter: <select name="staff" id="staff" onchange="window.location = '../9fc66b7e47269b60348557814c297de4cb2d8d12/afe4f16263308b092d15e04719f202efe1326813?staff=' + $(this).val() + '&month=' + $('#month').val() + '&year=' + $('#year').val()">
                @foreach($staff AS $user)
                <option value="{{$user->getKey()}}" @php if($user->getKey() == request('staff')) echo "selected"; @endphp>{{$user->descriptor}}</option>
                @endforeach
            </select>
        
            Monat: <select name="month" id="month" onchange="window.location = '../9fc66b7e47269b60348557814c297de4cb2d8d12/afe4f16263308b092d15e04719f202efe1326813?staff=' + $('#staff').val() + '&month=' + this.value + '&year=' + $('#year').val()">
                <option value="01" @php if(1 == request('month')) echo "selected"; @endphp>1</option>
                <option value="02" @php if(2 == request('month')) echo "selected"; @endphp>2</option>
                <option value="03" @php if(3 == request('month')) echo "selected"; @endphp>3</option>
                <option value="04" @php if(4 == request('month')) echo "selected"; @endphp>4</option>
                <option value="05" @php if(5 == request('month')) echo "selected"; @endphp>5</option>
                <option value="06" @php if(6 == request('month')) echo "selected"; @endphp>6</option>
                <option value="07" @php if(7 == request('month')) echo "selected"; @endphp>7</option>
                <option value="08" @php if(8 == request('month')) echo "selected"; @endphp>8</option>
                <option value="09" @php if(9 == request('month')) echo "selected"; @endphp>9</option>
                <option value="10" @php if(10 == request('month')) echo "selected"; @endphp>10</option>
                <option value="11" @php if(11 == request('month')) echo "selected"; @endphp>11</option>
                <option value="12" @php if(12 == request('month')) echo "selected"; @endphp>12</option>               
            </select>
            Jahr: <select name="year" id="year">
                <option value="2023">2023</option>
                <option value="2024">2024</option>
                <option value="2025">2025</option>
            </select>
        
       
            

        <table style="border-spacing: 0; padding: 0; border:0;" border=1>
    
       <?php 
            
           
            $events = \App\Models\PHPXPress\HwplusEvents::with('_meta._doctype')->get();
           
            $tickets = \App\Models\PHPXPress\HwplusManhours::
                            with('_manhours_at_tickets._client')
                            ->whereRaw('MONTH(date) = ' . request('month'))
                            ->whereRaw('YEAR(date) = ' . request('year'))
                            ->get();
        
            $d1 = date_create(request('year').'-'.request('month').'-01'); //current month/year
            $d2 = date_create($d1->format('Y-m-t')); //get last date of the month

            $days = date_diff($d1,$d2);

            $d = 1;

        ?>
        @while($d <= $days->days +1 )
        
        @php $sum = 0; @endphp
        
        @php 
            $weekend = \Carbon\Carbon::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT))->isWeekend(); 
        @endphp
        
        
        @php 
            $instance1 = CarbonGermanHolidays::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT));
        @endphp
        
        @php 
            $whday = "wh_" . \Str::lower(\Carbon\Carbon::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT))->format('l'));
            $whSumMonth += $selStaff->$whday;
        @endphp
        
       
        <tr >
            <td style="background-color: @if($instance1->isGermanHoliday(CarbonGermanHolidays::BAYERN)) red @elseif($weekend) orange @elseif($d % 2 != 0) #80CCCC; @else skyblue @endif"> 
                {{\Carbon\Carbon::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT))->formatLocalized('%A'); }}, {{$d}}.{{request('month')}}.{{request('year')}}
                @php echo implode(',' , $instance1->getGermanHolidaysForDay()); @endphp
            </td>
            <td style="width:100%;">
            
                @php $wdEvents = []; @endphp
                @foreach($events->where('_dates_at_hwplus_staff', $selStaff->getKey()) AS $event) 
                    @php    
                            
                            $start = \Carbon\Carbon::createFromDate($event->start_date);
                            $end = \Carbon\Carbon::createFromDate($event->end_date);
                            $date = \Carbon\Carbon::createFromDate(request('year') .'-'. request('month') .'-' . str_pad($d, 2, "0", STR_PAD_LEFT));
                            if($date->between($start,$end)) $wdEvents[] = $event;
                    @endphp
                @endforeach
            
                <table width="100%" style="border-spacing: 0; padding: 0; border:0;" border=0>
                
                    <tr style="background-color:{{$wdEvents ? 'gray' : $selStaff->color}}; color:white;">
                        <td style="width:20%"></td>
                        <td style=" width:60%;">
                            @if($wdEvents)
                            <table style="width:100%; border-spacing: 0; padding: 0; border:0;">
                            
                            @foreach($wdEvents AS $event)
                            
                            @switch($event->_meta->_doctype->identifier)
                            
                                @case('krank_bezahlt')
                                @case('urlaub_bezahlt')
                                    @php $sum += (float) str_replace(',', '.', $selStaff->$whday); @endphp
                                    
                                    <tr  onclick="editEvent('{{$event->getKey()}}')">
                                        <td>
                                            {{$event->descriptor}}
                                        </td>
                                        <td style="text-align:right;">
                                            {{$selStaff->$whday}}
                                        </td>
                                        
                                    </tr>
                                    @break;
                                    
                                @case('krankengeld')
                                @case('urlaub_unbezahlt')
                                    @php $sum = NULL; $whSumMonth = $whSumMonth  - $selStaff->$whday @endphp
                                    <tr  onclick="editEvent('{{$event->getKey()}}')">
                                        <td>
                                            {{$event->descriptor}}
                                        </td>
                                        <td style="text-align:right;">
                                            
                                        </td>
                                        
                                    </tr>
                                @break
                        
                            @endswitch
                            
                            @endforeach
                            </table>
                            @else
                            <table style="width:100%; border-spacing: 0; padding: 0; border:0;">
                            
                            @foreach($tickets->where('date', request('year') .'-'. request('month') .'-' . str_pad($d, 2, "0", STR_PAD_LEFT))->where('_staff', $selStaff->getKey()) AS $ticket)
                                @php $sum += (float) str_replace(',', '.', $ticket->workinghours); @endphp
                                <tr>
                                    <td>
                                        {{$ticket->_manhours_at_tickets->_client->descriptor}}
                                    </td>
                                    <td style="text-align:right;">
                                        {{$ticket->workinghours}}
                                    </td>
                                    
                                </tr>
                            @endforeach
                            
                            </table>
                            @endif
                        </td>
                        <td style="color:black; background-color:white;"></td>
                    </tr>
                    <tr>
                        <td></td>
                        @if(\Carbon\Carbon::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT))->isFuture())
                        <td></td>
                        @elseif($sum)
                        <td style="text-align:right; background-color:white; font-weight:bold">Gesamt: {{$sum}}</td>
                        @elseif(NULL === $sum OR $selStaff->$whday == 0)
                        <td></td>
                        @elseif(0 == $sum)
                        <td style="text-align:right; background-color:white; font-weight:bold"><br><button type="button" class="btn btn-secondary btn-sm" onclick="addEvent('{{request('year')}}', '{{request('month')}}' ,'{{str_pad($d, 2, 0, STR_PAD_LEFT)}}')">{{$sum}} - Korrektur</button><br><br></td>
                        
                        @else
                        <td style="text-align:right; background-color:white; font-weight:bold">Gesamt: {{$sum}}</td>
                        @endif
                        <td style="text-align:right; color:{{$user->color}}; background-color:white; font-weight:bold" > 
                        </td>
                    </tr>
                    
                    

                    <tr>
                        <td></td>
                        @if(\Carbon\Carbon::createFromDate(request('year'), request('month'), str_pad($d, 2, "0", STR_PAD_LEFT))->isFuture())
                        <td></td>
                        @elseif(NULL === $sum)
                        <td style="text-align:right; color:{{$user->color}}; background-color:white; font-weight:bold" >
                        @elseif($sum - $selStaff->$whday > 0)
                        @php $sumOvertime += $sum - $selStaff->$whday; @endphp
                        <td style="text-align:right; color:gray; background-color:white; font-weight:normal">+{{$sum - $selStaff->$whday}}</td>
                        @elseif($sum - $selStaff->$whday < 0)
                        @php $sumOvertime += $sum - $selStaff->$whday; @endphp
                        <td style="text-align:right; color:gray; background-color:white; font-weight:normal">{{$sum - $selStaff->$whday}}</td>
                        @elseif($sum - $selStaff->$whday == 0)
                        <td style="text-align:right; color:gray; background-color:white; font-weight:normal"></td>
                        @endif
                        <td style="text-align:right; color:{{$user->color}}; background-color:white; font-weight:bold" >
                        </td>
                    </tr>
                </table>
            
            </td>
        </tr>
        
        @php $gesamt += $sum; $d++; @endphp
        @endwhile
        
        <tr>
            
            <td style=""></td>
            <td>
                <table width="100%">
                <tr style="">
                        <td style="width:20%"></td>
                        <td style=" width:60%;">
                            <table style="width:100%; border-spacing: 0; padding: 0; border:0;">
                                <tr>
                                    <td>
                                        
                                    </td>
                                    <td style="text-align:right;">
                                        Gesamtstunden: {{$gesamt}}
                                    </td>
                                    
                                </tr>
                            
                                <tr>
                                    <td>
                                        
                                    </td>
                                    <td style="text-align:right;">
                                        Sollstunden: {{$whSumMonth}}
                                    </td>
                                    
                                </tr>
                            </table>
                        </td>
                        <td style="color:black; background-color:white;"></td>
                    </tr>
                </table>
            
            </td>
        </tr>
        
        <tr>
            
            <td colspan="2">Überstunden</td>

        </tr>
        
                <tr>
            
            <td style=""></td>
            <td>
                <table width="100%">
                <tr style="">
                        <td style="width:20%"></td>
                        <td style=" width:60%;">
                            <table style="width:100%; border-spacing: 0; padding: 0; border:0;">
                                <tr>
                                    <td></td>
                                    <td style="text-align:right;">
                                        Bisher: 0
                                    </td>
                                </tr>
                            
                                <tr>
                                    <td></td>
                                    <td style="text-align:right;">
                                        Dieser Monat: {{$sumOvertime}}
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td></td>
                                    <td style="text-align:right;">
                                        Zeitausgleich: 0
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td style="text-align:right;">
                                        Aktuell: 0
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style="color:black; background-color:white;"></td>
                    </tr>
                </table>
            
            </td>
        </tr>

        </table>
        </div>
    </body>
</html>
