<script type="text/javascript" language="javascript">
    function switchPage(page) {
        document.getElementById('pos').value = page;
        //submitForm();
        $("#ListForm").submit();
    }
    $(document).ready(function () {


        $("#newsList th").on("click", function (e) {
            var id = this.id;

            if (id == "Checkobx")
                return;

            var sortorder = 'ASC';
            var sort = $("#sort").val();
            if (sort == id) {
                sortorder = $("#sortorder").val();
                if (sortorder == 'ASC') {
                    sortorder = 'DESC';
                } else {
                    sortorder = 'ASC';
                }
            }
            //console.log("id: " + id + " - sortorder: " + sortorder + " - sort: " + sort);
            $("#sort").val(id);
            $("#sortorder").val(sortorder);
            $("#ListForm").submit();
        })
    })

</script>
<style>
    .table th{
        cursor: pointer;
    }
</style>
<form action="" method="post" name="hm" id="ListForm" enctype="multipart/form-data" class="form-inline">

    <input type="hidden" name="pos" id="pos" value="[% $pos %]" />
    <input type="hidden" name="sort" id="sort" value="[% $sort %]" />
    <input type="hidden" name="sortorder" id="sortorder" value="[% $sortorder %]" />


    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <h2 class="card-title-fcs">[% $user->gettext('Nachrichten') %]</h2>
        </div>

        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="form-group row mb-3">
                                <label class="control-label">[% $user->gettext('Eintr&auml;ge pro Seite') %]</label>
                                <select name="EntriesPerPage" onchange="document.hm.submit();" class="form-control text-center">
                                    <option value="10" [% if $EntriesPerPage == 10 %]selected="selected"[% /if %]>10</option>
                                    <option value="20" [% if $EntriesPerPage == 20 %]selected="selected"[% /if %]>20</option>
                                    <option value="50" [% if $EntriesPerPage == 50 %]selected="selected"[% /if %]>50</option>
                                    <option value="100" [% if $EntriesPerPage == 100 %]selected="selected"[% /if %]>100</option>
                                </select>    

                            </div>
                        </div>
                        <div class="col-lg-6 text-end">
                            <div class="btn-group">
                                <input type="button" id="btnNewsDetails" onclick="window.location.href = 'index.php?module=news&event=editNews';" value="[% $user->gettext('Neue Nachricht') %]" class="btn btn-outline-success" />                        
                            </div>
                        </div>
                    </div>
                </div>
            </div>
                                <div class="card panel-info shadow-sm rounded">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-lg-6 text-left" >
                                <h3 class="card-title text-left" style="font-size: 24px; display:inline">[% $user->gettext('Wichtige Nachricht')%]</h3>
                            </div>

                        </div>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped" style="min-height: 80px;">
                                <thead>
                                    <tr>
                                        <th id="Checkobx">
                                            <input type="checkbox" class="selectAllNews" value="0" name="AlleNews">
                                        </th>
                                        <th id='Title'>
                                            [% $user->gettext('Titel') %]
                                            
                                        </th>
                                        <th id="Autor">
                                            [% $user->gettext('Autor') %]
                                        </th>
                                        <th id='Published'>
                                            [% $user->gettext('Sichtbar') %]
                                        </th>
                                        <th id = 'CreateDate'>
                                            [% $user->gettext('Erstellt am') %]
                                        </th>
                                        <th id= 'CreateUser'>
                                            [% $user->gettext('Erstellt von') %]
                                        </th>
                                        <th id= 'UpdateDate'>
                                    [% if $hmcompany|strpos:"AVAG HOLDING" !== false %][% $user->gettext('Zuletzt ge&auml;ndert am') %][% else %][% $user->gettext('Letzte &Auml;nderung am') %][% /if %]
                                </th>
                                <th id = 'UpdateUser' >
                                    [% $user->gettext('Letzte &Auml;nderung von') %]
                                    [% if $sort == "UpdateUser" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th>[% $user->gettext('Aktion') %]</th>
                            </tr>
                        </thead>
                        <tbody class='sortable' id="ImportantNewsList">
                            [% foreach from=$ImportantNewsList item=news %]
                                <tr id="Row_[% $news.NewsID %]">
                                    <td><input type="checkbox" class="selectNews" value="[% $news.NewsID %]" name="News[]"></td>
                                    <td>[% $news.Title %]&nbsp;</td>
                                    <td>[% $news.Autor %]&nbsp;</td>
                                    <td>
                                        [% if $news.Published == 1 %]
                                            <span class="fa fa-check" style="color:green;"></span>
                                        [% else %]
                                            <span class="fa fa-times" style="color:red;"></span>
                                        [% /if %]
                                    </td>
                                    <td>[% $news.CreateDate|date_format:"%d.%m.%Y %H:%M" %]&nbsp;</td>
                                    <td>[% $news.CreateUser %]&nbsp;</td>
                                    <td>[% $news.UpdateDate|date_format:"%d.%m.%Y %H:%M" %]&nbsp;</td>
                                    <td>[% $news.UpdateUser %]&nbsp;</td>
                                    <td nowrap="nowrap">
                                        <div class="btn-group">
                                            <a href="index.php?module=news&event=editNews&id=[% $news.NewsID %]" class="btn btn-outline-secondary"title="[% $user->gettext('Bearbeiten') %]" ><span class="fa fa-pencil"></span></a>
                                            <a href='#' class="btn btn-outline-secondary" onclick="check_remove('index.php?module=news&pos=[% $pos %]&event=deleteNews&id=[% $news.NewsID %]');" title="[% $user->gettext('L&ouml;schen') %]"><span class="fa fa-trash"></span></a>

                                        </div>
                                    </td>
                                </tr>
                            [% /foreach %]

                        </tbody>
                    </table>


                </div>
              
              
               
            </div>
        </div>
            <div style="margin-top:10px;">
                <div class="card panel-info shadow-sm rounded">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-lg-6 text-left" >
                                <h3 class="card-title text-left" style="font-size: 24px; display:inline">[% $user->gettext('Nachrichtenliste')%]</h3> <span style="font-size: 17px;"> - [% $entries %] [% if $entries <= 1 %] [% $user->gettext('Nachricht gefunden') %] [% else %][% $user->gettext('Nachrichten gefunden') %] [% /if %]</span>
                            </div>

                        </div>
                    </div>

                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped" id="newsList">
                                <thead>
                                    <tr>
                                        <th id="Checkobx">
                                            <input type="checkbox" class="selectAllNews" value="0" name="AlleNews">
                                        </th>
                                        <th id='Title'>
                                            [% $user->gettext('Titel') %]
                                            [% if $sort == "Title" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                        </th>
                                        <th id="Autor">
                                            [% $user->gettext('Autor') %]
                                            [% if $sort == "Autor" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span> [% /if %]
                                        </th>
                                        <th id='Published'>
                                            [% $user->gettext('Sichtbar') %]
                                            [% if $sort == "Published" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                        </th>
                                        <th id = 'CreateDate'>
                                            [% $user->gettext('Erstellt am') %]
                                            [% if $sort == "CreateDate" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                        </th>
                                        <th id= 'CreateUser'>
                                            [% $user->gettext('Erstellt von') %]
                                            [% if $sort == "CreateUser" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                        </th>
                                        <th id= 'UpdateDate'>
                                    [% if $hmcompany|strpos:"AVAG HOLDING" !== false %][% $user->gettext('Zuletzt ge&auml;ndert am') %][% else %][% $user->gettext('Letzte &Auml;nderung am') %][% /if %]
                                    [% if $sort == "UpdateDate" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th id = 'UpdateUser' >
                                    [% $user->gettext('Letzte &Auml;nderung von') %]
                                    [% if $sort == "UpdateUser" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th>[% $user->gettext('Aktion') %]</th>
                            </tr>
                        </thead>
                        <tbody class='sortable' id="NewsList">

                            [% if file_exists($modulePath|cat:"/VBRB.tpl") %]
                                [% include file=$modulePath|cat:"/VBRB.tpl" %]           
                            [% /if %]      

                            [% foreach from=$NewsList item=news %]
                                <tr id="Row_[% $news.NewsID %]">
                                    <td><input type="checkbox" class="selectNews" value="[% $news.NewsID %]" name="News[]"></td>
                                    <td>[% $news.Title %]&nbsp;</td>
                                    <td>[% $news.Autor %]&nbsp;</td>
                                    <td>
                                        [% if $news.Published == 1 %]
                                            <span class="fa fa-check" style="color:green;"></span>
                                        [% else %]
                                            <span class="fa fa-times" style="color:red;"></span>
                                        [% /if %]
                                    </td>
                                    <td>[% $news.CreateDate|date_format:"%d.%m.%Y %H:%M" %]&nbsp;</td>
                                    <td>[% $news.CreateUser %]&nbsp;</td>
                                    <td>[% $news.UpdateDate|date_format:"%d.%m.%Y %H:%M" %]&nbsp;</td>
                                    <td>[% $news.UpdateUser %]&nbsp;</td>
                                    <td nowrap="nowrap">
                                        <div class="btn-group">
                                            <a href="index.php?module=news&event=editNews&id=[% $news.NewsID %]" class="btn btn-outline-secondary"title="[% $user->gettext('Bearbeiten') %]" ><span class="fa fa-pencil"></span></a>
                                            <a href='#' class="btn btn-outline-secondary" onclick="check_remove('index.php?module=news&pos=[% $pos %]&event=deleteNews&id=[% $news.NewsID %]');" title="[% $user->gettext('L&ouml;schen') %]"><span class="fa fa-trash"></span></a>

                                        </div>
                                    </td>
                                </tr>
                            [% foreachelse %]
                                <tr class="even">
                                    <td colspan="8">[% $user->gettext('Keine News gefunden') %]</td>
                                </tr>
                            [% /foreach %]

                        </tbody>
                    </table>


                </div>
              
                <div class="row">
               
                <div class="col-lg-2 col-sm-2"  data-toggle="tooltip" title="[% $user->gettext('Markieren Sie News um diese gesammelt zu bearbeiten') %]">
                <a style="margin-top: 0px !important;" title="[% $user->gettext('Auswahl bearbeiten') %]" class="btn btn-outline-primary disabled" id="ToggleNewsCollection" onclick="EditNewsCollection()" style="margin:20px 0;" ><span class='fa fa-pencil'></span> [% $user->gettext('Auswahl bearbeiten')%]</a>
                </div>
                <div class="col-lg-4 col-sm-4"></div>
                <div class="col-lg-6 col-sm-6 text-end">
                [% $Paging %]
                </div>
          
                </div>
               
            </div>
        </div>
    </div>
</div>
</div>
</form>

<div class="modal fade" id="NewsModal"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document" style="position: relative;display: table;overflow-y: auto;overflow-x: auto;width: auto;">
        <div class="modal-content">
            <div class="modal-body " id="EditNewsCollectionDiv">
                ...
            </div>
        </div>
    </div>
</div>

<script>
    var newsorder = [];
    var importantnewsorder = [];
    var index = 0;
    $(".sortable").sortable(
            {
                start: function () {
                    // $(".sortable").css('min-height', 50);
                    //   $(".NewSprintClass").collapse('show');


                },

                update: function (event, ui) {
                    
                    $("#NewsList").children().each(function () {
                        var currentElement = $(this);
                        // Show element
                        var ElementID = currentElement.attr('id');
                        var id = ElementID.split('_')[1];
                        newsorder.push({'index': index, 'id': id});
                        index++;

                    });
                    $("#ImportantNewsList").children().each(function () {
                        var currentElement = $(this);
                        // Show element
                        var ElementID = currentElement.attr('id');
                        var id = ElementID.split('_')[1];
                        importantnewsorder.push({'index': index, 'id': id});
                        index++;

                    });
                    $.post("index.php?module=news&event=SaveNewsPosition", {"data":newsorder}, function (res) {
                        //console.log(res);
                        newsorder = [];
                    });
                    
                    $.post("index.php?module=news&event=SaveImportantNewsPosition", {"data":importantnewsorder}, function (res) {
                        //console.log(res);
                        importantnewsorder = [];
                    });

                    },
            
        connectWith: ".sortable"
    });
    var changePage = false;

    var page = localStorage.getItem("page");

    if (page != [% $pos %]) {
        changePage = true;
    }

    localStorage.setItem("page", [% $pos %]);

    if (changePage) {
        var checkboxes = JSON.parse(localStorage.getItem('checkboxes')) || [];
    } else {
        var checkboxes = [];
        localStorage.removeItem("checkboxes");
    }

    if (checkboxes.length >= 1) {
        $("#ToggleNewsCollection").removeClass('disabled');
    }

    $(".selectNews").on("change", function () {
        if (this.checked) {
            if (checkboxes.indexOf($(this).val()) == -1) {
                checkboxes.push($(this).val());
            }
            if (checkboxes.length >= 1) {
                $("#ToggleNewsCollection").removeClass('disabled');
            }
        } else {
            if (checkboxes.indexOf($(this).val()) != -1) {
                checkboxes.splice(checkboxes.indexOf($(this).val()), 1)
            }
            if (checkboxes.length <= 0) {
                $("#ToggleNewsCollection").addClass('disabled');
            }
        }
        localStorage.setItem("checkboxes", JSON.stringify(checkboxes));
    });

    $(".selectNews").each(function () {
        if (checkboxes.indexOf($(this).val()) != -1) {
            $(this).prop("checked", true);
        }
    });

    $(".selectAllNews").on("change", function ()
    {
        var CheckBox = $(this);

        $(".selectNews").each(function ()
        {
            if ($(CheckBox).is(":checked"))
            {
                $(this).prop("checked", true);

                if (checkboxes.indexOf($(this).val()) == -1)
                    checkboxes.push($(this).val());

                if (checkboxes.length >= 1)
                    $("#ToggleNewsCollection").removeClass('disabled');
            } else
            {
                $(this).prop("checked", false);

                if (checkboxes.indexOf($(this).val()) != -1)
                    checkboxes.splice(checkboxes.indexOf($(this).val()), 1)

                if (checkboxes.length <= 0)
                    $("#ToggleNewsCollection").addClass('disabled');
            }
        });

        localStorage.setItem("checkboxes", JSON.stringify(checkboxes));
    });

    function EditNewsCollection()
    {
        var values = JSON.parse(localStorage.getItem("checkboxes"));
        var NewsIDs = values.join();
        if (values.length == 0)
        {
            alert("W�hlen Sie bitte mindestens eine News zum Bearbeiten");
            return;
        }

        $.ajax({
            type: "POST",
            url: "index.php?module=news&event=EditNewsCollection",
            data: {
                news: NewsIDs
            }
        }).done(function (data)
        {
            $('#EditNewsCollectionDiv').html(data);
            $('#NewsModal').modal('show');
        });

        return false;
    }

    function SubmitEditNews()
    {
        $('#ResultMessages li').remove(); //Alte Meldungen beim erneuten Aufruf entfernen

        var values = [];
        values = JSON.parse(localStorage.getItem("checkboxes"));
        var NewsIDs = values.join();

        var CheckedValue = $('input[name=EditNews]:checked').val()

        if (CheckedValue == -1)
        {
            console.log('L�schen');

            bootbox.confirm('Ausgew&auml;hlte News wirklich l&ouml;schen?', function (result)
            {
                if (result)
                {
                    $.post("index.php?module=news&event=DeleteMultipleNews&NewsIDs=" + NewsIDs).done(function (data)
                    {
                        $('#NewsModal').modal('hide');
                        location.reload();
                    });
                }
            });
        } else
        {
            $.post("index.php?module=news&event=ChangeNewsState&State=" + CheckedValue + "&NewsIDs=" + NewsIDs).done(function (data)
            {
                $('#NewsModal').modal('hide');
                location.reload();
            });
        }
    }

    function check_remove(url) {
        bootbox.confirm('[% $user->gettext('Nachricht wirklich l&ouml;schen?') %]', function (result) {
            if (result) {
                window.location.href = url;
            }
        })
    }
</script>
