<?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 Absensi 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">Laporan Absensi</li>
        </ol>
      </div>
    </div>
  </div>
</div>

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

    <div class="card card-outline card-info">
      <div class="card-header"><h3 class="card-title"><i class="fa fa-filter mr-1"></i> Filter Periode</h3></div>
      <div class="card-body">
        <form method="get" action="<?= site_url('output/laporan/absensi') ?>">
          <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 d-flex align-items-end">
              <button type="submit" class="btn btn-info btn-sm"><i class="fa fa-eye"></i> Tampilkan</button>
            </div>
          </div>
        </form>
      </div>
    </div>

    <div class="card card-outline card-info">
      <div class="card-header">
        <h3 class="card-title">
          <i class="fa fa-calendar mr-1"></i>
          Rekap Kehadiran — <?= bulan_indonesia($bulan) ?> <?= $tahun ?>
          <small class="text-muted ml-2"><?= html_escape((string) ($periode_label ?? '-')) ?></small>
        </h3>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-sm table-striped table-hover" id="tblAbsensi">
            <thead class="bg-info 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">Izin</th>
                <th class="text-center">Sakit</th>
                <th class="text-center">Cuti</th>
                <th class="text-center">Alpha</th>
                <th class="text-center">Terlambat</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($summary)): ?>
                <tr><td colspan="10" class="text-center text-muted py-4">
                  <i class="fa fa-inbox fa-2x d-block mb-2"></i> Belum ada data absensi periode ini.
                </td></tr>
              <?php else: ?>
                <?php $no = 1; foreach ($summary as $s): ?>
                  <tr>
                    <td><?= $no++ ?></td>
                    <td><?= htmlspecialchars($s->nip) ?></td>
                    <td><?= htmlspecialchars($s->nama_lengkap) ?></td>
                    <td><?= htmlspecialchars($s->nama_divisi ?? '-') ?></td>
                    <td class="text-center"><?= ($s->masuk ?? 0) + ($s->terlambat ?? 0) ?></td>
                    <td class="text-center"><?= $s->izin ?? 0 ?></td>
                    <td class="text-center"><?= $s->sakit ?? 0 ?></td>
                    <td class="text-center"><?= $s->cuti ?? 0 ?></td>
                    <td class="text-center"><?= $s->alpha ?? 0 ?></td>
                    <td class="text-center"><?= $s->terlambat ?? 0 ?> kali</td>
                  </tr>
                <?php endforeach; ?>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>
</section>

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