<?php


$cekID = new cekID();
$jmldata = $cekID->cek('user', 'id', $id_page);


$clsCrud = new crudData();
$editUser = $clsCrud->editData('user', 'id', $id_page);
$data = $editUser->fetch(PDO::FETCH_OBJ);

?>
<style>
    .fontInput {
        font-size: 11px;
        font-family: arial;
    }
</style>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>

    <div class="container col-12 mb-4 pb-5">
        <div class="row">
            <div class="col-12">
                <div class="col-12">
                    <div class="card  mx-3">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-person-plus"></span> Edit User Pengelola</b>
                        </div>
                        <div class="card-body mb-4">
                            <form action="" method="post">
                                <div class="row mx-3 mt-3" style="background-color: gainsboro;">
                                    <div class="col-md-6">
                                        <div class="card-body mt-4 px-2 mx-3">
                                            <div class="row">
                                                <label for="exampleEmail" class="col-sm-3 col-form-label">Nama User </label>
                                                <div class="col-sm-7">
                                                    <input type="text" name="nama" value="<?php echo $data->nama; ?>" class="form-control form-control-sm fontInput" placeholder="Nama User">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Jenis Kelamin</label>
                                                <div class="col-md-7">
                                                    <select name="kelamin" class="form-control form-control-sm fontInput small" required>
                                                        <option value="<?php echo $data->kelamin; ?>">
                                                            <?php if ($data->kelamin == 'L') {
                                                                echo "Laki-laki";
                                                            } else {
                                                                echo "Perempuan";
                                                            }; ?></option>
                                                        <option value="L">Laki-laki</option>
                                                        <option value="P">Perempuan</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="nis" class="col-sm-3 col-form-label">No. Telpon/WA</label>
                                                <div class="col-sm-7">
                                                    <input type="text" class="form-control form-control-sm fontInput" value="<?php echo $data->hp; ?>" placeholder="No Telpon User" name="hp">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label for="nis" class="col-sm-3 col-form-label">Email</label>
                                                <div class="col-sm-7">
                                                    <input type="email" class="form-control form-control-sm fontInput" value="<?php echo $data->email; ?>" placeholder="email@mail.com" name="email">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="card-body mt-4 px-2 mx-3">
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Username</label>
                                                <div class="col-md-7">
                                                    <input type="text" class="form-control form-control-sm fontInput" name="username_user" value="<?php echo $data->username; ?>" placeholder="Username">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Password</label>
                                                <div class="col-md-7">
                                                    <input type="password" class="form-control form-control-sm fontInput" name="password_user" value="passwrod" placeholder="Password" disabled>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Level User</label>
                                                <div class="col-md-7">
                                                    <select name="level_user" class="form-control form-control-sm fontInput small" required>
                                                        <option value="<?php echo $data->level; ?>"><?php echo ucfirst($data->level); ?></option>
                                                        <option value="admin">Admin</option>
                                                        <option value="teller">Teller</option>
                                                        <option value="bendahara">Bendahara</option>
                                                        <option value="monitoring">Monitoring</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <label class="col-sm-3 col-form-label">Status User</label>
                                                <div class="col-md-7">
                                                    <select name="status" class="form-control form-control-sm fontInput small" required>
                                                        <option value="<?php echo $data->status; ?>"><?php echo $data->status; ?></option>
                                                        <option>Aktif</option>
                                                        <option>Non Aktif</option>

                                                    </select>
                                                </div>
                                            </div>

                                        </div>
                                    </div>

                                </div>
                                <div class="my-2 pl-3 py-2 mx-3" style="background-color: gainsboro;">
                                    <div class="row">
                                        <div class="col-auto">
                                            <a <?php echo $link->page('seting/userAdmin/user_read'); ?> class="btn btn-primary btn-xs float-right mx-2"><i class="bi-arrow-left-circle"></i> Cancel</a>
                                            <button type="submit" class="btn btn-primary btn-xs float-right" name="submit"><i class="bi-save"></i> Simpan</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<?php


if (isset($_POST['submit'])) {

    $nama = htmlspecialchars($_POST['nama']);
    $kelamin = htmlspecialchars($_POST['kelamin']);
    $email = htmlspecialchars($_POST['email']);
    $hp = htmlspecialchars($_POST['hp']);
    $username_user = htmlspecialchars($_POST['username_user']);
    $id_user = md5($_POST['nama']);
    $level_user = htmlspecialchars($_POST['level_user']);
    $status = htmlspecialchars($_POST['status']);

    $user_admin = new crudData();

    $dataarray = [

        'nama' => $nama,
        'kelamin' => $kelamin,
        'email' => $email,
        'hp' => $hp,
        'username' => $username_user,
        'level' => $level_user,
        'status' => $status,
        'id' => $id_user
    ];
    $update = $user_admin->updateData('user', $dataarray, ['id', ' = ', $id_page]);

    $direct = new toast();
    echo  $direct->redirect('toast', 'update', 'seting/userAdmin/user_read');
} else {
    echo 'Error';
}

?>