<?php


if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('APP_PATH_ROOT')) define('APP_PATH_ROOT', dirname(__FILE__));
if (!defined('APP_PATH_WWW')) define('APP_PATH_WWW', dirname(__FILE__));
if (!defined('APP_PATH_CONFIG')) define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');


function USERS2_PRODUKT() {
	global $thiss;

	require_once dirname(__FILE__) . '/' . 'se-lib' . '/' . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('DB');


	$params = array();
	$params['toolbar'] = array();
	$params['toolbar'] []= '<a href="' . "?MENU_INIT=USERS2_PRODUKT" . "&ARG1=TREE" . '" title="' . "Drzewo" . '">' . "TREE" . '</a>';
	$params['toolbar'] []= '<a href="' . "?MENU_INIT=USERS2_PRODUKT" . "&ARG1=SES_TREE" . '" title="' . "Drzewo usług" . '">' . "SERVICE_TREE_TEST" . '</a>';

	// init columns if not set
	if (empty($_SESSION['USERS2_PRODUKT' . "_COLUMN"])) {
		SEF('USERS_COLUMN_INIT2');
		USERS_COLUMN_INIT2('USERS2_PRODUKT', '', 'RWX');

		// desc
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_ID']="Parent ID";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['A_STATUS']="";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['A_STATUS_INFO']="";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_NAME']="Nazwa produktu";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['S_OTHER_INFO']="";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['SES_SERVICE']="Usługa";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['SES_SERVICE_PARENT']="Usługa nadrzędna";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['SES_OPTION_REQUIRED']="Czy opcja jest wymagana";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['SES_PARENT_REQUIRED']="Czy usługa nadrzędna jest wymagana";
		//$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_S_PARAM1']="";// TODO: tech
		//$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_S_PARAM2']="";// TODO: tech
		//$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_S_LIMIT']="";// TODO: tech
		//$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['P_S_LAN']="";// TODO: tech
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['ID_COSTS_GROUPS']="";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['DESC_TO_LANG']['A_ADM_COMPANY']="";
		// perms
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_ID']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['A_STATUS']="RWX";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['A_STATUS_INFO']="RWX";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_NAME']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['S_OTHER_INFO']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['SES_SERVICE']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['SES_SERVICE_PARENT']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['SES_OPTION_REQUIRED']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['SES_PARENT_REQUIRED']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_S_PARAM1']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_S_PARAM2']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_S_LIMIT']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['P_S_LAN']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['ID_COSTS_GROUPS']="RWXC";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['PERMEDIT']['A_ADM_COMPANY']="";
		// base functions
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['FUNCPERMEDIT']['NEW_RECORD']="RWX";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['FUNCPERMEDIT']['TABLE_INSERTDB']="RWX";
		// additional functions
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['FUNCPERMEDIT']['ID_COSTS_GROUPS']="RWX";
		$_SESSION['USERS2_PRODUKT' . "_COLUMN"]['TYPESPECIAL']['ID_COSTS_GROUPS']="ID_COSTS_GROUPS";
	}


	// router
	global $ARG1, $ARG1_VAL;
	switch ($ARG1) {
		case 'TREE':
			USERS2_PRODUKT_TREE();
			break;
		case 'SES_TREE':
			USERS2_PRODUKT_SES_TREE();
			break;
		default:
			WIEVTABLE( $params );
			echo '<link rel="stylesheet" type="text/css" href="stuff/smoothness-1.8.13/jquery-ui-1.8.13.custom.css">';
			//echo '<link rel="stylesheet" type="text/css" href="stuff/ui.dropdownchecklist.themeroller.css">';
			echo '<script type="text/javascript" src="stuff/jquery.js"></script>';
			echo '<script type="text/javascript" src="stuff/jquery-ui.custom.min.js"></script>';
			echo '<script type="text/javascript" src="stuff/ui.dropdownchecklist.min.js"></script>';
			echo '<script type="text/javascript">' . "
jQuery(document).ready(function(){
	jQuery('.tbl-view thead form').on('submit', function(){
		//console.log(jQuery(this).serialize());
		//return false;
	});
	jQuery('.tbl-view thead select').each(function(ind, val){
		n=jQuery(this);
		var name=n.attr('name');
		if (name.substr(-2) != '[]') {
			n.attr('name', '' + name + '[]');
		}
		n.attr('multiple', 'true').dropdownchecklist();
	});
});
			" . '</script>';

	}
}


function USERS2_PRODUKT_EDIT() {
	EDIT_TABLE_RECORD();
}


function USERS2_PRODUKT_HIST() {
	WIEVTABLE_HIST();
}


function USERS2_PRODUKT_TREE() {
	global $thiss;

	echo '<h1>';
		echo '<a href="' . "?MENU_INIT=USERS2_PRODUKT" . '">' . "PRODUKTY" . '</a>';
		echo " &raquo; ";
		echo "Tree";
	echo '</h1>';

	$items = array();
	$sql = "select *
		from `" . $thiss->DETECT_TABLE_NAME . "`
		where
			`A_STATUS` not in('DELETED')
	";
	$res = DB::query( $sql );
	while ($r = DB::fetch( $res )) {
		$items [$r->ID] = $r;
	}

	Lib::loadClass('Tree');

	// build tree by P_ID - array or 0
	function Tree_Helper__create_tree_rec( $p_id, &$items ) {
		static $rec_ind;
		$rec_ind++;
		$ret = array();
		if (!array_key_exists($p_id, $items)) {
			return false;
		} else {
			$r = $items[$p_id];
			if (empty($r->sub)) {
				return false;
			}
			foreach ($r->sub as $k_id => $v) {
				$ret [$k_id] = Tree_Helper__create_tree_rec( $k_id, $items );
			}//end foreach
			return $ret;
		}
	}

	//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">items: ';print_r($items);echo'</pre>';
	$items_tree = array();// tree root
// --
	foreach ($items as $k_id => $r) {
		if ($r->P_ID == 0) {
			$items_tree [ $r->ID ] = array();
			if (array_key_exists($r->P_ID, $items)) {
				$items[$r->P_ID]->sub [$r->ID] = true;
			}
		} else {// array
			if (array_key_exists($r->P_ID, $items)) {
				$items[$r->P_ID]->sub [$r->ID] = true;
			}
		}
	}//end foreach
	ksort($items_tree);

	// set up has_childrens
	foreach ($items as $k_id => $r) {
		$items[$k_id]->has_childrens = !empty($r->sub);
	}//end foreach

	foreach ($items_tree as $k_id => $v_childrens) {
		if (array_key_exists($k_id, $items)) {
			$items_tree[$k_id] = Tree_Helper__create_tree_rec( $k_id, $items );
		}
	}

	function tree_callback__show_item_from_USERS2_PRODUKT( &$r, &$tree ) {
		$cls = array();
		$cls = (!empty($cls))? ' class="'.implode(' ', $cls).'"' : '';

		echo'<dl'.$cls.'>';
		echo'<dt>';

		$out_id = ($r->ID < 10)? '&nbsp;'.$r->ID.'' : $r->ID;
		echo'<b class="item_id btn-box">'.$out_id.'</b>';

		echo'<span class="desc">';
			//echo'<span class="btn-box">'.$r->A_STATUS.'</span>';
			echo' <b style="color:#333;">'.$r->P_NAME.'</b>';
			echo " " . $r->SES_SERVICE;
			echo " " . $r->SES_SERVICE_PARENT;
			echo " " . $r->SES_OPTION_REQUIRED;
		echo'</span>';
		echo'</dt>';
		echo'</dl>'."\n";
	}


	$tree = new Tree( 'USERS2_PRODUKT' );
	$tree->set_parent_id_col( 'P_ID' );
	$tree->set_param('show_item_callback', "tree_callback__show_item_from_USERS2_PRODUKT");
	$tree->set_param('ajax', "NIE");
	$tree->set_param('rozwin', true);

	echo '<style type="text/css">' . "
body{font-family:arial;}
.box{border:1px solid #999;padding:5px;margin:5px;}
.box-center{border:1px solid #999;padding:5px;margin:5px auto;}
.box-silver{border-color:#999;}
.box-red{border-color:#f00;}
.box-blue{border-color:#00f;}
.box-green{border-color:#008000;}
.box-hover-active{background:#fff;border-color:#999;}
.box-hover-active:hover{background:#eee;border-color:#000;}

.btn-box{margin:0 2px 0 0;padding:0 3px;background:#bbb;color:#fff;text-decoration:none;border:0;font-weight:bold;}
	" . '</style>';
	$tree->show_css();
	echo '<script type="text/javascript" src="stuff/jquery.js"></script>';
	$tree->show_js();
	echo "\n".'<div class="tree-wrap">'."\n";
	$tree->show_rec_by_tree($items_tree, $items);
	echo '</div>'."\n";
}


function USERS2_PRODUKT_SES_TREE() {
	global $thiss;

	echo '<h1>';
		echo '<a href="' . "?MENU_INIT=USERS2_PRODUKT" . '">' . "PRODUKTY" . '</a>';
		echo " &raquo; ";
		echo "SERVICE TREE";
	echo '</h1>';


	function tree_callback__show_item_from_USERS2_PRODUKT( &$r, &$tree ) {
		$cls = array();
		$cls = (!empty($cls))? ' class="'.implode(' ', $cls).'"' : '';

		echo'<dl'.$cls.'>';
		echo'<dt>';

		$out_id = ($r->ID < 10)? '&nbsp;'.$r->ID.'' : $r->ID;
		echo'<b class="item_id btn-box">'.$out_id.'</b>';

		echo'<span class="desc">';
			// select service
			if (!in_array($r->SES_SERVICE, array('GRUPA', 'SERWIS'))) {
				$js = "return Tree_USERS2_PRODUKT_select_service(this, '" . $r->ID . "', '" . $r->SES_SERVICE . "');";
				$checked = ($tree->get_param('selected_opcje_' . $r->SES_SERVICE))? ' checked="checked"' : '';
				echo '<input type="checkbox" name="' . "service_" . $r->ID . '" ' . $checked . ' onclick="' . $js . '">';
			}
			echo' <b style="color:#333;">'.$r->P_NAME.'</b>';
			if ($r->SES_SERVICE == 'OPCJA') {
				if ($r->SES_OPTION_REQUIRED == 'TAK') {
					echo "<code>OPCJA</code> wymagana";
				}
				echo " dla <code>" . $r->SES_SERVICE_PARENT . "</code>";
			} else {
				echo " <code>" . $r->SES_SERVICE . "</code>";
				if ($r->SES_SERVICE_PARENT) {
					echo " podrzędna pod <code>" . $r->SES_SERVICE_PARENT . "</code>";
				}
			}
		echo'</span>';
		echo'</dt>';
		echo'</dl>'."\n";
	}


	$items = array();
	$items_opcje = array();
	$sql = "select *
		from `" . $thiss->DETECT_TABLE_NAME . "`
		where
			`A_STATUS` not in('DELETED')
	";
	$res = DB::query( $sql );
	while ($r = DB::fetch( $res )) {
		if ($r->SES_SERVICE == 'OPCJA') {
			$items_opcje [$r->SES_SERVICE_PARENT] [$r->ID]= $r;
		} else {
			$items [$r->ID] = $r;
		}
	}

	Lib::loadClass('Tree');

	// build tree by P_ID - array or 0
	function Tree_Helper__create_tree_rec( $p_id, &$items ) {
		static $rec_ind;
		$rec_ind++;
		$ret = array();
		if (!array_key_exists($p_id, $items)) {
			return false;
		} else {
			$r = $items[$p_id];
			if (empty($r->sub)) {
				return false;
			}
			foreach ($r->sub as $k_id => $v) {
				$ret [$k_id] = Tree_Helper__create_tree_rec( $k_id, $items );
			}//end foreach
			return $ret;
		}
	}

	//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">items: ';print_r($items);echo'</pre>';
	$items_tree = array();// tree root
// --
	foreach ($items as $k_id => $r) {
		if ($r->P_ID == 0) {
			$items_tree [ $r->ID ] = array();
			if (array_key_exists($r->P_ID, $items)) {
				$items[$r->P_ID]->sub [$r->ID] = true;
			}
		} else {// array
			if (array_key_exists($r->P_ID, $items)) {
				$items[$r->P_ID]->sub [$r->ID] = true;
			}
		}
	}//end foreach
	ksort($items_tree);

	// set up has_childrens
	foreach ($items as $k_id => $r) {
		$items[$k_id]->has_childrens = !empty($r->sub);
	}//end foreach

	foreach ($items_tree as $k_id => $v_childrens) {
		if (array_key_exists($k_id, $items)) {
			$items_tree[$k_id] = Tree_Helper__create_tree_rec( $k_id, $items );
		}
	}

	$tree = new Tree( 'USERS2_PRODUKT' );
	$tree->set_parent_id_col( 'P_ID' );
	$tree->set_param('show_item_callback', "tree_callback__show_item_from_USERS2_PRODUKT");
	$tree->set_param('ajax', "NIE");
	$tree->set_param('rozwin', true);

	echo '<style type="text/css">' . "
body{font-family:arial;}
.box{border:1px solid #999;padding:5px;margin:5px;}
.box-center{border:1px solid #999;padding:5px;margin:5px auto;}
.box-silver{border-color:#999;}
.box-red{border-color:#f00;}
.box-blue{border-color:#00f;}
.box-green{border-color:#008000;}
.box-hover-active{background:#fff;border-color:#999;}
.box-hover-active:hover{background:#eee;border-color:#000;}

.btn-box{margin:0 2px 0 0;padding:0 3px;background:#bbb;color:#fff;text-decoration:none;border:0;font-weight:bold;}
.tree-wrap code{color:#A020F0;}
.tree-wrap button{margin:0; padding:0 6px;}
.tree-wrap input {margin:0; padding:0;}

#selected-service{margin:0; padding:2px; border:1px solid #CCC;}
#selected-service .service-options{margin:2px; padding:2px; border:1px solid #999; background:#eee;}
#selected-service ul{margin:3px;}
	" . '</style>';
	$tree->show_css();
	echo '<script type="text/javascript" src="stuff/jquery.js"></script>';
	echo '<script type="text/javascript">' . "
		function Tree_USERS2_PRODUKT_select_service(n, id, service){
			if (n.checked) {
				jQuery('#selected-service #service_' + service).show('medium');
			} else {
				jQuery('#selected-service #service_' + service).hide('medium');
			}
			//return false;
		}
	" . '</script>';
	$tree->show_js();
	echo '<table style="width:100%">';
	echo '<tr>';
	echo '<td style="width:50%; vertical-align:top;">';
		echo "\n".'<div class="tree-wrap">'."\n";
			echo '<b style="padding:0; font-family:monospace; font-size:14px;">' . "Usługi" . '</b>';
			$tree->show_rec_by_tree($items_tree, $items);
		echo '</div>'."\n";
	echo '</td>';
	echo '<td style="width:50%; vertical-align:top;">';
		echo '<div id="selected-service">';
			echo '<b style="padding:0 3px; font-family:monospace; font-size:14px;">' . "Dodatkowe opcje" . '</b>';
			foreach ($items_opcje as $k_service => $v_options_arr) {
				echo '<div id="' . "service_" . $k_service . '" style="display:none;" class="service-options">';
					echo '<b>' . $k_service . '</b>';
					echo '<ul>';
					foreach ($v_options_arr as $k_option_id => $v_option) {
						echo '<li>';
							$checked = (0)? ' checked="checked"' : '';
							if ($v_option->SES_OPTION_REQUIRED == 'TAK') {
								$checked = ' checked="checked" readonly="readonly"';
							}
							echo '<input type="checkbox" name="' . $k_service . '" value="' . $k_option_id . '" '.$checked.' />';
							echo " [" . $v_option->ID . "] " . $v_option->P_NAME;
							if ($v_option->SES_OPTION_REQUIRED == 'TAK') {
								echo ' <em>' . "(wymagana opcja)" . '</em>';
							}
						echo '</li>';
					}//end foreach
					echo '</ul>';
				echo '</div>';
			}//end foreach
		echo '</div>';
	echo '</td>';
	echo '</tr>';
	echo '</table>';
}


