<link rel="stylesheet" type="text/css" href="includes/js/select2/css/select2.min.css" />

<form action="index.php?module=settings&class=MailAccounts" method="post" name="hm" id="hm-Settings-EditLDAP" enctype="multipart/form-data" class='form-horizontal form-label-left'>

    <input type="hidden" name="MailAccount[mailaccountid]" value="[% $MailAccount.mailaccountid %]" />
    <input type="hidden" name="save" value="1" />
    <input type="hidden" name="editMailAccount" value="1" />
    <input type="hidden" name="task" value="editMailAccount" />


    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <div class="row">
                <span class="fcs-card-title h3">
                    <div class="col-sm-6">[% $user->gettext('MailAccount bearbeiten') %]</div>
                    <div class="col-sm-6 text-end">
                        <input type="submit" class="btn btn-outline-secondary" value="[% $user->gettext('Daten speichern') %]" />
                    </div>
                </span>
            </div>
        </div>
        <div class="card-body">
            [% if $Tools->FCS_Count($MailAccount.errors) %]
                [% foreach from=$MailAccount.errors item=Error %]
                    <div class="alert alert-warning">[% $Error %]</div>
                [% /foreach%]
            [% /if %]


            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Mail-Server') %]</label>
                <div class="col-sm-8">
                    <input type="text" name="MailAccount[mailserver]" id="M2TMailserver" value="[% $MailAccount.mailserver %]" [% if $MailAccount.mailserver_error %]class="textboxerror" title="[% $MailAccount.mailserver_error %]" [% else %]class="form-control"[% /if %] size="40" maxlength="300" />
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Mail-User') %]</label>
                <div class="col-sm-8">
                    <input type="text" name="MailAccount[mailuser]" value="[% $MailAccount.mailuser %]" [% if $MailAccount.mailuser_error %]class="textboxerror" title="[% $MailAccount.mailuser_error %]"[% else %]class="form-control"[% /if %] size="40" maxlength="300" />
                </div>
            </div>

            <div class="form-group row mb-3  NotForMSOfficeOAuth NotForMSGraphAPI">
                <label class="col-sm-4 control-label">[% $user->gettext('Passwort') %]</label>
                <div class="col-sm-8">
                    <input type="password" name="MailAccount[mailpassword]" value="[% if $MailAccount.mailpassword %]*********[% /if %]" placeholder="[% $user->gettext('Passwort eingeben')%]" [% if $MailAccount.mailpassword_error %]class="textboxerror" title="[% $MailAccount.mailpassword_error %]"[% else %]class="form-control"[% /if %] size="40" maxlength="300" />
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Mails auf Server belassen') %]</label>
                <div class="col-sm-8">
                    <input type="checkbox" name="MailAccount[leavemessages]" value="1" [% if $MailAccount.leavemessages == 1 %]checked="checked"[% /if %]/>
                </div>
            </div>


            <div class="form-group row mb-3 [% if $MailAccount.replyname_error %]has-error[% /if %]">
                <label class="col-sm-4 control-label">[% $user->gettext('Absendername') %]</label>
                <div class="col-sm-8">
                    <input type="text" name="MailAccount[replyname]" value="[% $MailAccount.replyname %]" class="form-control" maxlength="100" />
                    [% if $MailAccount.replyname_error %]<span class="help-block">[% $MailAccount.sendername_error %]</span>[% /if %]
                </div>
            </div>

            <div class="form-group row mb-3 [% if $MailAccount.replyaddress_error %]has-error[% /if %]">
                <label class="col-sm-4 control-label">[% $user->gettext('Absenderadresse') %]</label>
                <div class="col-sm-8">
                    <input type="email" name="MailAccount[replyaddress]" value="[% $MailAccount.replyaddress %]" class="form-control" maxlength="100" />
                    [% if $MailAccount.replyaddress_error %]<span class="help-block">[% $MailAccount.senderaddress_error %]</span>[% /if %]
                </div>
            </div>

            <div class="form-group row mb-3 [% if $MailAccount.groupid_error %]has-error[% /if %]">
                <label class="col-sm-4 control-label">[% $user->gettext('Gruppe') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[groupid]" class="form-control multiple" style="width: 100%">
                        [% foreach from=$SupporterGroups item=group key=GroupID %]
                            <option value="[% $GroupID %]" [% if $MailAccount.groupid == $GroupID %]selected="selected"[% /if %]>[% $user->gettext($group) %]</option>
                        [% /foreach %]

                    </select>
                    [% if $MailAccount.groupid_error %]<span class="help-block">[% $MailAccount.groupid_error %]</span>[% /if %]
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Supporter') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[supporterid]" [% if $MailAccount.supporterid_error %]class="textboxerror" title="[% $MailAccount.supporterid_error %]"[% else %]class="form-control multiple"[% /if %]  style="width: 100%">
                        [% foreach from=$Supporters item=supporter key=id %]
                            <option value="[% $id %]" [% if $MailAccount.supporterid == $id %]selected="selected"[% /if %]>[% $supporter %]</option>
                        [% /foreach %]
                    </select>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Standardkontakt') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[contactid]" id='Mail2TicketContact'[% if $MailAccount.contactid_error %]class="textboxerror" title="[% $MailAccount.contactid_error %]"[% else %]class="form-control"[% /if %]  style="width: 100%">
                        <option value='[% $M2TDefContactID %]' selected="selected">[% $M2TDefContactName %]</option>
                    </select>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Kategorie') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[categoryid]" id="CategorySelect" [% if $MailAccount.categoryid_error %]class="textboxerror" title="[% $MailAccount.categoryid_error %]"[% else %]class="form-control multiple"[% /if %]  style="width: 100%">
                        [% foreach from=$Categories item=category %]
                            <option value="[% $category.id %]" [% if $MailAccount.categoryid == $category.id %]selected="selected"[% /if %]>[% $category.name %]</option>
                        [% /foreach %]
                    </select>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Tickettyp') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[tickettypeid]" id="TypeSelect" [% if $MailAccount.typeid_error %]class="textboxerror" title="[% $MailAccount.typeid_error %]"[% else %]class="form-control multiple"[% /if %]  style="width: 100%">
                        [% foreach from=$TicketTypes item=type key=$id%]
                            <option value="[% $id %]" [% if $MailAccount.tickettypeid == $id %]selected="selected"[% /if %]>[% $user->gettext($type) %]</option>
                        [% /foreach %]
                    </select>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Mandant') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[mandantenid]" id="TypeSelect" [% if $MailAccount.mandanten_error %]class="textboxerror" title="[% $MailAccount.mandanten_error %]"[% else %]class="form-control multiple"[% /if %]  style="width: 100%">
                        [% foreach from=$Mandanten item=type key=$id%]
                            <option value="[% $id %]" [% if $MailAccount.mandantenid == $id %]selected="selected"[% /if %]>[% $user->gettext($type) %]</option>
                        [% /foreach %]
                    </select>
                    <span class="help-block">[% $user->gettext('Wenn ein Mandant durch das Postfach zugewiesen wird, wird nicht der Mandant des Absenders verwendet.') %]</span>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Diesen Server prüfen') %]</label>
                <div class="col-sm-8">
                    <input type="checkbox" name="MailAccount[active]" value="1" [% if $MailAccount.active == 1 %]checked="checked"[% /if %]/>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label" for="Mail2TicketMailDomain">[% $user->gettext('Emails nur von dieser Dom&auml;ne verarbeiten') %]</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="MailAccount[maildomain]" id="Mail2TicketMailDomain" value="[% $MailAccount.maildomain %]" size="30" />
                    <span class="help-block">[% $user->gettext('(falls leer - keine Beschr&auml;nkung)') %]. [% $user->gettext('Mehrere Domänen werden mit Semikolon getrennt eingetragen.') %]</span>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label" for="Mail2Ticket[MaildomainKeepInbox]">[% $user->gettext('Mails von nicht passenden Domains behalten') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[MaildomainKeepInbox]" value="1" [% if $MailAccount.maildomainkeepinbox == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[MaildomainKeepInbox]" value="0" [% if $MailAccount.maildomainkeepinbox == 0 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                    <span class="help-block">[% $user->gettext('Wenn aktiviert, werden Mails mit abweichender Absenderdomain nicht gelöscht, sondern lediglich als gelesen markiert.') %]</span>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Verbindungssicherheit') %]</label>
                <div class="col-sm-8">
                    <select size="1" name="MailAccount[secureconnection]" id="Mail2TicketSecureConnection" class="form-control multiple"  style="width: 100%">
                        <option value="0" [% if $MailAccount.secureconnection === 0 %]selected="selected"[% /if %]>[% $user->gettext('Keine') %]</option>
                        <option value="STARTTLS" [% if $MailAccount.secureconnection === 'STARTTLS' %]selected="selected"[% /if %]>[% $user->gettext('STARTTLS') %]</option>
                        <option value="TLS" [% if $MailAccount.secureconnection === 'TLS' %]selected="selected"[% /if %]>[% $user->gettext('SSL/TLS') %]</option>
                        <option value="MSOfficeOAuth" [% if $MailAccount.secureconnection === 'MSOfficeOAuth' %]selected="selected"[% /if %]>[% $user->gettext('MSOffice OAuth') %]</option>
                        <option value="MSGraphAPI" [% if $MailAccount.secureconnection === 'MSGraphAPI' %]selected="selected"[% /if %]>[% $user->gettext('MS Graph') %]</option>
                    </select>
                </div>
            </div>
                    
                    <div class="form-group row mb-3 MSOfficeOAuth">
                        <span class="help-block">[% $user->gettext('Bei der Verwendung von Microsoft Office 365 mit OAuth-Verfahren müssen über das Programm "OfficeTokenGenerator" im Heinzelmann-Service die benötigten Tokens für die verwendeten Mailadressen generiert werden.') %]</span>
                    </div>
                    <div class="form-group row mb-3  MSGraphAPI">
                        <span class="help-block">[% $user->gettext('Bei der Verwendung von Microsoft Office 365 mit OAuth-Verfahren müssen über das Programm "GraphTokenGenerator" im Heinzelmann-Service die benötigten Tokens für die verwendeten Mailadressen generiert werden.') %]</span>
                    </div>  
                    
            <div class="form-group row mb-3 MSOfficeOAuth MSGraphAPI">
                <label class="col-sm-4 control-label" for="Mail2TicketClientID">[% $user->gettext('Anwendungs-ID (Client)') %]</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="MailAccount[clientid]" id="Mail2TicketClientID" value="[% $MailAccount.clientid %]" size="30" />
                    <span class="help-block">[% $user->gettext('ApplikationID aus den Azure-Settings für die entsprechende Applikation') %]</span>
                </div>
            </div>
                
            <div class="form-group row mb-3 MSOfficeOAuth MSGraphAPI">
                <label class="col-sm-4 control-label" for="Mail2TicketTenantID">[% $user->gettext('Verzeichnis-ID (Mandant)') %]</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="MailAccount[tenantid]" id="Mail2TicketTenantID" value="[% $MailAccount.tenantid %]" size="30" />
                    <span class="help-block">[% $user->gettext('Mandanten-bzw. VerzeichnisID aus den Azure-Settings') %]</span>
                </div>
            </div>
                

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Antwort an Absender') %]</label>
                <div class="col-sm-8">
                    <label><input type="radio" name="MailAccount[responsetosender]" value="0" [% if $MailAccount.responsetosender != 1 && $MailAccount.responsetosender != 2 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]</label>
                    <label><input type="radio" name="MailAccount[responsetosender]" value="2" [% if $MailAccount.responsetosender == 2 %]checked="checked"[% /if %] /> [% $user->gettext('An den Absender') %]</label>
                    <label><input type="radio" name="MailAccount[responsetosender]" value="1" [% if $MailAccount.responsetosender == 1 %]checked="checked"[% /if %] /> [% $user->gettext('An alle Empfänger') %]</label>
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-5 control-label">[% $user->gettext('Verwendetes Template für ReplyMails') %]</label>
                <div class="col-sm-7">
                    <span class="input-group">
                        <select name="MailAccount[replymailtemplateid]" id="ReplyMailTemplateID" class="form-control" style="width: 100%">
                            [% html_options options=$ActiveReplyMailtemplates selected=$MailAccount.replymailtemplateid %]
                        </select>
                        <span class="input-group-text">
                            <a href="index.php?module=settings&amp;event=addMailTemplate" class="openInIFrameModal" data-fancybox-type="iframe" ><span class="fa fa-pencil"></span></a>                        
                        </span>                                    
                    </span>                                    
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Globale Kategoriezuordnung anwenden') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[catforsender]" value="1" [% if $MailAccount.catforsender == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[catforsender]" value="0" [% if $MailAccount.catforsender == 0 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                    <span class="help-block">[% $user->gettext('Hier können sie definieren, welche Kategorie 1 einem Ticket zugeordnet wird, wenn die eMail einen bestimmten Absender oder Betreff enthält. WICHTIG: Es wird erst der Betreff geprüft. Nur wenn dieser nicht in der Liste definiert ist, wird der Absender geprüft.') %]</span>
                </div>
            </div>


            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Globalen Betrefffilter anwenden') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[subjectfilter]" value="1" [% if $MailAccount.subjectfilter == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[subjectfilter]" value="0" [% if $MailAccount.subjectfilter == 0 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                </div>
            </div>
            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Generierte Mails nicht verarbeiten') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[checkautoreplys]" value="1" [% if $MailAccount.checkautoreplys == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[checkautoreplys]" value="0" [% if $MailAccount.checkautoreplys != 1 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                    <span class="help-block">[% $user->gettext('Mit dieser Option wird versucht, generierte Mails (Abwesenheitsnotizen, volle Postfächer o.ä. ) zu erkennen. Diese werden dann nicht vom Dienst verarbeitet.') %]</span>
                </div>
            </div>
            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Formatierungen aus HTML E-Mails anzeigen') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[useiframe]" value="1" [% if $MailAccount.useiframe == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[useiframe]" value="0" [% if $MailAccount.useiframe != 1 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                    <san class="help-block">[% $user->gettext('Die eMails werden als HTML-Datei gespeichert und mit den Formatierungen im Ticket angezeigt.') %]</san>
                </div>
            </div>
            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext('Nur Ticket-Bearbeitungen erlauben') %]</label>
                <div class="col-sm-8">
                    <input type="radio" name="MailAccount[ticketeditonly]" value="1" [% if $MailAccount.ticketeditonly == 1 %]checked="checked"[% /if %] /> [% $user->gettext('Ja') %]
                    <input type="radio" name="MailAccount[ticketeditonly]" value="0" [% if $MailAccount.ticketeditonly != 1 %]checked="checked"[% /if %] /> [% $user->gettext('Nein') %]
                    <span class="help-block">[% $user->gettext('Es werden nur TicketBEARBEITUNGEN per E-Mail durchgeführt. Es werden keine neuen Tickets angelegt.') %]</span>
                </div>
            </div>
                
            <div class="form-group row mb-3">
                <label class="col-sm-4 control-label">[% $user->gettext("Verwendetes Template für Antwort wenn 'Nur Ticket-Bearbeitungen erlauben' aktiv") %]</label>
                <div class="col-sm-8">
                    <span class="input-group">
                        <select name="MailAccount[replytpl4ticketeditonly]" id="ReplyTpl4TicketEditOnly" class="form-control" style="width: 100%">
                            [% html_options options=$ActiveReplyMailtemplates selected=$MailAccount.replytpl4ticketeditonly %]
                        </select>
                        <span class="input-group-text">
                            <a href="index.php?module=settings&amp;event=addMailTemplate" class="openInIFrameModal" data-fancybox-type="iframe" ><span class="fa fa-pencil"></span></a>                        
                        </span>                                    
                    </span>                                    
                </div>
            </div>               
                
                
        </div>
    </div>
</form>


[% if $reload %]
    <script>
        noty({
            text: '[% $Message %]',
            type: 'success',
            layout: 'top',
            //theme: 'bootstrapTheme',
            timeout: 3000,
            closeWith: ['click', 'hover']
        });
        setTimeout(
            function() 
            {
                parent.$("#myBigIFrameModal").modal("toggle");
            }, 3000);
        
    </script>
[% else %]
    <script>
        $(document).ready(function () {

            $(".multiple").select2({theme: 'bootstrap-5'});

            $("#CategorySelect").select2({theme: 'bootstrap-5'});
        })
    </script>
[% /if %]

<script>


        if ($("#Mail2TicketSecureConnection").val() === 'MSOfficeOAuth') {
            $('.NotForMSOfficeOAuth').hide();
        } else if($("#Mail2TicketSecureConnection").val() === 'MSGraphAPI') {
            $('.NotForMSGraphAPI').hide();
            $('#M2TMailserver').val('graph.microsoft.com').attr('readonly', true);
        } else {
            $('.NotForMSGraphAPI').hide();
            $('.NotForMSOfficeOAuth').show();
        }     

    $("#Mail2TicketContact").select2({theme: 'bootstrap-5',
        minimumInputLength: 1,
        ajax: {
            url: "index.php?module=contact&event=getJsonContacts",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data) {
                return {
                    results: data.items
                };
            }
        },
        escapeMarkup: function (markup) {
            return markup;
        }
    });

    $("#Mail2TicketSecureConnection").on('change', function (e) {

        if ($("#Mail2TicketSecureConnection").val() === 'MSOfficeOAuth') {
            $('.MSGraphAPI').hide();
            $('.MSOfficeOAuth').show();
            $('.NotForMSOfficeOAuth').hide();
            $('#M2TMailserver').val('smtp.office365.com').attr('readonly', true);            
        } else if ($("#Mail2TicketSecureConnection").val() === 'MSGraphAPI') {
            $('.MSOfficeOAuth').hide();
            $('.MSGraphAPI').show();
            $('.NotForMSGraphAPI').hide();
            $('#M2TMailserver').val('graph.microsoft.com').attr('readonly', true);
        } else {
            $('.NotForMSOfficeOAuth, .NotForMSGraphAPI').show();
            $('.MSOfficeOAuth').hide();
            $('.MSGraphAPI').hide();
            $('#M2TMailserver').attr('readonly', true);
        }        
    })
</script>

