<div class="row-fluid">
    <div class="pageTitle">
        <h1 class="display-inline-block"><span>Your profile</span></h1>
    </div>

    <div id="error_status"></div>

    <div id="normal-layout">

        <?php if (empty($user)) { ?>
        <p>We don't have any information about you.</p>
        <?php
          }
        else
         { ?>

         <table class="table table-bordered profile">
                <tr>
                    <td>User name</td>
                    <td id="username"><?= htmlspecialchars($user['id']) ?></td>
                </tr>
               <?php if ($this->session->userdata('mode') == 'internal') { ?>
                <tr>
                    <td>Email</td>
                    <td><?= htmlspecialchars($user['email']) ?></td>
                </tr>

                <?php } ?>
                <tr>
                    <td>Role memberships</td>

                    <td><div style="position: relative">
                            <div class="itemwrapper assigneditems">

                                <?php if (!empty($user['roles'])) {
                                    $i = 1;
                                    $first = true;
                                    $first_item_class = ' class="first"';


                                    echo '<ul id="roles" class="unstyled itemlist assignedList">';

                                    foreach ($user['roles'] as $item) {
                                        echo '<li ' . ($first === TRUE ? $first_item_class : '') . ' >';
                                        echo $item;
                                        echo "</li>";
                                        $i++;
                                        $first = FALSE;
                                    }

                                    echo '</ul>';
                                } else { ?>
                                    <div class="muted">No roles assigned</div>
                                <?php } ?>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
	    <?php if ($this->session->userdata('mode') == 'internal') { ?>
            <a data-toggle="modal" class="change_password" title="change password" id="<?= htmlspecialchars($user['id']) ?>">Change password</a>
        <?php } ?>

        <div>
             <div id="passwd_form" style="display:none"></div>
        </div>
        <?php } ?>
    </div>

</div>
<script type="text/javascript">

$('.change_password').click(function() {
    var userModalBody = {
        'header':'<h2>Change password</h2>',
        'body_url': MP_SITE_URL + '/auth/password_form',
        'onEscape': function(){}
    };
    $("<div></div>")
        .dialog_modal({
            passedArgs: ['',[],userModalBody],
            addBodyClass: 'form-buttons'
        })
});



 var options = {
    target:  '#admin_content',  // target element(s) to be updated with server response
    dataType: 'json',
    success: function (responseObj, statusText, xhr, $form) {
       $("#formInfoMessage").find('.alert').remove();

       if(responseObj.responseText!=undefined && responseObj.responseText.type!=undefined){
            $("#formInfoMessage").append('<div class="alert '+responseObj.responseText.type+'"><button class="close" data-dismiss="alert"><i class="bi bi-x"></i></button>'+ responseObj.responseText.content+'</div>');
            $("#formInfoMessage").show();
         }

        if(responseObj.message!=undefined){
            var success = false;
            $.each(responseObj.message,function(type,content){
                if ((type==="alert-error")||(type==="alert-warning")) {
                  $("#formInfoMessage").append('<div class="alert '+type+'"><button class="close" data-dismiss="alert"><i class="bi bi-x"></i></button>'+content+'</div>')
                  $("#formInfoMessage").show();
                } else {
                    $('#error_status').append('<div class="alert '+type+'"><button class="close" data-dismiss="alert"><i class="bi bi-x"></i></button>'+content+'</div>');
                    success = true;
                }
            });

            if (success===true) {$('.bootbox').modal('hide')};
         }

         if(responseObj.status!=undefined && responseObj.status=='all_ok'){
           $('#admin_content').html(responseObj.responseText);
           $('#edit_form_wrapper').remove();
         }

          window.scrollTo(null, ($('#formInfoMessage').position()));
        return;
    }
};



$('#change_password').on('submit',function(event){
    event.preventDefault();
    $(this).ajaxSubmit(options);
});

</script>
