<div class="col card--ui" id="card--dashboard">


    <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">Iscritti agli abbonamenti</h2>
        </div>

    </header>

    <a class="btn--ui lightGrey" href="/subscription_member"><i class="fa-solid fa-arrow-left"></i></a><br>

    @if ($subscription)
        <h3>{{$subscription->name ?? 'Abbonamento Sconosciuto'}}</h3>
    @endif


    <div class="showFilter" style="display:none" wire:ignore.self>
        <hr size="1">
        <div class="row g-3 mt-1">
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Tipologia di tesseramento</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterCards" multiple="multiple">
                            @foreach(getCards() as $card)
                                <option value="{{$card->id}}">{{$card->name}}
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Stato tesseramento</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterStatus" multiple="multiple">
                            <option value="2">Attivo
                            <option value="1">Sospeso
                            <option value="0">Non tesserato
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Certificato medico</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterCertificateScadenza" multiple="multiple">
                            <option value="1">Scaduti
                            <option value="2">In scadenza
                            <option value="3">Non presenti
                            <option value="4">Validi
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Tipologia certificato medico</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterCertificateType" multiple="multiple">
                            <option value="N">Non agonistico
                            <option value="A">Agonistico
                        </select>
                    </div>
                </div>
            </div>

        </div>
        <div class="row g-3 mt-1">
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Età</b>
                    </div>
                    <div class="col-md-6">
                        <div class="row align-items-center">
                            <div class="col-auto"><label class="form-check-label ms-2">Da</label></div>
                            <div class="col"><input class="form-control txt fromYear" type="number" min="0" step="1" name="fromYear"></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="row align-items-center">
                            <div class="col-auto"><label class="form-check-label ms-2">A</label></div>
                            <div class="col"><input class="form-control txt toYear" type="number" min="0" step="1" name="toYear"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Anno di nascita</b>
                    </div>
                    <div class="col-md-6">
                        <div class="row align-items-center">
                            <div class="col-auto"><label class="form-check-label ms-2">Da</label></div>
                            <div class="col"><input class="form-control txt fromFromYear" name="fromYearYear" type="number" min="0" step="1" name="fromFromYear"></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="row align-items-center">
                            <div class="col-auto"><label class="form-check-label ms-2">A</label></div>
                            <div class="col"><input class="form-control txt toToYear" type="number" min="0" step="1" name="toToYear"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="row">
                    <div class="col-md-12" style="margin-bottom:10px;">
                        <b>Periodo</b>
                    </div>
                    <div class="col-12">
                        <select class="form-select filterYear">
                            <option value="">--Seleziona--
                                @foreach($subscription_years as $s)
                                    <option value="{{$s}}">{{$s}}
                                @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row g-3 mt-2">
            <div class="col-md-12" style="text-align:right">
                <button class="btn--ui_outline light" onclick="reset()">RESET</button>
                <button class="btn--ui" onclick="loadDataTable()">FILTRA</button>
            </div>
        </div>
        <hr size="1">
    </div>

    <section id="resume-table">
        <div class="compare--chart_wrapper d-none"></div>

        <div id="filter">{{$filter}}</div>

        <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Cognome</th>
                    <th scope="col">Nome</th>
                    <th scope="col">Certificato</th>
                    <th scope="col">Stato</th>
                    <th scope="col">...</th>
                </tr>
            </thead>
            <tbody id="checkall-target">

            </tbody>
        </table>

    </section>
</div>

@push('scripts')
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>
        .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;
        }
    </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>
        .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;
        }
    </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>
        $(document).ready(function () {
            loadDataTable();
        });

        $('.filterStatus').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
        $('.filterCertificateScadenza').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
        $('.filterCertificateType').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
        $('.filterCards').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
        $('.filterYear').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });

        Livewire.on('load-data-table', () => {
            $('.filterStatus').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
            $('.filterCards').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
            $('.filterCertificateScadenza').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
            $('.filterCertificateType').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
            $('.filterYear').select2({ "language": { "noResults": function () { return "Nessun risultato"; } } });
            loadDataTable();
        });

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

        var filterStatus = localStorage.getItem("filterStatusSubscriptionMember");
        if (filterStatus) {
            $('.filterStatus').val(filterStatus).trigger('change');
        }

        var filterYear = localStorage.getItem("filterYearSubscriptionMember");
        if (filterYear) {
            $('.filterYear').val(filterYear).trigger('change');
        }

        var filterCards = localStorage.getItem("filterCardsSubscriptionMember");
        if (filterCards) {
            $('.filterCards').val(filterCards).trigger('change');
        }

        var filterCertificateScadenza = localStorage.getItem("filterCertificateScadenzaSubscriptionMember");
        if (filterCertificateScadenza) {
            $('.filterCertificateScadenza').val(filterCertificateScadenza).trigger('change');
        }

        var filterCertificateType = localStorage.getItem("filterCertificateTypeSubscriptionMember");
        if (filterCertificateType) {
            $('.filterCertificateType').val(filterCertificateType).trigger('change');
        }

        var fromYear = localStorage.getItem("fromYearSubscriptionMember");
        if (fromYear) {
            $('input[name="fromYear"]').val(fromYear);
        }
        var toYear = localStorage.getItem("toYearSubscriptionMember");
        if (toYear) {
            $('input[name="toYear"]').val(toYear);
        }
        var fromFromYear = localStorage.getItem("fromFromYearSubscriptionMember");
        if (fromFromYear) {
            $('input[name="fromYearYear"]').val(fromFromYear);
        }

        var toToYear = localStorage.getItem("toToYearSubscriptionMember");
        if (toToYear) {
            $('input[name="toToYear"]').val(toToYear);
        }

        function reset() {
            $('.filterStatus').val(null).trigger("change");
            $('.filterYear').val(null).trigger("change");
            $('.filterCards').val(null).trigger("change");
            $('.filterCertificateScadenza').val(null).trigger("change");
            $('.filterCertificateType').val(null).trigger("change");
            $('.fromYear').val('');
            $('.toYear').val('');
            $('.fromFromYear').val('');
            $('.toToYear').val('');
            destroyDataTable();
            loadDataTable();
        }

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

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

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

            var filterStatus = $('.filterStatus').val();
            localStorage.setItem("filterStatusSubscriptionMember", filterStatus);

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

            var filterCards = $('.filterCards').val();
            localStorage.setItem("filterCardsSubscriptionMember", filterCards);

            var filterCertificateScadenza = $('.filterCertificateScadenza').val();
            localStorage.setItem("filterCertificateScadenzaSubscriptionMember", filterCertificateScadenza);
            console.log(filterCertificateScadenza);

            var filterCertificateType = $('.filterCertificateType').val();
            localStorage.setItem("filterCertificateTypeSubscriptionMember", filterCertificateType);

            var fromYear = $('.fromYear').val();
            localStorage.setItem("fromYearSubscriptionMember", fromYear);

            var toYear = $('.toYear').val();
            localStorage.setItem("toYearSubscriptionMember", toYear);

            var fromFromYear = $('.fromFromYear').val();
            localStorage.setItem("fromFromYearSubscriptionMember", fromFromYear);

            var toToYear = $('.toToYear').val();
            localStorage.setItem("toToYearSubscriptionMember", toToYear);

            let date = new Date();
            let date_export = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}_`;


            $('#tablesaw-350').DataTable({
                serverSide: true,
                processing: true,
                stateSave: true,
                ajax: {
                    url: '/get_subscription_members?subscription_id=' + @this.subscription_id + '&filterStatus=' + filterStatus + '&filterCards=' + filterCards + '&filterCertificateScadenza=' + filterCertificateScadenza + '&filterCertificateType=' + filterCertificateType + '&fromYear=' + fromYear + '&toYear=' + toYear + '&fromFromYear=' + fromFromYear + '&toToYear=' + toToYear + '&filterYear=' + filterYear,
                },
                thead: {
                    'th': { 'background-color': 'blue' }
                },
                columns: [
                    { data: 'column_0' },
                    {
                        data: 'column_1',
                        render: function (data, type, row) {
                            var ret = '<a href="/members?member_detail=' + row["column_7"] + '&from=subscription_member">' + data + '</a>';
                            return ret;
                        }
                    },
                    {
                        data: 'column_2',
                        render: function (data, type, row) {
                            var ret = '<a href="/members?member_detail=' + row["column_7"] + '&from=subscription_member">' + data + '</a>';
                            return ret;
                        }
                    },
                    {
                        data: 'column_3',
                        render: function (data, type, row) {
                            var certInfo = data.split("|");
                            var status = certInfo[0];
                            var expireDate = certInfo[1] || "";
                            var html = '<span class="tablesaw-cell-content d-flex align-items-center">';
                            if (status === "0") {
                                html += '<i class="ico--ui check suspended me-2"></i> ';
                                html += 'Scaduto: ' + expireDate;
                            } else if (status === "1") {
                                html += '<i class="ico--ui check due me-2"></i>';
                                html += 'In scadenza: ' + expireDate;
                            } else if (status === "2") {
                                html += '<i class="ico--ui check active me-2"></i>';
                                html += 'Scadenza: ' + expireDate;
                                html += '<span style="float:right"></span>';
                            }else {
                                console.log(status);
                                html += '<i class="ico--ui check absent me-2"></i>';
                                html += 'Non consegnato';
                            }
                            html += '</span>';
                            return html;
                        }
                    },
                    {
                        data: 'column_4',
                        render: function (data, type, row) {
                            var html = '<span class="badge tessera-badge ' + (data == 0 ? 'due' : (data == 1 ? 'active' : 'suspended')) + '">' + (data == 0 ? 'Da pagare' : (data == 1 ? 'Pagato' : 'Sospeso')) + '</span>';
                            return html;
                        }
                    },
                    { 
                        data: 'column_5',
                        render: function (data, type, row) {
                            var html = '<a href="/rates?member_id=' + row["column_7"] + '&member_subscription_id=' + data + '"><b> <i class="fa-solid fa-edit"></i></b></a>';
                            return html;
                        }
                    }],
                layout: {
                    topStart: null,
                    topEnd: null,
                    top1A: {
                        buttons: [
                            {
                                extend: 'excelHtml5',
                                text: '<i class="fa-solid fa-file-excel"></i>',
                                action: newexportaction,
                                title: date_export + 'Iscritti abbonamenti',
                                filename: date_export + 'Iscritti abbonamenti',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'pdfHtml5',
                                text: '<i class="fa-solid fa-file-pdf"></i>',
                                action: newexportaction,
                                title: date_export + 'Iscritti abbonamenti',
                                filename: date_export + 'Iscritti abbonamenti',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            },
                            {
                                extend: 'print',
                                action: newexportaction,
                                text: '<i class="fa-solid fa-print"></i>',
                                title: date_export + 'Iscritti abbonamenti',
                                filename: date_export + 'Iscritti abbonamenti',
                                exportOptions: {
                                    columns: ":not(':last')"
                                }
                            }
                        ],
                    },
                    top1B: {
                        pageLength: {
                            menu: [[10, 25, 50, 100, 100000], [10, 25, 50, 100, "Tutti"]]
                        }
                    },
                    top1C: 'search',
                    bottomEnd: {
                        paging: {
                            boundaryNumbers: false
                        }
                    }
                },
                order: [[0, 'asc'], [1, 'asc']],
                pagingType: 'first_last_numbers',
                language: {
                    "url": "/assets/js/Italian.json",
                    paginate: {
                        first: '<i class="fa-solid fa-angles-left"></i>',
                        last: '<i class="fa-solid fa-angles-right"></i>',
                    }
                },
                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);
                }
            });

            $('#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()
            });
        }
    </script>
@endpush
