<div class="content-wrapper">
    <div class="content-header bg-link bg-primary">
        <div class="container-fluid">
            <div class="row mb-2" mb-2>
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Update User</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item active"><a class="text-dark" href="">Home /</a></li>
                        <li class="breadcrumb-iten active"><a class="text-dark" href="">Update User</a></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <?php

    $id = $_GET['idedit'];
    $dataedit = $koneksi->query("SELECT * FROM tbl_user a JOIN tbl_role b ON a.role_id = b.role_id WHERE user_id = '$id' ")->fetch_assoc();
    ?>


    <div class="card">
        <di class="card-header">
            <h1>Update Data User</h1>
        </di>
        <div class="card-body">

            <form action="" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Nama</label>
                    <input type="text" name="nama" class="form-control" value="<?= $dataedit['user_nama']; ?>">
                </div>
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" class="form-control" value="<?= $dataedit['user_username']; ?>">
                </div>
                <div class="form-group">
                    <label>Password Admin</label>
                    <input type="password" name="pass" class="form-control" value="<?= $dataedit['user_password']; ?>">
                </div>
                <div class="form-group">
                    <label>Lever User</label>
                    <select name="level" class="form-control">
                        <option value="">Silahkan Pilih</option>
                        <?php

                        $datalevel = $koneksi->query("SELECT * FROM tbl_role");

                        foreach ($datalevel as $pecah) : ?>
                            <option <?php echo $dataedit['role_id'] == $pecah['role_id'] ? 'selected' : '' ?> value="<?= $pecah['role_id'] ?>"><?= $pecah['role_nama'] ?></option>
                        <?php endforeach; ?>

                    </select>
                </div>
                <button type="submit" name="edit" class="btn btn-warning btn-block">Simpan</button>
            </form>


            <?php

            if (isset($_POST['edit'])) {
                $nama     =  $_POST['nama'];
                $username =  $_POST['username'];
                $pass     =  $_POST['pass'];
                $role_id    = $_POST['level'];


                $edit =  $koneksi->query("UPDATE tbl_user SET user_nama ='$nama',
                        user_username = '$username',
                        user_password = '$pass',
                        role_id = '$role_id' WHERE user_id ='$id'");


                if ($edit == TRUE) {
                    echo "<script>
                alert ('Data Berhasil Di Simpan');
                window.location= '?page=pages/user/view'
                </script>";
                } else {
                    echo "<script>
                alert ('Data Gagal Di Simpan');
                window.location= '?page=pages/user/edit&idedit=" . $id . "'
            </script>";
                }
            }

            ?>








        </div>