<?php
session_start();
if (!isset($_SESSION["login"])) {
    header("Location: login.php");
    exit;
}

require_once(dirname(__FILE__) . '\header.php');

// ambil id data di URL
$id = $_GET["id"];

// query data mahasiswa berdasarkan id
$mhs = query("SELECT * FROM user WHERE id= $id")[0];

// cek apakah tombol subit sudah ditekan atau belum
if (isset($_POST["submit"])) {
    // ambil data dari tiap elemen dalam form

    //cek apakah data berhasil di ubah atau tidak
    if (ubah($_POST) > 0) {
        echo "
            <script>
            alert('data berhasil diubah!');
            document.location.href = 'Admin_KelolaAkun.php';
            </script>
        ";
    } else {
        echo "<script>
            alert('data gagal diubah!');
            document.location.href = 'Admin_KelolaAkun.php';
            </script>";
    }
}
?>
<!DOCTYPE html>
<html>

<head>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <!-- CSS yang saya tambahkan -->
    <link rel="stylesheet" href="css/style.css">

    <title>ubah data akun</title>
    <style>
        button {
            margin-top: 20px;
            margin-bottom: 20px;
        }

        input {
            margin-left: 20px;
            width: 350px;
            margin-top: 10px;
        }

        .haha {
            margin-left: -150px;
        }

        .nnn {
            width: 250px;
        }
    </style>
</head>

<body>

    <h2>
        <center>Ubah Data User</center>
    </h2>
    <form action="" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?= $mhs["id"]; ?>">
        <input type="hidden" name="gambarLama" value="<?= $mhs["gambar"]; ?>">
        <div class="container">
            <div class="form-group row">
                <label for="nama" class="col-sm-2 col-form-label">Nama Lengkap</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="nama" id="nama" required value="<?php echo $mhs["nama"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="alamat" class="col-sm-2 col-form-label">Alamat Lengkap</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="alamat" id="alamat" required value="<?php echo $mhs["alamat"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="nim" class="col-sm-2 col-form-label">NIM</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="nim" id="nim" required value="<?php echo $mhs["nim"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="lahir" class="col-sm-2 col-form-label">Tempat/Tanggal Lahir</label>
                <div class="col-sm-10">
                    <input type="text" class="nnn" name="tempat" id="tempat" placeholder="Tempat..." required value="<?php echo $mhs["TTL"] ?>">
                </div>
            </div>
            <fieldset class="form-group">
                <div class="row">
                    <legend class="col-form-label col-sm-2 pt-0">Jenis Kelamin</legend>
                    <div class="col-sm-10">
                        <?php
                        if ($mhs["jenis"] == 'L') : ?>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="jenis" id="jenis" value="L" checked>
                                <label class="form-check-label" for="gridRadios1">
                                    Laki-laki
                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="jenis" id="jenis" value="P">
                                <label class="form-check-label" for="gridRadios2">
                                    Perempuan
                                </label>
                            </div>
                        <?php endif; ?>
                        <?php if ($mhs["jenis"] == 'P') : ?>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="jenis" id="jenis" value="L">
                                <label class="form-check-label" for="gridRadios1">
                                    Laki-laki
                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="jenis" id="jenis" value="P" checked>
                                <label class="form-check-label" for="gridRadios2">
                                    Perempuan
                                </label>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </fieldset>
            <div class="form-group row">
                <label for="telepon" class="col-sm-2 col-form-label">No. Telepon</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="telepon" id="telepon" required value="<?php echo $mhs["telepon"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="email" class="col-sm-2 col-form-label">Email</label>
                <div class="col-sm-10">
                    <input type="email" class="form-control" name="email" id="email" required value="<?php echo $mhs["email"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="kerja" class="col-sm-2 col-form-label">Pekerjaan</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="kerja" id="kerja" required value="<?php echo $mhs["pekerjaan"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="username" class="col-sm-2 col-form-label">Username</label>
                <div class="col-sm-10">
                    <input type="text" readonly class="form-control-plaintext" name="username" id="username" required value="<?php echo $mhs["username"] ?>">
                </div>
            </div>
            <div class="form-group row">
                <label for="password" class="col-sm-2 col-form-label">Password</label>
                <div class="col-sm-10">
                    <input type="text" readonly class="form-control-plaintext" name="password" id="password" required value="*******">
                </div>
            </div>
            <div class="form-group">
                <label for="gambar">Foto </label>
                <div class="col-sm-10">
                    <img src="img/<?= $mhs['gambar']; ?>" width="100"><br>
                    <input type="file" name="gambar" id="gambar">
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-10">
                    <center>
                        <button type="submit" class="btn btn-primary" name="submit">Update</button>
                    </center>
                </div>
            </div>
        </div>
    </form>

    <?php
    require_once(dirname(__FILE__) . '\footer.php');
    ?>
</body>

</html>