<?php namespace App\Controllers;

use CodeIgniter\Session\Session;
use DateInterval;
use Config\App;

class Charts extends BaseController {
    private $db;
	public function __construct() {
        $this->db = \Config\Database::connect();
	}

    public function getData($id){
        if ($id == 0)
            return $this->getAllData();
        else
            return $this->getDetailData($id);
    }

    private function getDetailData($id){
        $jetzt = date('Y-m-01');
        $query = <<<EOD
        SELECT 
        SUM(subamount)*(-1) AS Amount,
        description,account_id as id
        FROM 
        finanzen.budget_billdetails, finanzen.budget_bills,finanzen.budget_accounts 
        WHERE 
        datum >= '$jetzt'
        AND (datum < '$jetzt'::date + interval '1 month')
        AND budget_bills.id = bill_id 
        AND account_id = finanzen.budget_accounts.id
        AND (account_id IN (
            WITH RECURSIVE subordinates AS (
            SELECT
                id, 
                parent_id, 
                description
            FROM
                finanzen.budget_accounts
            WHERE
                id = $id
            UNION
                SELECT
                e.id,
                e.parent_id,
                e.description
                FROM
                finanzen.budget_accounts e
                INNER JOIN subordinates s ON s.id = e.parent_id) 
            SELECT
                id
            FROM
                subordinates
        ))
        GROUP BY description, account_id
        EOD;
    $data = array();
    $werte = array();
    $ids = array();
    $labels = array();
    $result = $this->db->query($query);
    foreach ($result->getResult() as $row) {
        $werte[] = $row->amount;
        $ids[] = $row->id;
        $labels[] = $row->description;
    } 
    $data['data'] = json_encode($werte);
    $data['labels'] = json_encode($labels);
    $data['ids'] = json_encode($ids);
    $data['title'] = "Kategorieübersicht > ".$jetzt;
    return view("charts/monthly", $data);
}
    private function getAllData(){
        $jetzt = date('Y-m-01');
        $query = <<<EOD
        SELECT * FROM (
            SELECT id, description,sum(amount)as summe FROM (SELECT id,description from finanzen.budget_accounts WHERE parent_id=0 AND type='output' AND LKZ is null) as w,
            LATERAL  
            (SELECT ABS(SUM(subamount)) as amount
                 FROM finanzen.budget_billdetails, finanzen.budget_bills 
                 WHERE datum >= '$jetzt' 
                     AND budget_bills.id = bill_id 
                     AND (account_id IN 
                         (
                WITH RECURSIVE subordinates AS (
                  SELECT
                    id, 
                    parent_id, 
                    description
                  FROM
                    finanzen.budget_accounts
                  WHERE
                    id = w.id
                  UNION
                    SELECT
                      e.id,
                      e.parent_id,
                      e.description
                    FROM
                      finanzen.budget_accounts e
                    INNER JOIN subordinates s ON s.id = e.parent_id) 
                  SELECT
                      id
                  FROM
                      subordinates
              ))
             ) AS x GROUP BY description, id) as i WHERE summe is not null;
    EOD;
    $data = array();
    $result = $this->db->query($query);
    foreach ($result->getResult() as $row) {
        $werte[] = $row->summe;
        $ids[] = $row->id;
        $labels[] = $row->description;
    } 
    $data['data'] = json_encode($werte);
    $data['labels'] = json_encode($labels);
    $data['ids'] = json_encode($ids);
    $data['title'] = "Monatsübersicht > ".$jetzt;
    return view("charts/monthly", $data);

//              SELECT description,amount FROM (SELECT id,description from finanzen.budget_accounts WHERE parent_id=0 AND type='output' AND LKZ is null) as w,
// LATERAL  
// (SELECT subamount as amount
//  	FROM finanzen.budget_billdetails, finanzen.budget_bills 
//  	WHERE datum >= DATE_TRUNC('month',current_date) 
//  		AND budget_bills.id = bill_id 
//  		AND (account_id IN 
// 			 (SELECT id from finanzen.budget_accounts WHERE (id = w.id OR parent_id = w.id))
// 			)
//  ) AS x
    }
}