<div class="card-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    <h3 class="title modal-title"></h3>
</div>
<form id="xyz">
    <div id="form_input" class="card-body" disabled>
             <input type="hidden" id="id_cabang" name="id_cabang" class="form-control" value="<?= ((isset($data))? $data->id_cabang :'') ?>">

        <?php
            $data_cabang = $this->Db_model->get('p_approval_izin', 'level_user, level_approval, id', array('id_cabang' => $data->id_cabang));

            $jumlah_level = $data_cabang->num_rows();
            $i = 1;
            foreach ($data_cabang->result() as $val) {
                $level = explode(',', $val->level_approval);
        ?>
        <div class="form-group">
            <p><b><?= $this->fungsi->get_nama_level($val->level_user) ?></b></p>
            <input type="hidden" id="level_user_<?= $i ?>" name="level_user_<?= $i ?>" class="form-control" value="<?= ((isset($val))? $val->level_user:'') ?>">
            <input type="hidden" id="id_<?= $i ?>" name="id_<?= $i ?>" class="form-control" value="<?= ((isset($val))? $val->id:'') ?>">
            <div class="row">
                <div class="col-md-2">
                    <input type="checkbox" id="level1_<?= $i ?>" name="level_<?= $i ?>-[]" value="7" <?=(isset($level) AND in_array('7', $level))? 'checked' : ''?> /> <?= $this->lang->line('supervisi'); ?>
                </div>
                <div class="col-md-3">
                    <input type="checkbox" id="level2_<?= $i ?>" name="level_<?= $i ?>-[]" value="2" <?=(isset($level) AND in_array('2', $level))? 'checked' : ''?>/> <?= $this->lang->line('menu_kepala_departemen'); ?>
                </div>
                <div class="col-md-3">
                    <input type="checkbox" id="level3_<?= $i ?>" name="level_<?= $i ?>-[]" value="5" <?=(isset($level) AND in_array('5', $level))? 'checked' : ''?>/> <?= $this->lang->line('menu_kepala_cabang'); ?>
                </div>
                <div class="col-md-2">
                    <input type="checkbox" id="level4<?= $i ?>" name="level_<?= $i ?>-[]" value="1" <?=(isset($level) AND in_array('1', $level))? 'checked' : ''?>/> HR
                </div>
                <div class="col-md-2">
                    <input type="checkbox" id="level5_<?= $i ?>" name="level_<?= $i ?>-[]" value="4" <?=(isset($level) AND in_array('4', $level))? 'checked' : ''?>/> <?= $this->lang->line('direksi'); ?>
                </div>
            </div>
        </div><hr><br>
        <?php
                $i++;
            }
        ?>   
        <input type="hidden" name="jumlah_level" id="jumlah_level" value="<?= $jumlah_level ?>" class="form-control">
    </div>
    <div class="card-footer">
        <div class="row">
            <div class="col-md-7 text-left">
                <div id="message"></div>
            </div>
            <div class="col-md-5 text-right">
                <!-- <button class="btn_save ladda-button" data-style="expand-left"></button> -->
                <button type="submit" id="btn_save" class="btn_save ladda-button" data-style="expand-left"></button>
            </div>
        </div><!-- ./row -->
    </div>

</form>

<script type="text/javascript">
    $(document).ready(function(e) {
        $("#xyz").on('submit', (function(e) {
            e.preventDefault();

            var loading = Ladda.create(document.getElementById("btn_save"));

            loading.start();

            $.ajax({
                url: "<?= route('pengaturan.izin.save') ?>",
                type: "POST",
                data: new FormData(this),
                contentType: false,
                cache: false,
                dataType: 'json',
                processData: false,
                success: function(result) {
                    if(result.success == true){
                        if (result.type == 'update') {
                            loading.stop();
                            $('#message').show();
                            $('#message').html(result.message);
                            list_data();
                            $('#modal_form').modal('hide');
                        } else {
                            loading.stop();
                            $('#message').show();
                            $('#message').html(result.message);
                            list_data();
                            $('#modal_form').modal('hide');
                        }
                }else{
                    loading.stop();
                    $('#message').show();
                    $('#message').html(result.message);
                }                  
                },
                error: function() {
                    loading.stop();
                    $('#message').show();
                    $('#message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong><?= $this->lang->line('error'); ?></strong></font>');
                }
            });
        }));
    });
</script>

