<link rel="stylesheet" href="includes/js/rating/SimpleStarRating.css">
<script src="includes/js/rating/SimpleStarRating.js"></script>
<script type="text/javascript">

    function switchPage(pos) {
        document.getElementById("pos").value = pos;
        submitForm();
    }

</script> 

<style>
    .table th{
        cursor: pointer;
    }
    .popover{
        max-width: 70%; /* Max Width of the popover (depending on the container!) */
    }

</style>
<form action="index.php?module=reporting&event=ratingsupporter" method="post" name="hm" id="hm" enctype="multipart/form-data" class="form-inline" >

    <input type="hidden" id="sort" name="sort" value="[% $sort %]" />
    <input type="hidden" id="sortorder" name="sortorder" value="[% $sortorder %]" />
    <input type="hidden" id="message" name="message" value="0" />
    <input type="hidden" id="type" name="type" value="" />
    <input type="hidden" id="pos" name="pos" value="" />
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <span class="fcs-card-title h2">[% $user->gettext('Ticketbewertung - nach Supporter') %]</span>
        </div>
        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    [% include file=$modulePath|cat:"/ratingFilter.tpl" %]
                </div>
            </div>


            <div class="card panel-info shadow-sm rounded">
                <div class="card-header">
                    <span class="fcs-card-title h3">
                        [% $entries %]
                        [% if $entries > 1 %] 
                            [% $user->gettext('Supporter') %]
                        [% else %]
                            [% $user->gettext('Supporter') %]
                        [% /if %]
                        [% $user->gettext('gefunden') %]
                    </span>
                </div>

                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr class="fields>">
                                    <th class="sort" id="id">[% $user->gettext('Supporter') %]</th>
                                    <th class="sort" id="ticketnum">[% $user->gettext('Anzahl Bewertung') %]</th>
                                    <th class="sort" id="cnttickets">[% $user->gettext('Anzahl Tickets') %]</th>
                                    <th class="sort" id="rating1">[% $user->gettext('0-1') %] <span class="fa fa-info" title="[% $user->gettext('Von 0 bis einschließlich 1') %]"></span></th>
                                    <th class="sort" id="rating2">[% $user->gettext('1-2') %]</th>
                                    <th class="sort" id="rating3">[% $user->gettext('2-3') %]</th>
                                    <th class="sort" id="rating4">[% $user->gettext('3-4') %]</th>
                                    <th class="sort" id="rating5">[% $user->gettext('4-5') %]</th>
                                    <th class="sort" id="rating">[% $user->gettext('Bewertung') %]</th>
                                </tr>
                            </thead>
                            <tbody>
                                [% foreach from=$rList item=r %]
                                    [% if $r.name %]
                                        <tr>
                                            <td><a href="index.php?module=reporting&event=rating&supporter=[% $r.user_id %]">[% $r.name %]</a></td>
                                            <td>[% if $r.ticketstotal %][% $r.ticketstotal %][% else %]0[% /if %]</td>
                                            <td>[% if $r.cnttickets %][% $r.cnttickets %][% else %]0[% /if %]</td>
                                            <td>[% if $r.rating1 %][% $r.rating1 %][% else %]0[% /if %]</td>
                                            <td>[% if $r.rating2 %][% $r.rating2 %][% else %]0[% /if %]</td>
                                            <td>[% if $r.rating3 %][% $r.rating3 %][% else %]0[% /if %]</td>
                                            <td>[% if $r.rating4 %][% $r.rating4 %][% else %]0[% /if %]</td>
                                            <td>[% if $r.rating5 %][% $r.rating5 %][% else %]0[% /if %]</td>
                                            <td>
                                                <a href="#" data-toggle="popover" id="alert_[% $r.user_id %]"  data-trigger="hover" data-html="true">
                                                    <span class="rating" id="rating_[% $r.user_id %]"  data-stars="5" data-default-rating="[% $r.rating %]" [% if $r.rating &&  $selected_criteria > 0 %]title="[% $r.rating|round:'2' %]"[% elseif !$r.rating %]title="[% $user->gettext('Es gibt noch keine Tickets, welche diesem Supporter zugewiesen sind und bewertet wurden') %]" [% /if %] style="display: inline-block;" disabled></span>
                                                </a>
                                                <script>
                                                    [% if $r.rating && !$selected_criteria %]
                                                    $.post('index.php?module=reporting&event=criteriasupporter&id=[% $r.user_id %]', {startdate: "[% $startdate %]", enddate: "[% $enddate %]", TicketType: "[% $selectedTicketType %]", GroupIDs: $("#GroupIDs").val()}, function (res) {
                                                        $('#alert_[% $r.user_id %]').popover({
                                                            title: '<span class="fa-solid fa-star" style="color:#3af;"></span><b>[% $r.rating|round:"2" %]</b>',
                                                            html: true,
                                                            content: res
                                                        });
                                                    })
                                                    [% /if %]
                                                </script>
                                            </td>
                                        </tr>
                                    <script>
                                        var r = new SimpleStarRating(document.getElementById('rating_[% $r.user_id %]'));
                                    </script>
                                [% /if %]
                            [% foreachelse %]
                                <tr class="even">
                                    <td colspan="9" style="text-align:center !important">[% $user->gettext('Es wurden keine Bewertungen gefunden...') %]</td>
                                </tr>

                            [% /foreach %]
                            </tbody>
                        </table>

                    </div>
                    <div class="col-lg-12 col-sm-12 hidden-sm hidden-xs">
                        <div class="col-lg-6 col-sm-6"></div>
                        <div class="col-lg-6 col-sm-6 text-end">
                            [% $Paging %]
                        </div>
                    </div>
                </div>
            </div>
        </div>



    </div>
</form>



<script>

    $(".datepicker").datepicker();
    var current_sort = $("#sort").val();
    var current_sortorder = $("#sortorder").val();

    showSortIcon();



    $(".sort").on("click", function (e) {
        var sort = $(this).attr('id');
        var sortorder = 'asc';
        if (current_sort === sort) {
            if (current_sortorder === 'asc') {
                sortorder = 'desc';
            } else {
                sortorder = 'asc';
            }
        }
        $("#sort").val(sort);
        $("#sortorder").val(sortorder);
        submitForm();
    })

    function showSortIcon() {
        var content = $("#" + current_sort).html();
        if (current_sortorder == 'desc') {
            content += '<span class="fa fa-chevron-down"></span>';

        } else {
            content += '<span class="fa fa-chevron-up"></span>';
        }
        $("#" + current_sort).html(content);
    }

    function setActive(id) {
        var isChecked = 0;
        var info = "";
        if ($('#checbox_active_' + id).is(":checked")) {
            isChecked = 1
        }
        ;
        if (isChecked == 1) {
            info = "[% $user->gettext('aktiviert')%]";
        } else {
            info = "[% $user->gettext('deaktiviert')%]";
        }
        $.post('index.php?module=field&event=setActive', {id: id, active: isChecked}, function (data) {
            if (data == 1) {
                noty({
                    text: "[% $user->gettext('Feld " +info+ ".') %]",
                    type: 'success',
                    timeout: notifyTimeout
                });
            } else {
                noty({
                    text: "[% $user->gettext('Feld kann momentan nicht aktualisiert werden.') %]",
                    type: 'error',
                    timeout: notifyTimeout
                });
            }

        })
    }

    function changePeriod(element) {

        var value = element.options[element.selectedIndex].value;
        var startdate = document.forms['hm'].elements['startdate'];
        var enddate = document.forms['hm'].elements['enddate'];
        var now = new Date();
        var Tag = now.getDate();
        var Monat = now.getMonth();

        if (value == 1) {
            startdate.value = "[% $today %]";
            enddate.value = startdate.value;
        } else if (value == 2) {
            startdate.value = "[% $yesterday %]";
            enddate.value = startdate.value;
        } else if (value == 3) {
            startdate.value = "[% $lastweek %]";
            enddate.value = "[% $today %]";
        } else if (value == 4) {
            startdate.value = "[% $lastmonth_start %]";
            enddate.value = "[% $lastmonth_end %]";
        }
    [% if $timing_add %]
        [% foreach from=$timing_add item=timing2 %]
        else if (value == [% $timing2.id %]) {

            startdate.value = "[% $timing2.start %]";
            enddate.value = "[% $timing2.end %]";
        }
        [% /foreach %]
    [% /if %]
        //document.hm.submit();
    }

    function changeCriteria() {

    }
</script>
