<?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 Payroll Bulanan</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">Payroll Bulanan</li>
        </ol>
      </div>
    </div>
  </div>
</div>

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

    <!-- Filter -->
    <div class="card card-outline card-danger">
      <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/payroll') ?>" id="formFilter">
          <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 gap-1">
              <button type="submit" class="btn btn-danger btn-sm mr-1"><i class="fa fa-eye"></i> Tampilkan</button>
              <?php if ($periode): ?>
                <a href="<?= site_url('output/laporan/payroll?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">
        <h5>Belum ada data</h5>
        <p>Pilih periode di atas untuk menampilkan laporan. Pastikan payroll periode tersebut sudah berstatus <strong>APPROVED</strong> atau <strong>LOCKED</strong>.</p>
      </div>
    <?php else: ?>

      <!-- Ringkasan -->
      <?php
        $total_bruto = array_sum(array_column($rows, 'gaji_bruto'));
        $total_pot   = array_sum(array_column($rows, 'total_potongan'));
        $total_neto  = array_sum(array_column($rows, 'gaji_neto'));
      ?>
      <div class="row">
        <div class="col-md-4">
          <div class="info-box bg-light border-left border-danger" style="border-left:4px solid #E53935 !important;">
            <span class="info-box-icon bg-danger"><i class="fa fa-users"></i></span>
            <div class="info-box-content">
              <span class="info-box-text">Total Pegawai</span>
              <span class="info-box-number"><?= count($rows) ?></span>
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="info-box bg-light" style="border-left:4px solid #43A047 !important;">
            <span class="info-box-icon bg-success"><i class="fa fa-plus-circle"></i></span>
            <div class="info-box-content">
              <span class="info-box-text">Total Bruto</span>
              <span class="info-box-number" style="font-size:16px;"><?= rupiah($total_bruto) ?></span>
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="info-box bg-light" style="border-left:4px solid #1565C0 !important;">
            <span class="info-box-icon bg-primary"><i class="fa fa-money"></i></span>
            <div class="info-box-content">
              <span class="info-box-text">Total Neto</span>
              <span class="info-box-number" style="font-size:16px;"><?= rupiah($total_neto) ?></span>
            </div>
          </div>
        </div>
      </div>

      <!-- Tabel -->
      <div class="card card-outline card-danger">
        <div class="card-header">
          <h3 class="card-title">
            <i class="fa fa-table mr-1"></i>
            Laporan Payroll — <?= 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>
            <span class="badge badge-<?= $periode->tipe_periode === 'THR' ? 'warning' : 'info' ?> ml-1"><?= $periode->tipe_periode ?? 'REGULER' ?></span>
            <span class="badge badge-<?= $periode->status === 'LOCKED' ? 'danger' : 'warning' ?> ml-2"><?= $periode->status ?></span>
          </h3>
        </div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-sm table-striped table-hover" id="tblLaporan">
              <thead class="bg-danger text-white">
                <tr>
                  <th>No</th>
                  <th>NIP</th>
                  <th>Nama Pegawai</th>
                  <th>Divisi</th>
                  <th class="text-center">Hadir</th>
                  <th class="text-center">Absen</th>
                  <th class="text-right">Gaji Bruto</th>
                  <th class="text-right">Potongan</th>
                  <th class="text-right">Gaji Neto</th>
                  <th class="text-center">Slip</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 class="text-center"><?= $r->hari_hadir ?? 0 ?></td>
                    <td class="text-center"><?= $r->hari_absen ?? 0 ?></td>
                    <td class="text-right"><?= rupiah($r->gaji_bruto) ?></td>
                    <td class="text-right"><?= rupiah($r->total_potongan) ?></td>
                    <td class="text-right font-weight-bold"><?= rupiah($r->gaji_neto) ?></td>
                    <td class="text-center">
                      <a href="<?= site_url('output/slip_gaji/cetak/' . $r->id_payroll) ?>" target="_blank"
                         class="btn btn-xs btn-outline-danger" title="Cetak Slip">
                        <i class="fa fa-print"></i>
                      </a>
                    </td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
              <tfoot class="bg-light font-weight-bold">
                <tr>
                  <td colspan="6" class="text-right">TOTAL</td>
                  <td class="text-right"><?= rupiah($total_bruto) ?></td>
                  <td class="text-right"><?= rupiah($total_pot) ?></td>
                  <td class="text-right"><?= rupiah($total_neto) ?></td>
                  <td></td>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      </div>
    <?php endif; ?>

  </div>
</section>

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