<!-- Component Contact begin -->

<link href="includes/js/fancytree/skin-bootstrap/ui.fancytree.css" rel="stylesheet" class="skinswitcher">
<script src="includes/js/fancytree/jquery.fancytree-all-deps.min.js"></script>
<script src="includes/js/fancytree/fcs_glyphoptions.js" ></script>

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

            </div>
        </div>
    </div>

    <div class="card-body">
        <div class="card panel-info shadow-sm rounded">
            <div class="card-body">
                <form class="form-inline fcs-form-padding" action="index.php?module=contact" method="post" name="hm" id="hm" enctype="multipart/form-data">
                    <input type="hidden" name="sort" id="sort" value="[% $sort %]" />
                    <input type="hidden" name="sortorder" id="sortorder" value="[% $sortorder %]" />
                    <input type="hidden" name="pos" value="[% $pos %]" id="pos" />
                    <input type="hidden" name="mandantid" value="[% $MandantenID %]" />
                    <input type="submit" style="display:none"/>

                    <div class="form-group row mb-3">
                        <label for="entries_per_page">[% $user->gettext('Eintr&auml;ge pro Seite:') %]</label>
                        <select class="form-control" name="entries_per_page" id="entries_per_page">
                            <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 row mb-3 list-form">
                        <label for="search">[% $user->gettext('Filter') %]:</label>
                        <input type="text" name="search" class="form-control" id="search" placeholder="Filter" value="[% $search %]">
                    </div>


                    <div class="form-group row mb-3 list-form">
                        <label for="mandantid">[% $user->gettext('Mandant') %]:</label>
                        <select  name="mandantid" id="mandantid" class="form-control" onchange="document.hm.submit();">
                            <option value=''>[% $user->gettext('--Mandant wählen--') %]</option>
                            [% html_options options=$Mandanten selected=$MandantenID %]
                        </select>
                    </div>

                    <div class="form-group row mb-3 list-form">
                        <label id="CompanyLabel" for="structure_descr">[% $user->gettext('Firmenstruktur') %]:</label>

                        <div class="input-group">
                            <input type="hidden" name="structure_id" value="[% $structure_id %]" id="structure_id" />
                            <input type="text" name="structure_descr" class="form-control" id="structure_descr" value="[% $structure_descr %]" style="width:300px;" placeholder="Firmenstruktur">
                            <a class="btn btn-outline-secondary" onclick="showComboBoxList();"><span class="fa fa-chevron-down"></span></a>
                        </div>
                        <div id="comboboxlist" style="display: none;">
                            <div id="comboboxres"></div>
                        </div>

                    </div>
                    <div class="form-group row mb-3">
                        <div class="checkbox">
                            <label for="showDuplicates">[% $user->gettext('Doppelte anzeigen') %]:</label>
                            <input type='checkbox' name='showDuplicates' value='1' [% if $showDuplicates %]checked='checked'[% /if %] onchange='submitForm();'/>
                        </div>
                    </div>
                    <div class="form-group row mb-3">
                        <a href="JavaScript:void(0);" onclick="switchPage(0);" class="btn btn-outline-primary" id="btnFilter" title="[% $user->gettext('Filtern') %]"><span class="fa fa-refresh"></span></a>
                    </div>

                [% if $user->hasRight(42) %]
                    <div class="form-group row mb-3">
                        <a href="index.php?module=contact&event=create" id="btnCreateNewContact" class="btn btn-outline-primary" ><span class="fa fa-plus"></span> [% $user->gettext('Kontakt anlegen') %]</a>
                    </div>
                [% /if %]


                </form>


            </div>
        </div>
        <div class = "card panel-info shadow-sm rounded">
            <div class="card-header">
                <h3 style="display:inline;">[% $user->gettext('Kontaktliste') %]</h3><span style="font-size:17px;"> - [% if $anzahl > 1 %][% $anzahl %] [% $user->gettext('Eintr&auml;ge gefunden') %][% /if %]</span>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr class="fields">
                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "Name" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]'sort').value = 'Name'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('Name') %]
                                    [% if $sort == "Name" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "Company" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]'sort').value = 'Company'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('Firma') %]
                                    [% if $sort == "Company" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>


                                [% if $user->Security->Company == 'Adalbert Pilipp GmbH' %]
                                    <th style="cursor:pointer;" onclick="document.getElementById(
                                        [% if $sort == "department" %]
                                            'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                        [% else %]'sort').value = 'department'
                                        [% /if %];
                                                    submitForm();">
                                        [% $user->gettext('Abteilung') %]
                                        [% if $sort == "department" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </th>
                                [% else %]
                                    <th style="cursor:pointer;" onclick="document.getElementById(
                                        [% if $sort == "Position" %]
                                            'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                        [% else %]
                                                    'sort'
                                                            ).value = 'Position'
                                        [% /if %];
                                                    submitForm();">
                                        [% $user->gettext('Position') %]
                                        [% if $sort == "Position" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </th>
                                [% /if %]

                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "PersonalNo" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]
                                                'sort'
                                                        ).value = 'PersonalNo'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('Personalnr.') %]
                                    [% if $sort == "PersonalNo" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "Phone" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]
                                                'sort'
                                                        ).value = 'Phone'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('Telefon') %]
                                    [% if $sort == "Phone" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "Email" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]
                                                'sort'
                                                        ).value = 'Email'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('E-Mail') %]
                                    [% if $sort == "Email" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th style="cursor:pointer;" onclick="document.getElementById(
                                    [% if $sort == "UserName" %]
                                        'sortorder').value = '[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]'
                                    [% else %]
                                                'sort'
                                                        ).value = 'UserName'
                                    [% /if %];
                                                submitForm();">
                                    [% $user->gettext('Benutzer') %]
                                    [% if $sort == "UserName" %]<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                </th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            [% if $Contacts %]
                                [% foreach from=$Contacts item=ContactData name=userlist %]
                                    <tr>
                                        <td>[% $ContactData.name %]</td>
                                        <td>[% $ContactData.company %]</td>
                                        [% if $user->Security->Company == 'Adalbert Pilipp GmbH' %]
                                            <td>[% $ContactData.department %]</td>
                                        [% else %]
                                            <td>[% $ContactData.com_position %] </td>
                                        [% /if %]
                                        <td>[% $ContactData.personalno %]</td>
                                        <td>[% $ContactData.phone %]</td>
                                        <td><a href="mailto:[% $ContactData.email %]" [% if !$ContactData.sendemails %]class="dontSendMails" style="color:lightgrey;" title="[% $user->gettext('Mailversand an Kontakt nicht aktiv') %]"[% /if %]>[% $ContactData.email %]</a></td>
                                        <td>
                                            [% if $user->hasRight(42) %]
                                                <a href="index.php?module=user&event=edit&id=[% $ContactData.user_id %]">[% $ContactData.username %]</a>
                                            [% else %]
                                                [% $ContactData.username %]
                                            [% /if %]
                                        </td>
                                        <td>
                                            <div class="btn-group">
                                                [% if $user->hasRight(43) %]
                                                    <a class="btn btn-outline-secondary" href="index.php?module=contact&amp;event=remove&amp;id=[% $ContactData.contact_id %]" onclick="return confirm('[% $ContactData.name %] \n[% $user->gettext('wirklich l&ouml;schen?') %]');"><span class="fa fa-trash" title="[% $user->gettext('Kontakt löschen') %]"></span></a>
                                                    [% /if %]
                                                    [% if $user->hasRight(42) %]
                                                    <a class="btn btn-outline-secondary" href="index.php?module=contact&amp;event=edit&amp;id=[% $ContactData.contact_id %]"><span class="fa fa-pencil" title="[% $user->gettext('Kontakt bearbeiten') %]"></span></a>
                                                    [% /if %]
                                                <a class="btn btn-outline-secondary" href="index.php?module=contact&amp;event=show&amp;id=[% $ContactData.contact_id %]"><span class="fa fa-eye" title="[% $user->gettext('Kontakt ansehen') %]"></span></a>
                                                    [% if $user->hasRight(42) %]
                                                    <a class="btn btn-outline-secondary openInBigIFrameModal" href="index.php?module=ticketlist&amp;event=user&amp;id=[% $ContactData.contact_id %]"><span class="fa fa-search" title="[% $user->gettext('Alle Tickets des Kontaktes zeigen') %]"></span></a>

                                                    <a class="btn btn-outline-secondary openInBigIFrameModal" href="index.php?module=ticketlist&TicketFromContact=[% $ContactData.contact_id %]"><span class="fa fa-search" title="[% $user->gettext('Alle Tickets des Kontaktes zeigen') %]"></span></a>
                                                    [% /if %]
                                            </div>
                                        </td>
                                    </tr>
                                [% /foreach %]
                            [% else %]
                                <tr class="odd">
                                    <td colspan="8">
                                        <div align="center">[% $user->gettext('Keine Eintr&auml;ge vorhanden') %]</div>
                                    </td>
                                </tr>
                            [% /if %]
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="15">
                                    [% $Paging %]
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- ###CONTENT### begin -->


<script type="text/javascript">

    function showComboBoxList() {
        var txtfield = document.getElementById("structure_descr");
        var userbox = document.getElementById("comboboxlist");

        if (userbox.style.visibility != "visible") {
            /*
             console.log(absTop(txtfield));
             var spantop = absTop(txtfield);

             var spanleft = absLeft(txtfield);
             var spanwidth = txtfield.offsetWidth;
             var spanheight = txtfield.offsetHeight;

             spantop = spantop + spanheight;
             spanwidth = spanwidth - 2;

             userbox.style.width = spanwidth + "px";
             userbox.style.left = spanleft + "px";
             userbox.style.top = spantop + "px";

             userbox.style.visibility = "visible";
             */
            var labelWidth = $("#CompanyLabel").outerWidth();
            var textBoxWidth = $("#structure_descr").outerWidth();
            console.log(textBoxWidth);
            $("#comboboxlist").css('margin-left', (labelWidth + 3) + 'px');
            $("#comboboxlist").css('max-width', textBoxWidth + 'px');
            userbox.style.visibility = "visible";

            userbox.style.display = "block";
        } else {
            userbox.style.visibility = "hidden";
            userbox.style.display = "none";
        }
    }

    function setStructureValue(sid, name) {
        document.getElementById('structure_descr').value = name;
        document.getElementById('structure_id').value = sid;
        document.getElementById('comboboxlist').style.display = "none";
        switchPage(0);
    }

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

    $(document).ready(function () {
        $(".dialog").fancybox({
            maxWidth: 800,
            maxHeight: 600,
            closeClick: false
        });
        $(".dialog_small").fancybox({
            closeClick: false,
            beforeLoad: function () {
                this.width = 250;
                this.height = 220;
            }
        });

        $('#img_descr').on("click", function () {
            $('#comboboxList').show();
            $('#comboboxList').fadeIn();
        })

        $("#comboboxres").fancytree({
            source: {
                url: "index.php?module=structure&event=JSONStructureTree&id=0"
            },
            extensions: ["glyph"],
            activeVisible: true,
            glyph: glyph_opts,
            checkbox: false,
            autoActivate: true, // Automatically activate a node when it is focused (using keys).
            selectMode: 1, // 1:single, 2:multi, 3:multi-hier
            activeVisible: true,
            activate: function (event, data) {
                var node = data.node;
                setStructureValue(node.key, node.title);
            },
            lazyLoad: function (event, data) {
                var node = data.node;
                data.result = {
                    url: node.data.src,
                    data: {mode: "children", parent: node.key},
                    cache: false
                };
            }
        }).show();

    })

</script>
<!-- Component Contact end -->

