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

class Bukubesar extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('DynamicModel', 'dm');

        if (!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
    }

    public function index()
    {

        if($this->session->userdata('role') != 'Admin'){
            show_error('Akses ditolak', 403);
        }
        
        $data = ["active_menu" => "buku_besar"];
        $this->load->view('partials/header', $data);
        $this->load->view('buku_besar/layout');
        $this->load->view('partials/footer');
    }

    // =========================
    // GET ACCOUNTS (FILTER)
    // =========================
    public function get_accounts()
    {
        $data = $this->dm->setTable('accounts')
                         ->order_by('kode_akun','ASC')
                         ->get();

        echo json_encode($data);
    }

    // =========================
    // GET DATA BUKU BESAR
    // =========================
    public function get_data()
    {
        $account_id = $this->input->get('account_id');
        $start      = $this->input->get('start_date');
        $end        = $this->input->get('end_date');

        $this->db->select('
            journals.tanggal,
            journals.no_ref,
            journals.keterangan,
            journal_details.debit,
            journal_details.kredit,
            accounts.nama_akun,
            accounts.tipe
        ');
        $this->db->from('journal_details');
        $this->db->join('journals','journals.id = journal_details.journal_id');
        $this->db->join('accounts','accounts.id = journal_details.account_id');

        if ($account_id) {
            $this->db->where('journal_details.account_id', $account_id);
        }

        if ($start && $end) {
            $this->db->where('journals.tanggal >=', $start);
            $this->db->where('journals.tanggal <=', $end);
        }
        // 🔥 FILTER DEBIT/KREDIT TIDAK BOLEH 0 SEMUA
        $this->db->group_start();
        $this->db->where('journal_details.debit >', 0);
        $this->db->or_where('journal_details.kredit >', 0);
        $this->db->group_end();
        
        $this->db->order_by('journals.tanggal','ASC');

        $rows = $this->db->get()->result();

        // =========================
        // HITUNG SALDO BERJALAN
        // =========================
        $saldo = 0;
        $result = [];

        foreach ($rows as $r) {

            // LOGIKA SALDO BERDASARKAN TIPE AKUN
            if (in_array($r->tipe, ['asset','expense'])) {
                $saldo += ($r->debit - $r->kredit);
            } else {
                $saldo += ($r->kredit - $r->debit);
            }

            $result[] = [
                'tanggal' => $r->tanggal,
                'no_ref' => $r->no_ref,
                'keterangan' => $r->keterangan,
                'debit' => $r->debit,
                'kredit' => $r->kredit,
                'saldo' => $saldo
            ];
        }

        echo json_encode($result);
    }
}