<?php

	require_once '../../../library_html/elements/core/lib/lib_elementsCore.php';
	require_once '../../../library_html/components/card/lib/lib_card.php';
	require_once '../../../library_html/components/table/lib/lib_table.php';

    function draw_acc_accounts() {
	    //Elements
	    include_once '../../../library_html/elements/core/lib/lib_elementsDraw.php';
	    //Accounting
	    include_once '../../../library_html/accounting/core/js/js_acc_accounts.php';
	    //include_once '../../../library_html/technical/core/js/js_technicalNavButtons.php';
	    $page = get_accounting_accounts();

	    //Draw page
	    new drawElement($page);
	}

    function get_accounting_accounts() {
    	$l_card = new lib_card();
    	$l_table = new lib_table();

		require_once '../../../library_html/properties/core/dat/dat_properties.php';
    	$dat = new dat_properties();
    	$result = $dat->select_settings(["user_account"=>$_COOKIE['user_account'],"property"=>"ACC_SEL_ORG"]);

    	if (count($result) > 0) {
	    	$org_id = $result[0]['setting'];

			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();
	    	$result = $dat->select_acc_organizations(["org_id"=>$org_id]);

	    	if (count($result) > 0) {

				//Get background
				$page = $l_card->get_card(['title'=>'Accounts - '.$result[0]['name'].' ('.$result[0]['org_id'].')']);
				$page->children[0]->children[1] = $l_table->get_table(null);
				$page->children[0]->children[1]->children[0]->children[] = [];
				$object = $page->children[0]->children[1]->children[0]->children[0];

				$lines = [];
				$lines[0] = get_acc_accounts_header();
				$lines = get_acc_accounts_rows($lines);
				$object = $page->children[0]->children[1]->children[0]->children = $lines;
				return $page;
			}
		}
		return [];
	}

	function get_acc_accounts_header() {
		require_once '../../../library_html/elements/table/lib/table.php';
		return Tr([
			['type'=>true,'item'=>'Account ID'],
			['type'=>true,'item'=>'Organization'],
			['type'=>true,'item'=>'Name'],
			['type'=>true,'item'=>'Cash'],
			['type'=>true,'item'=>'Type'],
			['type'=>true,'item'=>'Value']
		], null);
	}

	function get_acc_accounts_rows($lines) {
		require_once '../../../library_html/properties/core/dat/dat_properties.php';
    	$dat = new dat_properties();
    	$result = $dat->select_settings(["user_account"=>$_COOKIE['user_account'],"property"=>"ACC_SEL_ORG"]);
    	if (count($result) > 0) {
			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();
	    	$org_id = $result[0]['setting'];
			$result = $dat->select_acc_accounts(["org_id"=>$org_id]);
			require_once '../../../library_html/accounting/core/dat/dat_accounting_adv.php';
	    	$dat = new dat_accounting_adv();
			$values = $dat->select_acc_org_latest_totals(["org_id"=>$org_id]);
			$total = 0;
			foreach($result as $y => $x) {
				$value = 0;
				for ($i = 0; $i < count($values); $i++) {
					if ($values[$i]['account_id'] == $x['account_id']) {
						$value = $values[$i]['ammount'];
						break;
					}
				}
				$total += $value;
				$cash = "VALUE";
				if ($x['cash'] == 'C') $cash = "CASH";
				$acc_type = "ASSET";
				if ($x['acc_type'] == 'L') $acc_type = "LIABILITY";
				$lines[1 + $y] = Tr([
					['type'=>false,'item'=>$x['account_id']],
					['type'=>false,'item'=>$x['org_id']],
					['type'=>false,'item'=>$x['name']],
					['type'=>false,'item'=>$cash],
					['type'=>false,'item'=>$acc_type],
					['type'=>false,'item'=>number_format((float)$value, 2, '.', '')]
				], null);
			}
			$lines[] = Tr([
				['type'=>true,'item'=>"TOTAL"],
				['type'=>true,'item'=>""],
				['type'=>true,'item'=>""],
				['type'=>true,'item'=>""],
				['type'=>true,'item'=>""],
				['type'=>true,'item'=>number_format((float)$total, 2, '.', '')]
			], null);
			return $lines;
		} else {
			//Let user know that no organization is selected
		}
	}
?>