<?= $this->extend('backend/layout/main'); ?>
<?= $this->section('title'); ?>Edit User<?= $this->endSection(); ?>
<?= $this->section('content'); ?>
<div class="dashboard-influence">
    <div class="container-fluid dashboard-content">
        <div class="row">
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                <div class="page-header">
                    <h3 class="mb-2">Edit User </h3>
                    <div class="page-breadcrumb">
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="<?= url_to('admin_home') ?>" class="breadcrumb-link">Dashboard</a></li>
                                <li class="breadcrumb-item"><a href="<?= url_to('users') ?>" class="breadcrumb-link">Users</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Edit</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                <div class="card">
                    <h5 class="card-header">Fill the form to update user details</h5>
                    <div class="card-body">
                        <?= view_cell('AlertCell') ?>
                        <?php $validation = service('validation'); ?>
                        <?= form_open(route_to('edit_profile', $user->id), [
                            'csrf_id' => 'csrf_id',
                            'novalidate' => '',
                            'data-parsley-validate' => '',
                            'id' => 'create-user-form'
                        ]) ?>
                        <div class="form-group row">
                            <?= form_label('First Name <span class="text-danger">*</span>', 'first_name', ['class' => 'col-12 col-sm-3 col-form-label text-sm-right']) ?>
                            <div class="col-12 col-sm-8 col-lg-6">
                                <?= form_input(
                                    [
                                        'name' => 'first_name',
                                        'id' => 'first_name',
                                        'required' => true,
                                        'placeholder' => 'Enter your first name',
                                        'class' => 'form-control',
                                    ],
                                    set_value('first_name', $user->first_name),
                                ) ?>
                                <p class="text-danger small pt-2"><?= $validation->getError('first_name') ?></p>
                            </div>

                        </div>

                        <div class="form-group row">
                            <?= form_label('Last Name <span class="text-danger">*</span>', 'last_name', ['class' => 'col-12 col-sm-3 col-form-label text-sm-right']) ?>
                            <div class="col-12 col-sm-8 col-lg-6">
                                <?= form_input(
                                    [
                                        'name' => 'last_name',
                                        'id' => 'last_name',
                                        'required' => true,
                                        'placeholder' => 'Enter your last name',
                                        'class' => 'form-control',
                                    ],
                                    set_value('last_name', $user->last_name),
                                ) ?>
                                <p class="text-danger small pt-2"><?= $validation->getError('last_name') ?></p>
                            </div>

                        </div>

                        <div class="form-group row">
                            <?= form_label('Email <span class="text-danger">*</span>', 'email', ['class' => 'col-12 col-sm-3 col-form-label text-sm-right']) ?>
                            <div class="col-12 col-sm-8 col-lg-6">
                                <?= form_input(
                                    [
                                        'name' => 'email',
                                        'id' => 'email',
                                        'data-parsley-type' => 'email',
                                        'required' => true,
                                        'placeholder' => 'Enter your email address',
                                        'class' => 'form-control',
                                    ],
                                    set_value('email', $user->email),
                                ) ?>
                                <p class="text-danger small pt-2"><?= $validation->getError('email') ?></p>
                            </div>

                        </div>

                        <div class="form-group row">
                            <label class="col-sm-3 col-form-label text-sm-right">Role</label>
                            <div class="col-sm-6">
                                <div class="custom-controls-stacked">
                                    <label class="custom-control custom-checkbox">
                                        <input id="role" name="role" type="checkbox" <?php if ($user->role == 'Admin'): ?> checked <?php endif; ?> value="Admin" data-parsley-errors-container="#error-container1" class="custom-control-input">
                                        <span class="custom-control-label">Admin</span>
                                    </label>
                                    <p class="text-danger small pt-2"><?= $validation->getError('role') ?></p>
                                </div>

                            </div>

                        </div>
                        <?= form_hidden('id', $user->id) ?>
                        <div class="form-group row text-right">
                            <div class="col col-sm-10 col-lg-9 offset-sm-1 offset-lg-0">
                                <?= form_button(
                                    [
                                        'type' => 'submit',
                                        'class' => 'btn btn-space btn-primary',
                                    ],
                                    'Validate and Submit',
                                ) ?>
                                <a href="<?= url_to('users') ?>" class="btn btn-space btn-secondary">Cancel</a>
                            </div>
                        </div>
                        <?= form_close(); ?>
                    </div>
                </div>
            </div>

        </div>


    </div>
</div>
<?= $this->endSection(); ?>

<?= $this->section('js'); ?>
<script src="<?= base_url() ?>assets/backend/vendor/parsley/parsley.js"></script>
<?= $this->endSection(); ?>

<?= $this->section('scripts'); ?>
<script>
    $('#create-user-form').parsley();
    (function() {
        'use strict';
        window.addEventListener('load', function() {
            // Fetch all the forms we want to apply custom Bootstrap validation styles to
            var forms = document.getElementsByClassName('needs-validation');
            // Loop over them and prevent submission
            var validation = Array.prototype.filter.call(forms, function(form) {
                form.addEventListener('submit', function(event) {
                    if (form.checkValidity() === false) {
                        event.preventDefault();
                        event.stopPropagation();
                    }
                    form.classList.add('was-validated');
                }, false);
            });
        }, false);
    })();
</script>
<?= $this->endSection(); ?>