<?php

/**
 * @param $_GET['procesID'] - id procesu
 * 
 * example:
 *   index.php?MENU_INIT=PROCES_ADD_ZASOB&procesID=3885
 */
function PROCES_ADD_ZASOB() {

	$taskMsgs = array();
	$taskErrors = array();

	$task = V::get('_task', '', $_REQUEST);
	switch ($task) {
		case 'FRM_RM_ZASOB': {
			$procesID = V::get('procesID', 0, $_GET, 'int');
			$rmWskID = V::get('rmWskID', 0, $_POST, 'int');
			if (!$procesID || !$rmWskID) {
				$taskErrors[] = "Wrong param proces id or remove wskaźnik id!";
			}
			else {
				$db = DB::getDB();
				$sql_obj = new stdClass();
				$sql_obj->ID = $rmWskID;
				$sql_obj->A_STATUS = 'DELETED';

				$affected = $db->UPDATE_OBJ('CRM_WSKAZNIK', $sql_obj);
				if ($affected == 2) {
					$taskMsgs[] = "Usunieto wskaznik";
				} else if ($affected == 1) {
					$taskMsgs[] = "Usunieto wskaznik (nie zapisano historii)";
				} else if ($affected < 0) {
					$taskErrors[] = "Nie udało się usunąć zasobu - nic nie zmieniono";
				}
			}
			break;
		}
		case 'FRM_ADD_ZASOB': {
			if(V::get('DBG_NG', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">post (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'</pre>';}
			$procesID = V::get('procesID', 0, $_GET, 'int');
			$addZasobID = V::get('addZasobID', 0, $_POST, 'int');
			$addPrzypadekID = V::get('addPrzypadekID', 0, $_POST, 'int');
			if ($procesID > 0 && $addZasobID > 0 && $addPrzypadekID > 0) {
				$addComment = V::get('addComment', '', $_POST);

				$db = DB::getDB();
				$sql_obj = new stdClass();
				$sql_obj->ID_PROCES = $procesID;
				$sql_obj->ID_ZASOB = $addZasobID;
				$sql_obj->ID_PRZYPADEK = $addPrzypadekID;
				$sql_obj->OPIS_ZASOB = $db->_($addComment);
				$sql_obj->SORT_PRIO = 0;
				// nowy wskaznik na koniec listy pod procesem
				$sql = "select max(cw.`SORT_PRIO`) as MAX_SORT_PRIO
					from `CRM_WSKAZNIK` as cw
					where
						cw.`ID_PROCES`='{$sql_obj->ID_PROCES}'
				";
				$res = $db->query($sql);
				while ($r = $db->fetch($res)) {
					$sql_obj->SORT_PRIO = $r->MAX_SORT_PRIO + 1;
				}

				$new_id = $db->ADD_NEW_OBJ('CRM_WSKAZNIK', $sql_obj);
				if ($new_id > 0) {
					$taskMsgs[] = "Dodano zasób [{$addZasobID}] do procesu [{$procesID}]";
				} else {
					$taskErrors[] = "Nie udało się dodać zasobu";
				}
			}
			else if (!$addPrzypadekID) {
				$taskErrors[] = "Brak okreśłonego uprawnienia";
			}
			else if (!$addZasobID) {
				$taskErrors[] = "Brak numeru zasobu";
			}
			break;
		}
		case 'TYPESPECIAL': {
			$DBG = ('1' == V::get('DBG', '', $_REQUEST));
			header("Content-type: application/json");

			$fld = V::get('fld', '', $_GET);
			switch ($fld) {
				case 'addZasobID': {
					Lib::loadClass('TypespecialVariable');
					$typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');

					$query = V::get('q', '', $_REQUEST);
					$rawRows = null;
					$jsonData = array();
					$rows = $typeSpecialZasob->getValuesWithExports($query);
					if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'</pre>';}
					foreach ($rows as $kID => $vItem) {
						$itemJson = new stdClass();
						$itemJson->id = $vItem->id;
						$itemJson->name = $vItem->param_out;
						if (!empty($vItem->exports)) {
							$itemJson->exports = $vItem->exports;
						}
						$jsonData[] = $itemJson;
					}
					echo json_encode($jsonData);
					break;
				}
			}
			exit;
		}
	}

	SE_Layout::menu();

	Lib::loadClass('UserActivity');

	Lib::loadClass('ProcesHelper');
/* TODO: permy do pokazania tej funkcji
	$zasobID = ProcesHelper::getZasobTableID('CRM_WSKAZNIK');
	if ($zasobID <= 0) {
		echo UserActivity::showListInContainer();
		return;
	}

	$zasobObj = ProcesHelper::getZasobTableInfo($zasobID);
	if (!$zasobObj) {
		echo '<div class="alert alert-error">' . "Zasob TABELA ID={$zasobID} nie istnieje" . '</div>';
		echo UserActivity::showSimpleList();
		return;
	}
*/

	$procesID = V::get('procesID', '', $_GET);
	if (!$procesID) {
		echo '<div class="alert alert-error">' . "Wrong param proces ID!" . '</div>';
		return;
	}
	$db = DB::getDB();
	$proces = $db->get_by_id('CRM_PROCES', $procesID);
	Lib::loadClass('ProcesHelper');
	$proces->_zasoby = ProcesHelper::get_wskazniki( $proces->ID );


	Lib::loadClass('TypespecialVariable');
	$typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');

	?>
	<style type="text/css">
.conn_wsk {}
 .conn_wsk .conn_wsk-list {}
  .conn_wsk .conn_wsk-list .conn_wsk-list_item { line-height:22px; }
  .conn_wsk .conn_wsk-list .conn_wsk-list_item form { display:inline; margin:0; }
  .conn_wsk .conn_wsk-list_item-rmBtn { /*display:none;*/ opacity:0.4; margin:0; padding:0 10px; border:none; }
  .conn_wsk .conn_wsk-list_item:hover .conn_wsk-list_item-rmBtn { /*display:inline;*/ opacity:1; }
.conn_wsk-frm {}

 .conn_wsk .conn_wsk-opisyShowBtn { padding:0 0 0 10px; font-size:12px; font-weight:normal; color:#08C; cursor:pointer; }
.conn_wsk-list_item-desc,
.conn_wsk-list_item-longDesc { display:none; }
.opisyShow .conn_wsk-list_item-desc,
.opisyShow .conn_wsk-list_item-longDesc { display:block; }
	</style>
	<div class="container conn_wsk">
		<h3>Edycja procesu</h3>
		<blockquote>
			<b>Proces [<?php echo $proces->ID; ?>]</b>: <?php echo $proces->TYPE; ?> <?php echo $proces->DESC; ?>
			<p><?php echo $proces->OPIS; ?></p>
		</blockquote>
		<?php if (!empty($taskErrors)) : ?>
			<div class="alert alert-error">
				<button type="button" class="close" data-dismiss="alert">×</button>
				<?php echo implode('<br>', $taskErrors); ?>
			</div>
		<?php endif; ?>
		<?php if (!empty($taskMsgs)) : ?>
			<div class="alert alert-success">
				<button type="button" class="close" data-dismiss="alert">×</button>
				<?php echo implode('<br>', $taskMsgs); ?>
			</div>
		<?php endif; ?>
		<h5>Przypisane zasoby <em>(<?php echo (!empty($proces->_zasoby))? count($proces->_zasoby) : 0; ?>)</em>: <span class="conn_wsk-opisyShowBtn" onclick="jQuery(this).parent().parent().toggleClass('opisyShow')">+/- opisy</span></h5>
		<ul class="conn_wsk-list">
			<?php if (!empty($proces->_zasoby)) : ?>
				<?php foreach ($proces->_zasoby as $vZasob) : ?>
					<li class="conn_wsk-list_item">
						<?php echo $vZasob->CP_FORM_TREAT; ?> [<?php echo $vZasob->ID; ?>] <?php echo $vZasob->TYPE; ?> <b><?php echo $vZasob->DESC; ?></b>
						<form action="" method="POST" class="form-inline conn_wsk-list_item-frm-rm">
							<input type="hidden" name="_task" value="FRM_RM_ZASOB">
							<button name="rmWskID" value="<?php echo $vZasob->CW_ID; ?>" class="btn-link btn-small conn_wsk-list_item-rmBtn" title="usuń zasób"><i class="icon icon-remove"></i></button>
						</form>
						<?php if (!empty($vZasob->OPIS)) : ?>
							<p class="conn_wsk-list_item-desc">
								<?php echo $vZasob->OPIS; ?>
							</p>
						<?php endif; ?>
						<?php if (!empty($vZasob->OPIS_ZASOB)) : ?>
							<p class="conn_wsk-list_item-longDesc">
								<?php echo $vZasob->OPIS_ZASOB; ?>
							</p>
						<?php endif; ?>
					</li>
				<?php endforeach; ?>
			<?php endif; ?>
		</ul>
		<?php if ($typeSpecialZasob) : ?>
			<h5>Dodaj zasób:</h5>
			<form action="" method="POST" class="form-horizontal conn_wsk-frm">
				<input type="hidden" name="_task" value="FRM_ADD_ZASOB">
				<div class="control-group">
					<label class="control-label" for="addZasobID">Zasób</label>
					<div class="controls">
						<?php
							$fName = 'addZasobID';
							$fldParams = array();
							$fldParams['allowCreate'] = false;
							$fldParams['ajaxDataUrlBase'] = "index.php?FUNCTION_INIT=" . __FUNCTION__ . "&HEADER_NOT_INIT=YES&_task=TYPESPECIAL&fld={$fName}";
							//$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
							echo $typeSpecialZasob->showFormItem($tblID = -1, $fName, $selValue = '', $fldParams);
						?>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="addPrzypadekID">Uprawnienia</label>
					<div class="controls">
						<?php
							$uprawnienia = array();
							$db = DB::getDB();
							$sql = "select p.* from `CRM_PRZYPADEK` as p ";
							$res = $db->query($sql);
							while ($r = $db->fetch($res)) {
								$uprawnienia[$r->ID] = "{$r->FORM_TREAT}: {$r->OPIS}";// {$r->NAZWA} | {$r->PYTANIE} | 
							}
						?>
						<select name="addPrzypadekID" style="width:366px;" size="6">
							<?php foreach($uprawnienia as $kId => $vPermLabel) : ?>
								<option value="<?php echo $kId; ?>"><?php echo $vPermLabel; ?></option>
							<?php endforeach; ?>
						</select>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="addComment">Komentarz</label>
					<div class="controls">
						<textarea name="addComment" cols="80" rows="6" style="width:354px;"></textarea>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="submit" class="btn">dodaj</button>
					</div>
				</div>
			</form>
		<?php endif; ?>
	</div>
	<?php

}
