<?php
include '../../config/conn.php';

if ($_GET['aksi'] == '') {
    $id = decrypt($idd);
    $query = mysqli_query($conn, "SELECT * FROM pegawai WHERE id_pegawai='$_SESSION[id]'");
    $r = mysqli_fetch_array($query);
?>

    <div class="col" role="main">
        <div class="">
            <div class="clearfix"></div>
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title" style="text-transform: capitalize;">
                            <h2>Akun <small></small></h2>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">
                            <div class="col-md-6">
                                <form action="?module=<?php echo $_GET['module']; ?>&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <label for="nama">Id :</label>
                                            <input type="text" disabled class="form-control" requerid name="nama_sekolah" value="<?php echo $r['id_pegawai']; ?>" />
                                            <input type="hidden" name="id" value="<?php echo $r['id_pegawai']; ?>" />


                                            <label for="nama">Nama :</label>
                                            <input type="text" disabled class="form-control" requerid name="nama_sekolah" value="<?php echo $r['nama']; ?>" />

                                            <label for="nama">Username :</label>
                                            <input type="text" class="form-control" requerid name="username" value="<?php echo $r['username']; ?>" />

                                            <label for="password">Password :</label>
                                            <input type="password" class="form-control" value="<?php echo $r['password']; ?>" disabled />

                                            <label for="password">Ganti Password :</label>
                                            <input type="text" autocomplete="off" class="form-control" name="password" />
                                            <p>*) Kosongkan apabila password tidak diganti</p>

                                        </div>
                                    </div>


                                    <div class="ln_solid"></div>
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-success btn-sm"> Ganti</button>
                                        <a href="./" class="btn btn-default btn-sm">kembali</a><br><br><br><br>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>


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


    <?php  } elseif ($_GET['aksi'] == 'simpan_edit') {
    $module = $_GET['module'];

    $akun = mysqli_query($conn, "SELECT * FROM pegawai WHERE username='$_POST[username]'");
    $ketemu = mysqli_num_rows($akun);
    if ($ketemu > 0) {
        $cek1 = mysqli_query($conn, "SELECT * FROM pegawai WHERE id_pegawai='$_POST[id]'");
        $cek = mysqli_fetch_array($cek1);
        if ($_POST['username'] == $cek['username']) {
            $password   = md5($_POST['password']);
            if (empty($_POST['password'])) {
                $edit = mysqli_query($conn, "UPDATE pegawai SET username = '$_POST[username]'
                                    WHERE id_pegawai = '$_POST[id]'");
            } else {
                $edit = mysqli_query($conn, "UPDATE pegawai SET username = '$_POST[username]',password = '$password'
                                    WHERE id_pegawai = '$_POST[id]'");
            }

            if (!$edit) {
                echo "<script language='javascript'>alert('Gagal Diubah.');document.location='?module=" . $module . "';</script>";
            } else {
                echo "<script language='javascript'>Swal.fire({
                icon: 'success',
                title: 'Data berhasil disimpan',
                showConfirmButton: false
              });setInterval(function(){ window.location='logout.php'; }, 1200);</script>";
            }
        } else {
    ?>


            <script language='javascript'>
                Swal.fire({
                    title: 'Ganti Gagal?',
                    text: "Username telah digunakan!",
                    icon: 'warning',
                    confirmButtonColor: '#3085d6',
                    confirmButtonText: 'OK'
                }).then((result) => {
                    if (result.isConfirmed) {
                        document.location = '?module=<?php echo $module ?>'
                    }
                })
            </script>




<?php
        }
    } else {

        $password   = md5($_POST['password']);
        if (empty($_POST['password'])) {
            $edit = mysqli_query($conn, "UPDATE pegawai SET username = '$_POST[username]'
                                    WHERE id_pegawai = '$_POST[id]'");
        } else {
            $edit = mysqli_query($conn, "UPDATE pegawai SET username = '$_POST[username]',password = '$password'
                                    WHERE id_pegawai = '$_POST[id]'");
        }

        if (!$edit) {
            echo "<script language='javascript'>Swal.fire({
                icon: 'error',
                title: 'Data Gagal disimpan',
                showConfirmButton: false
              });setInterval(function(){ document.location='?module=" . $module . "'; }, 1200);</script>";
        } else {
            echo "<script language='javascript'>Swal.fire({
                icon: 'success',
                title: 'Data berhasil disimpan',
                showConfirmButton: false
              });setInterval(function(){ window.location='logout.php'; }, 1200);</script>";
        }
    }
}
