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

class SlipGaji extends Auth_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Payroll_model');
        $this->load->model('Pegawai_model');
        $this->load->helper('app_helper');
    }

    /**
     * Arsip slip gaji - daftar semua slip yang bisa dicetak
     */
    public function index()
    {
        $id_pegawai  = $this->input->get('id_pegawai');
        $tahun       = $this->input->get('tahun') ?: date('Y');
        $bulan       = $this->input->get('bulan') ?: date('n');
        $tipe_periode = $this->input->get('tipe_periode') ?: '';

        $periode_list = $this->Payroll_model->get_periode_list(['APPROVED', 'LOCKED']);
        $pegawai_list = $this->Pegawai_model->get_all();
        $slips        = $this->Payroll_model->get_payroll_arsip(null, $id_pegawai, $tahun ?: null, $bulan ?: null, $tipe_periode ?: null);

        $this->render('output/slip_gaji/arsip', [
            'title'        => 'Arsip Slip Gaji',
            'periode_list' => $periode_list,
            'pegawai_list' => $pegawai_list,
            'slips'        => $slips,
            'filter'       => [
                'id_pegawai' => $id_pegawai,
                'tahun'      => $tahun,
                'bulan'      => $bulan,
                'tipe_periode' => $tipe_periode,
            ],
        ]);
    }

    /**
     * Cetak slip gaji individual (HTML printable)
     */
    public function cetak($id_payroll)
    {
        $slip = $this->Payroll_model->get_payroll_single((int) $id_payroll);
        if (!$slip) {
            show_404();
        }

        $komponen = $this->Payroll_model->get_komponen_by_payroll((int) $id_payroll);

        // Pisah komponen pendapatan dan potongan
        $pendapatan = array_filter($komponen, function($k) {
            return in_array($k->jenis, ['GAPOK', 'TUNJANGAN']);
        });
        array_unshift($pendapatan, (object) [
            'id_komponen'   => null,
            'nama_komponen' => 'Gaji Pokok',
            'jenis'         => 'TUNJANGAN',
            'tipe_nilai'    => 'NOMINAL',
            'nilai'         => (float) ($slip->gaji_pokok ?? 0),
            'jumlah_hari'   => null,
            'sumber'        => 'SYSTEM_DEFAULT',
        ]);
        $potongan = array_filter($komponen, function($k) {
            return $k->jenis === 'POTONGAN';
        });

        // Muat view print (tanpa template sidebar)
        $this->load->view('output/slip_gaji/cetak', [
            'title'      => 'Slip Gaji - ' . $slip->nama_lengkap,
            'slip'       => $slip,
            'pendapatan' => array_values($pendapatan),
            'potongan'   => array_values($potongan),
        ]);
    }

    /**
     * Cetak slip kolektif per periode (semua pegawai di 1 halaman)
     */
    public function kolektif($id_periode)
    {
        $periode = $this->Payroll_model->get_periode_by_id((int) $id_periode);
        if (!$periode || !in_array($periode->status, ['APPROVED', 'LOCKED'])) {
            $this->session->set_flashdata('error', 'Periode tidak valid atau belum approved.');
            redirect('output/slip_gaji');
        }

        $payroll_list = $this->Payroll_model->get_payroll_detail((int) $id_periode);

        $data_slips = [];
        foreach ($payroll_list as $p) {
            $komponen   = $this->Payroll_model->get_komponen_by_payroll($p->id_payroll);
            $pendapatan = array_values(array_filter($komponen, function($k) {
                return in_array($k->jenis, ['GAPOK', 'TUNJANGAN']);
            }));
            array_unshift($pendapatan, (object) [
                'id_komponen'   => null,
                'nama_komponen' => 'Gaji Pokok',
                'jenis'         => 'TUNJANGAN',
                'tipe_nilai'    => 'NOMINAL',
                'nilai'         => (float) ($p->gaji_pokok ?? 0),
                'jumlah_hari'   => null,
                'sumber'        => 'SYSTEM_DEFAULT',
            ]);
            $potongan = array_values(array_filter($komponen, function($k) {
                return $k->jenis === 'POTONGAN';
            }));

            $data_slips[] = [
                'slip'       => $p,
                'pendapatan' => $pendapatan,
                'potongan'   => $potongan,
            ];
        }

        $this->load->view('output/slip_gaji/kolektif', [
            'title'      => 'Slip Gaji Kolektif - ' . bulan_indonesia($periode->bulan) . ' ' . $periode->tahun . ' (' . tgl_indonesia($periode->tanggal_mulai) . ' s/d ' . tgl_indonesia($periode->tanggal_selesai) . ')',
            'periode'    => $periode,
            'data_slips' => $data_slips,
        ]);
    }
}
