{{define "title"}}Update User - {{ .user.Name }}{{end}}
{{define "style"}}

{{end}}
{{define "content"}}

    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="/users">Users</a></li>
            <li class="breadcrumb-item"><a href="{{ .urlUsersView }}">{{ .user.Name }}</a></li>
            <li class="breadcrumb-item active" aria-current="page">Update</li>
        </ol>
    </nav>

    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Update User</h1>
    </div>

    <form class="user" method="post" novalidate>
        <div class="card shadow">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-12">
                        <h4 class="card-title">User Details</h4>
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="inputFirstName">First Name</label>
                            <input type="text"
                                   class="form-control {{ ValidationFieldClass $.validationErrors "UserUpdateRequest.FirstName" }}"
                                   placeholder="enter first name" name="FirstName" value="{{ .form.FirstName }}" required>
                            {{template "invalid-feedback" dict "fieldName" "UserUpdateRequest.FirstName" "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors }}
                        </div>
                        <div class="form-group">
                            <label for="inputLastName">Last Name</label>
                            <input type="text"
                                   class="form-control {{ ValidationFieldClass $.validationErrors "UserUpdateRequest.LastName" }}"
                                   placeholder="enter last name" name="LastName" value="{{ .form.LastName }}" required>
                            {{template "invalid-feedback" dict "fieldName" "UserUpdateRequest.LastName" "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors }}
                        </div>
                        <div class="form-group">
                            <label for="inputEmail">Email</label>
                            <input type="text"
                                   class="form-control {{ ValidationFieldClass $.validationErrors "UserUpdateRequest.Email" }}"
                                   placeholder="enter email" name="Email" value="{{ .form.Email }}" required>
                            {{template "invalid-feedback" dict "fieldName" "UserUpdateRequest.Email" "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors }}
                        </div>
                        <div class="form-group">
                            <label for="inputTimezone">Timezone</label>
                            <select class="form-control {{ ValidationFieldClass $.validationErrors "UserUpdateRequest.Timezone" }}" name="Timezone">
                                <option value="">Not set</option>
                                {{ range $idx, $t := .timezones }}
                                    <option value="{{ $t }}" {{ if CmpString $t $.form.Timezone }}selected="selected"{{ end }}>{{ $t }}</option>
                                {{ end }}
                            </select>
                            {{template "invalid-feedback" dict "fieldName" "UserUpdateRequest.Timezone" "validationDefaults" $.validationDefaults "validationErrors" $.validationErrors }}
                        </div>
                        <div class="form-group">
                            <label for="inputRoles">Roles</label>
                            <span class="help-block "><small>- Select at least one role.</small></span>
                            {{ range $r := .roles.Options }}
                                <div class="form-check">
                                    <input class="form-check-input {{ ValidationFieldClass $.validationErrors "Roles" }}"
                                           type="checkbox" name="Roles"
                                           value="{{ $r.Value }}" id="inputRole{{ $r.Value }}"
                                           {{ if $r.Selected  }}checked="checked"{{ end }}>
                                    <label class="form-check-label" for="inputRole{{ $r.Value }}">
                                        {{ $r.Title }}
                                    </label>
                                </div>
                            {{ end }}
                            {{template "invalid-feedback" dict "fieldName" "Roles" "validationDefaults" $.validationDefaults "validationErrors" $.validationErrors }}
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <input id="btnSubmit" type="submit" value="Save" class="btn btn-primary"/>
                        <a href="/users/{{ .user.ID }}" class="ml-2 btn btn-secondary" >Cancel</a>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <form class="user" method="post" novalidate>
        <div class="card mt-4">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-12">
                        <h4 class="card-title">Change Password</h4>
                        <p><small><b>Optional</b>. You can change the users' password by specifying a new one below. Otherwise leave the fields empty.</small></p>
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-md-6">
                       <div class="form-group">
                            <label for="inputPassword">Password</label>
                            <input type="password" class="form-control" id="inputPassword" placeholder="" name="Password" value="">
                            <span class="help-block "><small><a a href="javascript:void(0)" id="btnGeneratePassword"><i class="fas fa-random mr-1"></i>Generate random password </a></small></span>
                            {{template "invalid-feedback" dict "validationDefaults" $.passwordValidationDefaults "validationErrors" $.validationErrors "fieldName" "Password" }}
                        </div>
                        <div class="form-group">
                            <label for="inputPasswordConfirm">Confirm Password</label>
                            <input type="password" class="form-control" id="inputPasswordConfirm" placeholder="" name="PasswordConfirm" value="">
                            {{template "invalid-feedback" dict "validationDefaults" $.passwordValidationDefaults "validationErrors" $.validationErrors "fieldName" "PasswordConfirm" }}
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <input id="btnSubmit2" type="submit" name="action" value="Change Password" class="btn btn-primary btn-sm"/>
                    </div>
                </div>
            </div>
        </div>
    </form>
{{end}}
{{define "js"}}
    <script>
        function randomPassword(length) {
            var chars = "abcdefghijklmnopqrstuvwxyz!@#&*()-+<>ABCDEFGHIJKLMNOP1234567890";
            var pass = "";
            for (var x = 0; x < length; x++) {
                var i = Math.floor(Math.random() * chars.length);
                pass += chars.charAt(i);
            }
            return pass;
        }

        $(document).ready(function(){
            $("#btnGeneratePassword").on("click", function() {
                pwd = randomPassword(12);
                $("#inputPassword").attr('type', 'text').val(pwd)
                $("#inputPasswordConfirm").attr('type', 'text').val(pwd)
                return false;
            });
        });
    </script>
{{end}}
