<script type="text/javascript">
    
$('#cabang').select2({
    placeholder: 'Pilih Cabang',
    allowClear: true,
    ajax: {
      // type: 'POST',
      url: '<?= site_url("master/karyawan/get-cabang")  ?>',
      dataType: 'json',
      delay: 250,
      processResults: function (data) {
      return {
              results: data
          };
      },
      cache: false
  },
  minLength: 3
});

$('#departemen').select2({
    placeholder: 'Pilih Departemen',
    allowClear: true,
    ajax: {
      // type: 'POST',
      url: '<?= site_url("master/karyawan/get-departemen/")  ?>'+ $("#cabang").val(),
      dataType: 'json',
      delay: 250,
      processResults: function (data) {
      return {
              results: data
          };
      },
      cache: false
  },
  minLength: 3
});

$('#jabatan').select2({
    placeholder: 'Pilih Jabatan',
    allowClear: true,
    ajax: {
      // type: 'POST',
      url: '<?= site_url("master/karyawan/get-jabatan")  ?>',
      dataType: 'json',
      delay: 250,
      processResults: function (data) {
      return {
              results: data
          };
      },
      cache: false
  },
  minLength: 3
});

$('#atasan').select2({
    placeholder: 'Pilih Atasan',
    allowClear: true,
    ajax: {
      // type: 'POST',
      url: '<?= site_url("pengaturan/atasan-khusus/get-atasan") ."/" ?>'+$('#jabatan').val(),
      dataType: 'json',
      delay: 250,
      processResults: function (data) {
      return {
              results: data
          };
      },
      cache: false
  },
  minLength: 3
});

function get_atasan(){
    $('#atasan').val('');
    $('#atasan').select2({
        placeholder: 'Pilih Atasan',
        allowClear: true,
        ajax: {
          // type: 'POST',
          url: '<?= site_url("pengaturan/atasan-khusus/get-atasan") ."/" ?>'+$('#jabatan').val(),
          dataType: 'json',
          delay: 250,
          processResults: function (data) {
          return {
                  results: data
              };
          },
          cache: false
      },
      minLength: 3
    });
}

function get_departemen(){
    $('#departemen').val('');
    $('#departemen').select2({
        placeholder: 'Pilih Departemen',
        allowClear: true,
        ajax: {
          // type: 'POST',
          url: '<?= site_url("master/karyawan/get-departemen/")  ?>'+ $("#cabang").val(),
          dataType: 'json',
          delay: 250,
          processResults: function (data) {
          return {
                  results: data
              };
          },
          cache: false
      },
      minLength: 3
    });
}
</script>
<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>
<div id="form_input" class="card-body" disabled>
    <input type="hidden" id="id" class="form-control" value="<?= ((isset($data))?md5($data->id):'') ?>">

    <div class="form-group">
      <label>Cabang <i class="text-danger">*</i></label>
      <select class="form-control" name="cabang" id="cabang" class="select2 form-control" style="border-radius: 5px; width: 100%;" onchange="get_departemen()">
        <?php
              $id_cabang = $data->id_cabang;
              if ($id_cabang != 0) {
                  $selected_cabang = $this->db->query("SELECT id_cabang, nama FROM master_cabang WHERE id_cabang = '".$id_cabang."'");
          ?>
              <option value="<?=$id_cabang?>" selected><?=((isset($selected_cabang->row()->nama))?$selected_cabang->row()->nama:'')?></option>
          <?php
              }
          ?>
      </select>
    </div>
        
    <div class="form-group">
      <label>Departemen <i class="text-danger">*</i></label>
      <select class="form-control" name="departemen" id="departemen" class="select2 form-control" style="border-radius: 5px; width: 100%;">
        <?php
              $id_departemen = $data->id_departemen;
              if ($id_departemen != null) {
                  $selected_departemen = $this->db->query("SELECT id_departemen, nama FROM master_departemen WHERE id_departemen = '".$id_departemen."'");
          ?>
              <option value="<?=$id_departemen?>" selected><?=((isset($selected_departemen->row()->nama))?$selected_departemen->row()->nama:'')?></option>
          <?php
              }
          ?>
      </select>
    </div>

    <div class="form-group">
      <label>Jabatan <i class="text-danger">*</i></label>
      <select class="form-control" name="jabatan" id="jabatan" class="select2 form-control" style="border-radius: 5px; width: 100%;" onchange="get_atasan()">
          <?php
              $id_jabatan = $data->id_jabatan;
              if ($id_jabatan != 0) {
                  $selected_jabatan = $this->db->query("SELECT id_jabatan, nama FROM master_jabatan WHERE id_jabatan = '".$id_jabatan."'");
          ?>
              <option value="<?=$id_jabatan?>" selected><?=((isset($selected_jabatan->row()->nama))?$selected_jabatan->row()->nama:'-')?></option>
          <?php
              }
          ?>
      </select>
    </div>

    <div class="form-group">
      <label>Atasan <i class="text-danger">*</i></label>
      <select class="form-control" name="atasan" id="atasan" class="select2 form-control" style="border-radius: 5px; width: 100%;">
          <?php
              $atasan = $data->atasan;
              if ($atasan != 0) {
                  $selected_jabatan = $this->db->query("SELECT id_jabatan, nama FROM master_jabatan WHERE id_jabatan = '".$atasan."'");
          ?>
              <option value="<?=$atasan?>" selected><?=((isset($selected_jabatan->row()->nama))?$selected_jabatan->row()->nama:'-')?></option>
          <?php
              }
          ?>
      </select>
    </div>
</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 id="btn_new_input" class="btn" style="display:none">TAMBAH BARU</button>
            <button class="btn_save ladda-button" data-style="expand-left"></button>
        </div>
    </div><!-- ./row -->
</div>

