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

class Kas extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        if(!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
        $this->load->model('Kas_model');
        $this->load->model('Master_kas_model'); // Model Anda
        $this->load->library('form_validation');
        $this->load->helper('url');
    }

    public function index() {
        $data['title'] = 'Kas RT';
        $data['kas'] = $this->Kas_model->get_all();
        $data['saldo'] = $this->Kas_model->get_saldo();
        
        // Gunakan method dari model Anda
        $data['ringkasan_kas'] = $this->Master_kas_model->get_ringkasan_saldo();
        
        $data['pemasukan_bulan_ini'] = $this->Kas_model->get_pemasukan_bulan_ini();
        $data['pengeluaran_bulan_ini'] = $this->Kas_model->get_pengeluaran_bulan_ini();
        
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('kas/index', $data);
        $this->load->view('templates/footer');
    }

    public function pemasukan() {
        $data['title'] = 'Tambah Pemasukan';
        
        // Gunakan method get_aktif() dari model Anda (sudah include saldo_realtime)
        $data['kas_list'] = $this->Master_kas_model->get_aktif();

        $this->form_validation->set_rules('nominal', 'Nominal', 'required|numeric');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
        $this->form_validation->set_rules('master_kas_id', 'Kas Tujuan', 'required');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('kas/pemasukan', $data);
            $this->load->view('templates/footer');
        } else {
            $data_kas = array(
                'tanggal' => $this->input->post('tanggal'),
                'jenis' => 'pemasukan',
                'kategori' => $this->input->post('kategori'),
                'nominal' => $this->input->post('nominal'),
                'keterangan' => $this->input->post('keterangan'),
                'master_kas_id' => $this->input->post('master_kas_id'),
                'input_oleh' => $this->session->userdata('user_id')
            );

            $this->Kas_model->insert($data_kas);
            $this->session->set_flashdata('success', 'Pemasukan berhasil ditambahkan');
            redirect('kas');
        }
    }

    public function pengeluaran() {
        $data['title'] = 'Tambah Pengeluaran';
        
        // Gunakan method get_aktif() dari model Anda (sudah include saldo_realtime)
        $data['kas_list'] = $this->Master_kas_model->get_aktif();

        $this->form_validation->set_rules('nominal', 'Nominal', 'required|numeric');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
        $this->form_validation->set_rules('master_kas_id', 'Kas Sumber', 'required');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('kas/pengeluaran', $data);
            $this->load->view('templates/footer');
        } else {
            // Validasi saldo cukup
            $master_kas_id = $this->input->post('master_kas_id');
            $nominal = $this->input->post('nominal');
            
            // Gunakan method hitung_saldo() dari model Anda
            $saldo_tersedia = $this->Master_kas_model->hitung_saldo($master_kas_id);
            
            if($saldo_tersedia < $nominal) {
                $this->session->set_flashdata('error', 'Saldo kas tidak mencukupi! Saldo tersedia: Rp ' . number_format($saldo_tersedia, 0, ',', '.'));
                redirect('kas/pengeluaran');
            }

            $data_kas = array(
                'tanggal' => $this->input->post('tanggal'),
                'jenis' => 'pengeluaran',
                'kategori' => $this->input->post('kategori'),
                'nominal' => $nominal,
                'keterangan' => $this->input->post('keterangan'),
                'master_kas_id' => $master_kas_id,
                'input_oleh' => $this->session->userdata('user_id')
            );

            // Handle upload bukti dokumen jika ada
            if(!empty($_FILES['bukti_dokumen']['name'])) {
                $config['upload_path'] = './uploads/bukti/';
                $config['allowed_types'] = 'jpg|jpeg|png|pdf';
                $config['max_size'] = 2048;
                $config['file_name'] = time() . '_' . preg_replace('/[^a-zA-Z0-9]/', '_', $_FILES['bukti_dokumen']['name']);
                
                $this->load->library('upload', $config);
                
                if($this->upload->do_upload('bukti_dokumen')) {
                    $data_kas['bukti_dokumen'] = $this->upload->data('file_name');
                }
            }

            $this->Kas_model->insert($data_kas);
            $this->session->set_flashdata('success', 'Pengeluaran berhasil ditambahkan');
            redirect('kas');
        }
    }

    public function laporan() {
        $data['title'] = 'Laporan Kas';
        $bulan = $this->input->get('bulan') ?: date('m');
        $tahun = $this->input->get('tahun') ?: date('Y');
        
        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['kas_list'] = $this->Master_kas_model->get_aktif();
        $data['pemasukan'] = $this->Kas_model->get_pemasukan_periode($bulan, $tahun);
        $data['pengeluaran'] = $this->Kas_model->get_pengeluaran_periode($bulan, $tahun);
        $data['total_pemasukan'] = $this->Kas_model->get_total_pemasukan_periode($bulan, $tahun);
        $data['total_pengeluaran'] = $this->Kas_model->get_total_pengeluaran_periode($bulan, $tahun);
        
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('kas/laporan', $data);
        $this->load->view('templates/footer');
    }
}