<!DOCTYPE html>
<html lang="en">


<?php $this->load->view("kepaladinas/header"); ?>

<body>

  <?php $this->load->view("kepaladinas/side_topbar"); ?>

  <div id="wrapper">
    <div class="main-content">
      <div class="row small-spacing">
        <div class="col-xs-12">
          <div class="box-content card">
            <h4 class="box-title">List Pegawai </h4>
            <!-- /.dropdown js__dropdown -->
            <div class="card-content">
              <div class="form-group">
               <button type="button" class="btn btn-success btn-sm waves-effect waves-light" onclick="buka_modal_cetak()">Cetak List Pegawai</button>
              </div>
              <div style="overflow-x: auto">
                <table id="table_list_karyawan" class="table table-striped table-bordered display" style="width:100%">
                  <thead>
                    <tr>
                      <th>No</th>
                      <th>NIP</th>
                      <th>Nama</th>
                      <th>No Telpon</th>
                      <th>Jabatan</th>
                      <th>Pangkat</th>
                      <th>Aksi</th>
                    </tr>
                  </thead>

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

            <!-- /#flot-chart-1.flot-chart -->
          </div>
          <!-- /.box-content -->
        </div>
        <!-- /.col-xs-12 -->



      </div>
      <!-- /.row -->

      <?php $this->load->view('kepaladinas/footer') ?>

    </div>
    <!-- /.main-content -->
  </div>

  <div class="modal fade" id="modal_karyawan" tabindex="-1" role="dialog" aria-labelledby="myModalLabel-1" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog modal-lg" role="document">
      <form class="modal-content" onsubmit="return tambah_edit(event)" id="form_tambah_karyawan">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel-1"></h4>
          <!-- <p style="font-size: 10px;"><i>(Geserkan marker ke titik kordinat dinas)</i></p> -->
        </div>
        <div class="modal-body">
          <div class="form-group text-center" id="div_img" style="display: none;">
            <img src="" class="avatar" id="img_container" />
          </div>
          <div class="form-group">
            <label for="nik">Foto</label>
            <input type="file" class="form-control" id="foto" required="true">
          </div>
          <div class="form-group">
            <label for="nik">NIP Pegawai</label>
            <input type="hidden" id="status_form">
            <input type="text" class="form-control" id="nik" placeholder="Masukkan NIK Pegawai" minlength="14" maxlength="18" onkeypress="return isNumberKey(event)" required>
          </div>
          <div class="form-group">
            <label for="nama">Nama</label>
            <input type="text" class="form-control" id="nama" placeholder="Masukkan Nama Pegawai" required>
          </div>
          <div class="form-group">
            <label for="nama">No Telepon</label>
            <input type="text" class="form-control" id="no_telpon" placeholder="Masukkan No Telepon Pegawai" minlength="10" maxlength="14" onkeypress="return isNumberKey(event)" required>
          </div>
          <div class="form-group">
            <label for="jabatan">Jabatan</label>
            <select name="jabatan" id="jabatan" class="form-control" required>
              <option value="" disabled selected>-Pilih Jabatan</option>
              <option value="Kepala Dinas">Kepala Dinas</option>
              <option value="Sekretaris">Sekretaris</option>
              <option value="Bendahara Keuangan">Bendahara Keuangan</option>
              <option value="Kepala Bidang">Kepala Bidang</option>
              <option value="Analisis Kebijakan">Analisis Kebijakan</option>
              <option value="Adyatama Kepariwisataan dan Ekonomi">Adyatama Kepariwisataan dan Ekonomi</option>
              <option value="Kasubag Perencanaan">Kasubag Perencanaan</option>
              <option value="Fungsional Umum">Fungsional Umum</option>
              <option value="Kasubag Kepegawaian">Kasubag Kepegawaian</option>
              <option value="Staf">Staf</option>
            </select>
          </div>
          <div class="form-group">
            <label for="pangkat">Pangkat</label>
            <select name="pangkat" id="pangkat" class="form-control" required>
              <option value="" disabled selected>-Pilih Pangkat</option>
              <option value="Tiada">Tiada</option>
              <option value="I A">I A</option>
              <option value="I B">I B</option>
              <option value="I C">I C</option>
              <option value="I D">I D</option>
              <option value="II A">II A</option>
              <option value="II B">II B</option>
              <option value="II C">II C</option>
              <option value="II D">II D</option>
              <option value="III A">III A</option>
              <option value="III B">III B</option>
              <option value="III C">III C</option>
              <option value="III D">III D</option>
              <option value="IV A">IV A</option>
              <option value="IV B">IV B</option>
              <option value="IV C">IV C</option>
              <option value="IV D">IV D</option>
            </select>
          </div>
          <div class="form-group">
            <label for="keanggotaan">Keanggotaan</label>
            <select name="keanggotaan" id="keanggotaan" class="form-control" required>
              <option value="" disabled selected>-Pilih Keanggotaan</option>
              <option value="Sekretariat">Sekretariat</option>
              <option value="Olahraga">Olahraga</option>
              <option value="Pemuda">Pemuda</option>
              <option value="Ekonomi Kreatif">Ekonomi Kreatif</option>
              <option value="Pariwisata">Pariwisata</option>
              <option value="Keuangan">Keuangan</option>

            </select>
          </div>
          <div class="form-group">
            <label for="status">Status</label>
            <select name="status" id="status" class="form-control" required>
              <option value="" disabled selected>-Pilih Status</option>
              <option value="PNS">PNS</option>
              <option value="Kontrak">Kontrak</option>
            </select>
          </div>
          <div class="form-group">
            <label for="jenis_kelamin">Jenis Kelamin</label>
            <select name="jenis_kelamin" id="jenis_kelamin" class="form-control" required>
              <option value="" disabled selected>-Pilih Opsi</option>
              <option value="Laki-laki">Laki-laki</option>
              <option value="Perempuan">Perempuan</option>
            </select>
          </div>
          <div class="form-group">
            <label for="tanggal_lahir">Tanggal Lahir</label>
            <input type="date" class="form-control" id="tanggal_lahir" required>
          </div>
          <div class="form-group">
            <label for="tempat_lahir">Tempat Lahir</label>
            <input type="text" class="form-control" id="tempat_lahir" required>
          </div>
          <div class="form-group">
            <label for="alamat">Alamat</label>
            <textarea name="alamat" id="alamat" cols="30" rows="10" class="form-control" style="resize: none;" placeholder="Masukkan Alamat" required></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" id="button_simpan_edit" class="btn btn-primary btn-xs waves-effect waves-light"></button>
          <button type="button" class="btn btn-danger btn-xs waves-effect waves-light" data-dismiss="modal">Batalkan</button>
        </div>
      </form>
    </div>
  </div>

  <div class="modal fade" id="modal_list_pegawai" tabindex="-1" role="dialog" aria-labelledby="myModalLabel-1">
    <div class="modal-dialog modal-lg" role="document">
      <form class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel-1">Cetak List Pegawai</h4>
          <!-- <p style="font-size: 10px;"><i>(Geserkan marker ke titik kordinat dinas)</i></p> -->
        </div>
        <div class="modal-body">
          <div class="form-group" id="isi_surat"></div>

        </div>

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

  <?php $this->load->view('kepaladinas/scripts') ?>

  <script src="<?= base_url() ?>assets/plugin/datatables/media/js/jquery.dataTables.min.js"></script>
  <script src="<?= base_url() ?>assets/plugin/datatables/media/js/dataTables.bootstrap.min.js"></script>

  <script async>
    var id_dinas = <?= $id_dinas ?>;

    var data_karyawan;

    var table;

    function datatables() {
      table = $('#table_list_karyawan').DataTable({
        // "searching": false,
        "lengthMenu": [
          [5, 10, 15, -1],
          [5, 10, 15, "All"]
        ],
        "pageLength": 10,
        "ordering": true,
        "processing": true,
        "serverSide": true,
        // "order": [[ 4, 'desc' ]], 

        "ajax": {
          "url": server_url + "kepaladinas/karyawan",
          "type": "POST",
          data: {
            proses: 'table_karyawan'
          },

        },

        "columnDefs": [{
          "targets": [0],
          "orderable": false,
        }, ],
      });
    }
    datatables()

    document.getElementById("foto").onchange = function() {
      let foto_produk = document.getElementById('foto').files[0];
      check_file(foto_produk)
    }

    function buka_modal_tambah() { // buka modal untuk tambah karyawan
      $(".modal-title").html("Form Tambah Pegawai")
      $("#status_form").val("tambah")
      $("#button_simpan_edit").html("Tambah Pegawai")
      // reset form_tambah_karyawan
      $("#div_img").attr('style', "display : none")
      $("#form_tambah_karyawan")[0].reset()
      $('#modal_karyawan').modal('show');
    }

    async function karyawan_edit(nik) {
      // console.log(nik);
      $(".modal-title").html("Form Edit Pegawai")
      $("#status_form").val("edit")
      $("#button_simpan_edit").html("Edit Pegawai")
      $("#foto").attr('required', false)
      $("#foto").val(null)
      $("#div_img").removeAttr("style")


      let data;
      try {
        data = await $.ajax({
          url: server_url_api + "karyawan?nik=" + nik + "&id_dinas=" + id_dinas,
          type: 'get',
          async: false,
          beforeSend: function(res) {
            block_ui("Mengambil Data Pegawai");
          },
        });
        data = data.data
        data_karyawan = data;
        await $.unblockUI();
        $("#nik").val(data.nik)
        $("#nama").val(data.nama)
        $("#no_telpon").val(data.no_telpon)
        $("#jabatan").val(data.jabatan)
        $("#tanggal_lahir").val(data.tanggal_lahir)
        $("#alamat").html(data.alamat)
        $("#nik").attr("disabled", true)
        $("#img_container").attr("src", server_url + data.image)
        selectElement('pangkat', data.pangkat);
        selectElement('status', data.status);
        selectElement('jenis_kelamin', data.jenis_kelamin);
        selectElement('keanggotaan', data.keanggotaan);
        $("#tempat_lahir").val(data.tempat_lahir)
        $('#modal_karyawan').modal('show');
      } catch (error) {
        await $.unblockUI();
        const statusCode = error.status;
        console.log(error)
        console.log(statusCode)
        if (statusCode == 0 || statusCode == 500) return toastr.error("Jaringan atau server bermasalah, sila refresh kembali halaman");

        const message = error.responseJSON.message
        toastr.error(message);


      }
    }



    function cek_laporan(nik, stat) {
      if (stat == 'tidak') {
        toastr.info("Pegawai Ini Tidak Memiliki Laporan Kegiatan");
        return
      }

      window.location.href = server_url + "kepaladinas/karyawan/" + nik;
    }

    function buka_modal_cetak() {
      const laporan = `<embed src="${server_url}kepaladinas/cetak_list_pegawai" frameborder="1" width="100%" height="600px">`;
      $("#isi_surat").empty();
      $("#isi_surat").append(laporan);
      $('#modal_list_pegawai').modal('show');
    }
  </script>


</body>

<!-- Dibuat oleh Kicap Karan. https://www.kicap-karan.com -->

</html>