<?php

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



class Dashboard extends CI_Controller {

	

    public function __construct(){

        parent::__construct();		

        $this->load->model('M_dashboard');

    }
    private function date_to_date($date){
        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0];
    }
    private function bulan($b){
        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        return $bulan[intval($b)-1];
    }
    private function periode_thn(){

    	$row = $this->M_dashboard->periode_thn()->row_array();

    	return $row['periode_thn'];

    }

    public function periode_thn_view(){

    	$row = $this->M_dashboard->periode_thn()->row_array();

    	echo json_encode($row['periode_thn']);

    }

    public function tot_kegiatan(){

    	$num = $this->M_dashboard->data_pka($this->periode_thn())->num_rows();

    	echo json_encode($num);

    }

    public function dashboard_pengeluaran(){

    	$ttt_pengeluaran = 0;

        $result = $this->M_dashboard->data_pka($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_pengeluaran = $this->M_dashboard->data_pka_pengeluaran($result[$i]['kode_pka'])->result_array();

        	$tot_pengeluaran =0;

        	for($u=0; $u<count($res_pengeluaran);$u++){

        		$tot_pengeluaran += intval($res_pengeluaran[$u]["pengeluaran"]);

        	}

        	$ttt_pengeluaran += $tot_pengeluaran;

        }

        echo json_encode($ttt_pengeluaran);

    }

    public function dashboard_penerimaan(){

    	$ttt_penerimaan = 0;

        $result = $this->M_dashboard->data_pka($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_penerimaan = $this->M_dashboard->data_pka_penerimaan($result[$i]['kode_pka'])->result_array();

        	$tot_penerimaan =0;

        	for($u=0; $u<count($res_penerimaan);$u++){

        		$tot_penerimaan += intval($res_penerimaan[$u]["penerimaan"]);

        	}

        	$ttt_penerimaan += $tot_penerimaan;



            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);

            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);

        }

        echo json_encode($ttt_penerimaan);

    }

    public function tot_kegiatan_tersimpan(){

    	$num = $this->M_dashboard->data_pka_tersimpan($this->periode_thn())->num_rows();

    	echo json_encode($num);

    }

    public function dashboard_pengeluaran_tersimpan(){

    	$ttt_pengeluaran = 0;

        $result = $this->M_dashboard->data_pka_tersimpan($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_pengeluaran = $this->M_dashboard->data_pka_pengeluaran_tersimpan($result[$i]['kode_pka'])->result_array();

        	$tot_pengeluaran =0;

        	for($u=0; $u<count($res_pengeluaran);$u++){

        		$tot_pengeluaran += intval($res_pengeluaran[$u]["pengeluaran"]);

        	}

        	$ttt_pengeluaran += $tot_pengeluaran;

        }

        echo json_encode($ttt_pengeluaran);

    }

    public function dashboard_penerimaan_tersimpan(){

    	$ttt_penerimaan = 0;

        $result = $this->M_dashboard->data_pka_tersimpan($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_penerimaan = $this->M_dashboard->data_pka_penerimaan_tersimpan($result[$i]['kode_pka'])->result_array();

        	$tot_penerimaan =0;

        	for($u=0; $u<count($res_penerimaan);$u++){

        		$tot_penerimaan += intval($res_penerimaan[$u]["penerimaan"]);

        	}

        	$ttt_penerimaan += $tot_penerimaan;



            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);

            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);

        }

        echo json_encode($ttt_penerimaan);

    }















    /////////////////////admin////////////////////////////////

    ////////////////////////////////////admin///////////////////////////

    //////////////////////////////////////////////////////admin/////////////////////////////////////////////



    public function tot_kegiatan_admin(){

    	$num = $this->M_dashboard->data_pka_admin($this->periode_thn())->num_rows();

    	echo json_encode($num);

    }

    public function dashboard_pengeluaran_admin(){

    	$ttt_pengeluaran = 0;

        $result = $this->M_dashboard->data_pka_admin($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_pengeluaran = $this->M_dashboard->data_pka_pengeluaran_admin($result[$i]['kode_pka'])->result_array();

        	$tot_pengeluaran =0;

        	for($u=0; $u<count($res_pengeluaran);$u++){

        		$tot_pengeluaran += intval($res_pengeluaran[$u]["pengeluaran"]);

        	}

        	$ttt_pengeluaran += $tot_pengeluaran;



            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);

            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);

        }

        echo json_encode($ttt_pengeluaran);

    }

    public function dashboard_penerimaan_admin(){

    	$ttt_penerimaan = 0;

        $result = $this->M_dashboard->data_pka_admin($this->periode_thn())->result_array();

        for($i=0; $i<count($result);$i++){

        	$res_penerimaan = $this->M_dashboard->data_pka_penerimaan_admin($result[$i]['kode_pka'])->result_array();

        	$tot_penerimaan =0;

        	for($u=0; $u<count($res_penerimaan);$u++){

        		$tot_penerimaan += intval($res_penerimaan[$u]["penerimaan"]);

        	}

        	$ttt_penerimaan += $tot_penerimaan;



            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);

            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);

        }

        echo json_encode($ttt_penerimaan);

    }
    public function chart_log(){
        $bulan = date("m");
        $tahun = date("Y");
        $jumHari = cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
        $a = array();
        $b = array();
        for ($i=1; $i <= $jumHari; $i++) { 
            $tgl = strlen($i)==1 ? '0'.$i : $i;
            $date = $tahun."-".$bulan."-".$tgl;
            
            $result = $this->M_dashboard->chart_log($date)->row_array();
            $c = $result['c']=="" ? 0 : (integer)$result['c'];
            array_push($a, $this->date_to_date($date));
            array_push($b, $c);
        }
        $json['data'] = $b;
        $json['label'] = $a;
        $json['bulan'] = $this->bulan($bulan);
        echo json_encode($json);
    }




}

