<?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">Laporan Bank / Transfer 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"><a href="<?= site_url('output/laporan') ?>">Pusat Laporan</a></li>
          <li class="breadcrumb-item active">Laporan Bank</li>
        </ol>
      </div>
    </div>
  </div>
</div>

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

    <div class="card card-outline card-warning">
      <div class="card-header"><h3 class="card-title"><i class="fa fa-filter mr-1"></i> Pilih Periode</h3></div>
      <div class="card-body">
        <form method="get" action="<?= site_url('output/laporan/bank') ?>">
          <div class="row">
            <div class="col-md-3">
              <label>Bulan</label>
              <select name="bulan" class="form-control form-control-sm">
                <?php for ($m = 1; $m <= 12; $m++): ?>
                  <option value="<?= $m ?>" <?= ($bulan == $m) ? 'selected' : '' ?>><?= bulan_indonesia($m) ?></option>
                <?php endfor; ?>
              </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 ?>" <?= ($tahun == $y) ? 'selected' : '' ?>><?= $y ?></option>
                <?php endfor; ?>
              </select>
            </div>
            <div class="col-md-2">
              <label>Tipe Periode</label>
              <select name="tipe_periode" class="form-control form-control-sm">
                <option value="REGULER" <?= ($tipe_periode === 'REGULER') ? 'selected' : '' ?>>Reguler</option>
                <option value="THR" <?= ($tipe_periode === 'THR') ? 'selected' : '' ?>>THR</option>
              </select>
            </div>
            <div class="col-md-3 d-flex align-items-end">
              <button type="submit" class="btn btn-warning btn-sm mr-1"><i class="fa fa-eye"></i> Tampilkan</button>
              <?php if ($periode): ?>
                <a href="<?= site_url('output/laporan/bank?bulan=' . $bulan . '&tahun=' . $tahun . '&tipe_periode=' . $tipe_periode . '&format=excel') ?>"
                   class="btn btn-success btn-sm">
                  <i class="fa fa-file-excel-o"></i> Export Excel
                </a>
              <?php endif; ?>
            </div>
          </div>
        </form>
      </div>
    </div>

    <?php if (!$periode): ?>
      <div class="callout callout-info">
        <p>Pilih periode di atas untuk menampilkan daftar transfer gaji.</p>
      </div>
    <?php else: ?>
      <?php $grand_total = array_sum(array_column($rows, 'gaji_neto')); ?>

      <div class="card card-outline card-warning">
        <div class="card-header">
          <h3 class="card-title">
            <i class="fa fa-bank mr-1"></i>
            Daftar Transfer Gaji — <?= bulan_indonesia($periode->bulan) ?> <?= $periode->tahun ?>
            <small class="text-muted ml-2"><?= tgl_indonesia($periode->tanggal_mulai) ?> s/d <?= tgl_indonesia($periode->tanggal_selesai) ?></small>
          </h3>
          <div class="card-tools">
            <span class="badge badge-danger font-weight-bold" style="font-size:14px;">
              Total: <?= rupiah($grand_total) ?>
            </span>
          </div>
        </div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-sm table-striped table-hover" id="tblBank">
              <thead class="bg-warning">
                <tr>
                  <th>No</th>
                  <th>NIP</th>
                  <th>Nama Pegawai</th>
                  <th>Divisi</th>
                  <th>Nama Bank</th>
                  <th>No. Rekening</th>
                  <th>Atas Nama</th>
                  <th class="text-right">Nominal (Rp)</th>
                </tr>
              </thead>
              <tbody>
                <?php $no = 1; foreach ($rows as $r): ?>
                  <tr>
                    <td><?= $no++ ?></td>
                    <td><?= htmlspecialchars($r->nip) ?></td>
                    <td><?= htmlspecialchars($r->nama_lengkap) ?></td>
                    <td><?= htmlspecialchars($r->nama_divisi) ?></td>
                    <td><?= htmlspecialchars($r->nama_bank ?: '-') ?></td>
                    <td><?= htmlspecialchars($r->no_rekening ?: '-') ?></td>
                    <td><?= htmlspecialchars($r->nama_lengkap) ?></td>
                    <td class="text-right font-weight-bold"><?= rupiah($r->gaji_neto) ?></td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
              <tfoot class="font-weight-bold bg-light">
                <tr>
                  <td colspan="7" class="text-right">GRAND TOTAL</td>
                  <td class="text-right"><?= rupiah($grand_total) ?></td>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      </div>
    <?php endif; ?>
  </div>
</section>

<script>
$(function() {
  if ($.fn.DataTable && <?= !empty($rows) ? 'true' : 'false' ?>) {
    $('#tblBank').DataTable({
      paging: false, searching: true, ordering: true,
      language: { url: '<?= base_url('assets/plugins/datatables/i18n/Indonesian.json') ?>' }
    });
  }
});
</script>
