<?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_dates() {
	    //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_dates() {
    	$l_card = new lib_card();
    	$l_table = new lib_table();

		//Get background
		require_once '../../../library_html/accounting/core/lib/lib_acc_basic.php';
		$page = $l_card->get_card([]);
		$inputs = get_inputs();
		foreach ($inputs as $y => $x) {
			$page->children[0]->children[] = $x;
		}
		return $page;
	}

	function create_input_pair($_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($_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['value']) && $_p['value'] == $x) 
	        	$elem->children[1]->children[$y]->tags[] = 'selected';
	        }

        }

        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() {
		$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();
		$month_options = [];
		for ($i = 1; $i <= 12; $i++) {
			$month_options[] = $i;
		}

		//From and to
		$acc_options = [];
		for ($i = 2017; $i <= 2020; $i++) {
			$acc_options[] = $i;
		}
		$items[] = create_select_pair(['id'=>'acc-ins-date-month', 'label'=>'Month', 'options'=>$month_options, 'value'=>$month]);
		$items[] = create_select_pair(['id'=>'acc-ins-date-year', 'label'=>'Year', 'options'=>$acc_options, 'value'=>$year]);
		
		//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_date_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_date_cancel(".$month.",".$year.");"];
		$items[] = $cancel;

		return $items;
	}
?>