<section class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1><?php echo $page_title; ?></h1>
        <p class="text-muted mb-0"><?php echo $page_subtitle; ?></p>
      </div>
      <div class="col-sm-6 text-right">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?php echo site_url('dashboard'); ?>">Dashboard</a></li>
          <li class="breadcrumb-item active">Master Divisi</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="card card-danger card-outline">
      <div class="card-header d-flex align-items-center">
        <h3 class="card-title mr-auto"><i class="fa fa-sitemap mr-1"></i> Daftar Divisi</h3>
        <a href="<?php echo site_url('master/divisi/tambah'); ?>" class="btn btn-danger btn-sm">
          <i class="fa fa-plus mr-1"></i> Tambah Divisi
        </a>
      </div>
      <div class="card-body">
        <?php if (empty($list_divisi)): ?>
          <div class="empty-state">
            <i class="fa fa-sitemap"></i>
            <p>Belum ada data divisi. <a href="<?php echo site_url('master/divisi/tambah'); ?>">Tambah sekarang</a>.</p>
          </div>
        <?php else: ?>
          <div class="table-responsive">
            <table class="table table-bordered table-striped table-hover" id="tbl-divisi">
              <thead>
                <tr>
                  <th style="width:50px;">No</th>
                  <th>Kode</th>
                  <th>Nama Divisi</th>
                  <th>Keterangan</th>
                  <th style="width:100px;">Status</th>
                  <th style="width:120px;">Aksi</th>
                </tr>
              </thead>
              <tbody>
                <?php $no = 1; foreach ($list_divisi as $d): ?>
                <tr>
                  <td><?php echo $no++; ?></td>
                  <td><span class="badge badge-secondary"><?php echo html_escape($d->kode_divisi); ?></span></td>
                  <td><?php echo html_escape($d->nama_divisi); ?></td>
                  <td><?php echo html_escape($d->keterangan ?? '-'); ?></td>
                  <td>
                    <span class="badge <?php echo $d->is_active ? 'badge-aktif' : 'badge-nonaktif'; ?>">
                      <?php echo $d->is_active ? 'Aktif' : 'Nonaktif'; ?>
                    </span>
                  </td>
                  <td>
                    <a href="<?php echo site_url('master/divisi/edit/' . $d->id_divisi); ?>"
                       class="btn btn-primary btn-xs mr-1" title="Edit">
                      <i class="fa fa-pencil"></i>
                    </a>
                    <a href="<?php echo site_url('master/divisi/toggle/' . $d->id_divisi); ?>"
                       class="btn <?php echo $d->is_active ? 'btn-warning' : 'btn-success'; ?> btn-xs btn-confirm"
                       data-msg="<?php echo $d->is_active ? 'Nonaktifkan' : 'Aktifkan'; ?> divisi ini?"
                       title="<?php echo $d->is_active ? 'Nonaktifkan' : 'Aktifkan'; ?>">
                      <i class="fa fa-power-off"></i>
                    </a>
                  </td>
                </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</section>

<script>
$(function () {
  $('#tbl-divisi').DataTable({
    language: { url: '//cdn.datatables.net/plug-ins/1.13.8/i18n/id.json' },
    order: [[2, 'asc']],
    pageLength: 25
  });

  $(document).on('click', '.btn-confirm', function (e) {
    e.preventDefault();
    var href = $(this).attr('href');
    var msg  = $(this).data('msg') || 'Lanjutkan aksi ini?';
    Swal.fire({
      title: 'Konfirmasi',
      text: msg,
      icon: 'question',
      showCancelButton: true,
      confirmButtonColor: '#800000',
      cancelButtonText: 'Batal',
      confirmButtonText: 'Ya, lanjutkan'
    }).then(function (r) { if (r.isConfirmed) window.location.href = href; });
  });
});
</script>
