<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">Komponen Gaji</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-list-alt mr-1"></i> Daftar Komponen</h3>
        <a href="<?php echo site_url('master/komponen/tambah'); ?>" class="btn btn-danger btn-sm">
          <i class="fa fa-plus mr-1"></i> Tambah Komponen
        </a>
      </div>
      <div class="card-body">
        <table id="tbl-komponen" class="table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th style="width:40px;">#</th>
              <th>Kode</th>
              <th>Nama Komponen</th>
              <th>Jenis</th>
              <th>Cara Hitung</th>
              <th>Tipe Nilai</th>
              <th>Nilai Default</th>
              <th>Wajib</th>
              <th>Urutan</th>
              <th>Status</th>
              <th style="width:150px;">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($list as $i => $row): ?>
            <tr>
              <td><?php echo $i + 1; ?></td>
              <td><code><?php echo html_escape($row->kode_komponen); ?></code></td>
              <td><?php echo html_escape($row->nama_komponen); ?></td>
              <td>
                <span class="badge <?php echo $row->jenis === 'TUNJANGAN' ? 'badge-success' : 'badge-danger'; ?>">
                  <?php echo $row->jenis; ?>
                </span>
              </td>
              <td>
                <?php if (($row->cara_hitung ?? 'FLAT') === 'FORMULA'): ?>
                  <span class="badge badge-primary">Rumus</span>
                  <?php if (!empty($row->formula_expression)): ?>
                    <div><small class="text-muted"><?php echo html_escape($row->formula_expression); ?></small></div>
                  <?php endif; ?>
                <?php elseif (($row->cara_hitung ?? 'FLAT') === 'PER_HARI'): ?>
                  <span class="badge badge-info">Per Hari</span>
                <?php else: ?>
                  <span class="badge badge-secondary">Flat</span>
                <?php endif; ?>
              </td>
              <td>
                <?php if (($row->cara_hitung ?? 'FLAT') === 'FORMULA'): ?>
                  <span class="text-muted">Nominal hasil rumus</span>
                <?php elseif (($row->cara_hitung ?? 'FLAT') === 'PER_HARI'): ?>
                  <span class="text-muted">Nominal per hari</span>
                <?php else: ?>
                  <?php echo $row->tipe_nilai; ?>
                <?php endif; ?>
              </td>
              <td>
                <?php if (($row->cara_hitung ?? 'FLAT') === 'FLAT' && $row->tipe_nilai === 'PERSEN'): ?>
                  <?php echo $row->nilai_default; ?>%
                <?php else: ?>
                  Rp <?php echo rupiah($row->nilai_default); ?>
                <?php endif; ?>
              </td>
              <td><?php echo $row->is_wajib ? '<span class="badge badge-warning">Wajib</span>' : '-'; ?></td>
              <td><?php echo $row->urutan; ?></td>
              <td>
                <span class="badge <?php echo $row->is_active ? 'badge-aktif' : 'badge-nonaktif'; ?>">
                  <?php echo $row->is_active ? 'Aktif' : 'Nonaktif'; ?>
                </span>
              </td>
              <td>
                <a href="<?php echo site_url('master/komponen/edit/' . $row->id_komponen); ?>"
                   class="btn btn-xs btn-primary" title="Edit"><i class="fa fa-pencil"></i></a>
                <?php if (!$row->is_wajib): ?>
                <a href="<?php echo site_url('master/komponen/toggle/' . $row->id_komponen); ?>"
                   class="btn btn-xs <?php echo $row->is_active ? 'btn-warning' : 'btn-success'; ?> btn-confirm"
                   data-msg="<?php echo $row->is_active ? 'Nonaktifkan' : 'Aktifkan'; ?> komponen ini?"
                   title="Toggle Status"><i class="fa fa-power-off"></i></a>
                <?php endif; ?>
                <?php if (!$row->is_wajib && !in_array((int)$row->id_komponen, $used_ids ?? [])): ?>
                <a href="<?php echo site_url('master/komponen/hapus/' . $row->id_komponen); ?>"
                   class="btn btn-xs btn-danger btn-confirm"
                   data-msg="Hapus komponen &quot;<?php echo html_escape($row->nama_komponen); ?>&quot;? Tindakan ini tidak dapat dibatalkan."
                   title="Hapus"><i class="fa fa-trash"></i></a>
                <?php endif; ?>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</section>

<script>
$(function(){
  $('#tbl-komponen').DataTable({pageLength:25, language:{url:'//cdn.datatables.net/plug-ins/1.13.8/i18n/id.json'}});
  $(document).on('click','.btn-confirm',function(e){
    e.preventDefault(); var href=$(this).attr('href'), msg=$(this).data('msg')||'Lanjutkan?';
    Swal.fire({title:'Konfirmasi',text:msg,icon:'question',showCancelButton:true,
      confirmButtonColor:'#800000',cancelButtonText:'Batal',confirmButtonText:'Ya'})
      .then(function(r){if(r.isConfirmed)window.location.href=href;});
  });
});
</script>
