<?php

namespace App\Controllers;
use App\Models\mBills;
use CodeIgniter\Session\Session;


class Ajax extends BaseController
{

	protected $session;

	public function __construct()
	{
		$this->session = service('session');
	}

	public function getTabelData(){
		if (!$this->request->isAJAX()) return;
		$billing = model('App\Models\mBills');
		$start = $this->request->getPost('datum_start')??session('datum_start');
		$ende = $this->request->getPost('datum_ende')??session('datum_ende');
		$src = intval($this->request->getPost('source')) ?? 0;
		$_SESSION['datum_start'] = $start;
		$_SESSION['datum_ende'] = $ende;
		$_SESSION['source'] = $src;
		$result = $billing->getEntriesDet($start,$ende,$src);
		return json_encode($result);
	}

	public function getAccountsBalance(){
		if (!$this->request->isAJAX()) return;
		$billing = model('App\Models\mBills');
		return json_encode($billing->getAccountsBalance());
	}

	public function getScheduledData(){
		if (!$this->request->isAJAX()) return;
		$scheduled = model('App\Models\mScheduled');
		return json_encode($scheduled->getEntries());		
	}

	public function getToDosData(){
		if (!$this->request->isAJAX()) return;
		$billing = model('App\Models\mBills');
		return json_encode($billing->getToDos());
	}

	public function getOverviewData(){
		if (!$this->request->isAJAX()) return;
		$billing = model('App\Models\mBills');
		$result = $billing->getEntries();
        return json_encode($result);
	}

	public function deleteBill(){
		if (!$this->request->isAJAX()) return;		
		$bills = model('App\Models\mBills');
		$bills->deleteEntry($this->request->getPost('id'));
		return json_encode(["result"=>"done"]);
	}

	public function deleteScheduled(){
		if (!$this->request->isAJAX()) return;		
		$scheduled = model('App\Models\mScheduled');
		$scheduled->delete($this->request->getPost('id'));
		return json_encode(["result"=>"done"]);
	}
}

?>