<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Detail User Admin</h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="<?=base_url()?>home">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?=base_url()?>users">Data User Admin</a></li>
                    <li class="breadcrumb-item active">User Admin</li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <hr/>
    <section class="content">
        <div class="container-fluid">
            <form id="f1" action="#" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-lg-6">
                    <div class="card card-outline card-success">
                        <div class="card-header">
                            <div class="card-title">
                                Data User
                            </div>
                        </div>
                        <div class="card-body">
                            <input type="hidden" name="id" value="<?=$data->id_user?>">
                            <div class="form-group">
                                <label for="nama">Nama</label>
                                <input type="text" class="form-control form-control-border" id="nama" placeholder="Nama" name="nama" value="<?=$data->nama?>">
                                <span id="enama" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="username">NIP / NIK</label>
                                <input type="text" class="form-control form-control-border" id="username" placeholder="NIP / NIK" name="username" value="<?=$data->username?>">
                                <span id="eusername" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="email">Email</label>
                                <input type="email" class="form-control form-control-border" id="email" placeholder="Email" name="email" value="<?=$data->email?>">
                                <span id="eemail" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label>
                                <input type="password" class="form-control form-control-border" id="password" placeholder="Password" name="password">
                                <span id="epassword" class="error invalid-feedback"></span>
                            </div>
                        </div>
                        <div class="card-footer text-right">
                            <a id="btn_kembali" class="btn bg-gradient-warning" href="<?=base_url()?>users_admin"><span class="fas fa-arrow-left"></span> Batal</a>
                            <button id="btn_simpan" class="btn bg-gradient-primary"><span class="fas fa-check"></span> Simpan</button>
                            <a id="btn_delete" class="btn bg-gradient-danger" href="#"><span class="fas fa-trash"></span> Delete User</a>
                        </div>
                    </div>
                </div>
            </div>
            </form>
        </div>
    </section>
    <script>
        $(document).ready(function () {
            $("#btn_delete").click(function (e) {
                e.preventDefault();
                Swal.fire({
                    title:'Hapus User',
                    html: '<p>Yakin ingin menghapus user ini?</p>',
                    showDenyButton: true,
                    showCancelButton: true,
                    confirmButtonText: 'Yes',
                    denyButtonText: 'No',
                }).then((result) => {
                    Swal.fire({
                        text: 'Menyimpan data.. Silahkan tunggu..',
                        allowOutsideClick: false
                    });
                    Swal.showLoading();
                    $.post("<?=base_url()?>users_admin_delete",{id_user:'<?=$data->id_user?>'}).done(function (data) {
                        if (data.status == 'success')
                        {
                            Swal.fire({
                                text:'User Admin berhasil dihapus.',
                                icon : 'success'
                            }).then(function () {
                                window.location.href = '<?=base_url()?>users_admin';
                            });
                        }
                    }).fail(function (data){
                        Swal.fire({
                            text:'Terjadi kesalahan pada saat registrasi!',
                            icon : 'error'
                        });
                    });
                });
            });

            $("#f1").submit(function (e) {
                e.preventDefault();
                Swal.fire({
                    text: 'Menyimpan data.. Silahkan tunggu..',
                    allowOutsideClick: false
                });
                Swal.showLoading();
                var data_kirim = $("#f1").serialize();
                
                $.post("<?=base_url()?>users_admin_update",data_kirim).done(function (data) {
                    if (data.status == 'success')
                    {
                        Swal.fire({
                            text:'User Admin berhasil disimpan.',
                            icon : 'success'
                        }).then(function () {
                            window.location.href = '<?=base_url()?>users_admin_view/<?=$data->id_user?>';
                        });
                    }
                }).fail(function (data) {
                    if(data.statusText == 'input'){
                        Object.keys(data.responseJSON.messages).forEach(function(key){
                            $("#e"+key).html(data.responseJSON.messages[key]);
                            $("#e"+key).show();
                        });
                        Swal.close();
                    }else{
                        Swal.fire({
                            text:'Terjadi kesalahan pada saat registrasi!',
                            icon : 'error'
                        });
                    }
                });
            });
        });
    </script>
</div>