@extends('template')

@section('page-title')
    Checklist reporting.
@endsection

@section('tag-title')  @endsection

@section('page-sub-title')

@endsection

@section('main-content')

    <style>
        table.dataTable tbody td{ 
            padding: 3px 10px
        }
    </style>

    <div class="row">
        <div class="col-md-12  col-xl-12 col-lg-12 col-xm-6">
             <a href="{{url('checklist-job/details/'.$questionSetJob->id)}}" class="btn btn-primary action-btn  pull-right text-white float-end ">
                    Show details
             </a>
            <a target="_blank" href="{{\Illuminate\Support\Facades\Request::fullUrl().'?excel=1'}}" class="btn btn-info action-btn  float-end  pull-right text-white modal-effect mr-3" data-effect="effect-slide-in-right"> 
                <i class="fa fa-download pr-2">
                </i> Export to excel
            </a>
        </div>
        
<div class="row row-sm">
    <div class="col-md-12 col-xl-12 col-lg-12 col-xm-6">
        <section>
            <div class="card">
                <div class="card-header tx-medium bd-0">
                    <h5 class="mb-0">
                        Details of checklists <em class="text-primary">{{ $questionSetJob->question_set->title }}</em> 
                        for <em class="text-warning">{{ $questionSetJob->location->name }}</em>
                    </h5>
                </div>

                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="thead-dark">
                                <tr>
                                    <th>Category</th>
                                    <th>Questions</th>
                                    <th>Comments & Details</th>
                                    <th>Response</th>
                                    <th>Score</th>
                                    <th>Possible Score</th>
                                    <th>Corrective Action Plan</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($questionData as $index => $section)
                                    @foreach($section['categories'] as $category)
                                        @php
                                            $totalQuestion = count($category['questions']);
                                            $count_no = 0;

                                            foreach($category['questions'] as $question){
                                                if($question['answer'] == "No"){
                                                    $count_no++;
                                                }
                                            }
                                        @endphp

                                        @if($count_no != 0)
                                            <!--tr class="table-secondary text-primary">
                                                <th scope="row">{{ $category['category_name'] }} [{{ $section['section_name'] }}]</th>
                                                <td colspan="6"></td>
                                            </tr-->

                                            @foreach($category['questions'] as $question)
                                                @if($question['answer'] == "No")
                                                    <tr>
                                                        <td>{{ $category['category_name'] }}</td>
                                                        <td><em class="text-warning">{{ $question['content'] }}</em></td>
                                                        <td>{{ $question['answer_comment'] }}</td>
                                                        <td>{{ $question['answer'] }}</td>
                                                        <td>{{ $question['answer_score'] }}</td>
                                                        <td>{{ $question['possible_score'] }}</td>
                                                        <td>{{ $question['answer_corrective_action'] }}</td>
                                                    </tr>
                                                @endif
                                            @endforeach
                                        @endif
                                    @endforeach
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
                
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="thead-dark">
                                <tr>
                                    @if($rating = $questionSetJob->getDecision())
                                            <th class="item-title"><i class="fa fa-info-circle"></i> Overall rating</th>
                                    @endif
                                    @foreach($ratingFamilies as $ratingFamily)
                                        <?php
                                        $subRatings = $ratings->where('family_name', $ratingFamily)->values();
                                        $subRating = $subRatings->first();
                                        ?>
                                        <th><i class="fa fa-info-circle"></i> {{$ratingFamily}}</th>
                                    @endforeach
                                     @foreach($scorings as $scoring)
                                        <div class="item">  <div class="item">
                                            <th><i class="fa fa-info-circle"></i> Scoring
                                        </div>
                                    @endforeach
                                    <th> Duration </th>
                                 </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    @if($rating = $questionSetJob->getDecision())
                                            @if($rating)<td> <span class="rating-badge" style="background-color: {{$rating->color}}">{{$rating->label}}</span></td>@endif
                                    @endif
                                    @foreach($ratingFamilies as $ratingFamily)
                                        <?php
                                        $subRatings = $ratings->where('family_name', $ratingFamily)->values();
                                        $subRating = $subRatings->first();
                                        ?>
                                        <td><span class="rating-badge" style="background-color: {{$subRating->color}}">{{$subRating->label}}</span>
                                            @foreach($subRatings as $r)
                                                <div class="mr-4 badge badge-primary-transparent">{{$r->level_name}} <span class="badge bg-primary text-white ml-2">{{$r->nbAnswers}}</span></div>
                                            @endforeach
                                        </td>
                                    @endforeach
                                     @foreach($scorings as $scoring)
                                
                                            <td>
                                                 <span class="rating-badge" style="background-color: {{$scoring->color}}">{{$scoring->label}}</span>
                                                <div class="mt-1 top-info">{{$questionSetJob->getScorePercentText()}}</div>
                                            </td>
                                        
                                    @endforeach
                                     <td>
                                        <div class="mt-1 top-info">{{$questionSetJob->getDuration()}}</div>
                                      </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="card-body">
                        <h5 class="mb-0">
                            Last Audit <em class="text-primary">{{ $questionSetJobLast->question_set->title }}</em> 
                            for <em class="text-warning">{{ $questionSetJobLast->location->name }}</em>
                        </h5>
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="thead-dark">
                                <tr>
                                    @if($ratingLast = $questionSetJobLast->getDecision())
                                            <th class="item-title"><i class="fa fa-info-circle"></i> Overall rating</th>
                                    @endif
                                    @foreach($ratingFamiliesLast as $ratingFamilyLast)
                                        <?php
                                        //$subRatingsLast = $ratingFamilyLast->where('family_name', $ratingFamilyLast)->values();
                                        //$subRatingLast = $subRatingsLast->first();
                                        ?>
                                        <th><i class="fa fa-info-circle"></i> {{$ratingFamilyLast}}</th>
                                    @endforeach
                                    @foreach($scoringsLast as $scoringLast)
                                        <div class="item">  <div class="item">
                                            <th><i class="fa fa-info-circle"></i> $scoringLast </th>
                                        </div>
                                    @endforeach
                                    <th> Duration </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    @if($ratingLast = $questionSetJobLast->getDecision())
                                            @if($ratingLast)<td> <span class="rating-badge" style="background-color: {{$ratingLast->color}}">{{$ratingLast->label}}</span></td>@endif
                                    @endif
                                    @foreach($ratingFamiliesLast as $ratingFamily)
                                        <?php
                                        $subRatingsLast = $ratingsLast->where('family_name', $ratingFamily)->values();
                                        $subRatingLast = $subRatingsLast->first();
                                        ?>
                                        <td><span class="rating-badge" style="background-color: {{$subRatingLast->color}}">{{$subRatingLast->label}}</span>
                                            @foreach($subRatingsLast as $r)
                                                <div class="mr-4 badge badge-primary-transparent">{{$r->level_name}} <span class="badge bg-primary text-white ml-2">{{$r->nbAnswers}}</span></div>
                                            @endforeach
                                        </td>
                                    @endforeach
                                     @foreach($scoringsLast as $scoring)
                                        <td>
                                             <span class="rating-badge" style="background-color: {{$scoring->color}}">{{$scoring->label}}</span>
                                            <div class="mt-1 top-info">{{$questionSetJobLast->getScorePercentText()}}</div>
                                        </td>
                                    @endforeach
                                     <td>
                                        <div class="mt-1 top-info">{{$questionSetJobLast->getDuration()}}</div>
                                      </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>

</div>
@endsection


@section('page-style')
  
@endsection

@section('page-script')
    <script src="{{url('assets/plugins/datatable/js/jquery.dataTables.min.js')}}"></script>
    <script src="{{url('assets/plugins/bootstrap-daterangepicker/daterangepicker.js')}}"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.4/xlsx.core.min.js"></script>
    <script src="{{url('assets/plugins/export-to-excel/jhxlsx.js')}}"></script>
    <script src="{{url('assets/plugins/export-to-excel/FileSaver.js')}}"></script>
    <script src="{{url('assets/plugins/export-to-excel/Blob.js')}}"></script>

    <script !src="">
        $(function(e) {
            $('#period').daterangepicker(
                {alwaysShowCalendars: !0,
                    locale: {
                        format: 'DD/MM/YYYY',
                        "separator": " - ",
                        "applyLabel": "Valider",
                        "cancelLabel": "Annuler",
                        "fromLabel": "De",
                        "toLabel": "A",
                        "customRangeLabel": "Custom",
                        "daysOfWeek": [
                            "Dim",
                            "Lun",
                            "Mar",
                            "Mer",
                            "Jeu",
                            "Ven",
                            "Sam"
                        ],
                        "monthNames": [
                            "Janvier",
                            "Février",
                            "Mars",
                            "Avril",
                            "Mai",
                            "Juin",
                            "Juillet",
                            "Août",
                            "Septembre",
                            "Octobre",
                            "Novembre",
                            "Décembre"
                        ],
                        "firstDay": 1
                    },
                    ranges: {
                        "Hier": [moment().subtract(1, "days"), moment().subtract(1, "days")],
                        "7 derniers jours": [moment().subtract(7, "days"), moment()],
                        "30 derniers jours": [moment().subtract(29, "days"), moment()],
                        "Le mois en cours": [moment().startOf("month"), moment().endOf("month")],
                        "Le mois dernier": [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")],
                        "3 derniers mois": [moment().subtract(3, "month"), moment()],
                        "12 derniers mois": [moment().subtract(12, "month"), moment()],
                    },
                },
                function (t, a, e) {
                }
            );

            function refreshData(){
                 $('#data-table').DataTable({
                    lengthMenu: [[10, 25, 50,100,200], [10, 25, 50, 100, 200]],
                    pageLength: 50,
                    responsive: true,
                    processing : true,
                    serverSide: true,
                    ordering: false,
                    ajax: '{{url(Request::path().'/ajax_list?current_path='.Request::path())}}'+'&period='+$('#period').val()+'&location_id='+$('#location_id').val()+'&status_id='+$('#status_id').val(),
                    //ajax: '{{url(Request::path().'/ajax_list?current_path='.Request::path())}}'+'&period='+$('#period').val()+'&location_id='+$('#location_id').val()+'&status_id='+$('#status_id').val()+'&user_id='+$('#user_id').val(),
                    destroy : true,
                    language: {
                        searchPlaceholder: 'Search...',
                        sSearch: '',
                        lengthMenu: '_MENU_',
                        sProcessing : "Loading..."
                    },
                });
            }

            $(document).on('click','#data-refresher',function (e) {
                e.preventDefault();
                refreshData();
            });

            refreshData();

            $('#checklist-reporting-filter').on('click', function () {

                var $container = $('#checklist-reporting-container');
                $container.html('<p>Loading...</p>');
                var req = $.ajax({
                    url: '{{url(config('app.admin_prefix').'/reporting_checklist_ajax_list')}}',
                    type: "GET",
                    data: {question_set_job_id: $('#question-set-job-id').val() ,is_excel: 0},
                    dataType: "html"
                });
                req.done(function (response, textStatus, jqXHR) {
                        $('#checklist-reporting-container').html(response);
                    });
                    req.fail(function (jqXHR, textStatus) {
                    swal("Erreur!", "Error during operation.", "warning");
                    });
                }
            );
            $('#checklist-reporting-filter').click();


            $('#btn-export-excel').on('click', function () {
                let question_set_job_id = @json($questionSetJob->id);

                var req = $.ajax({
                    url: '{{ url(config("app.admin_prefix")."/reporting_checklist_excel") }}/' + question_set_job_id,
                    type: "GET",
                    data: {
                        question_set_job_id: question_set_job_id,
                        is_excel: 1
                    },
                    dataType: "html"
                });

                /*req.done(function(response) {
                    console.log('Export réussi');
                });

                req.fail(function(jqXHR, textStatus) {
                    console.error("Erreur d'export : " + textStatus);
                });*/
            });
            
        });

    </script>
@endsection
