<link rel="stylesheet" type="text/css" href="includes/js/datatables_1.11.5/datatables.min.css"/>
<script type="text/javascript" src="includes/js/datatables_1.11.5/datatables.min.js"></script>

<link rel="stylesheet" href="includes/jstree/jstree.min.css" />
<script src="includes/jstree/jstree.min.js"></script>

<script src="modules/knowledgebase/javascript/FAQCategoryTreeAndTable.js"></script>


<script>



    function keyEvent(event) {
        if (event.keyCode == 13) {
        event.preventDefault(); // Verhindert Submit
        submitForm(); // Optional: ruft trotzdem deine Suche auf
    }
    }

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

    //jsTree
    const hasRightToEdit = [% if $user->hasRight(19) %] true [% else %]false [% /if %];
    var s_create = "[% $user->gettext('Neue Rubrik') %]";
    var s_rename = "[% $user->gettext('Umbenennen') %]";
    var s_remove = "[% $user->gettext('Löschen') %]";
    var s_edit = "[% $user->gettext('Bearbeiten') %]";
    var s_confirm_delete = "[% $user->gettext('Wirklich löschen?') %]";
    var s_create_name = "[% $user->gettext('Name des Knotens') %]";
var s_name_placeholder = "[% $user->gettext('Geben Sie den Namen des Knotens ein') %]";
var s_create_description = "[% $user->gettext('Beschreibung der Rubrik') %]";
var s_description_placeholder = "[% $user->gettext('Geben Sie eine Beschreibung für den Knoten ein') %]";
var s_create_submit = "[% $user->gettext('Rubrik erstellen') %]";
var s_create_cancel = "[% $user->gettext('Abbrechen') %]";
var s_missing_fields = "[% $user->gettext('Bitte füllen Sie sowohl den Namen als auch die Beschreibung aus.') %]";


    //DataTable
    var s_lengthMenu = "[% $user->gettext('Zeige _MENU_ Eintr&auml;ge') %]";
    var s_zeroRecords = "[% $user->gettext('Keine Eintr&auml;ge gefunden') %]";
    var s_info = "[% $user->gettext('Eintrag _START_ bis _END_ (von _TOTAL_)') %]";
    var s_infoEmpty = " ";
    var s_infoFiltered = "[% $user->gettext(' - gefiltert aus _MAX_ Eintr&auml;gen') %]";
    var s_search = "[% $user->gettext('Filter') %]";
    var s_first = "&lt;&lt;";
    var s_last = "&gt;&gt;";
    var s_next = "&gt;";
    var s_previous = "&lt;";
</script>


<form action="index.php?module=knowledgebase" method="post" name="hm" onsubmit="return false" id="hm" enctype="multipart/form-data" class="form-horizontal form-label-left">
    <input type="hidden" name="pos" id="pos" value="[% $pos %]" />
    <!-- ###CONTENT### begin -->
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
        <div class="row">
            <div class="col-sm-6">
                <h2 class="card-title-fcs">                 
                        [% $user->gettext('Knowledgebase') %]
                </h2>
            </div>
            <div class="col-sm-6 text-end">
            <div class="btn-group">
                [% if $user->CanCreateKBEntry() || $user->hasRight(19) || $user->hasRight(15) %]<a href="index.php?module=knowledgebase&event=create" class="btn btn-primary">[% $user->gettext('Beitrag erstellen') %]</a>[% /if %]
                [% if $user->CanCreateKBEntry() || $user->hasRight(19) %]<a href="index.php?module=knowledgebase&event=release" class="btn btn-primary">[% $user->gettext('Beitragfreigabe') %]</a>[% /if %]
            </div>
            </div>
        </div>
          

        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">

                    [% if $user->Security->Company == 'Autohaus Wolfsburg' %]
                        [% if $FAQSearchForm == true %]
                        [% else %]

                            <div class="card panel-info shadow-sm rounded">
                                <div class="card-header">
                                    <h3 class="card-title">[% $user->gettext('Suche') %]</h3>
                                </div>
                                <div class="card-body">
                                    <div class="form-group row mb-3">
                                        <label class="col-lg-4 control-label" for="kbsearch">[% $user->gettext('Volltextsuche') %]</label>
                                        <div class="col-lg-8">
                                            <input type="text" name="search" id="kbsearch" class="form-control" onkeyup="keyEvent(event)">
                                        </div>
                                    </div>
                                    <a href="javascript:void(0);" class="btn btn-outline-primary" onclick="submitForm();"> [% $user->gettext('Suchen') %] </a>


                                </div>
                            </div>

                        [% /if %]
                    [% /if %]
                    [%*
                    <div class="card panel-info shadow-sm rounded">
                        <div class="card-header">
                            <h3 class="card-title">[% $user->gettext('Rubriken') %]</h3>
                        </div>
                        <div class="card-body">

                            <ul class="list-group">
                                <li class="list-group-item"><strong><a href="?module=knowledgebase" class="active">[% $user->gettext('alle Rubriken anzeigen') %]</a></strong></li>
                                            [% foreach from=$ActiveCategoriesList item=category %]
                                    <li class="list-group-item [% if $category->FAQCategoryID == $selectedCid %]list-group-item-info[% /if%]"><a href="?module=knowledgebase&cid=[% $category->FAQCategoryID %]">[% $category->Name %]</a></li>
                                    [% /foreach %]
                            </ul>
                        </div>
                    </div>
                    *%]


                    <div class="card panel-info shadow-sm rounded">
                        <div class="card-header d-flex align-items-center justify-content-between">
                            <h3 class="card-title mb-0">[% $user->gettext('Rubriken') %]</h3>
                            <div class="d-flex align-items-center">
                                <input type="text" id="faq-tree-search" class="form-control w-auto ms-3"
                                       style="background-color: #fff; border: 1px solid #ced4da;"
                                       placeholder="[% $user->gettext('Filtern') %]" />
                                [% if $user->hasRight(19) %]
                                <button id="create-root-node" type="button" class="btn btn-primary ms-3" title="[% $user->gettext('Neue Hauptkategorie') %]">
                                    <span class="fas fa-plus"></span>
                                </button>
                                [% /if %]
                            </div>
                        </div>
                        <div class="card-body">

                        <div id="faq-tree"></div>

                        </div>
                    </div>

                    [% if $user->Security->Company != 'Autohaus Wolfsburg' %]
                        [% if $FAQSearchForm == true %]
                        [% else %]
                            <div class="card panel-info shadow-sm rounded">
                                <div class="card-header">
                                    <h3 class="card-title">[% $user->gettext('Suche') %]</h3>
                                </div>
                                <div class="card-body">

                                    <div class="form-group row mb-3">
                                        <label class="col-lg-4 control-label" for="kbsearch">[% $user->gettext('Volltextsuche') %]</label>
                                        <div class="col-lg-8">
                                            <input type="text" name="search" id="kbsearch" class="form-control" onkeyup="keyEvent(event)">
                                        </div>
                                    </div>

                                    <a href="javascript:void(0);" class="btn btn-outline-primary" onclick="submitForm();"> [% $user->gettext('Suchen') %] </a>


                                </div>
                            </div>
                        [% /if %]
                    [% /if %]

                    [% if $user->hasRight(31) && ($user->Security->Company == 'Pflitsch GmbH & Co KG' || $user->Security->Company == 'VR meine Raiffeisenbank eG') %]
                    <div class="card panel-info shadow-sm rounded">
                        <div class="card-header">
                            <div class="row">
                                <div class="col-sm-6">
                                    <h2 class="card-title">[% $user->gettext('Knowledgebase') %]</h2>
                                </div>
                                <div class="col-sm-6 text-end">
                                    <span>[% $user->gettext('TOP 10') %]</span>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <ul class="list-group">
                                [% foreach from=$FAQTop10 item=faq name=top10 %]
                                    <li class="list-group-item">
                                        [% if  $faq.thumbnail  %]
                                            <img src='index.php?module=upload&class=download&event=ShowImage&name=Medium_[%  $faq.thumbnail %]' style="max-height: 50px; margin-bottom: 10px;">
                                        [% /if %]
                                        <span class="badge bg-secondary" data-container="body" data-bs-toggle="popover" data-placement="top" data-content="[% $user->gettext('Aufrufe') %]" title="[% $faq.faqvisit.visits %] [% $user->gettext('Aufrufe') %]">[% if $faq.visits %][% $faq.visits %][% else %]0[% /if %] </span>
                                        [% if !$user->UserID %]
                                            <a href="?module=knowledgebase&fid=[% $faq.faqdataid %]&justdetails=1" data-fancybox-type="iframe" class="dialog">
                                            [% else %]
                                                <a href="?module=knowledgebase&fid=[% $faq.faqdataid %]" >
                                                [% /if %]
                                                [% $faq.shortdescription|truncate:3000:" ...":true %]
                                            </a>
                                    </li>
                                [% foreachelse %]
                                    <li class="list-group-item"> [% $user->gettext('Keine Knowledgebaseeinträge vorhanden') %] </li>
                                    [% /foreach %]
                            </ul>
                        </div>
                    </div>
                    <div class="card panel-info shadow-sm rounded">
                        <div class="card-header portlet-header">
                                <div class="row">
                                <div class="col-sm-6">
                                    <h2 class="card-title">[% $user->gettext('Knowledgebase') %] </h2>
                                </div>
                                <div class="col-sm-6 text-end">
                                    <span>[% $user->gettext('Neusten Fünf') %]</span>
                                </div>
                            </div>

                        </div>
          
                        <div class="card-body portlet-content">
                            <ul class="list-group">
                              [% foreach from=$FAQLatest5 item=faq %]
                                <li class="list-group-item">
                                    [% if $faq.Thumbnail %]
                                        <img src='index.php?module=upload&class=download&event=ShowImage&name=Medium_[% $faq.Thumbnail %]' style="max-height: 50px; margin-bottom: 10px;">
                                    [% /if %]

                                    <span class="pull-right" title="[% $user->gettext('Erstellt am') %]: [% $faq.CreateDate|date_format:"%d.%m.%Y %H:%M" %]">
                                        <span class="fa fa-calendar"></span>
                                        [% $faq.CreateDate|date_format:"%d.%m.%Y" %]
                                    </span>

                                    [% if !$user->UserID %]
                                        <a href="?module=knowledgebase&fid=[% $faq.FAQDataID %]&justdetails=1" data-fancybox-type="iframe" class="dialog">
                                    [% else %]
                                        <a href="?module=knowledgebase&fid=[% $faq.FAQDataID %]">
                                    [% /if %]
                                        [% $faq.ShortDescription|truncate:3000:" ...":true %]
                                    </a>
                                </li>
                            [% foreachelse %]
                                <li class="list-group-item">[% $user->gettext('Keine Knowledgebaseeinträge vorhanden') %]</li>
                            [% /foreach %]
                            </ul>
                        </div>
                    </div>
                    [% /if %]

                </div>
                [% if $user->Security->Company == 'Pflitsch GmbH & Co KG' || $user->Security->Company == 'VR meine Raiffeisenbank eG' %]
                    <div class="col-md-9">
                    [% else %]
                        <div class="col-md-6">
                        [% /if %]

                        <div class="card panel-info shadow-sm rounded">
                            <div class="card-header">
                                <h3 class="card-title">[% $user->gettext('Knowledgebase') %]</h3>
                            </div>
                            <div class="card-body">

                                    <!-- Loading Spinner, wird angezeigt, während die Artikel geladen werden -->
                                <div id="loading-spinner" class="d-none text-center my-3">
                                    <i class="fa fa-spinner fa-spin fa-3x text-primary"></i>
                                </div>
                                <div id="ArticleSearchResultTable">
                                    [% if $ActiveCategories %]
                                        <h3>[% $user->gettext('Rubriken') %]</h3>
                                        <ul class="list-group">
                                            [% foreach from=$ActiveCategories item=category %]
                                                [% if $category->faqdata_count %]
                                                    <li class="list-group-item"><a href="?module=knowledgebase&cid=[% $category->FAQCategoryID %]">[% $category->Name %]</a> ([% $category->faqdata_count %] [% $user->gettext('Eintr&auml;ge') %])</li>
                                                [% /if %]
                                            [% /foreach %]
                                        </ul>


                                    [% /if %]
                                    [% if $CategoryEntries && !$FAQSearch %]

                                        [% include file=$modulePath|cat:"/ArticleSearchResultTable.tpl" CategoryEntries=$CategoryEntries ListCategoryName=$ListCategoryName ListCategoryID=$ListCategoryID showRating=$showRating available=$available user=$user %]

                                    [% /if %]

                                    [% if $FAQEntry && !$FAQSearch %]
                                        [% include file=$modulePath|cat:"/FAQEntry.tpl" %]
                                    [% /if %]
                                    [% if $FAQSearch || $FAQSearchForm %]
                                        [% include file=$modulePath|cat:"/FAQSearch.tpl" %]
                                    [% /if %]
                                    [% $FAQContent %]
                                 </div>
                            </div>
                        </div>

                    </div>  <!-- Mittlere Spalte -->

                    [% if $user->Security->Company != 'Pflitsch GmbH & Co KG' && $user->Security->Company != 'VR meine Raiffeisenbank eG' %]
                        <div class="col-md-3">
                            [% if $user->hasRight(31) %]


                                <div class="card panel-info shadow-sm rounded">
                                    <div class="card-header">
                                        <div class="row">
                                            <div class="col-sm-6">
                                                <h2 class="card-title">[% $user->gettext('Knowledgebase') %]</h2>
                                            </div>
                                            <div class="col-sm-6 text-end">
                                                <span>[% $user->gettext('TOP 10') %]</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="card-body">
                                        <ul class="list-group">
                                            [% foreach from=$FAQTop10 item=faq name=top10 %]
                                                <li class="list-group-item">
                                                    [% if  $faq.thumbnail  %]
                                                        <img src='index.php?module=upload&class=download&event=ShowImage&name=Medium_[%  $faq.thumbnail %]' style="max-height: 50px; margin-bottom: 10px;">
                                                    [% /if %]
                                                    <span class="badge bg-secondary" data-container="body" data-bs-toggle="popover" data-placement="top" data-content="[% $user->gettext('Aufrufe') %]" title="[% $faq.faqvisit.visits %] [% $user->gettext('Aufrufe') %]">[% if $faq.visits %][% $faq.visits %][% else %]0[% /if %] </span>
                                                    [% if !$user->UserID %]
                                                        <a href="?module=knowledgebase&fid=[% $faq.faqdataid %]&justdetails=1" data-fancybox-type="iframe" class="dialog">
                                                        [% else %]
                                                            <a href="?module=knowledgebase&fid=[% $faq.faqdataid %]" >
                                                            [% /if %]
                                                            [% $faq.shortdescription|truncate:3000:" ...":true %]
                                                        </a>
                                                </li>
                                            [% foreachelse %]
                                                <li class="list-group-item"> [% $user->gettext('Keine Knowledgebaseeinträge vorhanden') %] </li>
                                                [% /foreach %]
                                        </ul>
                                    </div>
                                </div>

                                <div class="card panel-info shadow-sm rounded">
                                    <div class="card-header portlet-header">
                                         <div class="row">
                                            <div class="col-sm-6">
                                                <h2 class="card-title">[% $user->gettext('Knowledgebase') %] </h2>
                                            </div>
                                            <div class="col-sm-6 text-end">
                                                <span>[% $user->gettext('Neusten Fünf') %]</span>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="card-body portlet-content">
                                        <ul class="list-group">
                                            [% foreach from=$FAQLatest5 item=faq %]
                                                <li class="list-group-item">
                                                     [% if  $faq.Thumbnail  %]
                                                        <img src='index.php?module=upload&class=download&event=ShowImage&name=Medium_[%  $faq.Thumbnail %]' style="max-height: 50px; margin-bottom: 10px;">
                                                    [% /if %]
                                                    <span class="pull-right" title="[% $user->gettext('Erstellt am') %]: [% $faq.CreateDate|date_format:"%d.%m.%Y %H:%M" %]">
                                                        <span class="fa fa-calendar"></span> 
                                                        [% $faq.CreateDate|date_format:"%d.%m.%Y" %]

                                                    </span>

                                                    [% if !$user->UserID %]
                                                        <a href="?module=knowledgebase&fid=[% $faq.FAQDataID %]&justdetails=1" data-fancybox-type="iframe" class="dialog">
                                                        [% else %]
                                                            <a href="?module=knowledgebase&fid=[% $faq.FAQDataID %]" >
                                                            [% /if %]
                                                            [% $faq.ShortDescription|truncate:3000:" ...":true %]                                                     

                                                        </a>

                                                </li>
                                            [% foreachelse %]
                                                <li class="list-group-item"> [% $user->gettext('Keine Knowledgebaseeinträge vorhanden') %] </li>
                                                [% /foreach %]
                                        </ul>
                                    </div>
                                </div>
                            [% /if %]
                        </div>
                    [% /if %]
                </div>
            </div>
        </div>
    </div>

</form>
<!-- Modal für das Erstellen eines neuen Knotens -->
<div class="modal fade" id="createNodeModal" tabindex="-1" role="dialog" aria-labelledby="createNodeModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="createNodeModalLabel">[% $user->gettext('Neue Rubrik anlegen') %]</h5>
          <span aria-hidden="true"><span  data-bs-dismiss="modal" class="fa fa-times"></span></span>
      </div>
      <div class="modal-body">
        <form id="createNodeForm">
            <input type="hidden" id="node-id" name="id" />
          <div class="form-group">
            <label for="node-name">[% $user->gettext('Name der Rubrik') %]</label>
            <input type="text" class="form-control" id="node-name" placeholder="[% $user->gettext('Name eingeben') %]" required>
          </div>
          <div class="form-group">
            <label for="node-description">[% $user->gettext('Beschreibung') %]</label>
            <textarea class="form-control" id="node-description" placeholder="[% $user->gettext('Beschreibung eingeben') %]"></textarea>
          </div>
         <div class="form-group">
            <label for="group-name">[% $user->gettext('Bearbeitbar für folgende Gruppen') %]</label>
            <select class="form-control" id="GroupCategorySelect" multiple>
            [% foreach from=$groups key=key item=value %]
                <option value="[% $key %]">[% $value %]</option>
            [% /foreach %]
            </select>
          </div>
          <br>
          <button type="submit" class="btn btn-primary">[% $user->gettext('Speichern') %]</button>
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">[% $user->gettext('Abbrechen') %]</button>
        </form>
      </div>
    </div>
  </div>
</div>


<script>
$('#createNodeModal').on('shown.bs.modal', function () {
    if (!$('#GroupCategorySelect').hasClass('select2-hidden-accessible')) {
        $('#GroupCategorySelect').select2({
            dropdownParent: $('#createNodeModal'),
            width: '100%'
        });
    }
});
</script>

<!-- ###CONTENT### end -->
