<script type="text/javascript">

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

</script>

<style>
    .table th{
        cursor: pointer;
    }
    .table tr td{
        cursor: pointer;
    }
</style>
<form action="index.php?module=assets&class=swproductcatalogs&assign=1" method="post" name="hm" onsubmit="" 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" name="pos" value="[% $pos %]" id="pos">
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <span class="fcs-card-title h2">[% $user->gettext('Softwareprodukt-Kataloge') %]</span>
        </div>
        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    <div class="fcs-form-padding">
                        <div class="form-group row mb-3">
                            <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>&nbsp;&nbsp;&nbsp;
                        </div>
                            
                        <div class="form-group row mb-3">
                            <label class="control-label">
                                [% $user->gettext('Produktname') %]
                            </label>
                            <input type="text" class="form-control Filter" value="[% if $Filter.prodname %][% $Filter.prodname %][% /if %]" id="FilterProdName" name="Filter[prodname]">
                        </div>
                        
                        <div class="form-group row mb-3">
                            <label class="control-label">
                                [% $user->gettext('Version') %]
                            </label>
                            <input type="text" class="form-control Filter" value="[% if $Filter.prodversion %][% $Filter.prodversion %][% /if %]" id="FilterProdVersion" name="Filter[prodversion]">
                        </div>
                            
                        <div class="form-group row mb-3">
                            <label class="control-label">
                                [% $user->gettext('Vendor') %]
                            </label>
                            <input type="text" value="[% if $Filter.prodvendor %][% $Filter.prodvendor %][% /if %]" class="form-control Filter" id="FilterProdVendor" name="Filter[prodvendor]">
                        </div> 

                        <div class="form-group row mb-3">
                            <label class="control-label">
                                [% $user->gettext('Sprachen') %]
                            </label>
                            <input type="text" class="form-control Filter" id="FilterProdLanguage" value="[% if $Filter.prodlanguage %][% $Filter.prodlanguage %][% /if %]" name="Filter[prodlanguage]">
                        </div>
                        
                        <div class="form-group row mb-3">
                            <label class="control-label">
                                [% $user->gettext('Beschreibung') %]
                            </label>
                            <input type="text" class="form-control Filter" id="FilterDescr" value="[% if $Filter.descr %][% $Filter.descr %][% /if %]" name="Filter[descr]">
                        </div>
                            
                        [% if $user->hasRight(36) || $user->hasRight(810) %]
                            <div class="btn-group">
                                <button class="btn btn-outline-secondary"><span class="fa fa-search"></span></button>
                                <a class="btn btn-outline-secondary" onclick="clearInput();"><span class="fa fa-arrows-rotate"></span></a>
                                [%**%]
                            </div>
                        [% /if %]
                    </div>
                </div>
            </div>

            <div class="card panel-info shadow-sm rounded">
                <div class="card-header">
                    <div class="row">
                        <div class="col-sm-9">
                             <span class="fcs-card-title h3">
                            [% $entries %]
                            [% if $entries > 1 %]
                                [% $user->gettext('Kataloge') %]
                            [% else %]
                                [% $user->gettext('Katalog') %]
                            [% /if %]
                            [% $user->gettext('gefunden') %]
                        </span>
                        </div>
                        <div class="col-sm-3 text-end">
                      
                        </div>
                    </div>
                   
                </div>

                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered  table-condensed" id="CatTable">
                            <thead>
                                <tr class="fields>">
                                    <th class="sort" id="swproductcatalogid">[% $user->gettext('ID') %]</th>
                                    <th class="sort" id="prodname">[% $user->gettext('Produktname') %]</th>
                                    <th class="sort" id="prodversion">[% $user->gettext('Version') %]</th>
                                    <th class="sort" id="prodvendor">[% $user->gettext('Händler') %]</th>
                                    <th class="sort" id="prodlanguage">[% $user->gettext('Sprachen') %]</th>
                                    <th class="sort" id="descr">[% $user->gettext('Beschreibung') %]</th>
                                   
                                </tr>
                            </thead>
                            <tbody>
                                [% foreach from=$catalogs item=cat key=catid %]
                               
                                    <tr>
                                    <a href="javascript: assignCatalog([% $catid %])">
                                        <td onclick="assignCatalog([% $catid %]);">[% $catid %]</td>
                                        <td onclick="assignCatalog([% $catid %]);">[% $cat.prodname %]</td>
                                        <td onclick="assignCatalog([% $catid %]);">[% $cat.prodversion %]</td>
                                        <td onclick="assignCatalog([% $catid %]);">[% $cat.prodvendor %]</td>
                                        <td onclick="assignCatalog([% $catid %]);">[% $cat.prodlanguage %]</td>
                                        <td onclick="assignCatalog([% $catid %]);">[% $cat.descr %]</td>
                                    </a>
                                    </tr>
                               
                                [% foreachelse %]
                                    <tr class="even">
                                        <td colspan="9" style="text-align:center !important">[% $user->gettext('Es wurden keine Felder 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>
    $('.select2').select2({theme: 'bootstrap-5'});
</script>
<div class="modal fade" id="removeModal" tabindex="-1" role="dialog" aria-labelledby="removeModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body modalContent">
                ...
            </div>
        </div>
    </div>
</div>

<script>
    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 clearInput() {
    $(".Filter").val("");
    document.hm.submit();
}

/**
 * 
 */
function assignCatalog(catalogid) {
    console.log(catalogid);
    $.post("index.php?module=assets&class=swproducts&event=assignCatalog",{catalogid:catalogid, productid:0}, function(data){
        console.log(data);
    })
    parent.closeIframe();
}

</script>
