<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\PengajuanModel;

class Laporan extends BaseController
{
    public function index()
    {
        $pengajuanModel = new PengajuanModel();

        $periode = $this->request->getGet('periode');
        $tahun = $this->request->getGet('tahun') ?: date('Y');
        $status = $this->request->getGet('status');

        $query = $pengajuanModel->select('pengajuan.*, pegawai.nama as pegawai_nama, pegawai.nip_baru as pegawai_nip')
            ->join('pegawai', 'pegawai.nip_baru = pengajuan.target_nip', 'left')
            ->where('pengajuan.tahun', $tahun);

        if (!empty($periode)) {
            $query->where('pengajuan.periode', $periode);
        }

        if (!empty($status)) {
            $query->where('pengajuan.status', $status);
        }

        $data = [
            'title' => 'Laporan Kenaikan Pangkat',
            'riwayat' => $query->orderBy('pengajuan.created_at', 'DESC')->findAll(),
            'filter' => [
                'periode' => $periode,
                'tahun' => $tahun,
                'status' => $status
            ]
        ];

        return view('admin/laporan/index', $data);
    }

    public function cetak()
    {
        // For printing or export purposes later
    }
}
