<?php

namespace App\Controllers\Owner;

use App\Controllers\BaseController;
use App\Models\FinanceModel;

class Profit extends BaseController
{
    public function getChartData()
    {
        // Berfungsi menyuplai data JSON ke ChartJS di halaman View
        $financeModel = new FinanceModel();
        
        // Contoh mengambil tren laba 6 bulan terakhir
        $chartData = [];
        for ($i = 5; $i >= 0; $i--) {
            $month = date('Y-m', strtotime("-$i month"));
            $start = $month . "-01";
            $end   = date('Y-m-t', strtotime($start));
            
            $chartData['labels'][] = date('F Y', strtotime($start));
            $chartData['data'][]   = $financeModel->getLabaPeriode($start, $end) ?? 0;
        }

        return $this->response->setJSON($chartData);
    }
}