
<form action="" method="post" name="hm"  id="hm" enctype="multipart/form-data" class="form-horizontal form-label-left">
    <input type="hidden" name="creating" value="1">
    <div class="card panel-fcs-border shadow-sm">
        <div class="card-header">
            <h3 class="card-title">[% $user->gettext('Neu Registrieren') %]</h3>
        </div>
        <div class="card-body">
            [% if $UserCreated == 1 %]
                <div class="alert alert-success">
                    [% $user->gettext('Ihre Zugangsdaten wurden an Ihre E-Mail-Adresse gesendet.') %]
                </div>
            [% /if %]
            [% if $UserCreated == 2 %]
                <div class="alert alert-error">
                    [% $user->gettext('Ihre Zugang konnte nicht eingerichtet werden. Bitte wenden Sie sich ggf. an den Administrator.') %]
                </div>
            [% /if %]
            [% if $UserCreated == 3 %]
                <div class="alert alert-warning">
                    [% $user->gettext('Der Benutzer wurde angelegt, aber Ihre Zugangsdaten konnten nicht an die angegebene E-Mail-Adresse gesendet werden.') %]
                </div>
            [% /if %]

            <div class="card panel-info shadow-sm rounded">
                <div class="card-body">
                    <div class="btn-group">
                        [% if $UserCreated != 1 && $UserCreated != 2 %]
                            <input type="submit" value="[% $user->gettext('Registrieren') %]" class="btn btn-outline-primary">
                        [% /if %]
                        <input type="button" value="[% $user->gettext('Zurück') %]" onclick="window.location.href = 'index.php';" class="btn btn-outline-secondary">
                    </div>
                </div>
            </div>
            [% if $UserCreated != 1 && $UserCreated != 2 %]
                <div class="card panel-info shadow-sm rounded">
                    <div class="card-header">
                        <h3 class="card-title"></h3>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            [% if $Error.Name == 1 || $Error.Username == 1 %]
                                [% if $Error.Name == 1 %]
                                    <div class="alert alert-danger">[% $user->gettext('Es wurde kein Name angegeben') %]</div>
                                [% /if %]
                                [% if $Error.Username == 1 %]
                                    <div class="alert alert-danger">
                                        [% if $contact.username != "" %]
                                            [% $user->gettext('Der Benutzername wird bereits verwendet') %]
                                        [% else %]
                                            [% $user->gettext('Es wurde kein Benutzername angegeben') %]
                                        [% /if %]
                                    </div>
                                [% /if %]
                                [% if $Error.Company == 1 %]
                                    <div class="alert alert-danger">
                                            [% $user->gettext('Es wurde keine Firma gewählt') %]
                                    </div>
                                [% /if %]
                            [% /if %]

                            <div class="col-md-6">

                            <div class="form-group row mb-3" id = "company-name">
                                <label class="col-lg-4 col-sm-4 control-label">
                                    [% $user->gettext('Firma') %] [% if $isCompanyRequired %]*[% /if %]
                                </label>
                                <div class="col-lg-8 col-sm-8">
                                    <div class="input-group">
                                        <select class="form-control" id="CompanyID" name="CompanyID"  tabIndex="1" [% if $isCompanyRequired %]required[% /if %]>
                                            [% html_options options=$Companys selected=$contact.companyid %]
                                        </select>
                                        <a class="btn btn-outline-secondary">
                                            <span class="fa fa-plus" data-bs-toggle="collapse" href="#NewCompanyField" title="[% $user->gettext('Neue Firma anlegen')%]"></span>
                                        </a>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group row mb-3 collapse" id="NewCompanyField">
                                <label class="col-lg-4 col-sm-4 control-label">[% $user->gettext('Neue Firma anlegen') %]</label>
                                <div class="col-lg-8 col-sm-8">

                                    <div class="input-group">
                                        <input type="text" class="form-control" name="newcompany" id="newcompany" />
                                        <span class="input-group-text">
                                            <span class="fa fa-floppy-disk" title="[% $user->gettext('Neue Firma speichern') %]" id="newcompanysave" />
                                        </span>
                                    </div>
                                    <div class="help-block" id="NewCompanyMessage" style="display:none;">[% $user->gettext('Die Firma wurde angelegt') %]</div>
                                    <div class="help-block" id="NewCompanyErrMessage" style="display:none;">[% $user->gettext('Die Firma konnte nicht angelegt werden.') %]</div>
                                </div>
                            </div>
                                <div class="form-group row mb-3 [% if $Error.Name == 1 %]has-error[% /if %]">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Name') %] *</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="name" value="[% $contact.name %]"  class="form-control required" tabindex="2" required>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Position') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="com_position" value="[% $contact.com_position %]"  class="form-control" tabindex="8">
                                    </div>
                                </div>
                                <div class="form-group row mb-3 [% if $Error.Username == 1 %]has-error[% /if %]">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Username') %] *</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="username" value="[% $contact.username %]"  class="form-control required" tabindex="9" required="required">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <hr/>



                        <div class="row">

                            [% if $Error.EMail == 1 %]
                                <div class="alert alert-danger">
                                    [% if $contact.email != "" %]
                                        [% $user->gettext('Keine gültige E-Mail-Adresse') %]
                                    [% else %]
                                        [% $user->gettext('Es wurde kein E-Mail-Adresse angegeben') %]
                                    [% /if %]
                                </div>
                            [% /if %]
                            <div class="col-md-6">
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Straße') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="street" value="[% $contact.street %]"  class="form-control" tabindex="3">
                                    </div>
                                </div>
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Plz / Ort') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="plz" value="[% $contact.plz %]" class="form-control" tabindex="4" style="width:40%;">
                                        <input type="text" name="city" value="[% $contact.city %]" class="form-control" tabindex="5" style="width:60%;">
                                    </div>
                                </div>

                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Bundesland') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="state" value="[% $contact.state %]"  class="form-control" tabindex="6">
                                    </div>
                                </div>
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Land') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="country" value="[% $contact.country %]"  class="form-control" tabindex="7">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Telefon') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="phone" value="[% $contact.phone %]"  class="form-control" tabindex="10">
                                    </div>
                                </div>
                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Mobil') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="mobil" value="[% $contact.mobil %]"  class="form-control" tabindex="11">
                                    </div>
                                </div>

                                <div class="form-group row mb-3">
                                    <label class="col-lg-4 control-label">[% $user->gettext('Fax') %]</label>
                                    <div class="col-lg-8">
                                        <input type="text" name="fax" value="[% $contact.fax %]"  class="form-control" tabindex="12">
                                    </div>
                                </div>
                                <div class="form-group row mb-3 [% if $Error.EMail == 1 %]has-error[% /if %]">
                                    <label class="col-lg-4 control-label">[% $user->gettext('E-Mail') %] * </label>
                                    <div class="col-lg-8">
                                        <input type="text" name="email" value="[% $contact.email %]"  class="form-control required" tabindex="13" required>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group row mb-3">
                                    <label class="col-lg-2 control-label">[% $user->gettext('Zusätzliche Informationen') %]</label>
                                    <div class="col-lg-10">
                                        <textarea class="form-control" style="width:100%;height: 20em;" tabindex="14" name="misc">[% $contact.misc %]</textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            [% /if %]
        </div>
    </div>
</form>

<script>
    $('#CompanyID').select2({theme: 'bootstrap-5',
        minimumInputLength: 1,
        [% if $PopUp === true %]dropdownParent: $('#LargeModal'),[% /if %]
        ajax: {
            delay: 250,
            url: 'index.php?module=login&event=getCompaniesJson',
            data: function (params) {
                var query = {
                    search: params.term,
                    type: 'public'
                }

                // Query parameters will be ?search=[term]&type=public
                return query;
            }
        }
    });

    $('#NewCompanyIcon, #NewCompanyLable').on("click", function () {
        $('#NewCompanyForm').toggle();
    });
    $('#newcompany').keypress(function (e) {
        if (e.which == 13)
            $('#newcompanysave').trigger("click");
    });
    $('#newcompanysave').on("click", function () {
        $.getJSON("index.php?module=login&event=jsonStoreCompany", {newcompany: $('#newcompany').val()}, function (json) {
            //alert("JSON Data: " + json.CompanyID);
            if (json.CompanyIsNew) {
                $('#CompanyID').append($("<option></option>").attr("value", json.CompanyID).text(json.CompanyName));
                $('#NewCompanyMessage').show();
            }
            $('#CompanyID').val(json.CompanyID);
        });
    });
</script>
