<?php

Lib::loadClass('View');

class ProcesLogStepView extends View {

	/**
	 * @param proces_log - ProcesLogModel
	 * @param proces_init - raw Proces object - main PROCES_INIT
	 * @param proces_step - raw Proces object - current PROCES
	 * @param wskazniki - array of raw objects from ProcesHelper::get_wskazniki
	 * @param current_edit - TODO: class of current edit form view!
	 * @param next_steps - array of next steps, if empty then END
	 */
	function render() {
		$modelLog = $this->get('proces_log');
		$proces_init = $this->get('proces_init');
		$proces_step = $this->get('proces_step');
		$wskazniki = $this->get('wskazniki');
		$next_steps = $this->get('next_steps');
		$current_edit = $this->get('current_edit');
		$out = '';

		if ($title_out = $this->get('title')) {
			$out .= '<h1>' . $title_out . '</h1>' . "\n";
		}

		$current_edit_wsk_ids = array();
		if ($current_edit) {
			$current_edit_wsk_ids = $current_edit->get_wsk_ids();
		}

		$out = '';

		$out .= '<script type="text/javascript">' . "
function showHideOpis(n) {
	var p=n.nextSibling;
	if (p.style.display == 'none') {
		p.style.display = '';
		n.innerHTML = '- ' + n.innerHTML;
	} else {
		p.style.display = 'none';
		if (n.innerHTML.substring(0, 2) == '- ') {
			n.innerHTML = n.innerHTML.substring(2);
		}
	}
	return false;
}
function showHideWskazniki(n) {
	var p=jQuery(n).parents('li:first')
	if (!p) return false;
	if (p.hasClass('hide-wskazniki')) {
		p.removeClass('hide-wskazniki');
		n.innerHTML = '- ' + n.innerHTML;
	} else {
		p.addClass('hide-wskazniki');
		if (n.innerHTML.substring(0, 2) == '- ') {
			n.innerHTML = n.innerHTML.substring(2);
		}
	}
	return false;
}

if (typeof(resetForm) === 'undefined') {
function resetForm(n){
	for (i = 0; i < n.form.elements.length; i++) {
		switch (n.form.elements[i].type.toLowerCase()){
			case 'text':
				n.form.elements[i].value='%';
				break;
			case 'select-one':
//				n.form.elements[i].options[0].selected = true;
//				break;
			case 'select-multiple':
				for (z = 0; z < n.form.elements[i].options.length; z++) {
					n.form.elements[i].options[z].selected = false;
					if (n.form.elements[i].options[z].hasAttribute('selected')) {
						n.form.elements[i].options[z].removeAttribute('selected');
					}
				}
				// select first element
				n.form.elements[i].options[0].selected = true;
				n.form.elements[i].options[0].setAttribute('selected', 'selected');
				break;
			default:
				
		}
	}
	return false;
}
}
		" . '</script>' . "\n";
		$out .= '<style type="text/css">' . "
.proces-info .selected p {background-color:#FFEFEF;}
.proces-info .selected .btn-box {background-color:#CC7575;}

.tbl-view .tbl-filters .i {width:90%;}
.tbl-view .tbl-head th {font-size:x-small;}
		" . '</style>' . "\n";
		$out .= '<div class="proces-info">';
		$out .= '<div class="header">';
		$out .= App::link("Proces Log <em>(" . $modelLog->get('ID') . ")</em>", array('_log_id'=>$modelLog->get('ID'))) . ". PROCES_INIT: " . '<b style="padding:0 2px; background:#FF4646; color:#fff;">' . $modelLog->get('ID_PROCES_INIT') . '</b>';
		$out .= " " . '<span style="color:red">' . $proces_init->DESC . '</span>';
		if ($proces_init->OPIS != '') {
			$out .= ' <a href="#" onclick="' . "return showHideOpis(this)" . '">' . "opis" . '</a>';
			$out .= '<span class="a-hide" style="display:none;">' . '<br />' . $proces_init->OPIS . '</span>';
		}
		$out .= '</div>' . "\n";// .header
		$out .= '<div class="cnt">' . "\n";

		if ($modelLog->get('ID_STEP') > 0 && $modelLog->get('ID_STEP') != $modelLog->get('ID_PROCES_INIT')) {
			$out .= '<div class="tree-wrap">' . "\n";
			$out .= '<ul><li class="last hide-wskazniki"><dl class="has-no-children">';
			$out .= '<dt>';
			$out .= '<div class="has_wsk">';
				$out .= '<b class="item_id btn-box">' . $modelLog->get('ID_STEP') . '</b>';
				$out .= '<span class="desc">';
				$out .= '<b>' . $proces_step->DESC . '</b>';
				$out .= ' <a href="#" onclick="' . "return showHideWskazniki(this)" . '" class="btn btn-green">' . "wskazniki" . '</a>';
				if ($proces_step->OPIS != '') {
					$out .= ' <a href="#" onclick="' . "return showHideOpis(this)" . '" class="btn">' . "opis" . '</a>';
					$out .= '<span class="a-hide" style="display:none;background:#f9f9f9;">' . '<br />' . $proces_step->OPIS . '</span>';
				}
				$out .= '</span>';// .desc
			$out .= '</div>';// .has_wsk
			$out .= '</dt>';
			$out .= '<dd><ul>' . "\n";
			// wskazniki
			$last_key_id = end(array_keys($wskazniki));
			foreach ($wskazniki as $k_key_id => $v_wsk) {
				$cls = array();
				if ($k_key_id == $last_key_id) $cls[] = 'last';
				if (in_array($k_key_id, $current_edit_wsk_ids)) $cls[] = 'selected';
				$out .= '<li' . ((!empty($cls))? ' class="'.implode(' ', $cls).'"' : '') . '><p>';
					$out .= '<span class="btn-box btn-box-silver">' . $v_wsk->CW_ID . '</span>';
					$out .= '<span class="green">' . "[" . $v_wsk->ID . "]" . '</span>';
					$out .= '<i title="' . $v_wsk->CP_PYTANIE . '">' . $v_wsk->CP_FORM_TREAT . '</i>';
					$out .= " " . $v_wsk->TYPE . " ";
					$out .= '<b>' . $v_wsk->DESC . '</b>';
					$out .= " " . '<span class="green">' . $v_wsk->OPIS . '</span>';
					$out .= " " . '<span style="color:#000">' . $v_wsk->OPIS_ZASOB . '</span>';
				$out .= '</p></li>';
			}
			$out .= '</ul></dd>';
			$out .= '</dl></li></ul>';
			$out .= '</div>' . "\n";// .tree-wrap
		} else if (!empty($wskazniki)) {
			$out .= '<div class="tree-wrap">' . "\n";
			$out .= '<ul>' . "\n";
			// wskazniki
			$last_key_id = end(array_keys($wskazniki));
			foreach ($wskazniki as $k_key_id => $v_wsk) {
				$out .= '<li' . (($k_key_id == $last_key_id)? ' class="last"' : '') . '>';
					$out .= '<span class="btn-box btn-box-silver">' . $v_wsk->CW_ID . '</span>';
					$out .= '<span class="green">' . "[" . $v_wsk->ID . "]" . '</span>';
					$out .= '<i title="' . $v_wsk->CP_PYTANIE . '">' . $v_wsk->CP_FORM_TREAT . '</i>';
					$out .= " " . $v_wsk->TYPE . " ";
					$out .= '<b>' . $v_wsk->DESC . '</b>';
					$out .= " " . '<span class="green">' . $v_wsk->OPIS . '</span>';
					$out .= " " . '<span style="color:#000">' . $v_wsk->OPIS_ZASOB . '</span>';
				$out .= '</li>';
			}
			$out .= '</ul>';
			$out .= '</div>' . "\n";// .tree-wrap
		}

		$out .= '</div>' . "\n";// .cnt
		$out .= '</div>' . "\n";// .proces-info

		if ($modelLog->get('TYPE') == 'END') {
			$out .= '<div class="next-steps">';
				$out .= '<div class="header">' . "Proces został zakończony" . '</div>';
				$out .= '<div class="cnt">';
					$out .= '';// TODO: link?
				$out .= '</div>';// .cnt
			$out .= '</div>' . "\n";// .next-steps
		}
		else {
			// current edit form
			$out .= '<div class="current-edit">';
			if ($current_edit) {
				$current_edit->add_hidden_field("task", "PROCES_LOG");
				$current_edit->add_hidden_field("function_init", "fun_PROCES_STEP_SAVE");
				$out .= $current_edit->form();
			} else {
				$out .= '<p>' . "Brak zdefiniowanych akcji w tym kroku - można przejść do następnego." . '</p>';
			}
			$out .= '</div>' . "\n";// .current-edit

			// next steps
			$out .= '<div class="next-steps">';
				$out .= '<div class="header">' . "Przejdź do następnego kroku:" . '</div>';
				$out .= '<div class="cnt">';
				$out .= '<ul>';
				foreach ($next_steps as $k => $v_next_step_link) {
					$out .= '<li>' . $v_next_step_link . '</li>';
				}
				$out .= '</ul>';
				$out .= '</div>';// .cnt
			$out .= '</div>' . "\n";// .next-steps
		}
		$out .= '<br /><br />';

		return $out;
	}

}
