@extends('layout.app')

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<meta name="csrf-token" content="{{ csrf_token() }}">

@section('left-menu')
    @parent
    <ul>
        <a href="/strazacy"><li>Strażacy<img src="/img/left_menu_icon/more.png"></li></a>
        <a href="/szkolenia"><li><font size="-2">Badania/Szkolenia</font><img src="/img/left_menu_icon/more.png"></li></a>
    </ul>
@stop


@section('center-area')
    @parent

    @if( auth()->check())
        @if( auth()->user()->fireStationID == NULL)
            Jednostka nie istnieje
        @else
            <center><h1>{{$training->trainingName}}</h1></center>
            <table id="editableTable" class='table'>

                <thead>
                    <th>Imię i Nazwisko</th>
                    <th>Ważne od:</th>
                    <th>Ważne do:</th>
                    <th>Akcje</th>
                </thead>

                <tbody>
                @foreach($fireFighters as $fireFighter)
                    <tr id="{{$fireFighter->userID}}">
                        <td>{{$fireFighter->name}} {{$fireFighter->surname}}</td>
                        <td>{{$fireFighter->dateOfComplete}}</td>
                        <td>{{$fireFighter->dateOfExpiry}}</td>
                        <td>
                            <button class="btn btn-warning" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="cancelButton('{{$fireFighter->userID}}', '{{$fireFighter->dateOfComplete}}', '{{$fireFighter->dateOfExpiry}}')">Anuluj</button>
                            <button class="btn btn-success" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="updateButton('{{$fireFighter->userID}}', '{{$fireFighter->dateOfComplete}}', '{{$fireFighter->dateOfExpiry}}')">Zapisz</button>
                            <button class="btn btn-secondary" type="submit" id="{{$fireFighter->userID}}" onclick="editButton('{{$fireFighter->userID}}')">Edytuj</button>
                            <button class="btn btn-danger btn-delete" type="submit" id="{{$fireFighter->userID}}" onclick="deleteButton('{{$fireFighter->userID}}')">Usuń</button></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {{ $fireFighters->links() }}

        @endif
    @else
        Brak autoryzacji
    @endif

@stop

<script type="text/javascript">

    function editButton(firefighterID){
        $('.btn-secondary', '#'+firefighterID).css('display', 'none');
        $('.btn-warning', '#'+firefighterID).css('display', 'inline');
        $('.btn-success', '#'+firefighterID).css('display', 'inline');
        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(1)").html('<input type="date" name="dateOfComplete">');
        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(2)").html('<input type="date" name="dateOfExpiry">');
    }


    function cancelButton(firefighterID, dateOfComplete, dateOfExpiry){
        $('.btn-secondary', '#'+firefighterID).css('display', 'inline');
        $('.btn-warning', '#'+firefighterID).css('display', 'none');
        $('.btn-success', '#'+firefighterID).css('display', 'none');

        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(1)").html(dateOfComplete);
        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(2)").html(dateOfExpiry);
    }

    function updateButton(firefighterID, dateOfComplete, dateOfExpiry){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });


        $.ajax({
            type: 'post',
            url: '/szkolenia/addTrainingsFireFighters',
            data: {
                'firefighterID': firefighterID,
                'trainingID': '{{$training->id}}',
                'dateOfComplete': $('input[name=dateOfComplete]').val(),
                'dateOfExpiry': $('input[name=dateOfExpiry]').val()
            },
            success: function(data) {
                    $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(1)").html($('input[name=dateOfComplete]').val());
                    $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(2)").html($('input[name=dateOfExpiry]').val());

                    $('.btn-secondary', '#'+firefighterID).css('display', 'inline');
                    $('.btn-warning', '#'+firefighterID).css('display', 'none');
                    $('.btn-success', '#'+firefighterID).css('display', 'none');
                    console.log('sukces')
            },
            error: function(jqXhr, json, errorThrown){// this are default for ajax errors
                var errors = jqXhr.responseJSON;
                var errorsHtml = '';
                $.each(errors['errors'], function (index, value) {
                    errorsHtml += '<ul class="list-group"><li class="list-group-item alert alert-danger">' + value + '</li></ul>';
                });
                //I use SweetAlert2 for this
                swal.fire({
                    title: "Error " + jqXhr.status + ': ' + errorThrown,// this will output "Error 422: Unprocessable Entity"
                    html: errorsHtml,
                    width: 'auto',
                    confirmButtonText: 'Spróbuj ponownie',
                    cancelButtonText: 'Anuluj Edycję',
                    showCancelButton: true,
                }).then((result) => {
                    if(result.value){
                        console.log('spróbuj')
                    } else if(result.dismiss === Swal.DismissReason.cancel){
                       console.log('anuluj');
                       cancelButton(firefighterID, dateOfComplete, dateOfExpiry);
                    }
                })
            },

        });


    }


    function deleteButton(firefighterID){
        swal.fire({
            title: "Czy chcesz wyczyścić?",
            width: 'auto',
            confirmButtonText: 'Tak',
            cancelButtonText: 'Nie',
            showCancelButton: true,
        }).then((result) => {
            if(result.value){

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    type: 'post',
                    url: '/szkolenia/addTrainingsFireFighters/delete',
                    data: {
                        'firefighterID': firefighterID,
                        'trainingID': '{{$training->id}}',
                    },
                    success: function(data) {
                        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(1)").html('');
                        $('.btn-secondary', '#'+firefighterID).parents("tr").find("td:eq(2)").html('');
                        $('.btn-secondary', '#'+firefighterID).css('display', 'inline');
                        $('.btn-warning', '#'+firefighterID).css('display', 'none');
                        $('.btn-success', '#'+firefighterID).css('display', 'none');


                    }
                });
            }
        })

    }
</script>
