
<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=rating" 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 Ticket') %]</span>
        </div>
        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                        <link rel="stylesheet" href="includes/js/bootstrap-select-1.12.4/dist/css/bootstrap-select.min.css">

    <!-- Latest compiled and minified JavaScript -->
                            <script src="includes/js/bootstrap-select-1.12.4/dist/js/bootstrap-select.min.js"></script>
                        
                            <!-- (Optional) Latest compiled and minified JavaScript translation files -->
                            <script src="includes/js/bootstrap-select-1.12.4/dist/js/i18n/defaults-[% if $user->language_type == "de" %]de_DE[% else %]en_US[% /if %].min.js"></script>
                            
                        <div class="fcs-form-padding">
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Eintr&auml;ge pro Seite') %]
                                </label>
                        
                                <select  name="entries_per_page" onchange="document.hm.submit();" class="form-control">
                                    <option value="10" [% if $entries_per_page == 10 %]selected="selected"[% /if %]>10</option>
                                    <option value="20" [% if $entries_per_page == 20 %]selected="selected"[% /if %]>20</option>
                                    <option value="50" [% if $entries_per_page == 50 %]selected="selected"[% /if %]>50</option>
                                    <option value="100" [% if $entries_per_page == 100 %]selected="selected"[% /if %]>100</option>
                                </select>
                        
                            </div>
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Kategorie') %]
                                </label>
                        
                                <select  name="cat" onchange="document.hm.submit();" class="form-control">
                                    <option value="-1" [% if !selected_group %]selected="selected"[% /if %]>[% $user->gettext('Alle')%]</option>
                                    <option value="0" [% if $without_cat  %]selected="selected"[% /if %]>[% $user->gettext('Ohne Kategorie')%]</option>
                                    [% foreach from=$cats item=$cat %]
                                    <option value="[% $cat.id %]" [% if $selected_cat == $cat.id  %]selected="selected"[% /if %]>[% $cat.name %]</option>
                                    [% /foreach %]
                                </select>
                        
                            </div>
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Gruppe') %]
                                </label>
                        
                                <select  name="group" onchange="document.hm.submit();" class="form-control">
                                    <option value="0" [% if !selected_group %]selected="selected"[% /if %]>[% $user->gettext('Alle')%]</option>
                                    [% foreach from=$groups item=$group %]
                                    <option value="[% $group.group_id %]" [% if $selected_group == $group.group_id  %]selected="selected"[% /if %]>[% $group.name %]</option>
                                    [% /foreach %]
                                </select>
                        
                            </div>
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Supporter') %]
                                </label>
                                <select  name="supporter" onchange="document.hm.submit();" class="form-control">
                                    <option value="0" [% if !selected_supporter %]selected="selected"[% /if %]>[% $user->gettext('Alle')%]</option>
                                    [% foreach from=$supporters item=$supporter %]
                                    <option value="[% $supporter.userid %]" [% if $selected_supporter == $supporter.userid  %]selected="selected"[% /if %]>[% $supporter.name %]</option>
                                    [% /foreach %]
                                </select>
                        
                            </div>
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Firma') %]
                                </label>
                        
                                 <select  name="company" onchange="document.hm.submit();" class="form-control">
                                    <option value="0" [% if !selected_company %]selected="selected"[% /if %]>[% $user->gettext('Alle')%]</option>
                                    [% foreach from=$companies item=$company %]
                                    <option value="[% $company.companyid %]" [% if $selected_company == $company.companyid  %]selected="selected"[% /if %]>[% $company.company %]</option>
                                    [% /foreach %]
                                </select>
                        
                            </div>
                            <div class="form-group">
                                <label class="control-label">
                                    [% $user->gettext('Kontakt') %]
                                </label>
                        
                                <select  name="contact" onchange="document.hm.submit();" class="form-control">
                                    <option value="0" [% if !selected_contact %]selected="selected"[% /if %]>[% $user->gettext('Alle')%]</option>
                                    [% foreach from=$contacts item=$contact %]
                                    <option value="[% $contact.contact_id %]" [% if $selected_contact == $contact.contact_id  %]selected="selected"[% /if %]>[% $contact.name %]</option>
                                    [% /foreach %]
                                </select>
                        
                            </div>
                        
                            <div class="form-group">
                                <label class="control-label">[% $user->gettext('Zeitraum')%]</label>
                        
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class = "input-group-text">[% $user->gettext('Von')%] </span>
                                        <input type="text" value="[% if $startdate %][% $startdate|date_format:"%d.%m.%Y" %][% /if %]" autocomplete="off"  name="startdate" class="form-control datepicker" onkeyup="keyEvent(event)" />
                                        <span class = "input-group-text">[% $user->gettext('Bis')%] </span>
                                        <input type="text" value="[% if $enddate %][% $enddate|date_format:"%d.%m.%Y" %][% /if %]" autocomplete="off"  name="enddate" class="form-control datepicker" onkeyup="keyEvent(event)" />
                                    </div>
                                </div>
                                <select class="form-control" name="period" onchange="changePeriod(this);">
                                    <option value="0" [% if !$period %]selected="selected" [% /if %]>[% $user->gettext('Zeitraum') %]</option>
                                    <option value="1" [% if $period == 1 %]selected="selected" [% /if %]>[% $user->gettext('Heute') %]</option>
                                    <option value="2" [% if $period == 2 %]selected="selected" [% /if %]>[% $user->gettext('Gestern') %]</option>
                                    <option value="3" [% if $period == 3 %]selected="selected" [% /if %]>[% $user->gettext('Letzte Woche') %]</option>
                                    <option value="4" [% if $period == 4 %]selected="selected" [% /if %]>[% $user->gettext('Letzten Monat') %]</option>
                                    <option value="5" [% if $period == 5 %]selected="selected" [% /if %]>[% $user->gettext('Letztes Jahr') %]</option>
                                    <option value="6" [% if $period == 6 %]selected="selected" [% /if %]>[% $user->gettext('Aktuelles Jahr') %]</option>
                                    [% if $timing_add %]
                                        [% foreach from=$timing_add key=k item=v %]
                                            <option value="[% $v.id %]">[% $k %]</option>
                                        [% /foreach %]
                                    [% /if %]                         
                                </select>
                            </div>
                        
                            [% if $Tools->FCS_Count($Groups) > 1 %]    
                                <div class="form-group">
                                    <select name="GroupIDs[]" id="GroupIDs" title="[% $user->gettext('Tickets nach Gruppen filtern') %]" class="form-control selectpicker" data-actions-box="true" data-live-search="true" data-select-all-text="[% $user->gettext('Alle') %]" data-deselect-all-text="[% $user->gettext('Keine') %]" multiple>
                                        [% html_options options=$Groups selected=$selectedGroups %]
                                    </select>
                                </div>      
                            [% /if %]
                            [% if $Tools->FCS_Count($TicketTypes) > 1 %]    
                                <div class="form-group">
                                    <label class="control-label">
                                    [% $user->gettext('Tickettyp') %]
                                    </label>
                                    <select name="TicketType" id="TicketType" class="form-control" onchange="document.hm.submit();">
                                        [% html_options options=$TicketTypes selected=$selectedTicketType %]
                                    </select>
                                </div>            
                            [% /if %]
                            [% if !ratingcriteria %]
                            <div class="form-group">
                        
                                <select class="form-control" name="criteria" onchange="document.hm.submit();">
                                    <option value="0">[% $user->gettext('Alle Kriterien') %]</option>
                                    [% if $criteria_list %]
                                        [% foreach from=$criteria_list item=criteria %]
                                            <option value="[% $criteria.criteria_id %]" [% if $criteria.criteria_id == $selected_criteria %]selected="selected"[% /if %]>[% $criteria.criteria_name %]</option>
                                        [% /foreach %]
                                    [% /if %]                         
                                </select>
                            </div>
                            [% /if %]
                        
                            <div class="form-group">
                                <div class="btn-group">
                                <button class="btn btn-outline-primary" onclick=""><span class="fa fa-search"></span></button>
                                <a href="#" onclick="
                                            var input = $('<input>').attr('type', 'hidden').attr('name', 'action').attr('id', 'action').val('xls');
                                            $('#hm').append(input);                
                                            $('#hm').submit();
                                            $('#action').remove();
                                            return false;" class="btn btn-outline-primary"><span>EXPORT</span></a>
                                    [%*
                                    
                                    <a href="#" id="PrintButton" data-fancybox-type="iframe" class="dialog btn btn-outline-primary"><span class="fa fa-print"></span> [% $user->gettext('DRUCKEN') %]</a>                                
                                    *%]    
                                </div>
                            </div>
                        
                            
                        
                        </div>
        <script>
            
$('.selectpicker').selectpicker();            
$('.selectpicker').on('hide.bs.select', function (e, clickedIndex, isSelected, previousValue) {
    document.hm.submit();
})            
        </script>
                </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('Ticket') %]
                        [% else %]
                            [% $user->gettext('Ticket') %]
                        [% /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('Ticket') %]</th>
                                    <th class="sort" id="rating">[% $user->gettext('Bewertung') %]</th>
                                    <th class="" id="comment">[% $user->gettext('Kommentar') %]</th>
                                    <th class="sort" id="date">[% $user->gettext('Datum') %]</th>
                                </tr>
                            </thead>
                            <tbody>
                                [% foreach from=$rList item=r %]
                                    <tr>
                                        <td><a href="index.php?module=ticket&event=show&id=[% $r.ticket_id %]">(#[% $r.ticket_id %]) [% $r.ticketdescr %]</a></td>
                                        <td>
                                            <a href="#" id="alert_[% $r.ticket_id %]" data-toggle="popover" data-trigger="hover" data-html="true" >
                                                <span class="rating" id="rating_[% $r.ticket_id %]" [% if $r.rating && $selected_criteria > 0 %]title="[% $r.rating|round:'2' %]"[% /if %] data-stars="5" data-default-rating="[% $r.rating %]" style="display: inline-block;" disabled></span>
                                            </a>
                                            [% if $r.rating && !$selected_criteria %]
                                                <script>
                                                    $.get('index.php?module=ticket&event=showRating&id=[% $r.ticket_id %]#content', function (res) {
                                                        console.log("[% $r.ticket_id %]" + " was called");
                                                        $('#alert_[% $r.ticket_id %]').popover({
                                                            title: '<span class="fa-solid fa-stars" style="color:#3af;"></span><b>[% $r.rating|round:"2" %]</b>',
                                                            html: true,
                                                            content: res
                                                        });
                                                    })
                                                </script>
                                            [% /if %]
                                        </td>
                                        <td style="max-width: 250px;">
                                            [% $r.ratingcomment %]
                                        </td>
                                        <td>[% if $r.ratingdate %][% $r.ratingdate|date_format:"%d.%m.%Y %H:%M" %][% /if %]</td>
                                    </tr>
                                <script>
                                    var r = new SimpleStarRating(document.getElementById('rating_[% $r.ticket_id %]'));
                                </script>
                            [% foreachelse %]
                                <tr class="even">
                                    <td colspan="8" 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 %]";
        } else if (value == 5) {
            startdate.value = "[% $lastyear_start %]";
            enddate.value = "[% $lastyear_end %]";
        } else if (value == 6) {
            startdate.value = "[% $currentyear_start %]";
            enddate.value = "[% $currentyear_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();
    }
</script>
