<?php 

function submit_btn($action, $label = "Submit", $id = "submit", $class = "btn btn-primary", $attr = array(), $is_return = false) {
	ob_start();
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	?>
	<input type="hidden" name="action" id="<?php echo ($id != 'submit') ? $id.'-' : ''; ?>action" value="<?php echo $action; ?>">
	<button class="<?php echo $class; ?> submit" id="<?php echo $id.'-'.$action; ?>" disabled 
		<?php if (_count($attr)): ?>
			<?php foreach ($attr as $key => $value): 
				echo " ".$key."=\"".$value."\"";
			endforeach ?>
		<?php endif ?>
	><?php echo $label; ?></button>
	<?php
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function submit_btn_html($action, $label = "Submit", $id = "submit", $class = "btn btn-primary", $attr = array()) {
	return submit_btn($action, $label, $id, $class, $attr, true);
}

function submit_btn_hidden($action, $ctrl_class = "submit", $attr = array(), $is_return = false) {
	ob_start();
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	add_page_javascript("app/assets/js/page-scripts/hidden-submit.js");
	$id = "handle-".$ctrl_class;
	?>
	<div class="hidden-submit" data-ctrl-class="<?php echo $ctrl_class ?>" data-action="<?php echo $action ?>"></div>
	<input type="hidden" name="action" id="<?php echo ($id != 'submit') ? $id.'-' : ''; ?>action" value="<?php echo $action; ?>">
	<button style="display:none;" class="submit" id="<?php echo $id.'-'.$action; ?>" disabled 
		<?php if (_count($attr)): ?>
			<?php foreach ($attr as $key => $value): 
				echo " ".$key."=\"".$value."\"";
			endforeach ?>
		<?php endif ?>
	>Submit hidden</button>
	<?php
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function alert($id = 'alert', $is_return = false) {
	ob_start();
	?>
	<div id="<?php echo $id; ?>" class="alert alert-success" role="alert" style="display: none;"><span></span></div>
	<?php
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function label($content = '', $html = 'h3', $is_return = false) {
	ob_start();
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	echo '<'.$html.'>'.$content.'</'.$html.'>';
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function input($args, $is_return = false) {
	if(!is_array($args)):
		echo "argument must be a single dimensional array";
		return;
	endif;
	if(!isset($args['label'])):
		echo "You must provide a label";
		return;
	endif;
	if(!isset($args['id'])):
		echo "You must provide an id";
		return;
	endif;
	ob_start();
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	global $app;
	$label = $args['label'];
	unset($args['label']);
	$id = $args['id'];
	unset($args['id']);
	if(!isset($args['name'])) :
		$args['name'] = $id;
	endif;
	if(!isset($args['type'])) :
		$args['type'] = 'text';
	endif;
	if(!isset($args['inline'])) :
		$args['inline'] = false;
	endif;
	if(!isset($args['class']) && ($args['type'] != 'checkbox' && $args['type'] != 'radio')) :
		$args['class'] = ($args['type'] === 'number') ? 'numberInput '.$id : 'form-control '.$id;
	endif;
	if($args['type'] == 'number') {
		add_page_javascript("app/assets/js/number-input.js");
		add_page_stylesheet("app/assets/css/number-input.css", "numberInput");
	}
	$hint = (isset($args['hint'])) ? $args['hint'] : false;
	if($hint) unset($args['hint']);
	do_action("before_".$id."_".$app->post_meta['permalink'], $args);
	?>
	<?php if ($args['type'] !== 'checkbox' && $args['type'] !== 'radio'): ?>
		<div class="form-group" id="form-group-<?php echo $id ?>">
	<?php endif ?>
	
	<?php 
	if($args['type'] != 'checkbox' && $args['type'] != 'radio' && $args['type'] != 'hidden' && $label != '') :
	 ?>
		<label for="<?php echo $id; ?>"><?php echo $label; echo (isset($args['required'])) ? '<span class="error mark-compulsory">*</span>' : ''; ?></label>
	<?php endif; ?>
	<?php if ($args['type'] === 'password'): ?>
		<div class="koweb-password-field">
	<?php endif ?>
		<input id="<?php echo $id; ?>"
			<?php 
				foreach ($args as $key => $value) {
					echo ' '.$key.'="'.$value.'"';
				}
				?>
		>
	<?php if($args['type'] === 'password') : ?>
	      <i class="fa fa-eye koweb-password-eye"></i>
	      </div>
	<?php endif; ?>
	<?php
				if(!($args['type'] != 'checkbox' && $args['type'] != 'radio')) {
					echo '<label style="font-weight: normal;" for="'.$id.'">&nbsp;'.$label.'</label>';
				}
			 ?>
	<?php if ($args['type'] !== 'checkbox' && $args['type'] !== 'radio'): ?>
		<?php if ($hint): ?>
			<small id="<?php echo $id; ?>-hint" class="form-text text-muted"><?php echo $hint; ?></small>
		<?php endif ?>
	</div>
	<?php elseif($args['inline']): ?>
		&nbsp;&nbsp;
	<?php else: ?>
		<br>
	<?php endif;	
	do_action("after_".$id."_".$app->post_meta['permalink'], $args);
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function textarea($args, $wyswyg = false, $is_return = false) {
	if(!is_array($args)):
		echo "argument must be a single dimensional array";
		return;
	endif;
	if(!isset($args['label'])):
		echo "You must provide a label";
		return;
	endif;
	if(!isset($args['id'])):
		echo "You must provide an id";
		return;
	endif;
	ob_start();
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	if(isset($args['readonly'])) {
		$wyswyg = false;
	}
	if($wyswyg) {
		?>
		<link rel="stylesheet" type="text/css" href="app/assets/Trumbowyg-master/dist/ui/trumbowyg.min.css">
		<style type="text/css">
			.trumbowyg-box, .trumbowyg-editor {
				margin: 0;
			}
		</style>
		<?php
		add_page_javascript('app/assets/Trumbowyg-master/dist/trumbowyg.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/colors/trumbowyg.colors.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/giphy/trumbowyg.giphy.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/base64/trumbowyg.base64.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/noembed/trumbowyg.noembed.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/pasteimage/trumbowyg.pasteimage.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/preformatted/trumbowyg.preformatted.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/insertaudio/trumbowyg.insertaudio.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/upload/trumbowyg.upload.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/trumbowyg.init.js');
	}
	$label = $args['label'];
	unset($args['label']);
	$id = $args['id'];
	unset($args['id']);
	if(!isset($args['name'])) :
		$args['name'] = $id;
	endif;
	if(isset($args['value'])) :
		$inp_value = $args['value'];
		unset($args['value']);
	else:
		$inp_value = '';
	endif;
	if(!isset($args['class'])) :
		$args['class'] = 'form-control';
	endif;
	if($wyswyg) {
		$args['class'] .= " wyswyg-editor";
	}
	$hint = (isset($args['hint'])) ? $args['hint'] : false;
	if($hint) unset($args['hint']);
	?>
	<div class="form-group">
		<?php if($label != '') : ?>
			<label for="<?php echo $id; ?>"><?php echo $label; echo (isset($args['required'])) ? '<span class="error mark-compulsory">*</span>' : ''; ?></label>
		<?php endif; ?>
		<textarea id="<?php echo $id; ?>"
			<?php 
				foreach ($args as $key => $value) {
					echo ' '.$key.'="'.$value.'"';
				}
				echo '>'.$inp_value.'</textarea>';
			 ?>
		
		<?php if ($hint): ?>
			<small id="<?php echo $id; ?>-hint" class="form-text text-muted"><?php echo $hint; ?></small>
		<?php endif ?>
	</div>
	<?php
	global $app;	
	do_action("after_".$id."_".$app->post_meta['permalink'], $args);
	$template = ob_get_contents();
	ob_get_clean();
	if($is_return) return $template;
	echo $template;
}

function select($args, $options = array(), $selected = '') {
	if(!is_array($args)):
		echo "argument must be a single dimensional array";
		return;
	endif;
	if(!isset($args['label'])):
		echo "You must provide a label";
		return;
	endif;
	if(!isset($args['id'])):
		echo "You must provide an id";
		return;
	endif;
	add_page_stylesheet("app/assets/css/lib/chosen/chosen.min.css", "chosen");
	add_page_stylesheet("app/assets/css/form.css", "formCss");
	$label = $args['label'];
	unset($args['label']);
	$id = $args['id'];
	unset($args['id']);
	if(!isset($args['name'])) :
		$args['name'] = $id;
	endif;
	if(!isset($args['class'])) :
		$args['class'] = 'standardSelect';
	endif;
	$hint = (isset($args['hint'])) ? $args['hint'] : false;
	if($hint) unset($args['hint']);
	?>
	<div class="form-group">
		<?php if($label != '') : ?>
			<label for="<?php echo $id; ?>"><?php echo $label; echo (isset($args['required'])) ? '<span class="error mark-compulsory">*</span>' : ''; ?></label>
		<?php endif; ?>
		<select id="<?php echo $id; ?>"
			<?php 
				foreach ($args as $key => $value) {
					echo ' '.$key.'="'.$value.'"';
				}
				echo '>'; 
				if($selected == '' && !isset($args['multiple'])) :
					echo '<option value="">Select...</option>';
				endif;
				if(_count($options) > 0):
					foreach ($options as $option => $opt_value) {
						if(isset($args['multiple'])) {
							// selected must be an array
							if(is_array($selected)) {
								$set_selected = (in_array($opt_value, $selected)) ? 'selected' : '';
							}
						} else {
							$set_selected = ($selected == $opt_value) ? 'selected' : '';
						}
						echo '<option value="'.$opt_value.'" '.$set_selected.'>'.$option.'</option>';
					}
				endif;
				echo'</select>';
			 ?>
		
		<?php if ($hint): ?>
			<small id="<?php echo $id; ?>-hint" class="form-text text-muted"><?php echo $hint; ?></small>
		<?php endif ?>
	</div>
	<?php
	global $app;
	do_action("after_".$id."_".$app->post_meta['permalink'], $args);
	global $post_meta;
	add_page_javascript("app/assets/js/lib/chosen/chosen.jquery.js");
	add_page_javascript("app/assets/js/lib/chosen/chosen.init.js");
}

function dynamic_select($args, $extend, $seperator = ",", $key = 'id', $val = 'id', $selected = array(), $table = 'post', $load = false) {
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/dynamic-select.js');
	$args = array_merge($args, ['multiple' => 'multiple', 'class' => 'standardSelect dynamicSelect']);
	if(!isset($extend['type'])) {
		echo "Please set the type attr in the extend var";
		return;
	}
	if(_count($selected) && is_array($selected) && $load) {
		add_page_javascript('app/assets/js/page-scripts/dynamic-select-load.js');
		?>
		<div data-id="<?php echo $args['id'] ?>" data-items='<?php echo json_encode($selected); ?>' class="dynamicSelectLoader"></div>
		<?php
	}
	?>
	<span id="<?php echo $args['id'] ?>-args" data-val='<?php echo $val; ?>' data-table='<?php echo $table; ?>' data-seperator='<?php echo $seperator ?>' data-key='<?php echo $key ?>' data-args='<?php echo json_encode($extend); ?>'></span>
	<?php
	select($args, array(), $selected);
}

function selectgrp($args, $options = array(), $selected = '') {
	if(!is_array($args)):
		echo "argument must be a single dimensional array";
		return;
	endif;
	if(!isset($args['label'])):
		echo "You must provide a label";
		return;
	endif;
	if(!isset($args['id'])):
		echo "You must provide an id";
		return;
	endif;
	$label = $args['label'];
	unset($args['label']);
	$id = $args['id'];
	unset($args['id']);
	if(!isset($args['name'])) :
		$args['name'] = $id;
	endif;
	if(!isset($args['class'])) :
		$args['class'] = 'form-control';
	endif;
	$hint = (isset($args['hint'])) ? $args['hint'] : false;
	if($hint) unset($args['hint']);
	?>
	<div class="form-group">
		<?php if($label != '') : ?>
			<label for="<?php echo $id; ?>"><?php echo $label; echo (isset($args['required'])) ? '<span class="error mark-compulsory">*</span>' : ''; ?></label>
		<?php endif; ?>
		<select id="<?php echo $id; ?>"
			<?php 
				foreach ($args as $key => $value) {
					echo ' '.$key.'="'.$value.'"';
				}
				echo '>'; 
				if($selected == '' && !isset($args['multiple'])) :
					echo '<option value="">Select...</option>';
				endif;
				if(_count($options) > 0):
					foreach ($options as $grp_label => $opt_grp) {
						echo '<optgroup label="'.$grp_label.'">';
						if(_count($opt_grp) > 0) {
							foreach ($opt_grp as $option => $opt_value) {
								if(isset($args['multiple'])) {
									// selected must be an array
									if(is_array($selected)) {
										$set_selected = (in_array($opt_value, $selected)) ? 'selected' : '';
									}
								} else {
									$set_selected = ($selected == $opt_value) ? 'selected' : '';
								}
								echo '<option value="'.$opt_value.'" '.$set_selected.'>'.$option.'</option>';
							}
						}
						echo '</optgroup>';
					}
				endif;
				echo'</select>';
			 ?>
		
		<?php if ($hint): ?>
			<small id="<?php echo $id; ?>-hint" class="form-text text-muted"><?php echo $hint; ?></small>
		<?php endif ?>
	</div>
	<?php
	global $app;	
	do_action("after_".$id."_".$app->post_meta['permalink'], $args);
}

function selectgrp_html($args, $options = array(), $selected = '') {
	if(!is_array($args)):
		return "argument must be a single dimensional array";
	endif;
	if(!isset($args['label'])):
		return "You must provide a label";
	endif;
	if(!isset($args['id'])):
		return "You must provide an id";
	endif;
	$label = $args['label'];
	unset($args['label']);
	$id = $args['id'];
	unset($args['id']);
	if(!isset($args['name'])) :
		$args['name'] = $id;
	endif;
	if(!isset($args['class'])) :
		$args['class'] = 'form-control';
	endif;
	$html = "<div class=\"form-group\">";
	if($label != '') :
		$html .= "<label for=\"".$id."\">".$label; 
		$html .= isset($args['required']) ? "<span class=\"error mark-compulsory\">*</span>" : '';
		$html .= "</label>";
	endif;
	$html .= "<select id=\"".$id."\"";
	foreach ($args as $key => $value) {
		$html .= " ".$key."=\"".$value."\"";
	}
	$html .= ">"; 
	if($selected == '' && !isset($args['multiple'])) :
		$html .= "<option value=\"\">Select...</option>";
	endif;
	if(_count($options) > 0):
		foreach ($options as $grp_label => $opt_grp) {
			$html .= "<optgroup label=\"".$grp_label."\">";
			if(_count($opt_grp) > 0) {
				foreach ($opt_grp as $option => $opt_value) {
					$set_selected = "";
					if(isset($args['multiple'])) {
						// selected must be an array
						if(is_array($selected)) {
							$set_selected = (in_array($opt_value, $selected)) ? 'selected' : '';
						}
					} else {
						$set_selected = ($selected == $opt_value) ? 'selected' : '';
					}
					$html .= "<option value=\"".$opt_value."\" ".$set_selected.">".$option."</option>";
				}
			}
			$html .= "</optgroup>";
		}
	endif;
	$html .= "</select>";
	$html .= "<div class=\"error\"><em></em></div>";
	$html .= "</div>";
	return $html;
}


function checkBox($args, $state = false) {
	if($state) {
    	$args = array_merge(['checked' => 'checked', 'type' => 'checkbox', 'value' => 1], $args);
    } else {
    	$args = array_merge(['type' => 'checkbox', 'value' => 1], $args);
    }
    input($args);
}

function radio($args, $options, $value = false) {
	if(!is_array($options)) {
		echo "Options is not a valid array";
		return;
	}
	if(!_count($options)) {
		echo "Please set options";
		return;
	}
	foreach ($options as $key => $label) {
		if($value === $key) {
			$setting = array_merge(['checked' => 'checked', 'type' => 'radio', 'label' => $label, 'id' => $args['name'].$key, 'value' => $key], $args);
		} else {
			$setting = array_merge(['type' => 'radio', 'label' => $label, 'id' => $args['name'].$key, 'value' => $key], $args);
		}
		input($setting);
	}
} 

function printDiv($div = 'print') {
	global $post_meta;
	?>
	<div class="col-md-12 pb-2 pt-2">
	<button onclick="instantPrint('<?php echo $div ?>')" class="btn btn-default"><i class="fa fa-print"></i>&nbsp;Print</button>
	</div>
	<!-- <a href="#" data-divname="<?php echo $div; ?>" id="downloadPdf" class="btn btn-default">Download PDF</a>
	<form method="post" action="pdf.php" id="downloadPdfForm">
		<input type="hidden" name="content" value="" id="content">
	</form> -->
	<?php
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}

function ctc_btn($text_div_id, $btn_text = "Copy Text", $display = true) {
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	$html = "<div id=\"ctc-cont-".$text_div_id."\">";
	$html .= "<button class=\"ctc-btn btn btn-primary btn-sm\" id=\"ctc-btn-".$text_div_id."\" data-id=\"".$text_div_id."\" data-text-field=\"".$text_div_id."\">".$btn_text."</button>";
	$html .= "</div>";
	if($display) {
		echo $html;
	} else {
		return $html;
	}
}