<!-- Component Contact begin -->
<form action="" method="post" name="hm" id="hm" enctype="multipart/form-data" style="margin:0;padding:0;" class="form-inline fcs-form-padding">

    <input type="hidden" name="pos" value="[% $pos %]" id="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">
            <span class="fcs-card-title h2">[% $user->gettext("Firmen verwalten")%]</span>
        </div>
        <div class="card-body">
            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    <div class="form-group row mb-3">
                        <label class="control-label form-check-label" for="withDeleted" > [% $user->gettext('Gelöschte Kontakte berücksichtigen') %] </label>                                       
                        <input type="checkbox" class="form-check-input" name="withDeleted" value="1" [% if $withDeleted %]checked="checked" [% /if %] onchange="document.hm.submit();" />
                    </div>
                    <div class="form-group row mb-3">
                        <label class="control-label" for="entries_per_page">[% $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 row mb-3">
                        <label class="control-label" for="search">[% $user->gettext('Filter') %]:</label>
                        <div class="input-group">
                            <input type="text" name="search" class="form-control" value="[% $search %]" onkeyup="if (event.keyCode == 13) {submitForm();}">
                                <a href="JavaScript:void(0);" onclick="switchPage(0);" class="btn btn-outline-primary"><span class="fa fa-search"></span></a>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card panel-info shadow-sm rounded">
                <div class="card-header">
                    <span class="fcs-card-title h3">[% $user->gettext('Firmenliste') %]</span><span class="fcs-card-title h4">[% if $anzahl > 1 %](- [% $anzahl %] &nbsp;[% $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">
                                    <td style="cursor:pointer;" onclick="
                                        [% if $sort == "Company" %]
                                            document.getElementById('sortorder').value='[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]';
                                        [% else %]
                                            document.getElementById('sort').value = 'Company';
                                        [% /if %]submitForm();">
                                        [% $user->gettext('Firma') %]
                                        [% if $sort == "Company" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </td>
[% if $Tools->isSennebogen() %]
                                    <td style="cursor:pointer;" onclick="
                                        [% if $sort == 'pAAccount' %]
                                            document.getElementById('sortorder').value='[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]';
                                        [% else %]
                                            document.getElementById('sort').value='pAAccount';
                                        [% /if %]submitForm();">
                                        [% $user->gettext('proAlpha Account') %] 
                                        [% if $sort == "pAAccount" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </td>
[% /if %]
                                    <td style="cursor:pointer;" onclick="
                                        [% if $sort == "CountContacts" %]
                                            document.getElementById('sortorder').value='[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]';
                                        [% else %]
                                            document.getElementById('sort').value='CountContacts';
                                        [% /if %]submitForm();">
                                        [% $user->gettext('Anzahl Kontakte') %] 
                                        [% if $sort == "CountContacts" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </td>
                                    <td style="cursor:pointer;" onclick="
                                        [% if $sort == "CountTickets" %]
                                            document.getElementById('sortorder').value='[% if $sortorder == "DESC" %]ASC[% else %]DESC[% /if %]';
                                        [% else %]
                                            document.getElementById('sort').value='CountTickets';
                                        [% /if %]submitForm();">
                                        [% $user->gettext('Anzahl Tickets') %]
                                        [% if $sort == "CountTickets" %]&nbsp;<span class="fa fa-chevron-[% if $sortorder == "DESC" %]down[% else %]up[% /if %]"></span>[% /if %]
                                    </td>
                                    <td>&nbsp;</td>
                                </tr>
                            </thead>
                    <tbody>
             [% foreach from=$Companys item=company name=companylist %]

                <tr>
                    <td id="Company_[% $company.companyid %]" >&nbsp;[% $company.company %] [% if ! $company.companyid %]<span class="error">[% $company.contactcompany %]</span>[% /if %]</td>
                    [% if $Tools->isSennebogen() %]
                    <td> <select class="form-control select2" data-companyid="[% $company.companyid %]" id="PAAccount_[% $company.companyid %] %]" style="width:230px">[% if $company.pasupporterid %]<option value="[% $company.pasupporterid %]">[%  $company.username %] ([%  $company.paaccount %])</option>[% /if %]</select></td>
                    [% /if %]
                    <td>&nbsp;[% $company.countcontacts %]&nbsp;</td>
                    <td>&nbsp;[% $company.counttickets  %]&nbsp;</td>
                    <td>
                        <div>
                            [% if $user->hasRight(43) %]
                                <a href="index.php?module=contact&amp;class=company&amp;event=remove&amp;id=[% $company.companyid %]" data-fancybox-type="iframe" class="btn btn-outline-secondary dialog" onclick="return confirm('[% $company.company %] \n[% $user->gettext('wirklich löschen?') %]');"><span class="fa fa-trash" title="[% $user->gettext('L&ouml;schen') %]"></span></a>
                            [% /if %]
        
                            [% if $user->hasRight(42) %]
                                <a href="javascript:editCompany([% $company.companyid %], '[% $company.company %]', '[% $company.paaccount %]');"  class="btn btn-outline-secondary editCompanyBtn"><span class="fa fa-pencil" title="[% $user->gettext('Bearbeiten') %]"></span></a>
                            [% /if %]
        
                            [% if FALSE && $user->hasRight(42) %]
                                <a href="index.php?module=ticketlist&event=companytickets&id=[% $company.companyid %]" data-fancybox-type="iframe" class="btn btn-outline-secondary dialog"><span class="fa fa-search"></span></a>
         
                            [% /if %]
                        </div>
                    </td>
                    </tr>

                    [% foreachelse %]
                
                        <tr class="even">
                            <td colspan="4">
                                <div align="center">[% $user->gettext('Keine Einträge vorhanden') %]</div>
                            </td>
                        </tr>
                    [% /foreach %]
                
                    <tr>
                        <td colspan="4" >
                            <div class="form-group row mb-3">
                                <label for="newcompany" class="control-label">[% $user->gettext('Neue Firma anlegen') %]:</label>
                                <div class="input-group">
                                    <input class="form-control" type="text" id="newcompany" name="newcompany" maxlength="100" onkeyup="if (event.keyCode == 13) {submitForm();}" />
                                        <a href="javascript:submitForm();" class="btn btn-outline-primary" id="saveChange"><span class="fa fa-floppy-disk"></span></a>
                                </div>    
                            </div>
                        </td>
                    </tr>
                
                </tbody>
                <tfoot>
                    
                </tfoot>
                        </table>
                          [% $Paging %]
                    </div>
                </div>
            </div>
        </div>
    </div>
   
</form>


<script type="text/javascript">


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

    function editCompany(id, CompanyName, pAAccount){
        if(id){
           

    $('.editCompanyBtn').addClass('disabled');            
            var Formular = '<span id="editCompanyNameMessage"></span><div class="input-group"><input type="text" class="form-control" id="editCompanyName" value="'+ CompanyName +'" /><input type="hidden" class="form-control" id="editCompanyID" value="'+ id +'" /><a href="javascript:saveChanges('+ id +');" class="btn btn-outline-primary" id="saveChange"><span class="fa fa-floppy-disk"></span></a></div>'
            $('#Company_'+id).html(Formular);
        }
    }

    $('#editCompanyName').keypress(function (e) {
        if (e.which == 13){
            var id = $('#editCompanyID').val();
            if(id) saveChanges(id);
        }
    });

    function saveChanges(id){
        var NewName = $('#editCompanyName').val();
        console.log(NewName);
        if('' != NewName){
            $.post("index.php?module=contact&class=company&event=jsonUpdateCompany", { newcompany: NewName, id: id }, function(json) {
                console.log(json);
                if(json != '""'){ 
                    var Message = '<span class="error">'+json+'</span><br />';
                    $('#editCompanyNameMessage').html(Message);
                } else {
                    $('#Company_'+id).html('&nbsp;'+NewName);
                    $('.editCompanyBtn').removeClass('disabled');
                }
            });
        }
        
    }

    $(document).ready(function() {

        $(".dialog_ticketlist")
        .fancybox({
            type: 'iframe',
            autoSize : false,
            autoResize: true,
            autoCenter: true,
            beforeLoad : function() {
                this.width  = 1300;
                this.height = 600;
            }
        });
[% if $Tools->isSennebogen() %]
$('.select2').select2({
  ajax: {
    url: 'index.php?module=contact&class=company&event=getSupportersJson',
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        selVal: $(this).val()
      };
    }
  }
    }).on('select2:select', function (e) {   
        var data = e.params.data;
        $.post('index.php?module=contact&class=company&event=setPAUserForCompany',{companyid: $(this).data('companyid'), userid:data.id});
    });

[% /if %]
                });

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

