    [% META title = 'User &raquo; Register New User' -%]
<!-- BEGIN [% template.name %] -->

    [% # PROCESS dumper.tt dump = errs %]
    [% # PROCESS dumper.tt dump = registered_users %]
    [% # PROCESS dumper.tt dump = user_details %]
    [% # PROCESS dumper.tt dump = input_params %]
    [% # PROCESS dumper.tt dump = user_details.last_name.remove("\\\'") %]

    <h2>Registration Details</h2>

    [% IF errs; # INCLUDE dumper.tt dump = errs; %]
        <p class="error">[% c.cfg('msg').dfv_errors %]</p>
    [% ELSE %]
        <p class="indent">Usage: preview message -> post message.</p>
        <div><strong>Paste Application Details:</strong></div>
    [% END %]

    <form method="post" action="[% app_url %]/admin/user/new_user"
        name="new_user" id="new_user" enctype="application/x-www-form-urlencoded">
        <div>
          <textarea name="src_data" rows=8
            cols=80>[% c.query.param('src_data') # DO NOT LINE BREAK %]</textarea>
        </div>

        [% IF user_groups AND NOT new_user_create_success %]
        <p class="bold">User group:
            <select name="group_id" id="user_group">
                <option value="">&nbsp;</option>
                [% FOREACH group IN user_groups %]
                <option value="[% group.id %]" [%
                    'selected' IF group.group_name.match('guest') # default to %]>
                    [% group.group_label %]
                </option>
                [% END %]
             </select>
            <span class="red">&#171; select one</span>
        </p>
        [% END %]

        <p class="indent">
            [% IF new_user_create_success %]
                <a href="[% app_url %]/admin/user/new_user">create new user &#187;</a>
            [% ELSE %]
                <input type="submit" name="preview" value="Preview" />
                [% IF user_details AND NOT errs;
                        class = has_role ? 'normal' : 'red' %]
                    <input type="hidden" value="[% user_details.user_location_id %]"
                        name="user_location_id" />
                    <input type="submit" name="post" value="Post" class="[% class %]" />
                [% ELSE %]
                    <input type="submit" value="Post" disabled />
                [% END %]
                <input type="button" value="Clear"
                    onclick="document.new_user.src_data.value = '';" />
                [% IF user_details AND NOT has_role %]
                    <span class="red">check spelling/case of 'position' entry</span>
                [% END %]
            [% END %]
        </p>
    </form>

    [% IF user_details; # PROCESS dumper.tt dump = preview %]
        <h3 class="header">Preview:</h3>
        [% IF registered_users.size; # have potential duplicates:
            cols = ['username' 'last_name' 'first_name' 'email'];
            BLOCK row;
                class = user_details.${col} == user.${col} ? 'red' : 'normal';
            %]
                <td class="[% class %]">[% user.${col} %]</td>
            [% END %]

            <h4 class="indent">Registered users with similar username:</h4>
            <table class="indent">
                <tr>
                    <th>username</th>
                    <th>last name</th>
                    <th>first name</th>
                    <th>email</th>
                    <th>location</th>
                    <th>last login</th>
                    <th>active</th>
                </tr>
                [% FOREACH user IN registered_users %]
                    <tr>
                        [% FOREACH col IN cols; INCLUDE row; END %]
                        <td>[% user.user_location.location_name %]</td>
                        <td>[% user.last_login.strftime('%d.%b.%Y %H:%M') %]</td>
                        <td>[% user.active %]</td>
                    </tr>
                [% END %]
            </table>
        [% END %]

        <div style="width: 80%; border: solid thin; margin: 1em; padding: 1em;">
            <table class="results">
                <tr>
                    <td class="label">UserID:</td>
                    <td>
                        [% userid = user_details.username || input_params.username | upper;
                            IF userid; userid; ELSE %]
                        <span class="red">CANNOT CREATE USERNAME</span>[% END %]
                        [% errs.error_username %]
                    </td>
                </tr>
                <tr>
                    <td class="label">LastName:</td>
                    <td>
                        [% ( user_details.last_name || input_params.last_name ) | ucfirst %]
                        [% IF errs.error_last_name %]
                          <span class="red">[% errs.error_last_name %]</span>
                        [% END %]
                    </td>
                </tr>
                <tr>
                    <td class="label">FirstName:</td>
                    <td>[% ( user_details.first_name || input_params.first_name ) | ucfirst %]
                        [% IF errs.error_first_name %]
                            <span class="red">[% errs.error_first_name %] </span>
                        [% END %]
                    </td>
                </tr>
                [% IF errs.error_full_name # only needed if entry already exists %]
                <tr>
                    <td class="label">FullName:</td>
                    <td>
                        [% ( user_details.first_name || input_params.first_name ) | ucfirst %]
                        [% ( user_details.last_name  || input_params.last_name )  | ucfirst %]
                        <span class="red">[% errs.error_full_name %] </span>
                    </td>
                </tr>
                [% END %]
                <tr>
                    <td class="label">Email:</td>
                    <td>
                        [% email = user_details.email || input_params.email;
                            email %]
                        [% IF errs.error_email; errs.error_email;
                            ELSIF NOT email.match('nhs.(net|uk)$') %]
                            <span class="red">&#171; check email belongs to
                              valid domain</span>
                        [% END %]
                    </td>
                </tr>
                <tr>
                    [% class = user_details.user_location_id ? 'normal' : 'red' %]
                    <td class="label">Location:</td>
                    <td class="[% class %]">
                        [% user_details.location || input_params.location %]
                        [% IF errs.error_user_location_id.match('missing') %]
                            &#171; unknown user location
                        [% END %]
                    </td>
                </tr>
                <tr>
                    <td class="label">Designation:</td>
                    [% class = has_role OR new_user_create_success ? 'normal' : 'red' %]
                    <td class="[% class %]">
                        [% user_details.designation || input_params.designation %]
                        [% UNLESS has_role OR new_user_create_success %]
                            &#171; does not exist
                        [% END %]
                    </td>
                </tr>
                <tr>
                    <td class="label">Service:</td>
                    <td>
                        [% user_details.service || input_params.service # deleted if invalid %]
                        [% errs.error_service %]
                    </td>
                </tr>
            </table>

            [% UNLESS errs %]
                <hr />
                <pre>[% INCLUDE admin/user/new_user_msg.tt | html %]</pre>
            [% END %]
        </div>
    [% END %]
<!-- END [% template.name %] -->
