<?php
function _replaceData($subject,$item)
{
	preg_match_all('/\{[a-zA-Z\-\_]+\}/', $subject, $matches);
	if(sizeof($matches[0]))
		foreach($matches[0] as $ech)
			$subject =  str_replace($ech, $item[substr($ech,1,-1)], $subject);	
			
	return $subject;
}

function _toAttrs($opts,$item=false,$sep=[]) 
{
	$sep[] = 'className';
	$sep[] = 'class';	
	$sep[] = 'checked';	
	
	$options = [ 'attr'=>'' ];

	foreach($opts as $key=>$val) {	
	
		if($item)
			$val = _replaceData($val,$item);

		if(in_array($key,$sep) && !@is_numeric($key)) {
			
			if($key=="className" || $key=="class")
				$options['class'] = " $val";
			else
				$options[$key] = " $val";	
		} 
		else{
			
			if(!@is_numeric($key))
				$options['attr'].= " $key=\"$val\"";
			else
				$options['attr'].= " $val";				
		}
	}
	return $options;
}

function _stringCondtion($string)
{
	$splt = [];
	$condtn = '';
	$splt = explode('<=',$string);
	
	if(sizeof($splt)>1)
	$condtn = '<=';
	else
	{
		$splt = explode('>=',$string);	
		
		if(sizeof($splt)>1)
		$condtn = '>=';
		else {
			
			$splt = explode('==',$string);	
			
			if(sizeof($splt)>1)
			$condtn = '==';
			else {
				
				$splt = explode('!=',$string);
				
				if(sizeof($splt)>1)
				$condtn = '!=';
				else
				{
					$splt = explode('<',$string);
					
					if(sizeof($splt)>1)
					$condtn = '<';
					else
					{
						$splt = explode('>',$string);
						
						if(sizeof($splt)>1)
						$condtn = '>';
					}
				}
			}
		}
	}

	switch($condtn) {
		
		case "<=";
		return ($splt[0]<=$splt[1]);
		break;
		
		case ">=";
		return ($splt[0]>=$splt[1]);
		break;	
		
		case "==";
			if($splt[1]!='NULL')
				return ($splt[0]==$splt[1]);
			else
				return (is_null($splt[0]) || $splt[0]=='');	
		break;
		
		case "!=";
			if($splt[1]!='NULL')
				return ($splt[0]!=$splt[1]);
			else
				return (!is_null($splt[0]) && $splt[0]!='');		
		break;
		
		case ">";
		return ($splt[0]>$splt[1]);
		break;
		
		case "<";
		return ($splt[0]<$splt[1]);
		break;		
	}
}


// table list
function _tableList($title,$url,$dataSource,$cols,$opts)
{
	global $const_account_pageLimit;
	if($opts['pagination'])
	$pageCnt = (((isset($_REQUEST['page']) && (int)$_REQUEST['page']>0)?$_REQUEST['page']:1)-1)*$const_account_pageLimit;
	$output = $dataSource;

	if($emptNode = array_search('',$cols))
		array_splice($cols, $emptNode, 1);

	if(isset($opts['buttons']) && $opts['buttons']) {	
		?>
		<div class="pull-right">
			<?php
			foreach($opts['buttons'] as $btn) {
				
				if(is_array($btn)) {
						
					if(!$btn['menu']) {
						
						$attr = "";
						$className = "";
						
						if($btn['opts']) {
							$tmpAttr = _toAttrs($btn['opts']);
							$attr = $tmpAttr['attr'];
							$className = $tmpAttr['class'];						
						}
						
						if($btn['link']) {
							?>
							<a href="<?php echo $btn['link']?>" class="btn btn-<?php echo $btn['btn'].' '.$className?>" <?php echo $attr?>><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?></a>
							<?php	
						}
						else {
							?>
							<button type="button" class="btn btn-<?php echo $btn['btn'].' '.$className?>" <?php echo $attr?>><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?></button>
							<?php					
						}
					}
					else {				
						?>
						<div class="btn-group">
							<button type="button" class="btn btn-<?php echo $btn['btn']?> dropdown-toggle" data-toggle="dropdown"><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?>
							&nbsp;
							<span class="caret"></span>
							<span class="sr-only">Toggle Dropdown</span>
							</button>
							<ul class="dropdown-menu dropdown-menu-right" role="menu">
								<?php
								foreach($btn['menu'] as $bSub) {
									if($bSub=="divider") {
										?>
										<li class="divider"></li>
										<?php
									}
									else {
										$attr = "";
										$className = "";
										
										if($bSub['opts']) {
											
											$tmpAttr = _toAttrs($bSub['opts']);
											$attr = $tmpAttr['attr'];
											$className = $tmpAttr['class'];										
										}									
										?>
										<li><a <?php echo ($bSub['link'])?('href="'.$bSub['link'].'"'):''?> <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?>><?php echo ($bSub['ico'])?'<span class="fa fa-'.$bSub['ico'].'"></span>':''?><?php echo $bSub['label']?></a></li>
										<?php
									}
								}
								?>
							</ul>
						</div>	
						<?php
					}
				}				
			}
			?>	
		</div>
		<?php
	}
	?>
	<div class="clearfix"></div>	
	<div class="box">

		<div class="box-header">
			<h3 class="box-title"><?php echo $title?></h3>

			<div class="box-tools">

			<div class="form-inline">				   

				<?php
				if($opts['search']!==false) {
					$prefix = ($opts['prefix'])?$opts['prefix']:'';
					
					$sqry = @explode('?',$url);
					$QUrl = $sqry[0];
					$Qquery = '';
					if(sizeof($sqry)>1) {
						parse_str($sqry[1], $subSqry);
						if(sizeof($subSqry))
							foreach($subSqry as $key=>$val) {
								if($key!=$prefix.'search')
									$Qquery.= '&'.$key.'='.$val;
							}
					}					
					?>
					<div class="input-group">
						
						<input type="text" id="_<?php echo $prefix?>search" name="<?php echo $prefix?>search" value="<?php echo $_REQUEST[$prefix."search"]?>" class="form-control input-sm pull-right" placeholder="Search" onkeyup="if(window.event.keyCode==13){ window.location.href = '<?php echo $QUrl.'?'.$Qquery?>&<?php echo $prefix?>search='+document.getElementById('_<?php echo $prefix?>search').value; };">
						<div class="input-group-btn">
							<button type="button" onclick="window.location.href = '<?php echo $QUrl.'?'.$Qquery?>&<?php echo $prefix?>search='+document.getElementById('_<?php echo $prefix?>search').value;" class="btn btn-sm btn-default"><i class="fa fa-search"></i></button>						
						</div>
						
					</div>
					<?php
				}
				?>
				
			</div>
		
			</div>
		</div>		

		<div class="box-body no-padding">
		<table class="table table-hover table-bordered table-striped _tableList">
		<tr>
			<th class="hidden-xs">#</th>
			<?php
			$tttt = 0;
			foreach($cols as $each) {
				$attr = "";
				$className = "";
				
				if(isset($each['opts']) && $each['opts']) {
					$tmpAttr = _toAttrs($each['opts'],$item);
					$attr = $tmpAttr['attr'];
					$className = $tmpAttr['class'];							
				}
				
				?>
				<th <?php echo ($className!='')?'class="'.$className.'"':''?>>
				
					<?php					
					if($each['sort']) {
						$prefix = ($opts['prefix'])?$opts['prefix']:'';
						$qryStr[$prefix.'sortBy'] = $each['sort'];
						$qryStr[$prefix.'sortType'] = (@$_REQUEST[$prefix."sortBy"]==$each['sort'] && $_REQUEST[$prefix."sortType"]=="asc")?"desc":"asc";
						
						if(@$_REQUEST[$prefix."search"]!='' && $opts['search']!==false)
							$qryStr[$prefix.'search'] = $_REQUEST[$prefix."search"];
							
						?>

						<a href="<?php echo _queryString(_exceptQuery($url,[ $prefix.'sortBy',$prefix.'sortType' ]),$qryStr)?>">
						<?php echo $each['label']?> <i class="fa fa-fw fa-sort"></i>
						</a>
						
						<?php
					}
					else {
						echo $each['label'];
					}
					
					if(isset($each['filter']) && $each['filter']) {
						if($each['filter']['data']) {
							
							$prefix = ($opts['prefix'])?$opts['prefix']:'';
							$currFltrRqt = @explode(',',_request($prefix.$each['filter']['name']));
							?>

							<div class="navbar-custom-menu">
							<ul class="nav navbar-nav">
								<li class="dropdown messages-menu">
									
									<span class="<?php echo ((_request($prefix.$each['filter']['name'])!='' && sizeof(_request($prefix.$each['filter']['name']))>0)?'active ':'')?>dropdown-toggle" data-toggle="dropdown">
									<i class="fa fa-filter"></i>
									</span>
									
									<ul class="dropdown-menu">
										<li>
										<ul class="menu">
											<?php
											foreach($each['filter']['data'] as $eachFilter) {
												
												$tmpcurrFltrRqt = array_diff($currFltrRqt, [$eachFilter["value"]]);
												
												if(!@in_array($eachFilter["value"],$currFltrRqt))
													$tmpcurrFltrRqt[] = $eachFilter["value"];
												?>
												<li>
													<a>
														<h4>
														<label>
														<input type="checkbox" class="<?php echo $prefix.$each['filter']['name']?>" <?php echo (@in_array($eachFilter["value"],$currFltrRqt))?'checked="checked"':''?> value="<?php echo urlencode($eachFilter["value"])?>"/>&nbsp;&nbsp;<?php echo $eachFilter["title"]?>
														</label>
														</h4>
													</a>
												</li>
												<?php
											}
											?>
										</ul>
										</li>
										<li class="footer">

											<div class="btn-group">
												<a href="<?php echo _exceptQuery($url,[ $prefix.$each['filter']['name'] ])?>" class="btn center-block"><i class="fa fa-refresh"></i> Reset</a>
												<a onclick="applyTableListFilter('<?php echo _exceptQuery($url,[ $prefix.$each['filter']['name'] ])?>','<?php echo $prefix.$each['filter']['name']?>');" class="btn center-block"><i class="fa fa-filter"></i> Apply</a>
											</div>
											
										</li>
									</ul>
								
								</li>
							</ul>
							</div>
							
							<?php
						}
					}					
					?>
					
				</th>				
				<?php
			}
			
			if($opts['checkbox']) {			
				?>
				<th></th>
				<?php
			}
			if($opts['action']) {	
				?>
				<th></th>
				<?php
			}
			?>
		</tr>
		<?php
		$cnt = 1;
		if(sizeof((array)$output))
			foreach($output as $item) {
				?>
				<tr>
					<td class="hidden-xs" width="50"><?php echo $cnt+$pageCnt?></td>
					<?php

					foreach($cols as $each) {
						$attr = "";
						$className = "";
						if(@$each['opts']) {							
							$tmpAttr = _toAttrs($each['opts'],$item);
							$attr = $tmpAttr['attr'];
							$className = $tmpAttr['class'];								
						}
						?>
						<td <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?>>
						<?php
						if(@$each['link']) {
							$each['link']	= _replaceData($each['link'],$item);
							?>
							<a href="<?php echo $each['link']?>">
							<?php
						}
						
						$badge = false;
						if(@$each['badge'])
							foreach($each['badge'] as $bdg) {
								if($bdg['condition'] && $bdg['color']) {
									if(
										_stringCondtion(
											_replaceData($bdg['condition'],$item)
										)
									)
									$badge = $bdg['color'];
								}
							}
						
						if($badge) {
							?>
							<span class="badge bg-<?php echo $badge?>">
							<?php
						}
						
						echo	(@$each['ftn'])?
								$each['ftn']($item[$each['field']]):
								$item[$each['field']];
						
						if($badge) {
							?>
							</span>
							<?php
						}
						
						if(@$each['link'])
						{ ?> </a> <?php }
						?>						
						</td>
						<?php
					}
					
					if($opts['checkbox']) {
						$attr = "";
						$className = "";
						if($opts['checkbox']['opts']) {							
							$tmpAttr = _toAttrs($opts['checkbox']['opts'],$item);
							$attr = $tmpAttr['attr'];
							$className = $tmpAttr['class'];								
						}						
						?>
						<td width="40">
							
							<label>
							<input type="checkbox"<?php echo ($opts['checkbox'][0]!='')?' name="'.$opts['checkbox'][0].'[]"':''?> class="minimal <?php echo $className?>" <?php echo $attr?>>
							</label>

						</td>
						<?php
					}
					
					if($opts['action']) {
						?>
						<td width="55">
							<div class="btn-group">
							
								<button type="button" class="dropdown-toggle" data-toggle="dropdown">
								<span class="fa fa-ellipsis-h"></span>
								<span class="sr-only">Toggle Dropdown</span>
								</button>
								
								<ul class="dropdown-menu dropup dropdown-menu-right" role="menu" >
								
									<?php
									foreach($opts['action'] as $act)
									{
										if($act!="") {
											
											if($act=="divider") {
												?>
												<li class="divider"></li>
												<?php
											}
											else {
												$toShow = true;
												
												if(@$act['conditionFtn']) {
													
													$tempArgs = [];												
													if($act['conditionFtn']['args'])
													{
														if(@is_array($act['conditionFtn']['args']))
														{

															if(sizeof($act['conditionFtn']['args']))
																foreach($act['conditionFtn']['args'] as $ech)
																	$tempArgs[] = _replaceData($ech,$item);
														}
														$toShow = call_user_func_array($act['conditionFtn']['name'],$tempArgs);
													}
													else
													$toShow = $act['conditionFtn']['name']();		
																							
												}
												
												if(@$act['condition'])
													foreach($act['condition'] as $actCdtn)
													{
														if(
															!_stringCondtion(
																_replaceData($actCdtn,$item)
															) 
															&& $toShow
														)
														$toShow = false;
													}											

												if(
													($toShow && is_null(@$act['conditionFtn']['status'])) 
													|| 
													(!is_null($act['conditionFtn']['status']) && $toShow==$act['conditionFtn']['status'])
												)
												{
													if(@$act['link'])
														$act['link']	= _replaceData($act['link'],$item);
													$act['label']	= _replaceData($act['label'],$item);
													
													$attr = "";
													$className = "";
													
													if(@$act['opts'])
													{
														$tmpAttr = _toAttrs($act['opts'],$item);
														$attr = $tmpAttr['attr'];
														$className = @$tmpAttr['class'];														
													}												
													?>
													<li>
														<a <?php echo (@$act['link'])?('href="'.@$act['link'].'"'):''?> <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?> >
														<?php echo ($act['ico'])?'<span class="fa fa-'.$act['ico'].'"></span>':''?><?php echo $act['label']?>
														</a>
													</li>
													<?php
												}
											}
										}
									}
									
									/*
									?>
									<li class="dropdown-submenu"> 
										<a class="submenu" tabindex="-1">More options <i class="fa fa-caret-right pull-right"></i></a>
										<ul class="dropdown-menu">
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
										</ul>
									</li>
									<li class="dropdown-submenu"> 
										<a class="submenu" tabindex="-1"><i class="fa fa-caret-left"></i> More options</a>
										<ul class="dropdown-menu dropdown-menu-left">
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
											<li><a tabindex="-1" href="#">Second level link</a></li>
										</ul>
									</li>
									<?php
									*/
									?>
								</ul>
							
							</div>				
						</td>						
						<?php
					}
					?>

				</tr>				
				<?php
				$cnt++;
			}
		else {
			
			?>
				<tr>
					<td colspan="<?php echo sizeof($cols)+(($opts['action'])?1:0)+(($opts['checkbox'])?1:0)+1?>" class="text-center">
					<strong>No data found!</strong>
					</td>
				</tr>
			<?php
		}
		?>
		</table>
		</div>

		<div class="box-footer clearfix">
			<?php
			if(isset($output) && sizeof($output) && $opts['pagination'])
			{
				if($opts['prefix'])
					$opts['pagination']->pagePrefix = $opts['prefix'];

				$opts['pagination']->pager($url);
			}
			?>			  
		</div>			

	</div>
	<?php
}

//form open
function _formOpen($opts=[ 'method'=>'POST', 'enctype'=>'multipart/form-data' ])
{
	$attr = "";
	$className = "";
	if(sizeof($opts))
	{		
		$tmpAttr = _toAttrs($opts);
		$attr = $tmpAttr['attr'];
		$className = (isset($tmpAttr['class'])?$tmpAttr['class']:'');			
	}
	?>
	<form class="form-horizontal<?php echo $className?>" <?php echo $attr?>>	
	<?php
}

//form open
function _formClose()
{
	?>
	</form>	
	<?php
}

// Content header
function _contentHeader($title,$links=[])
{
	?>
	<section class="content-header">
	  <h1>
	  <?php echo $title?>
	  </h1>
	  <ol class="breadcrumb">
		<li><a href="./"><i class="fa fa-list"></i> Dashboard</a></li>
		<?php
		if(sizeof($links))
		foreach($links as $each)	
		{
			?>
			<li><a href="<?php echo $each["link"]?>"><?php echo ($each["ico"])?'<i class="fa fa-'.$each["ico"].'"></i> ':''?><?php echo $each[0]?></a></li>
			<?php
		}
		?>
		<li class="active"><?php echo $title?></li>
	  </ol>
	</section>
	<?php
}

// Form sub titles
function _formSubTitle($title)
{
	?>
	<div class="row">
	<div class="col-sm-12">
	
	<h4><?php echo $title?></h4>
	
	</div>
	</div>
	<?php
}

// Form Input Groups
function _formGroup($fields,$cntrlGroup=false,$group=true)
{
	if($fields)
	{
		if($group)
		{
			$groupClass = ($cntrlGroup)?'control-group input-group addMoreControl':'form-group';
			?>
			<div class="<?php echo $groupClass?>">
			<?php
		}
		
		foreach($fields as $ech)
		{
			if(is_array($ech))
			{
				$attr = "";	
				$className = "";
				if($ech['opts'])
				{
					$tmpAttr = _toAttrs($ech['opts']);
					$attr = $tmpAttr['attr'];
					$className = $tmpAttr['class'];						
				}
				
				$parentAttr = "";	
				$parentClassName = "";
				if($ech['parentOpts'])
				{					
					$tmpAttr = _toAttrs($ech['parentOpts']);
					$parentAttr = $tmpAttr['attr'];
					$parentClassName = $tmpAttr['class'];						
				}				

				if($ech[0]=="hidden")
				{
					?>
					<input <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech[name])?$ech[name]:$ech['id']).(($cntrlGroup)?'[]':'')?>" type="<?php echo $ech[0]?>" value="<?php echo ($_REQUEST[($ech[name])?$ech[name]:$ech['id']]!='' && !$cntrlGroup)?$_REQUEST[($ech[name])?$ech[name]:$ech['id']]:$ech[value]?>">			
					<?php
				}
				else				
				{
					$colClass = '';
					
					if($ech['col'])
					$colClass = 'col-md-'.$ech['col'];
					
					if($ech['xsCol'])
					$colClass.= ' col-xs-'.$ech['xsCol'];
				
					if($ech['smCol'])
					$colClass.= ' col-sm-'.$ech['smCol'];
					
					if($ech['mdCol'])
					$colClass.= ' col-md-'.$ech['mdCol'];	
					
					if($ech['lgCol'])
					$colClass.= ' col-lg-'.$ech['lgCol'];
				
					$offsetClass = ' ';
					
					if($ech['offset'])
					$offsetClass = ' col-md-offset-'.$ech['offset'];
					
					if($ech['xsOffset'])
					$offsetClass.= ' col-xs-offset-'.$ech['xsOffset'];
				
					if($ech['smOffset'])
					$offsetClass.= ' col-sm-offset-'.$ech['smOffset'];
					
					if($ech['mdOffset'])
					$offsetClass.= ' col-md-offset-'.$ech['mdOffset'];	
					
					if($ech['lgOffset'])
					$offsetClass.= ' col-lg-offset-'.$ech['lgOffset'];				

				
					if($colClass!='')
					{
						?>
						<div class="<?php echo $colClass.$offsetClass?> <?php echo $parentClassName?>" <?php echo $parentAttr?>>
						<?php
					}
					
					if($cntrlGroup)
					{
						?>
						<div class="row">
						<?php
					}					
					
					//no break for button
					if($ech['label']!='' && $ech['nobreak']!==true && $ech[0]!="checkbox")
					{
						?>
						<label class="control-label" <?php echo ($ech['id'])?('for="'.$ech['id'].'"'):''?>><?php echo ($ech[0]!="button")?$ech['label']:'&nbsp;'?></label><br/>
						<?php	
					}
				
					if($ech['inputGroupBtn'] || $ech['inputGroupSelect'])
					{
						?>
						<div class="input-group">
						<?php
					}

						if($ech['htmlPrepend'])
							echo $ech['htmlPrepend'];
				
						if($ech[0]=="button") {				
							?>
							<button class="btn <?php echo $className?>" <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech['name'])?$ech['name']:$ech['id']).(($cntrlGroup)?'[]':'')?>" ><?php echo $ech['label']?></button>			
							<?php
						}					
				
						if(in_array($ech[0],["text","password","email","number"])) {				
							?>							
							<input class="form-control<?php echo $className?>" <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech['name'])?$ech['name']:$ech['id']).(($cntrlGroup)?'[]':'')?>" placeholder="<?php echo ($ech['placeholder'])?$ech['placeholder']:$ech['label']?>" type="<?php echo $ech[0]?>" value="<?php echo ($ech[0]!='password')?(($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]!='' && !$cntrlGroup)?$_REQUEST[($ech['name'])?$ech['name']:$ech['id']]:$ech['value']):''?>">			
							<?php
						}

						
						if($ech[0]=="checkbox") {
							
							if($ech['nobreak']!==true) {								
								?>
								<br/>
								<?php
							}
							?>
							<input <?php echo ($className)?('class="'.$className.'"'):''?> <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech['name'])?$ech['name']:$ech['id']).(($cntrlGroup)?'[]':'')?>" type="<?php echo $ech[0]?>" value="<?php echo $ech['value']?>" <?php echo ((($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]!='' && $_REQUEST[($ech['name'])?$ech['name']:$ech['id']]==$ech['value']) || (!isset($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]) && !$_POST && ($ech['opts']['checked']===true || $ech['opts']['checked']=='checked')))?'checked="checked"':'')?>>
							<label class="control-label" <?php echo ($ech['id'])?('for="'.$ech['id'].'"'):''?>><?php echo $ech['label']?></label>
							<?php						
							if($ech['nobreak']!==true) {								
								?>
								<br/>
								<?php
							}
							else {
							?>
							&nbsp;
							<?php
							}
						}
						
						
						if($ech[0]=="textarea") {				
							?>
							<textarea class="form-control<?php echo $className?>" <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech['name'])?$ech['name']:$ech['id']).(($cntrlGroup)?'[]':'')?>" placeholder="<?php echo ($ech['placeholder'])?$ech['placeholder']:$ech['label']?>"><?php echo ($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]!='' && !$cntrlGroup)?$_REQUEST[($ech['name'])?$ech['name']:$ech['id']]:$ech['value']?></textarea>			
							<?php
						}						

						
						if($ech[0]=="select") {
							?>
							<select class="form-control<?php echo $className?>" <?php echo ($ech['multiple'])?'multiple':''?> <?php echo $attr?> <?php echo ($ech['id'])?('id="'.$ech['id'].'"'):''?> name="<?php echo (($ech['name'])?$ech['name']:$ech['id']).(($ech['multiple'] || $cntrlGroup)?'[]':'')?>" data-placeholder="<?php echo ($ech['placeholder'])?$ech['placeholder']:$ech['label']?>">
							<?php
							if($ech['dataSource']) {
								
								if(!$ech['multiple'] && strpos($className, 'select2') !== false) {
									?>
									<option value=""></option>
									<?php
								}
								
								if(!$ech['multiple'] && $ech['placeholder'] && strpos($className, 'select2') === false) {
									?>
									<option value=""><?php echo $ech['placeholder']?></option>
									<?php
								}								
								
								foreach($ech['dataSource'] as $option) {
									if(!$ech['multiple'])
										$selected = 
										(
											($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]!='' && !$cntrlGroup)?
											(($_REQUEST[($ech['name'])?$ech['name']:$ech['id']]==$option["value"])?'selected':''):
											(($ech['value']==$option["value"])?'selected':'')
										); 
									else 
										$selected = 									
										(
											($_REQUEST[($ech['name'])?$ech['name']:$ech['id']][0]!='' && !$cntrlGroup)?
											((@in_array($option["value"],$_REQUEST[($ech['name'])?$ech['name']:$ech['id']]))?'selected':''):
											(($ech['value']==$option["value"])?'selected':'')
										);
									?>
									<option <?php echo $selected?>  value="<?php echo $option["value"]?>"><?php echo $option["title"]?></option>
									<?php
								}
							}
							?>
							</select>					
							<?php
						}						
						
						if($ech['htmlAppend'])
							echo $ech['htmlAppend'];

					if($ech['inputGroupBtn'] || $ech['inputGroupSelect'])
					{
						?>
						<div class="input-group-btn"> 
						<?php
					}
					
					if($ech['inputGroupBtn'])
					{
						$attr = "";	
						$className = "";
						if($ech['inputGroupBtn']['opts'])
						{
							$tmpAttr = _toAttrs($ech['inputGroupBtn']['opts']);
							$attr = $tmpAttr['attr'];
							$className = $tmpAttr['class'];							
						}							
						?>						
							<button class="btn <?php echo $className?>" <?php echo $attr?>>
							<?php
							if($ech['inputGroupBtn']['ico'])
							{
								?>
								<i class="glyphicon glyphicon-<?php echo $ech['inputGroupBtn']['ico']?>"></i> 
								<?php
							}
							echo $ech['inputGroupBtn']['label'];
							?>
							</button>							
						<?php
					}

					if($ech['inputGroupSelect'])
					{
						$attr = "";	
						$className = "";
						if($ech['inputGroupSelect']['opts'])
						{
							$tmpAttr = _toAttrs($ech['inputGroupSelect']['opts']);
							$attr = $tmpAttr['attr'];
							$className = $tmpAttr['class'];							
						}

						if($ech['inputGroupSelect']['dataSource'])
						{
							$selected = '';
							foreach($ech['inputGroupSelect']['dataSource'] as $option)
							{
								if($_REQUEST[($ech['inputGroupSelect']['name'])?$ech['inputGroupSelect']['name']:$ech['inputGroupSelect']['id']]!='' && !$cntrlGroup)
								{
									if($_REQUEST[($ech['inputGroupSelect']['name'])?$ech['inputGroupSelect']['name']:$ech['inputGroupSelect']['id']]==$option["value"])
										$selected = $option["title"];
								}
								else
									if($ech['inputGroupSelect'][value]==$option["value"])
										$selected = $option["title"];
							}
							?>
							<input <?php echo ($ech['inputGroupSelect']['id'])?('id="'.$ech['inputGroupSelect']['id'].'"'):''?> name="<?php echo (($ech['inputGroupSelect']['name'])?$ech['inputGroupSelect']['name']:$ech['inputGroupSelect']['id']).(($cntrlGroup)?'[]':'')?>" type="hidden" value="<?php echo ($_REQUEST[($ech['inputGroupSelect']['name'])?$ech['inputGroupSelect']['name']:$ech['inputGroupSelect']['id']]!='' && !$cntrlGroup)?$_REQUEST[($ech['inputGroupSelect']['name'])?$ech['inputGroupSelect']['name']:$ech['inputGroupSelect']['id']]:$ech['inputGroupSelect']['value']?>">
							<button tabindex="-1" data-toggle="dropdown" class="btn dropdown-toggle <?php echo $className?>" <?php echo $attr?>>
								<span id="dropdown-<?php echo $ech['inputGroupSelect']['id']; ?>">
								<?php echo $selected?>
								</span>
								<span class="caret"></span>
								<span class="sr-only">Toggle Dropdown</span>
							</button>
							<ul class="dropdown-menu pull-right"> 
								<?php
								foreach($ech['inputGroupSelect']['dataSource'] as $option)
								{
									?>
									<li>
										<a onclick="document.getElementById('<?php echo $ech['inputGroupSelect']['id']?>').value='<?php echo addslashes($option["value"])?>'; document.getElementById('dropdown-<?php echo $ech['inputGroupSelect']['id']?>').innerHTML='<?php echo addslashes($option["title"])?>';">
										<?php echo $option["title"]?>
										</a>
									</li>
									<?php
								}
								?>
							</ul>								
							<?php
						}												
					}
					
					if($ech['inputGroupBtn'] || $ech['inputGroupSelect'])
					{
						?>
						</div>
						</div>
						<?php
					}

					if($cntrlGroup)
					{
						?>
						</div>
						<?php
					}					
										
					if($colClass!='')
					{
							
						?>
						</div>
						<?php
					}					
				}
			}
		}
		
		if($cntrlGroup)
		{
			?>		
			<div class="input-group-btn"> 			
				<button class="btn <?php echo $cntrlGroup['remove']?'btn-danger remove':''?><?php echo $cntrlGroup['add-more']?'btn-success  add-more':''?>" type="button" <?php echo $cntrlGroup['add-more']?' data-before="'.$cntrlGroup['add-more']['before'].'" data-copy="'.$cntrlGroup['add-more']['copy'].'"':''?>>
				<i class="glyphicon glyphicon-<?php echo $cntrlGroup['remove']?'remove':($cntrlGroup['add-more']?'plus':'ban-circle')?>"></i> 
				</button>
			</div>			
			<?php
		}
		
		if($group)
		{		
			?>
			</div>
			<?php
		}		
	}
}

// Form Control Groups for Addmore
function _controlGroup($fields,$cntrlGroup=[ remove=>true, hiddenCopy=>false ])
{
	if($cntrlGroup[hiddenCopy])
	{
		?>
		<div class="<?php echo $cntrlGroup[hiddenCopy]?> hide">
		<?php
	}
	_formGroup($fields,$cntrlGroup);
	if($cntrlGroup[hiddenCopy])
	{
		?>
		</div>
		<?php
	}	
}

// Form Elements without groups
function _formElement($fields)
{
	_formGroup($fields,false,false);	
}

// Modal header
function _modalHeader($title='',$id='')
{
	if($title!='')
	{
		?>
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
			<h4 class="modal-title"><?php echo $title?></h4>
		</div>
		<?php
	}
	?>
	<div class="modal-body">	
	<?php
	if($id!='')
	{
		?>
		<div id="<?php echo $id?>" class="box-header"></div>		
		<?php
	}
	?>
	<div class="box-body">
	<?php
}


// Modal footer
function _modalFooter($footerBtns=[])
{
	?>
	</div>	
	</div>	
	<?php
	if(sizeof($footerBtns))
	{
		?>
		<div class="modal-footer">		
		<?php
		foreach($footerBtns as $btn)
		{
			if($btn!='') {
				
					
				$attr = "";
				$customClass = '';
				
				if($btn['opts']) {
					$tmpAttr = _toAttrs($btn['opts']);
					$attr = $tmpAttr['attr'];
					$customClass = ' '.$tmpAttr['class'];						
				}
					
				if(!$btn['menu']) {
					
					if($btn['link']) {
						?>
						<a href="<?php echo $btn[link]?>" class="btn btn-<?php echo $btn[btn].$customClass?>" <?php echo $attr?>><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?></a>
						<?php	
					}
					else {
						?>
						<button type="button" class="btn btn-<?php echo $btn['btn'].$customClass?>" <?php echo $attr?>><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?></button>
						<?php					
					}
				}
				else {				
					?>
					<div class="btn-group" class="<?php echo $customClass?>" <?php echo $attr?>>
						<button type="button" class="btn btn-<?php echo $btn['btn']?> dropdown-toggle" data-toggle="dropdown"><?php echo ($btn['ico'])?'<span class="fa fa-'.$btn['ico'].'"></span> ':''?><?php echo $btn['label']?>
						&nbsp;
						<span class="caret"></span>
						<span class="sr-only">Toggle Dropdown</span>
						</button>
						<ul class="dropdown-menu dropdown-menu-right" role="menu">
							<?php
							foreach($btn['menu'] as $bSub)
							{
								if($bSub!='') {
									
									if($bSub=="divider") {
										?>
										<li class="divider"></li>
										<?php
									}
									else {
										$attr = "";
										$className = "";
										
										if($bSub['opts'])
										{			
											$tmpAttr = _toAttrs($bSub['opts']);
											$attr = $tmpAttr['attr'];
											$className = $tmpAttr['class'];									
										}								
										?>
										<li>
											<a <?php echo ($bSub['link'])?('href="'.$bSub['link'].'"'):''?> <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?>>
												<?php echo ($bSub['ico'])?'<span class="fa fa-'.$bSub['ico'].'"></span> ':''?>
												<?php echo $bSub['label']?>
											</a>
										</li>
										<?php
									}
								}
							}
							?>
						</ul>
					</div>	
					<?php
				}
			}			
		}
		?>
		</div>
		<?php
	}
}

$_navTabItems = [];
$_navTabVertical = [];
$_navTabVertical[0] = false;

// Tab Open
function _tabOpen($tabs,$opts=[ 'vertical'=>false ])
{
	global $_navTabItems;
	global $_navTabVertical;
	
	$tabR = ((isset($opts['prefix']))?$opts['prefix'].'_':'').'tab';
	
	$node = sizeof($_navTabItems);
	
	$_navTabItems[$node] = [];
	
	if(!$opts['vertical']) {
		?>
		<div class="nav-tabs-custom">
		<?php
	}
	else
	$_navTabVertical[$node] = true;	
	?>
		<input type="hidden" name="<?php echo $tabR?>" id="nav-tabs-custom_<?php echo $tabR?>" <?php echo (_request($tabR)!='')?'value="'._request($tabR).'"':''?> />
		<ul id="nav-tabs_<?php echo $tabR?>" class="nav nav-tabs<?php echo ((@$opts['responsive']!==false)?' nav-tabs-responsive ':'')?> <?php echo (!$opts['vertical'])?' ':$opts['vertical']['navClass']?>" role="tablist" >
			<?php
			$ti = 0;
			if(isset($tabs) && sizeof($tabs)) {
				foreach($tabs as $tab) {
					$attr = "";
					$onClick = "";
					$className = '';
					
					if($tab['opts']) {
						$tmpAttr = _toAttrs($tab['opts'],false,['onclick','responsive']);
						$attr = $tmpAttr['attr'];
						$className = $tmpAttr['class'];	
						$onClick = @$tmpAttr['onclick'];							
					}
					
					if(!$tab['menu']) {
						$_navTabItems[$node][] = [ 
							'id'=>_slug($tab['label']), 
							'active'=>( ($ti==0 && _slug(_request($tabR))=='') || (_slug(_request($tabR))==_slug($tab['label'])) )?true:false 
						];
						?>
						<li data-id="nav-tabs_<?php echo $tabR?>" <?php echo ( ($ti==0 && _slug(_request($tabR))=='') || (_slug(_request($tabR))==_slug($tab['label'])) )?'class="active nav-tabs-item"':'class="nav-tabs-item"'?> >
							<a href="#<?php echo _slug($tab['label'])?>" onclick="document.getElementById('nav-tabs-custom_<?php echo $tabR?>').value='<?php echo _slug($tab['label'])?>';<?php echo $onClick?>" <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?> role="tab" data-toggle="tab">
							<?php echo ($tab['ico'])?'<span class="fa fa-'.$tab['ico'].'"></span> ':''?><?php echo $tab['label']?>
							</a>
						</li>
						<?php
						$ti++;
					}
					else {
						?>
						<li class="dropdown <?php echo $className?>" <?php echo $attr?>>
							<a class="dropdown-toggle" data-toggle="dropdown" href="#">
							<?php echo ($tab['ico'])?'<span class="fa fa-'.$tab['ico'].'"></span> ':''?> 
							<?php echo $tab['label']?>
							<span class="caret"></span> 
							</a>
							<ul class="tab-dropdown-menu dropdown-menu">
								<?php
								foreach($tab['menu'] as $bSub) {
									if($bSub=="divider") {
										?>
										<li role="presentation" class="divider"></li>
										<?php
									}
									else {
										$attr = "";
										$className = "";
										
										if(isset($bSub['opts'])) {									
											$tmpAttr = _toAttrs($bSub['opts']);
											$attr = $tmpAttr['attr'];
											$className = $tmpAttr['class'];										
										}									
										?>
										<li role="presentation">
											<a role="menuitem" tabindex="-1" <?php echo ($bSub['link'])?('href="'.$bSub['link'].'"'):''?> <?php echo ($className!='')?'class="'.$className.'"':''?> <?php echo $attr?>>
											<?php echo ($bSub['ico'])?'<span class="fa fa-'.$bSub['ico'].'"></span>':''?><?php echo $bSub['label']?>
											</a>
										</li>
										<?php
									}
								}
								?>								
							</ul>
						</li>						
						<?php
					}
				}
				
				if(($opts['responsive']!==false)) {
					
					?>
					<li class="dropdown responsive-tab-menu"> 
						<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true">MORE <span class="caret"></span></a>
						<ul class="dropdown-menu">
						</ul>
					</li>				
					<?php
				}
			}
			?>							

		</ul>
		<div class="tab-content <?php echo ($opts['vertical'])?'':$opts['vertical']['contentClass']?>">
	<?php
	return $node;
}

// Tab Close
function _tabEnd($node=0)
{
	global $_navTabItems;
	global $_navTabVertical;
	
	?>
	</div>
	<?php	
	$_navTabItems[$node] = [];
	
	if(!$_navTabVertical[$node]) {
		?>
		</div>
		<?php
	}

	$_navTabVertical[$node] = false;	
}

// Tab Pane/Item open
function _paneOpen($opts=[],$node=0)
{
	global $_navTabItems;	
	$attr = '';
	$className = '';
	if(sizeof((array)$_navTabItems[$node])) {
		
		if($opts) {
			$tmpAttr = _toAttrs($opts);
			$attr = $tmpAttr['attr'];
			$className = $tmpAttr['class'];						
		}		
		?>
		<div class="tab-pane <?php echo $className.(($_navTabItems[$node][0]['active'])?' active':'')?>" <?php echo $attr?> id="<?php echo $_navTabItems[$node][0]['id']?>">
		<?php
	}
}

// Tab Pane/Item close
function _paneEnd($node=0)
{
	global $_navTabItems;	
	if(sizeof((array)$_navTabItems[$node])) {
		?>
		</div>
		<?php
	}
	if(isset($_navTabItems[$node]))
	array_shift($_navTabItems[$node]);
}
?>
