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

class Dashboard extends Auth_Controller
{
    public function index()
    {
        $this->load->model('Dashboard_model');

        $periode_aktif = $this->Dashboard_model->get_periode_aktif();

        $data = array(
            'title'          => 'Dashboard | ' . app_name(),
            'page_title'     => 'Dashboard',
            'page_subtitle'  => 'Ringkasan operasional payroll periode aktif',
            'stat'           => $this->Dashboard_model->get_stat_cards($periode_aktif),
            'periode_aktif'  => $periode_aktif,
            'divisi_chart'   => $this->Dashboard_model->get_pegawai_per_divisi(),
            'payroll_chart'  => $this->Dashboard_model->get_payroll_trend(6),
            'batch_terbaru'  => $this->Dashboard_model->get_batch_terbaru(5),
            'aktivitas'      => $this->Dashboard_model->get_aktivitas_terbaru(8),
        );

        $this->render('dashboard/index', $data);
    }

    // AJAX: ringkasan stat cards (untuk auto-refresh)
    public function ajax_stat()
    {
        $this->load->model('Dashboard_model');
        $periode_aktif = $this->Dashboard_model->get_periode_aktif();
        $stat = $this->Dashboard_model->get_stat_cards($periode_aktif);
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(['success' => true, 'data' => $stat]));
    }
}
