<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">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-item"><a class="text-dark" href="">User</a></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>




    <div class="card px-2">
        <div class="card-header">
            <h1>Tabel User</h1>
        </div>

        <div class="card-boby">
            <div class="table-responsive">

                <button data-toggle="modal" title=" Tambah Data" data-target="#tambah" class="btn btn-primary mb-5"><i class="fa fa-plus"></i></button>

                <table class="table table-bordered" id='example1'>
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Nama</th>
                            <th>Usernama</th>
                            <!-- <th>Password</th> -->
                            <th>Level User</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>

                        <?php

                        $datauser = $koneksi->query("SELECT * FROM tbl_user a JOIN tbl_role b ON a.role_id = b.role_id");
                        // print_r($datauser);
                        // exit;


                        foreach ($datauser as $no => $pecah) :

                        ?>
                            <tr>
                                <td><?= ++$no ?></td>
                                <td><?= $pecah['user_nama'] ?></td>
                                <td><?= $pecah['user_username'] ?></td>
                                <!-- <td><?= $pecah['user_password'] ?> </td> -->
                                <td><?= $pecah['role_nama'] ?></td>
                                <td>
                                    <a href="?page=pages/user/edit&idedit=<?= $pecah['user_id'] ?>" data-toggle="tooltip" title="Edit User" class="btn btn-warning"><i class="fa fa-edit"></i></a>
                                    <a href="?page=pages/user/hapus&idhapus=<?= $pecah['user_id'] ?>" data-toggle="tooltip" title="Hapus User" class="btn btn-danger"><i class="fa fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>

                </table>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="tambah" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Tambah Data User</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Nama</label>
                        <input type="text" name="nama" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Username</label>
                        <input type="text" name="username" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <input type="password" name="pass" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="">Level User</label>
                        <select name="role" class="form-control">
                            <option value="">Silahkan Pilih</option>

                            <?php

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

                            foreach ($datarole as $pecah) :

                            ?>
                                <option value="<?= $pecah['role_id'] ?>"><?= $pecah['role_nama'] ?></option>
                            <?php endforeach; ?>

                        </select>
                    </div>

            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary" name="simpan">Simpan</button>
            </div>
            </form>

            <?php

            if (isset($_POST['simpan'])) {
                $nama     =  $_POST['nama'];
                $username =  $_POST['username'];
                $pass     =  $_POST['pass'];
                $role_id = $_POST['role'];
                // var_dump($role_id);
                // exit;

                $simpan = $koneksi->query("INSERT INTO tbl_user(
                        user_nama,
                        user_username,
                        user_password,
                        role_id) VALUES('$nama','$username','$pass','$role_id')");

                if ($simpan == 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/view'
                     </script>";
                }
            }




            ?>

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