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

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

    function get_accounting_entries() {
    	$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
				require_once '../../../library_html/accounting/core/lib/lib_acc_basic.php';
	    		$month = get_acc_month();
				$str_length = 2;
	    		$month = substr("00{$month}", -$str_length);
	    		$year = get_acc_year();
				$str_length = 4;
	    		$year = substr("0000{$year}", -$str_length);
				$page = $l_card->get_card(['title'=>'Create Accounting Entry - '.$result[0]['name'].' ('.$result[0]['org_id'].') - '.$month.'/'.$year]);
				$inputs = get_inputs_2();
				foreach ($inputs as $y => $x) {
					$page->children[0]->children[] = $x;
				}
				return $page;
			}
		}
		return [];
	}

	function create_input_pair_2($_p) {
		//id
		//placeholder
		//description
		//label
		//value

		$elem = new Element('div', null);
        $elem->properties['class'] = ['form-group','mb-2'];

        if (isset($_p['label'])) {
	        $elem->children[0] = new Element('label', null);
	        $elem->children[0]->properties['for'] = [$_p['id']];
	        $elem->children[0]->text = $_p['label'];
	    }

        $elem->children[1] = new Element('input', null);
        if (isset($_p['id'])) $elem->children[1]->properties['id'] = [$_p['id']];
        $elem->children[1]->properties['class'] = ['form-control','form-control-sm'];
        $elem->children[1]->properties['type'] = ['text'];
        $elem->children[1]->properties['aria-describedby'] = ['textHelp'];
        if (isset($_p['value'])) $elem->children[1]->properties['value'] = [$_p['value']];
        if (isset($_p['placeholder'])) $elem->children[1]->properties['placeholder'] = [$_p['placeholder']];

        if (isset($_p['description'])) {
	        $elem->children[2] = new Element('small', null);
	        $elem->children[2]->properties['class'] = ['form-text','text-muted'];
	        $elem->children[2]->text = $_p['description'];
	    }

        return $elem;
	}

	function create_select_pair_2($_p) {
		//id
		//placeholder
		//description
		//label
		//options
		//value

		$elem = new Element('div', null);
        $elem->properties['class'] = ['form-group'];

        if (isset($_p['label'])) {
	        $elem->children[0] = new Element('label', null);
	        $elem->children[0]->properties['for'] = ['acc_ins_entry-ammount'];
	        $elem->children[0]->text = $_p['label'];
	    }

        $elem->children[1] = new Element('select', null);
        if (isset($_p['id'])) $elem->children[1]->properties['id'] = [$_p['id']];
        $elem->children[1]->properties['class'] = ['form-control','form-control-sm'];
        $elem->children[1]->properties['type'] = ['text'];
        $elem->children[1]->properties['aria-describedby'] = ['textHelp'];
        if (isset($_p['value'])) $elem->children[1]->properties['value'] = [$_p['value']];
        if (isset($_p['placeholder'])) $elem->children[1]->properties['placeholder'] = [$_p['placeholder']];

        if (isset($_p['options'])) {
        	foreach ($_p['options'] as $y => $x) {
	        	$elem->children[1]->children[$y] = new Element('option', null);
	        	$elem->children[1]->children[$y]->text = $x;
	        }

        }

        if (isset($_p['description'])) {
	        $elem->children[2] = new Element('small', null);
	        $elem->children[2]->properties['class'] = ['form-text','text-muted'];
	        $elem->children[2]->text = $_p['description'];
	    }

        return $elem;
	}

	function get_inputs_2() {
		$items = [];

		//Day
		require_once '../../../library_html/accounting/core/lib/lib_acc_basic.php';
	    include_once '../../../library_html/base/dates/dat_dates.php';
		$year = get_acc_year();
		$month = get_acc_month();
		$day = get_acc_day();
		$days = get_days_of_month(['year'=>$year]);
		$total = $days[$month - 1];
		$days_options = [];
		for ($i = 0; $i < $total; $i++) {
			$days_options[] = ($i + 1);
		}
		if ($day > $total) $day = $total;
		$items[] = create_select_pair(['id'=>'acc-ins-ent-day', 'description'=>'Day on which transaction was completed', 'label'=>'Day', 'value'=>$day, 'options'=>$days_options]);

		//From and to
		$accounts = get_acc_accounts_list();
		$acc_options = ['__NONE__ - None'];
		for ($i = 0; $i < count($accounts); $i++) {
			$acc_options[] = $accounts[$i]['account_id']." - ".$accounts[$i]['name'];
		}
		$items[] = create_select_pair_2(['id'=>'acc-ins-ent-from', 'description'=>'From which account', 'label'=>'From', 'options'=>$acc_options]);
		$items[] = create_select_pair_2(['id'=>'acc-ins-ent-to', 'description'=>'To which account', 'label'=>'To', 'options'=>$acc_options]);
		$items[] = create_input_pair_2(['id'=>'acc-ins-ent-ammount','placeholder'=>'0.00', 'description'=>'Transaction ammount', 'label'=>'Ammount']);
		$items[] = create_input_pair_2(['id'=>'acc-ins-ent-description', 'description'=>'Why was this entry made?', 'label'=>'Description']);
		
		//Buttons
		$submit = new Element('button', null);
		$submit->text = "Submit";
		$submit->properties['class'] = ['btn','btn-primary','btn-sm'];
		$submit->properties['id'] = ['acc-ins-ent-submit'];
		$submit->properties['onclick'] = ['acc_ins_ent_submit();'];
		$items[] = $submit;

		$cancel = new Element('button', null);
		$cancel->text = "Cancel";
		$cancel->properties['class'] = ['btn','btn-secondary','btn-sm'];
		$cancel->properties['id'] = ['acc-ins-ent-cancel'];
		$cancel->properties['onclick'] = ["acc_ins_ent_cancel('".$day."');"];
		$items[] = $cancel;

		return $items;
	}
?>