<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1 class="m-0">Arsip Slip Gaji</h1>
      </div>
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?= site_url('dashboard') ?>">Dashboard</a></li>
          <li class="breadcrumb-item active">Arsip Slip Gaji</li>
        </ol>
      </div>
    </div>
  </div>
</div>

<section class="content">
  <div class="container-fluid">

    <?php if ($this->session->flashdata('success')): ?>
      <div class="alert alert-success alert-dismissible fade show">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <?= $this->session->flashdata('success') ?>
      </div>
    <?php endif; ?>

    <!-- Filter -->
    <div class="card card-outline card-danger">
      <div class="card-header"><h3 class="card-title"><i class="fa fa-filter mr-1"></i> Filter Slip Gaji</h3></div>
      <div class="card-body">
        <form method="get" action="<?= site_url('output/slip_gaji') ?>">
          <div class="row">
            <div class="col-md-3">
              <label>Pegawai</label>
              <select name="id_pegawai" class="form-control form-control-sm select2">
                <option value="">-- Semua Pegawai --</option>
                <?php foreach ($pegawai_list as $pg): ?>
                  <option value="<?= $pg->id_pegawai ?>" <?= ($filter['id_pegawai'] == $pg->id_pegawai) ? 'selected' : '' ?>>
                    <?= htmlspecialchars($pg->nip . ' - ' . $pg->nama_lengkap) ?>
                  </option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="col-md-2">
              <label>Tahun</label>
              <select name="tahun" class="form-control form-control-sm">
                <?php for ($y = date('Y'); $y >= date('Y') - 5; $y--): ?>
                  <option value="<?= $y ?>" <?= ($filter['tahun'] == $y) ? 'selected' : '' ?>><?= $y ?></option>
                <?php endfor; ?>
              </select>
            </div>
            <div class="col-md-2">
              <label>Bulan</label>
              <select name="bulan" class="form-control form-control-sm">
                <?php for ($m = 1; $m <= 12; $m++): ?>
                  <option value="<?= $m ?>" <?= ($filter['bulan'] == $m) ? 'selected' : '' ?>><?= bulan_indonesia($m) ?></option>
                <?php endfor; ?>
              </select>
            </div>
            <div class="col-md-2">
              <label>Jenis</label>
              <select name="tipe_periode" class="form-control form-control-sm">
                <option value="" <?= empty($filter['tipe_periode']) ? 'selected' : '' ?>>Semua</option>
                <option value="REGULER" <?= ($filter['tipe_periode'] === 'REGULER') ? 'selected' : '' ?>>Reguler</option>
                <option value="THR" <?= ($filter['tipe_periode'] === 'THR') ? 'selected' : '' ?>>THR</option>
              </select>
            </div>
            <div class="col-md-3 d-flex align-items-end">
              <button type="submit" class="btn btn-danger btn-sm mr-1"><i class="fa fa-search"></i> Tampilkan</button>
              <a href="<?= site_url('output/slip_gaji') ?>" class="btn btn-secondary btn-sm"><i class="fa fa-times"></i> Reset</a>
            </div>
          </div>
        </form>
      </div>
    </div>

    <!-- Tabel Slip -->
    <div class="card card-outline card-danger">
      <div class="card-header">
        <h3 class="card-title"><i class="fa fa-list mr-1"></i> Daftar Slip Gaji</h3>
      </div>
      <div class="card-body p-0">
        <table class="table table-striped table-hover table-sm" id="tblArsip">
          <thead class="bg-danger text-white">
            <tr>
              <th>No</th>
              <th>NIP</th>
              <th>Nama Pegawai</th>
              <th>Divisi</th>
              <th>Periode</th>
              <th>Jenis</th>
              <th class="text-right">Gaji Neto</th>
              <th class="text-center">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php if (empty($slips)): ?>
              <tr><td colspan="8" class="text-center text-muted py-4">
                <i class="fa fa-inbox fa-2x d-block mb-2"></i>
                Belum ada data slip gaji.
              </td></tr>
            <?php else: ?>
              <?php $no = 1; foreach ($slips as $s): ?>
                <tr>
                  <td><?= $no++ ?></td>
                  <td><?= htmlspecialchars($s->nip) ?></td>
                  <td><?= htmlspecialchars($s->nama_lengkap) ?></td>
                  <td><?= htmlspecialchars($s->nama_divisi) ?></td>
                  <td>
                    <?= bulan_indonesia($s->bulan) ?> <?= $s->tahun ?><br>
                    <small class="text-muted"><?= tgl_indonesia($s->tanggal_mulai) ?> s/d <?= tgl_indonesia($s->tanggal_selesai) ?></small>
                  </td>
                  <td>
                    <span class="badge badge-<?= ($s->tipe_periode ?? 'REGULER') === 'THR' ? 'warning' : 'info' ?>">
                      <?= htmlspecialchars($s->tipe_periode ?? 'REGULER') ?>
                    </span>
                  </td>
                  <td class="text-right"><?= rupiah($s->gaji_neto) ?></td>
                  <td class="text-center">
                    <a href="<?= site_url('output/slip_gaji/cetak/' . $s->id_payroll) ?>" target="_blank"
                       class="btn btn-xs btn-outline-danger" title="Cetak Slip">
                      <i class="fa fa-print"></i> Cetak
                    </a>
                  </td>
                </tr>
              <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</section>

<script>
$(function() {
  if ($.fn.DataTable) {
    $('#tblArsip').DataTable({
      responsive: true,
      language: { url: '<?= base_url('assets/plugins/datatables/i18n/Indonesian.json') ?>' }
    });
  }
  if ($.fn.select2) {
    $('.select2').select2({ width: '100%' });
  }
});
</script>
