<?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_org() {
	    //Elements
	    include_once '../../../library_html/elements/core/lib/lib_elementsDraw.php';
	    //Accounting
	    include_once '../../../library_html/accounting/core/js/js_acc_organizations.php';
	    //include_once '../../../library_html/technical/core/js/js_technicalNavButtons.php';
	    $page = get_accounting_organizations();

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

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

		//Get background
		$page = $l_card->get_card(['title'=>'Organizations']);
		$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_org_header();
		$lines = get_acc_org_rows($lines);
		$object = $page->children[0]->children[1]->children[0]->children = $lines;
		return $page;
	}

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

		//Get background
		$page = $l_card->get_card(['title'=>'Organizations']);
		$page->children[0]->children[1] = $l_table->get_table();
		$object = $page->children[0]->children[1]->children[0]->children[0];
		$lines = [];
		$lines[0] = get_acc_org_header();
		$lines = get_acc_org_rows($lines);
		$object->children = $lines;
		return $page;
	}

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

	function get_acc_org_rows($lines) {
		require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
    	$dat = new dat_accounting();
		$result = $dat->select_acc_organizations(null);

		foreach($result as $y => $x) {
			$lines[1 + $y] = Tr([
				['type'=>false,'item'=>$x['org_id']],
				['type'=>false,'item'=>$x['name']],
				['type'=>false,'item'=>$x['code']],
				['type'=>false,'item'=>'<button type="button" class="btn btn-info btn-sm" onclick="select_acc_org(\''.$x['org_id'].'\');">Select</button>']
			], null);
		}
		return $lines;
	}
?>