<?php 
    if(empty($edit)){
        $edit = null;
    }
?>
<div class="panel panel-grey">
    <div class="panel-heading">
        <div class="panel-heading-btn">
            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-inverse" data-click="panel-collapse"><i class="fa fa-minus"></i></a>
        </div>
        <h4 class="panel-title">Data</h4>
        <hr>
    </div>
    <form class="form-horizontal" action="<?= $url; ?>" method="post">
    <div class="panel-body">
            <div class="form-group m-b-12">
                <label class="col-sm-2 control-label">Username</label>
                <div class="col-sm-5">
                    <input type="text" name="username" class="form-control" placeholder="User Name" value="<?php echo !is_null($edit) ? $edit['username'] : '' ?>">
                </div>
            </div>
            <?php if(is_null($edit)) : ?>
            <div class="form-group m-b-12">
                <label class="col-sm-2 control-label">Nama Lengkap</label>
                <div class="col-sm-5">
                    <input type="text" name="nama" class="form-control" placeholder="Nama Lengkap" value="<?php echo !is_null($edit) ? $edit['nama'] : '' ?>">
                </div>
            </div>
            <?php endif; ?>
            <div class="form-group m-b-12">
                <label class="col-sm-2 control-label">Email</label>
                <div class="col-sm-5">
                    <input type="email" name="email" class="form-control" placeholder="Email" value="<?php echo !is_null($edit) ? $edit['email'] : '' ?>">
                </div>
            </div>
            <div class="form-group m-b-12">
                <label class="col-sm-2 control-label">Role User</label>
                <div class="col-sm-5">
                    <select id="role_user" name="role_user[]" onchange='pilih_analis(this.value);' class="form-control select2" style="width: 100%;" multiple="">
                        <option></option>
                        <?php foreach ($role_user as $r) : ?>
                            <option <?= multi_selected($edit, 'id_login_group', $r['id']); ?> >
                                <?php echo $r['nama']; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group m-b-12" id="analis">
                <label class="col-sm-2 control-label">Role Analis</label>
                <div class="col-sm-5">
                    <select name="role_analis[]" class="form-control select2" style="width: 100%;" multiple="">
                        <option></option>
                        <?php foreach ($role_analis as $r) : ?>
                            <option <?= multi_selected($edit, 'role_analis', $r['id']); ?> >
                                <?= $r['nama']; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group m-b-12 ubah_password">
                <label class="col-sm-2 control-label"></label>
                <div class="col-sm-5">
                    <label><input type="checkbox" id='ubah_password' onclick="edit_password()">Ubah Password</label>
                </div>
            </div>
            <div class="form-group m-b-12 password">
                <label class="col-sm-2 control-label">Password<?php echo !is_null($edit) ? ' Baru' : ''; ?></label>
                <div class="col-sm-5">
                    <input type="password" id='password' name="password" class="form-control" placeholder="Password">
                </div>
            </div>
            <div class="form-group m-b-12 repassword">
                <label class="col-sm-2 control-label">Konfirmasi Password<?php echo !is_null($edit) ? ' Baru' : ''; ?></label>
                <div class="col-sm-5">
                    <input type="password" id='repassword' name="repassword" onkeyup="cekPass();" class="form-control" placeholder="Konfirmasi Password">
                    <div class="alert alert-danger" id='note' role="alert">
                            Repassword yang anda masukkan salah, silahkan periksa kembali
                    </div>
                </div>
            </div>
        </div>
        <div class="panel-footer">
            <div class="form-group m-b-12">
            <div class="col-sm-2"></div>
            <div class="col-sm-5">
                <button type="submit" class="btn btn-success" >Simpan</button>
                <button type="reset" class="btn btn-warning" >Reset</button>
                <a href="<?php echo site_url('data_user') ?>" class="btn btn-danger">Batal</a>
            </div>
            </div>
        </div>
    </form>
</div>

<script>
    $(document).ready(function () {
    <?php if(!is_null($edit) && in_array(3, json_decode($edit['id_login_group']))) : ?>
        $('#analis').show();
    <?php else : ?>
        $('#analis').hide();
    <?php endif; ?>

    <?php if(!is_null($edit)) { ?>
        $('.password').hide();
        $('.repassword').hide();
        $('.ubah_password').show();
    <?php } else{ ?>
        $('.password').show();
        $('.repassword').show();
        $('.ubah_password').hide();
    <?php } ?>

    $('#note').hide();
    });

    function edit_password()
    {
        if ($('#ubah_password').is(':checked')) {
            $('.password').show();
            $('.repassword').show();
        }else{
            $('.password').hide();
            $('.repassword').hide();
        }
    }

    function cekPass()
    {
        var pass    = $('#password').val();
        var repass  = $('#repassword').val();

        if(pass != repass){
            $('#note').show();
        }else{
            $('#note').hide();
        }
    }
    
    //var id_analis = '|';
    function pilih_analis(x)
    {
        var id_analis = $('#role_user').val();
        
        if (id_analis.includes("3")) {
            $('#analis').show();
        } else {
            $('#analis').hide();
        }

    }
</script>
