<div class="col card--ui" id="card--dashboard" style="padding-top: 0px !important;">

    <header id="title--section" style="display:none !important"  class="d-flex align-items-center justify-content-between">
        <div class="title--section_name d-flex align-items-center justify-content-between">
            <i class="ico--ui title_section utenti me-2"></i>
            <h2 class="primary">Pagamento corsi</h2>
        </div>

    </header>


    <div class="dt-container">

        <div class="row" style="margin-top: 10px; margin-bottom:10px;">
            <div class="col-md-4">
                <div class="col-md ">
                    <div class="dt-buttons btn-group flex-wrap">
                        <div class="btn-group">
                            <button class="btn btn-secondary buttons-collection" wire:click="export()" style="color:black !important">ESPORTA</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <!-- <div class="row"> -->
                    <div class="dt-length">
                        <label class="align-items-center d-flex dt-length-0 flex-row gap-2 justify-content-center">
                            Visualizza
                            <!-- <div class="col-md-4"> -->
                                <select name="tablesaw-350_length" aria-controls="tablesaw-350" class="form-select form-select-sm m-0" id="dt-length-0" wire:model="pageLength">
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                    <option value="100000">Tutti</option>
                                </select>
                            <!-- </div> -->
                            <!-- <div class="col-md-3">elementi</div> -->
                            elementi
                        </label>
                    </div>
                <!-- </div> -->
            </div>
            <div class="col-md-4" style="text-align:right">
                <a href="#" class="showHideFilter btn--ui" ><i class="fa-solid fa-sliders"></i></a><br>
            </div>
        </div>
    </div>

    <div class="showFilter" style="display:none; margin-top:10px;"  wire:ignore.self>
        <hr size="1">
        <div class="row g-3">
            <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Anno</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterYear" >
                            <option value="">--Seleziona--
                            @foreach($course_years as $c)
                                @php
                                $selected = '';
                                try
                                {
                                    list($f, $t) = explode("-", $c);
                                    if ($f == date("Y"))
                                        $selected = 'selected';
                                } catch (Exception $e) {

                                }
                                @endphp
                                <option value="{{$c}}" {{$selected}}>{{$c}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Corso</b>
                    </div>
                    <div class="col-12" >
                        <select class="form-select filterCourse" multiple="multiple" wire:model="filterCourse">
                            @foreach($courses as $c)
                                <option value="{{$c}}">{{$c}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Livello</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterLevel"  multiple="multiple" wire:model="filterLevel">
                            @foreach($course_levels as $c)
                                <option value="{{$c->id}}">{{$c->name}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Frequenza</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterFrequency" multiple="multiple" wire:model="filterFrequency">
                            @foreach($course_frequencies as $c)
                                <option value="{{$c->id}}">{{$c->name}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            {{-- <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Tipologia</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterType"  multiple="multiple" wire:model="filterType">
                            @foreach($course_types as $c)
                                <option value="{{$c->id}}">{{$c->name}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div> --}}
            <div class="col-md-2">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Durata</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterDuration"  multiple="multiple" wire:model="filterDuration">
                            @foreach($course_durations as $c)
                                <option value="{{$c->id}}">{{$c->name}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>

        </div>
        <div class="row g-3" style="margin-top:20px;">
            <div class="col-md-12" style="text-align:right">
                <button class="btn--ui_outline light" onclick="reset()">CANCELLA</button>
                <button class="btn--ui" wire:click="search()">FILTRA</button>
            </div>
        </div>
        <hr size="1">
    </div>

    <div class="row text-right">
        <div class="col-md-12" style="text-align:right">
            <button style="display:none; width:200px;" class="btn--ui btNewPayment" onclick="newPayment()">NUOVA ENTRATA</button>
            <button class="btn--ui primary btStopPayment" style="display:none; width:200px; color:white" onclick="suspendPayment()">Sospendi</button>
            <button class="btn--ui primary btResumePayment" style="display:none; width:200px; color:white" onclick="resumePayment()">Riattiva</button>
        </div>
    </div>

    <!-- <section id="resume-table" class="scrollTable" style="margin-top:10px;"> -->
    <section id="resume-table" class="scrollTable course_list-table" style="margin-top:10px;">
        <div class="compare--chart_wrapper d-none"></div>


            <table class="table tablesaw tableHead tablesaw-stack pagamento--corsi display nowrap" id="tablesaw-350" width="100%">
                <thead>
                    <tr>
                        <th scope="col" class="sticky-col-header first-zero"></th>
                        <th scope="col" class="sticky-col-header first-col" >Corso</th>
                        <th scope="col" class="sticky-col-header second-col"><a wire:click="setSort(2)" style="cursor:pointer">Cognome</a></th>
                        <th scope="col" class="sticky-col-header third-col"><a wire:click="setSort(3)" style="cursor:pointer">Nome</a></th>
                        <th scope="col"><a wire:click="setSort(4)" style="cursor:pointer">Iscrizione</a></th>
                        @foreach($months as $idx => $m)
                            <th scope="col"><a wire:click="setSort({{$idx + 5}})" style="cursor:pointer">&nbsp;&nbsp;&nbsp;&nbsp;{{$m}}</a></th>
                        @endforeach
                    </tr>
                </thead>
                <tbody id="checkall-target">

                    @foreach($records as $idx => $record)
                        <tr>
                            <td class="sticky-col-header first-zero">{{$record["column_18"]}}</td>
                            <td class="sticky-col-header first-col">{{$record["column_19"]}}</td>
                            <td class="sticky-col-header second-col">{{$record["column_0"]}}</td>
                            <td class="sticky-col-header third-col">{{$record["column_1"]}}</td>
                            <td>
                                @php
                                list($color, $value) = explode("§", $record["column_2"]);
                                @endphp
                                @if($color == 0)
                                    <a style="width:100%;float:right; text-align:right; display:block;{{$color == 1 ? '' : 'cursor:pointer;'}}" class="{{$color == 1 ? 'green' : 'orange'}} sub_{{$record["column_16"]}}" onclick="setSubscription('{{$record["column_15"]}}', '{{$record["column_17"]}}', '{{$record["column_16"]}}')" style="cursor: pointer;"><small>{{$value}}</small></a>
                                @else
                                    <a style="width:100%;float:right; text-align:right; display:block;" class="{{$color == 1 ? 'green' : 'orange'}}"><small>{{$value}}</small></a>
                                @endif
                            </td>
                            @for($ii=3; $ii<=14; $ii++)
                                @php
                                list($color, $value, $diff) = explode("§", $record["column_" . $ii]);
                                @endphp
                                <td>

                                    @if ($color == 'orange')
                                        <a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="{{$color}} {{$diff == 'X' ? 'half' : ''}} lnk_{{$record["column_16"]}}" id="lnk_{{$record["column_15"]}}_{{$ii - 2}}_{{$record["column_17"]}}" onclick="setPayMonth('{{$record["column_15"]}}', {{$ii - 2}}, '{{$record["column_17"]}}', '{{$record["column_16"]}}')" style="cursor: pointer;"><small>{{$value}}</small></a>
                                    @else
                                        @if ($color != 'wgrey')
                                            @if($color == 'yellow')
                                                <a style="width:100%;float:right; text-align:right; display:block;cursor:pointer;" class="{{$color}} lnk_{{$record["column_16"]}}" id="lnk_{{$record["column_15"]}}_{{$ii - 2}}_{{$record["column_17"]}}" onclick="resumePayMonth('{{$record["column_15"]}}', {{$ii - 2}}, '{{$record["column_17"]}}', '{{$record["column_16"]}}')"><small>{{$value}}</small></a>
                                            @else
                                                <a style="width:100%;float:right; text-align:right; display:block;" class="{{$color}}"><small>{{$value}}</small></a>
                                            @endif
                                        @endif
                                    @endif

                                </td>
                            @endfor

                        </tr>
                    @endforeach

                </tbody>
                <tfoot id="checkall-target">
                    <tr>
                        <th class="sticky-col-header first-zero" style="border-bottom:none;"></th>
                        <th class="sticky-col-header first-col" style="border-bottom:none;"></th>
                        <th class="sticky-col-header second-col" style="border-bottom:none;"></th>
                        <th class="sticky-col-header third-col" style="border-bottom:none;"></th>
                        @foreach($totS as $yyy)
                            @if (isset($yyy))
                                <th style="padding-left:5px !important;padding-right:5px !important;border-bottom:none;">{!!$yyy!!}</th>
                            @else
                                <th style="padding-left:5px !important;padding-right:5px !important;border-bottom:none;">aa</th>
                            @endif
                        @endforeach

                        <!--<th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>
                        <th style="padding-left:5px !important;padding-right:5px !important;"></th>-->
                    </tr>
                </tfoot>
            </table>

            <div class="row">
                <div class="col-md-6">
                    Vista da {{(($currentPage - 1) * $pageLength) + 1}} a {{$currentPage * $pageLength}} di {{$totalRecords}} elementi
                </div>
                <div class="col-md-6" style="text-align:right">
                        <ul class="pagination" style="margin:0px !important;float:right">
                            @if($pages < 10)
                                @for($p=1; $p<=$pages;$p++)
                                    <li class="page-item {{$p == $currentPage ? 'active' : ''}}"><a class="page-link" wire:click="setPage({{$p}})" href="#">{{$p}}</a></li>
                                @endfor
                            @else
                                <li class="page-item {{1 == $currentPage ? 'active' : ''}}"><a class="page-link" wire:click="setPage(1)" href="#">1</a></li>
                                @if($currentPage > 4)
                                    <li class="page-item "><a class="page-link" href="#">...</a></li>
                                @endif
                                @for($p=2; $p<=$pages - 1;$p++)
                                    @if($p > $currentPage - 3 && $p < $currentPage + 3)
                                        <li class="page-item {{$p == $currentPage ? 'active' : ''}}"><a class="page-link" wire:click="setPage({{$p}})" href="#">{{$p}}</a></li>
                                    @endif
                                @endfor
                                @if($currentPage < ($pages - 3))
                                    <li class="page-item "><a class="page-link" href="#">...</a></li>
                                @endif
                                <li class="page-item {{$pages == $currentPage ? 'active' : ''}}"><a class="page-link" wire:click="setPage({{$pages}})" href="#">{{$pages}}</a></li>
                            @endif
                        </ul>

                </div>
            </div>




    </section>

</div>

@push('scripts')

@endpush

@push('scripts')
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>

        @media (min-width: 992px) {
            .sidebar--ui ~ div.col {
                max-height: 100vh;
            }
        }

    .scrollTable {

            padding: 15px;
            max-width: 100% !important;
            overflow-x: auto;
            overflow-y: auto;
            white-space: nowrap;
            border: 1px solid #ddd;
        }

        ::-webkit-scrollbar-thumb {
            background: #e4e4e4;
            border-radius: 10px;
        }

        .select2-container--default .select2-selection--single{
            background-color: #E9F0F5;
            border: 0.0625rem solid #DFE5EB;
            font-size: 0.75rem;
        }
        .select2-selection
        {
            height: 38px !important;
        }
        .select2-selection__rendered
        {
            padding-top:3px;
        }
        .select2 {
            width:100% !important;
        }
        tfoot #checkall-target > tr > th
        {
            padding-right:5px !important;
        }
        /*.container-fluid
        {
            display: table-row !important;
        }
        .row.row.header--gestionale
        {
            padding-left:20px !important;
        }*/


    </style>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
@endpush

@push('scripts')
    <link href="/css/datatables.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>


    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>

        #tablesaw-350_wrapper > div.justify-content-between
        {
            background-color: white;
            position: sticky;
            z-index: 100000;
            padding-top:10px;
            top: -15px;
            left: 0px;

        }


        /*.table-v-scroll > div, #checkall-target
        {
            overflow-x: auto !important;
            overflow-y: hidden !important;
        }*/

        table.tableHead thead {
        /* Important */
            position: sticky;
            z-index: 100;
            top: -17px;
        }

        table thead {
        /* Important */
            position: sticky;
            z-index: 100;
            top: 0;
        }
        .select2-container--default .select2-selection--single{
            background-color: #E9F0F5;
            border: 0.0625rem solid #DFE5EB;
            font-size: 0.75rem;
        }
        .select2-selection
        {
            height: 38px !important;
        }
        .select2-selection__rendered
        {
            padding-top:3px;
        }
        .select2 {
            width:100% !important;
        }
        .select2-selection--multiple{
            overflow: hidden !important;
            height: auto !important;
        }
        .select2-container {
            box-sizing: border-box;
            display: inline-block;
            margin: 0;
            position: relative;
            vertical-align: middle;
        }
        .select2-container .select2-selection--single {
            box-sizing: border-box;
            cursor: pointer;
            display: block;
            height: 38px;
            user-select: none;
            -webkit-user-select: none;
        }
        .select2-container .select2-selection--single .select2-selection__rendered {
            display: block;
            padding-left: 8px;
            padding-right: 20px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .select2-selection__choice__display{
            color:#000000 !important;
        }
        .sticky-col-header {
            position: -webkit-sticky !important;
            position: sticky !important;
            background-color: '#f6f8fa' !important;
        }
        .sticky-col {
            position: -webkit-sticky !important;
            position: sticky !important;
            background-color: white;
        }
        .first-zero {
            width: 20px;
            min-width: 20px;
            max-width: 20px;
            left: 0px;
            z-index:99;
        }
        .first-col {
            width: 100px;
            min-width: 100px;
            max-width: 100px;
            left: 0px;
            z-index:99;
        }

        .second-col {
            width: 150px;
            min-width: 150px;
            max-width: 150px;
            left: 100px;
            z-index:99;
        }
        .third-col {
            width: 150px;
            min-width: 150px;
            max-width: 150px;
            left: 250px;
            z-index:99;
        }
    </style>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script src="/assets/js/datatables.js"></script>
    <script src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.dataTables.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
@endpush

@push('scripts')
    <script>

        var totals = '';

        $(document).ready(function() {
            loadDataTable();
        } );

        $('.filterYear').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterYear').val('{{date("Y")-1}}-{{date("Y")}}').trigger("change");
        $('.filterCourse').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterCourse').on('change', function (e) {
            var data = $('.filterCourse').select2("val");
            if (data == null) data = [];
            @this.set('filterCourse', data);
            //@this.search();
        });

        $('.filterLevel').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterLevel').on('change', function (e) {
            var data = $('.filterLevel').select2("val");
            if (data == null) data = [];
            @this.set('filterLevel', data);
            //@this.search();
        });

        $('.filterFrequency').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterFrequency').on('change', function (e) {
            var data = $('.filterFrequency').select2("val");
            if (data == null) data = [];
            @this.set('filterFrequency', data);
            //@this.search();
        });

        $('.filterType').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterType').on('change', function (e) {
            var data = $('.filterType').select2("val");
            if (data == null) data = [];
            @this.set('filterType', data);
            //@this.search();
        });

        $('.filterDuration').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
        $('.filterDuration').on('change', function (e) {
            var data = $('.filterDuration').select2("val");
            if (data == null) data = [];
            // @this.set('filterDuration', data);
            //@this.search();
        });

        Livewire.on('load-data-table', () => {
            $('.filterYear').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            $('.filterCourse').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            $('.filterLevel').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            $('.filterFrequency').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            $('.filterType').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            $('.filterDuration').select2({"language": {"noResults": function(){return "Nessun risultato";}}});
            loadDataTable();
            setTimeout(() => {
                setMaxWidth();
                setMaxHeight();
            }, 1);
        });

        Livewire.on('destroy-data-table', () => {
            $('#tablesaw-350').DataTable().destroy();
        });

        function destroyDataTable()
        {
            $('#tablesaw-350').DataTable().destroy();
        }

        var isFilter = false;
        $(document).ready(function() {
            isFilter = Number(localStorage.getItem("isFilter_CourseList")) ?? false;
            if (isFilter) {
                $(".showFilter").show();
            } else {
                $(".showFilter").hide();
            }
            $(document).on("click",".showHideFilter",function() {
                if (isFilter)
                {
                    isFilter = false;
                    $(".showFilter").hide();
                    $(".showFilter").removeClass("filter_shown");
                }
                else
                {
                    isFilter = true;
                    $(".showFilter").show();
                    $(".showFilter").addClass("filter_shown");
                }
                localStorage.setItem("isFilter_CourseList", Number(isFilter));
            });
        } );

        function searchAction()
        {
            //destroyDataTable();
            //@this.search();
        }

        var filterYear = localStorage.getItem("filterYearCourseList");
        if (filterYear)
        {
            $('.filterYear').val(filterYear).trigger('change');
        }
        var filterCourse = localStorage.getItem("filterCourseCourseList");
        if (filterCourse)
        {
            $('.filterCourse').val(filterCourse).trigger('change');
        }
        var filterLevel = localStorage.getItem("filterLevelCourseList");
        if (filterLevel)
        {
            $('.filterLevel').val(filterLevel).trigger('change');
        }
        var filterFrequency = localStorage.getItem("filterFrequencyCourseList");
        if (filterFrequency)
        {
            $('.filterFrequency').val(filterFrequency).trigger('change');
        }
        var filterDuration = localStorage.getItem("filterDurationCourseList");
        if (filterDuration)
        {
            $('.filterDuration').val(filterDuration).trigger('change');
        }
        var filterType = localStorage.getItem("filterTypeCourseList");
        if (filterType)
        {
            $('.filterType').val(filterType).trigger('change');
        }

        function reset()
        {
            $('.filterYear').val(null).trigger("change");
            $('.filterCourse').val(null).trigger("change");
            $('.filterLevel').val(null).trigger("change");
            $('.filterFrequency').val(null).trigger("change");
            $('.filterDuration').val(null).trigger("change");
            $('.filterType').val(null).trigger("change");

            destroyDataTable();

            //localStorage.clear();

            loadDataTable();
        }

        function loadDataTable(){

            /*
            if ( $.fn.DataTable.isDataTable('#tablesaw-350') ) {
                $('#tablesaw-350').DataTable().destroy();
            }

            var filterYear = $('.filterYear').val();
            localStorage.setItem("filterYearCourseList", filterYear);

            var filterCourse = $('.filterCourse').val();
            localStorage.setItem("filterCourseCourseList", filterCourse);

            var filterLevel = $('.filterLevel').val();
            localStorage.setItem("filterLevelCourseList", filterLevel);

            var filterDuration = $('.filterDuration').val();
            localStorage.setItem("filterDurationCourseList", filterDuration);

            var filterFrequency = $('.filterFrequency').val();
            localStorage.setItem("filterFrequencyCourseList", filterFrequency);

            var filterType = $('.filterType').val();
            localStorage.setItem("filterTypeCourseList", filterType);
            */
            /*
            let date = new Date();
            let date_export = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}_`;


            var output_table = $('#tablesaw-350').DataTable({
                //scrollX: true,
                //scrollY: true,
                //scrollCollapse: true,
                scrollY: '400px',
                serverSide: true,
                stateSave: true,
                ajax: {
                    url : '/get_course_list?filterCourse=' + filterCourse + '&filterDuration=' + filterDuration + '&filterLevel=' + filterLevel + '&filterType=' + filterType + '&filterFrequency=' + filterFrequency + '&filterYear=' + filterYear,
                    dataSrc: function (json){
                        if(json.totals){
                            totals = json.totals;
                        }
                        return json.data;
                    }
                },
                thead: {
                'th': {'background-color': 'blue'}
                },
                columns: [
                    {
                        data: 'column_18',
                        "orderable": false,
                        className: 'red',
                        render: function (data, type, row, meta){
                            return data;
                        }
                    },
                    {
                        data: 'column_19',
                        className: 'red',
                        className: 'sticky-col  first-col'
                    },
                    {
                        data: 'column_0',
                        className: 'red',
                        className: 'sticky-col second-col',
                        render: function (data, type, row){
                            var ret = '<a href="/members?member_detail=' + row["column_17"] + '&from=course_list">' + data + '</a>';
                            return ret;
                        }
                    },
                    {
                        data: 'column_1',
                        className: 'sticky-col third-col',
                        render: function (data, type, row){
                            const x = data.split("§");
                            var ret = '<a href="/members?member_detail=' + row["column_17"] + '&from=course_list">' + data + '</a>';
                            return ret;
                        }
                    },
                    {
                        data: 'column_2',
                        orderSequence: ['asc', 'desc'],
                        className: 'ciccio',
                        render: function (data, type, row){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 0)
                            {
                                var ret = '<a style="width:100%;float:right; text-align:right; display:block; ' + (x[0] == 1 ? '' : 'cursor:pointer;') + '" class="' + (x[0] == 1 ? 'green' : 'orange') + ' sub_' + row["column_16"] + '" onclick="setSubscription(' + row["column_15"] + ', ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + (x[0] == 1 ? 'green' : 'orange') + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_3',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_1_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 1, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_4',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_2_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 2, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_5',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_3_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 3, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_6',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_4_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 4, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_7',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_5_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 5, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_8',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_6_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 6, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_9',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_7_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 7, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_10',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_8_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 8, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_11',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_9_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 9, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_12',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_10_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 10, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_13',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_11_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 11, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    },
                    {
                        data: 'column_14',
                        orderSequence: ['asc', 'desc'],
                        render: function (data, type, row, meta){
                            const x = data.split("§");
                            var ret = '';
                            if (x[0] == 'orange')
                            {
                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_12_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 12, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                            }
                            else
                            {
                                if (x[0] != 'wgrey')
                                    ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + x[0] + '"><small>' + x[1] + '</small></a>';
                            }
                            return ret;
                        }
                    }
                ],
                layout: {
                    topStart : null,
                    topEnd : null,
                    top1A: {
                        buttons: [
                            {
                                extend: 'collection',
                                text: 'ESPORTA',
                                buttons: [
                                    {
                                        extend: 'excelHtml5',"action":newexportaction,
                                        title: date_export + 'Pagamento corsi',
                                        filename: date_export + 'Pagamento corsi',
                                        orientation: 'landscape',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        },
                                        footer:false,
                                        customize: function(xlsx) {
                                            var sheet = xlsx.xl.worksheets['sheet1.xml'];
                                            const letters = ["E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"];

                                            for(y=0; y<letters.length; y++)
                                            {
                                                z = y + 4;
                                                var c = output_table.column(z).data().toArray();
                                                //console.log(c);
                                                for (var i = 0; i < c.length; i++)
                                                {
                                                    var j = c[i].split("§");
                                                    if (j[0] == '0' || j[0] == 'orange')
                                                    {
                                                        $('row:nth-child('+(i+3)+') [r^="' + letters[y] + '"]', sheet).attr('s', '10');
                                                        //sheet.table.body[i+1][z].fillColor = 'orange';
                                                    }
                                                    if (j[0] == '1' || j[0] == 'green')
                                                    {
                                                        $('row:nth-child('+(i+3)+') [r^="' + letters[y] + '"]', sheet).attr('s', '40');
                                                        //doc.content[1].table.body[i+1][z].fillColor = 'green';
                                                    }
                                                    if (j[0] == 'yellow')
                                                    {
                                                        $('row:nth-child('+(i+3)+') [r^="' + letters[y] + '"]', sheet).attr('s', '20');
                                                        //doc.content[1].table.body[i+1][z].fillColor = 'blue';
                                                    }
                                                }
                                            }
                                        },


                                    },
                                    {
                                        extend: 'pdfHtml5',"action":newexportaction,
                                        title: date_export + 'Pagamento corsi',
                                        filename: date_export + 'Pagamento corsi',
                                        orientation: 'landscape',
                                        exportOptions: {
                                            columns: ":not(':last')"
                                        },
                                        footer:false,
                                        customize: function(doc) {
                                            for(z=4; z<=16; z++)
                                            {
                                                var c = output_table.column(z).data().toArray();
                                                for (var i = 0; i < c.length; i++)
                                                {
                                                    var j = c[i].split("§");
                                                    if (j[0] == '0' || j[0] == 'orange')
                                                    {
                                                        doc.content[1].table.body[i+1][z].fillColor = 'orange';
                                                    }
                                                    if (j[0] == '1' || j[0] == 'green')
                                                    {
                                                        doc.content[1].table.body[i+1][z].fillColor = 'green';
                                                    }
                                                    if (j[0] == 'yellow')
                                                    {
                                                        doc.content[1].table.body[i+1][z].fillColor = 'blue';
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ],
                                dropup: true
                            }
                        ]
                    },
                    top1B : {
                        pageLength: {
                            menu: [[10, 25, 50, 100, 100000], [10, 25, 50, 100, "Tutti"]]
                        }
                    },
                    top1C :'search',
                },
                order: [[2, 'asc'], [3, 'asc']],
                pagingType: 'numbers',
                "language": {
                    "url": "/assets/js/Italian.json"
                },
                "fnInitComplete": function (oSettings, json) {
                    var html = '&nbsp;<a href="#" class="showHideFilter btn--ui"><i class="fa-solid fa-sliders"></i></a>';
                    $(".dt-search").append(html);
                },
                "footerCallback": function (row, data, start, end, display) {
                    let api = this.api();

                    api.column(0).footer().innerHTML = '';
                    api.column(1).footer().innerHTML = '';
                    api.column(2).footer().innerHTML = '';
                    api.column(3).footer().innerHTML = '';

                    for(var i=4; i<=15; i++)
                    {
                        api.column(i).footer().innerHTML = '';
                    }

                    if (totals != '')
                    {

                        totals.split("_").forEach(function (item)
                        {
                            var x = item.split("§");
                            if (x[1] != undefined)
                                api.column(x[0]).footer().innerHTML = x[1];
                            else
                                api.column(x[0]).footer().innerHTML = '';
                        });

                    }

                }
            });

            $('#tablesaw-350 thead tr th').addClass('col');
            $('#tablesaw-350 thead tr th').css("background-color", "#f6f8fa");
            $('#tablesaw-350').on('draw.dt', function() {
                $('[data-bs-toggle="popover"]').popover()
            });
            */
            jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
                return this.flatten().reduce( function ( a, b ) {
                    if ( typeof a === 'string') {
                        start = a.indexOf("€");
                        a = a.substr(start + 2).replace("</small></a>", "").replace(",",".") * 1;
                    }
                    if ( typeof b === 'string' && b.indexOf('green') > 0) {
                        start = b.indexOf("€");
                        b = b.substr(start + 2).replace("</small></a>", "").replace(",",".") * 1;
                    }
                    else
                    {
                        b = 0;
                    }

                    return a + b;
                }, 0 );
                } );

        }

        Livewire.on('setTotals', (tot) =>
        {
            totals = tot;
            console.log(totals);
        });

        var subscription = false;
        var months = [];
        var selectedCourseId = 0;
        var selectedMemberId = 0;
        var selectedMemberCourseId = 0;
        var selectedMonth = 0;

        function setPayMonth(course_id, month, member_id, id)
        {
            if (selectedMemberId > 0 && selectedMemberId != member_id)
            {
                months = [];
                subscription = false;
            }
            if (selectedMemberCourseId != id)
            {
                $(".sub_" + selectedMemberCourseId).addClass("orange").removeClass("blue").removeClass("selected");
                $(".lnk_" + selectedMemberCourseId).addClass("orange").removeClass("blue").removeClass("selected");
            }
            selectedCourseId = course_id;
            selectedMemberId = member_id;
            selectedMemberCourseId = id;
            selectedMonth = month;

            if(!months.includes(month))
            {
                months.push(month);
                $("#lnk_" + course_id + "_" + month + "_" + member_id).addClass("blue").addClass("selected").removeClass("orange");
            }
            else
            {
                months.splice(months.indexOf(months), 1);
                $("#lnk_" + course_id + "_" + month + "_" + member_id).addClass("orange").removeClass("blue").removeClass("selected");
            }
            $(".btResumePayment").hide();
            if (months.length > 0)
            {
                $(".btNewPayment").show();
                $(".btStopPayment").show();
            }
            else
            {
                $(".btNewPayment").hide();
                $(".btStopPayment").hide();
            }
        }

        function resumePayMonth(course_id, month, member_id, id)
        {

            if (selectedMemberId > 0 && selectedMemberId != member_id)
            {
                months = [];
                subscription = false;
            }
            if (selectedMemberCourseId != id)
            {
                $(".sub_" + selectedMemberCourseId).removeClass("orange").addClass("blue").addClass("selected");
                $(".lnk_" + selectedMemberCourseId).removeClass("orange").addClass("blue").addClass("selected");
            }
            selectedCourseId = course_id;
            selectedMemberId = member_id;
            selectedMemberCourseId = id;
            selectedMonth = month;
            console.log(month);
            if(!months.includes(month))
            {
                months.push(month);
                $("#lnk_" + course_id + "_" + month + "_" + member_id).removeClass("blue").removeClass("selected").addClass("selected");
            }
            else
            {
                months.splice(months.indexOf(months), 1);
                $("#lnk_" + course_id + "_" + month + "_" + member_id).removeClass("orange").addClass("blue").removeClass("selected");
            }
            $(".btNewPayment").hide();
            $(".btStopPayment").hide();
            if (months.length > 0)
            {
                $(".btResumePayment").show();

            }
            else
            {
                $(".btResumePayment").hide();
            }
        }

        function setSubscription(course_id, member_id, id)
        {
            if (selectedMemberCourseId != id)
            {
                $(".sub_" + selectedMemberCourseId).addClass("orange").removeClass("blue").removeClass("selected");
                $(".lnk_" + selectedMemberCourseId).addClass("orange").removeClass("blue").removeClass("selected");
                months = [];
                subscription = false;
            }
            selectedCourseId = course_id;
            selectedMemberId = member_id;
            selectedMemberCourseId = id;
            if (subscription)
            {
                $(".sub_" + selectedMemberCourseId).addClass("orange").removeClass("blue").removeClass("selected");
                subscription = false;
                $(".btNewPayment").hide();
            }
            else
            {
                $(".sub_" + selectedMemberCourseId).addClass("blue").addClass("selected").removeClass("orange");
                subscription = true;
                $(".btNewPayment").show();
            }
        }

        function newPayment()
        {
            @this.newPayment(selectedCourseId, months.toString(), selectedMemberId, selectedMemberCourseId, subscription);
        }

        function suspendPayment()
        {
            @this.suspendPayment(selectedCourseId, selectedMonth, selectedMemberId, selectedMemberCourseId, subscription);
        }

        function resumePayment()
        {
            @this.resumePayment(selectedCourseId, selectedMonth, selectedMemberId, selectedMemberCourseId, subscription);
        }


        // $( document ).ready( function(){

        //     setMaxWidth();
        //     setMaxHeight();
        //     $( window ).bind( "resize", setMaxWidth );
        //     $( window ).bind( "resize", setMaxHeight );

        // });

        // function setMaxWidth() {
        //     $("#resume-table").width( Math.round( $(window ).width() - size ) ) ;
        //     //$(".justify-content-between").css({"width": Math.round( $(window ).width() - size) + "px;"}); //.width( Math.round( $(window ).width() - size ) ) ;
        // }
        // function setMaxHeight() {
        //     console.log('height:' + $(window ).height() + 'px !important');
        //     //$("div.row.h-100").attr('style', 'height:' + ($(window ).height() + 50) + 'px !important');
        //     $("#resume-table").height( Math.round( $(window ).height() - 220 ) ) ;
        // }

    </script>
@endpush

