<?php 

function display_image_uploader($args) {
	global $post_meta;
	?>
	<style type="text/css">
		label.cabinet {
			display: block;
			cursor: pointer;
			padding: 0;
			margin: auto;
			/*width: 200px;*/
			position: relative;
		}

		label.cabinet input.file{
			position: relative;
			height: 100%;
			width: auto;
			opacity: 0;
			-moz-opacity: 0;
		  filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);
		  margin-top:-30px;
		}

		#upload-demo{
			width: 250px;
			height: 250px;
		  padding-bottom:25px;
		}
		figure figcaption {
			position: absolute;
			right: 10px;
			color: #fff;
			bottom: 0px;
			padding-left: 9px;
			padding-bottom: 5px;
			text-shadow: 0 0 10px #000;
			display: inline-block;
		}

		.cabinet figure img {
			display: block;
			margin: auto;
		}

		#item-img {
			display: none;
		}
	</style>
	<link href='app/assets/css/croppie.css' rel='stylesheet' />
	<div class="col-lg-12">
		<div id="datacapture"  data-boundarywidth=<?php echo $args['boundarywidth'] ?> data-boundaryheight=<?php echo $args['boundaryheight'] ?> data-width=<?php echo $args['width'] ?> data-height=<?php echo $args['height'] ?> data-url="<?php echo $args['url']; ?>"></div>
			<label class="cabinet center-block" style="width: <?php echo $args['width']; ?>px; height: <?php echo $args['height']; ?>px">
				<figure>
					<img src="<?php echo ($args['url'] != '') ? $args['url'] : 'https://user.gadjian.com/static/images/personnel_boy.png' ?>" class="gambar img-responsive img-thumbnail" id="item-img-output" style="width: <?php echo $args['width']; ?>px; height: <?php echo $args['height']; ?>px" />
					<figcaption><i class="fas fa-camera"></i></figcaption>
			    </figure>
				<input type="file" id="item-img" class="file center-block" name="<?php echo $args['image-name'] ?>"/>
				<input type="hidden" id="imagebase64" name="imagebase64">
			</label>
		<div class="modal" id="cropImagePop" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
			    <div class="modal-content">
				<div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				  <h4 class="modal-title" id="myModalLabel">Edit Photo</h4>
				</div>
				<div class="modal-body" style="height: <?php echo $args['boundarywidth'] +100 ?>px">
		            <div id="upload-demo" class=""></div>
		      	</div>
				<div class="modal-footer">
					<input type="range" name="">
			        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
			        <button type="button" id="cropImageBtn" class="btn btn-primary">Crop</button>
			   	</div>
			    </div>
			</div>
		</div>
	</div>
	<?php
	add_page_javascript('app/assets/js/page-scripts/Croppie-master/croppie.js');
	add_page_javascript('app/assets/js/page-scripts/image-uploader.js');
}

function croppie_api($args = array()) {
	$defaults = [
		"width" => 200,
		"height" => 200,
		"url" => "app/images/croppie-default.png",
		"id" => md5(rand(1, 5000)),
		"image-name" => "croppie-file",
		"enable_resize" => false,
		"required" => false,
		"placeholder" => "Image"
	];
	$args = array_merge($defaults, $args);
	$args["boundarywidth"] = $args["width"] + 5;
	$args["boundaryheight"] = $args["height"] + 5;
	?>
	<link href='app/assets/css/croppie-init.css' rel='stylesheet' />
	<link href='app/assets/css/croppie.css' rel='stylesheet' />
	<div class="croppie-container">
		<div id="datacapture-<?php echo $args['id'] ?>" data-width="<?php echo $args['width'] ?>" data-height="<?php echo $args['height'] ?>" data-url="<?php echo $args['url']; ?>" data-enable_resize=<?php echo $args['enable_resize'] ?>></div>
			<label class="cabinet" id="cabinet-<?php echo $args['id'] ?>">
				<figure>
					<img src="<?php echo $args['url']; ?>" class="gambar img-responsive img-thumbnail croppie-image" data-control-id="<?php echo $args['id'] ?>" id="croppie-output-<?php echo $args['id'] ?>" data-default-src="<?php echo $args['url'] ?>"/>
					<figcaption><i class="fas fa-camera"></i></figcaption>
			    </figure>
				<input type="file" class="croppie-file" data-control-id="<?php echo $args['id'] ?>" id="item-img-<?php echo $args['id'] ?>" class="file center-block" name="<?php echo $args['image-name'] ?>" accepts="image/*"/>
				<input type="hidden" id="imagebase64-<?php echo $args['id'] ?>" placeholder="<?php echo $args['placeholder'] ?>" <?php echo ($args['required']) ? 'required' : ''; ?> name="imagebase64-<?php echo $args['image-name'] ?>">
			</label>
		<div class="modal" id="cropImagePop-<?php echo $args['id'] ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			
			<div class="modal-dialog" id="cropDiag-<?php echo $args['id'] ?>" style="width: <?php echo $args['boundarywidth'] +40 ?>px; ">
			    <div class="modal-content">
				<!-- <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				  <h4 class="modal-title" id="myModalLabel-<?php echo $args['id'] ?>">Edit Photo</h4>
				</div> -->
				<div class="modal-body" style="min-width: <?php echo $args['boundarywidth'] +40 ?>px; ">
					<div class="handle">
						<div class="top" id="top-<?php echo $args['id'] ?>"></div>
						<div class="bottom" id="bottom-<?php echo $args['id'] ?>"></div>
						<div class="left" id="left-<?php echo $args['id'] ?>"></div>
						<div class="right" id="right-<?php echo $args['id'] ?>"></div>
					</div>
		            <div id="croppie-preview-<?php echo $args['id'] ?>" data-control-id="<?php echo $args['id'] ?>" class="croppie-preview"></div>
		            <div class="croppie-controls">
						<div class="btn-group">
			            	<button type="button" class="croppie-cancel-btn btn btn-primary btn-sm" data-control-id="<?php echo $args['id'] ?>" data-dismiss="modal">close</button>
			            	<button type="button" class="croppie-rotate-btn btn btn-primary btn-sm" data-control-id="<?php echo $args['id'] ?>" data-deg="90">left</button>
			            	<button type="button" class="croppie-rotate-btn btn btn-primary btn-sm" data-control-id="<?php echo $args['id'] ?>" data-deg="-90">right</button>
				        	<button type="button" class="croppie-crop-btn btn btn-primary btn-sm" data-control-id="<?php echo $args['id'] ?>" id="cropImageBtn-<?php echo $args['id'] ?>">crop</button>
			            </div>
					</div>
		      	</div>
				
			    </div>
			</div>
		</div>
	</div>
	<?php
	add_page_javascript('app/assets/js/page-scripts/Croppie-master/croppie.js');
	add_page_javascript('app/assets/js/page-scripts/Croppie-master/croppie-init.js');
}

function add_db_section_to_form($args) {
	// don't add db section if section database is turned off
    if(!get_option("section_database")) return false;

    if(!current_user_can("change_db_section")) return false;

    if(isset($args['id'])) {
    	$user = new user;
    	$user_meta = $user->get_user_meta($args['id']);
    	$exempt_users = get_option("exempt_users");
    	if(in_array($user_meta['role'], $exempt_users))
    		return false;
    }

    if(!isset($args['attr'])) {
    	$args['attr'] = array();
    }

    // defaults
    $args['label'] = !isset($args['label']) ? 'Database Section' : $args['label'];
    $args['selected'] = !isset($args['selected']) ? '' : $args['selected'];

    $settings = array_merge($args['attr'], ['label' => $args['label'], 'id' => 'db_section_id', 'required' => 'required']);
    $settings = array_unique($settings);

    $section_field = get_option("section_field");
    if($args['all']) {
    	$options = ['All' => 'all'];
    } else $options = [];
    $options = array_merge($options, get_select_options_from_post(['type' => $section_field, 'status' => 'active'], 'section_name', 'id', true, 'ASC'));
    select($settings, $options, $args['selected']);
}

function delete_post_button($args) {
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	?>
	<a href="javascript:void(0)" class="ajax-action red" data-title="<span class='red'>Attention </span> Please!!!" data-content="Are you sure you want to permanently delete this item?<br>Note that you cannot retrieve an item deleted permanently from the system. <br>Click `continue` to proceed and `cancel` to abort" data-callback="refreshPage" data-action-callback="delete_post" data-id="<?php echo $args['id'] ?>" data-return-method="<?php echo $args['return_method'] ?>">delete</a>
	<?php
}

function profile_state($args) {
	$user = new user;
	$user->set_login_status($args);
	$session = Session::getInstance();
	?>
	<table class="table">
		<tr>
	      <td class="pt-0 pb-0">Role</td>
	      <td class="pt-0 pb-0"><?php echo get_role_name($args['role']); ?></td>
	    </tr>
	    <tr>
	      <td class="pt-0 pb-0">Duration Online</td>
	      <td class="pt-0 pb-0"><?php echo (isset($user->duration_online)) ? _time_btw_string($user->duration_online, false, true) : 'NA'; ?></td>
	    </tr>
	    <tr>
	      <td class="pt-0 pb-0">Last Online</td>
	      <td class="pt-0 pb-0"><?php echo (isset($user->last_login)) ? date(_DATE_FORMAT_.' '._TIME_FORMAT_, $user->last_login) : 'NA'; ?></td>
	    </tr>
	    <tr>
	      <td class="pt-0 pb-0">Login Status</td>
	      <td class="pt-0 pb-0"><i class="fas fa-circle <?php echo (isset($user->login_status) && $user->login_status == 'online') ? 'text-success' : 'text-danger'; ?>"></i>&nbsp;&nbsp;<?php echo (isset($user->login_status)) ? $user->login_status : 'NA'; ?></td>
	    </tr>
	    <?php if (current_user_can("delete_user") && $args['id'] !== $session->id): ?>
	    <tr>
	    	<td>
	    		<?php if ($args['status'] == "active"): ?>
	    			<a href="javascript:void(0)" class="btn btn-danger ajax-action" data-id="<?php echo $args['id'] ?>" data-action-callback="deactivate_user" data-callback="refreshPage" data-content="Are you sure you want to deactivate this user account? <br>This implies that the user can no longer access this portal.">Deactivate User</a>
	    		<?php else: ?>
	    			<a href="javascript:void(0)" class="btn btn-success ajax-action" data-id="<?php echo $args['id'] ?>" data-action-callback="activate_user" data-callback="refreshPage" data-content="Are you sure you want to activate this user account? By so doing, the user will be able to access this portal.">Activate User</a>
	    		<?php endif ?>
	    	</td>
	    	<td>
	    		<?php if ($args['status'] == "inactive"): ?>
	    			<a href="javascript:void(0)" class="btn btn-danger ajax-action" data-id="<?php echo $args['id'] ?>" data-action-callback="delete_user" data-content="Are you sure you want to delete <?php echo get_fullname($args) ?>'s account? <br>By so doing, all the information about this particular user will be lost. If you still have need to access user information, please click on cancel and consider using the deactivation button instead.">Delete User</a>
	    		<?php endif ?>
	    	</td>
	    </tr>
	    <?php endif ?>
	    <?php do_action("more_profile_state", $args); ?>
	</table>
	<?php
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}

function drag_n_drop_uploader($args) {
	global $app;
	global $post_meta;
	$user_meta = $app->user_meta;
	if(!isset($args['ref'])) {
		echo "You must provide value for ref";
		return;
	}
	if(isset($args['user_id'])) {
		$user_id = $args['user_id'];
	} else $user_id = $user_meta['id'];
	if(!isset($args['id'])) {
		$args['id'] = $args['ref'];
	}
	if(!isset($args['max_files'])) {
		$args['max_files'] = get_option("dz_default_max_files");
	}
	if(!isset($args['max_size'])) {
		$args['max_size'] = 300;
	}
	$exts = "";
	if(!isset($args['info'])) {
		$args['info'] = "<pre style=\"line-height: 1em;font-size: small;color: brown;\">Max Files: ".$args['max_files']." files<br/>";
		$args['info'] .= "Max File Size: ".$args['max_size']." MB<br/>";
		$args['info'] .= "Allowed Files: ";
		if(isset($args['accepts']) && $args['accepts'] !== "") {
			$exts = mime_str_to_ext($args['accepts']);
			$args['info'] .= $exts;
		} else $args['info'] .= "All file types";
		$args['info'] .= "</pre>";
	}
	if(!isset($args['msg'])) {
		$args['msg'] = "Click or Drop files here to upload";
	}
	add_page_javascript('app/elements/form.js');
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('app/assets/js/dropzone/dropzone.js');
	add_page_javascript('app/assets/js/magnify/dist/jquery.magnify.js');
	add_page_javascript('app/assets/js/magnify/init.js');
	add_page_javascript('app/assets/js/dropzone-init.js')
	?>
	<style type="text/css">
		.dz-image img{ width: inherit; height: inherit;}
	</style>
	<link rel="stylesheet" type="text/css" href="app/assets/js/dropzone/dropzone.css">
	<link href="app/assets/js/magnify/dist/jquery.magnify.css" rel="stylesheet">
	<form method="post" data-clearform="1" enctype="multipart/form-data" action="app/includes/ajax.php" data-max-files="<?php echo $args['max_files'] ?>" data-max-size="<?php echo $args['max_size'] ?>" data-id="<?php echo $args['id'] ?>" id="my-awesome-dropzone-<?php echo $args['id'] ?>" class="dropzone mt-4 mb-0" data-accepts="<?php echo $args['accepts'] ?>" data-msg="<?php echo $args['msg'] ?>" data-exts="<?php echo $exts ?>">
		<input type="hidden" id="user_id-<?php echo $args['id'] ?>" name="user_id" value="<?php echo $user_id ?>" />
		<input type="hidden" id="ref-<?php echo $args['id'] ?>" name="ref" value="<?php echo $args['ref'] ?>" />
		<input type="hidden" id="id-<?php echo $args['id'] ?>" name="id" value="<?php echo $args['id'] ?>" />
		<div class="fallback">
			<div class="files-uploaded">
				<ul class="file-list" data-user-id="<?php echo $user_id ?>" data-ref="<?php echo $args['ref'] ?>" id="fileList-<?php echo $args['id'] ?>"></ul>
			</div>
		    <input name="file" type="file" accept="<?php echo $args['accepts'] ?>" />
		    <button type="submit" class="submit">Upload</button>
		    <?php 
		    	add_page_javascript('app/assets/js/page-scripts/submit.js'); 
		    ?>
		</div>
	  <input type="hidden" name="action" value="dropzone_image_library" />
	</form>
	<div class="mt-0 mb-5"><?php echo $args['info'] ?></div>
	<?php
}

function login_as_user_btn($args) {
	if(!current_user_can('login_as_user'))
		return;
	?>
	<div class="row mb-4">
		<div class="col-md-12">
		<a class="btn btn-sm btn-primary text-center" href="dashboard?action=<?php echo base64_encode('login_as_user') ?>&id=<?php echo $args['id'] ?>"><i class="fas fa-user-secret"></i>&nbsp;LOGIN AS <?php echo get_fullname($args); ?></a>
		</div>
	</div>
	<?php
}

function _display_message($args) {
	$class = isset($args['class']) ? $args['class'] : 'alert alert-danger';
	?>
	<div class="row">
		<div class="col-md-12">
			<div class="<?php echo $class ?>"><?php echo $args['msg'] ?></div>
		</div>
	</div>
	<?php
}

function click_to_copy_button($args) {
	?>
	<div class="mt-2 mb-2" id="ctc-cont<?php echo (isset($args["id"])) ? "-".$args["id"] : ""; ?>">
		<button class="<?php echo $args['class'] ?>" <?php echo (isset($args["success-class"])) ? 'data-success-class="'.$args["success-class"].'"' : ''; ?> <?php echo (isset($args["id"])) ? 'data-id="'.$args["id"].'"' : ''; ?> onclick="ctc_copyText(this);" data-text-field="<?php echo $args['text-field']; ?>" id="ctc-btn"><i class="fas fa-copy"></i> Click here to copy</button>
	</div>
	<script type="text/javascript">
	function ctc_copyText(btnid){
			var list = document.getElementById(btnid.dataset.textField).innerHTML;
			var input = document.createElement("INPUT");
			input.setAttribute("type", "text");
			input.setAttribute('id', 'ctc_temp_input');
			input.value = list;
			if(btnid.dataset.id == null) {
				var cont = "ctc-cont";
			} else var cont = "ctc-cont-"+btnid.dataset.id;
			var x = document.getElementById(cont).appendChild(input);
			x.select();
	  		document.execCommand("Copy");
	  		input.remove();
	  		var btn = document.getElementById(cont).children[0];
	  		if(btnid.dataset.successClass != null) {
	  			btn.className = btnid.dataset.successClass;
	  		}
	  		btn.innerHTML = 'COPIED <i class="fas fa-check"></i>';
		}
	</script>
	<?php
}

function before_headers() {
	global $app;
    $get = $_GET;
    $title = $app->post_meta['title'] . " | " . get_option('company_name');
	$url = get_option('home_url').$app->post_meta['permalink'];
	if(_count($_GET)) {
		$url .= "?".http_build_query($_GET);
	}
	$meta = [
		'title' => $title,
		'og:title' => $title,
		'og:type' => "website",
		'description' => '',
		'og:url' => $url,
		'og:image' => '',
		'twitter:title' => $title,
		'twitter:description' => '',
		'twitter:image' => '',
		'twitter:card' => '',
		'keywords' => ''
	];
    $meta = apply_filters("filter_page_meta_tags", $meta, $app->post_meta, $get);
	?>
		<meta charset="utf-8">
	    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	    <title><?php echo $meta['title'] ?></title>
	    <meta name="keywords" content="<?php echo $meta['keywords'] ?>" />
		<meta name="description" content="<?php echo $meta['description'] ?>"/>
		<meta property="og:title" content="<?php echo $meta['og:title'] ?>" />
		<meta property="og:type" content="<?php echo $meta['og:type'] ?>" />
		<meta property="og:url" content="<?php echo $meta['og:url'] ?>" />
		<meta property="og:image" content="<?php echo $meta['og:image'] ?>" />
		<meta name="twitter:title" content="<?php echo $meta['twitter:title'] ?>">
		<meta name="twitter:description" content="<?php echo $meta['twitter:description'] ?>">
		<meta name="twitter:image" content="<?php echo $meta['twitter:image'] ?>">
		<meta name="twitter:card" content="<?php echo $meta['twitter:card'] ?>">
	<?php
}

function headers() {
	$primary_color = ($option = get_option('primary_color')) ? $option : "#337ab7";
    $secondary_color = ($option = get_option('secondary_color')) ? $option : "#1a2226";
	?>
	<style type="text/css">
		:root {
		  --primaryColor: <?php echo $primary_color ?>;
		  --secondaryColor: <?php echo $secondary_color ?>;
		}
	</style>

  <?php 
  	add_page_stylesheet("app/bower_components/bootstrap/dist/css/bootstrap.min.css", "bootstrap");
  	add_page_stylesheet("app/assets/css/bootstrap3-modifier.css", "bootstrapModifier");
  	add_page_stylesheet("app/assets/plugins/fontawesome-free/css/all.min.css", "fontawesome");
  	add_page_stylesheet("app/assets/css/jquery-confirm.min.css", "jqueryConfirm");
  	add_page_stylesheet("app/assets/css/koweb.css", "koweb");
  	add_page_stylesheet("app/assets/css/animate.css", "animate");
  	add_page_stylesheet("app/assets/css/notification.css", "notifications");
  ?>
  <script src="app/assets/plugins/jquery/jquery.min.js?ver=<?php echo VERSION ?>"></script>
  <link href="<?php echo get_option('company-icon') ?>" rel="shortcut icon" />
  <link rel="stylesheet" href="app/assets/css/print.css?ver=<?php echo VERSION ?>" media="print">
  <?php if (get_option("browser_update")): ?>
  	<script> 
		var $buoop = {required:{e:-2,f:-3,o:-2,s:-2,c:-4},insecure:true,unsupported:true,api:2019.12 }; 
		function $buo_f(){ 
		 var e = document.createElement("script"); 
		 e.src = "app/browser-update.org/update.min.js"; 
		 document.body.appendChild(e);
		};
		try {document.addEventListener("DOMContentLoaded", $buo_f,false)}
		catch(e){window.attachEvent("onload", $buo_f)}
	</script>
  <?php endif ?>
  <style type="text/css">
  	.notifyjs-bootstrap-base {
		white-space: inherit !important;
		max-width: 250px;
		min-width: 200px;
	}
	.koweb-password-field {
		position: relative;
	}
	.koweb-password-eye {
		position: absolute;
		right: 6px;
		top: 30%;
		font-size: 14px !important;
		opacity: 0.6;
		color: #848484;
		cursor: pointer;
	}
  </style>
	<?php
}

function footers() {
	// add_page_javascript("app/assets/plugins/bootstrap/js/bootstrap.bundle.min.js");
	add_page_javascript("app/bower_components/bootstrap/dist/js/bootstrap.min.js");
	add_page_javascript("app/assets/js/notify.min.js");
	add_page_javascript("app/assets/js/notifications.js");
	add_page_javascript("app/assets/js/jquery-confirm.min.js");
	add_page_javascript("app/assets/js/koweb-validator.js");
	add_page_javascript("app/assets/js/apply-required-fields.js");
	// add_page_javascript("app/assets/magnific-popup/dist/jquery.magnific-popup.min.js");
	?>
	<!-- inAppPopup -->
	<div class="modal fade" id="inAppPopup" tabindex="-1" role="dialog" aria-labelledby="inAppPopupLabel" aria-hidden="true">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-body">
	      	<button type="button" class="close" data-dismiss="modal" aria-label="Close">
	          <span aria-hidden="true">&times;</span>
	        </button>
	        <iframe src="request-payout?ispopup=true" width="100%" height="500px" frameborder="0"></iframe>
	      </div>
	    </div>
	  </div>
	</div>
	<input type="hidden" name="validationRegex45456ghk" id="validationRegex45456ghk" value="<?php echo get_option('number_text_regex') ?>">
    <script type="text/javascript">
        jQuery(document).ready(function($){
            $(document).on("click", ".koweb-password-eye", function(e){
            	e.preventDefault();
            	var inp_el = $(this).parent().find("input");
            	if($(inp_el).attr("type") === "password") {
            		$(inp_el).attr("type", "text");
            		$(this).removeClass("fa-eye").addClass("fa-eye-slash");
            	} else {
            		$(inp_el).attr("type", "password");
            		$(this).removeClass("fa-eye-slash").addClass("fa-eye");
            	}
            });
            $('#inAppPopup').modal({ show: false});
        })
    </script>
    <?php render_page_scripts();
}

function build_filter_columns($args) {
	$columns = $args['all'];
	?>
	<div class="row">
		<div class="col-md-12">
			<?php if (is_array($columns)): ?>
				<?php if (_count($columns)): ?>
					<?php foreach ($columns as $key => $value): ?>
						<input type="checkbox" id="<?php echo $key ?>" name="column[<?php echo $key ?>]" value="<?php echo $value ?>" <?php echo (isset($args['selected'][$key]) && $args['selected'][$key]) ? "checked" : ""; ?>>&nbsp;<label for="<?php echo $key ?>"><?php echo $value ?></label>&nbsp;
					<?php endforeach ?>
				<?php endif ?>
			<?php endif ?>
		</div>
	</div>
	<?php
}

function visitors_filter() {
	do_action("build_filter_columns", ["all" => [
			'request_time' => 'Time',
			'visitor_name' => "Visitor's Name",
			'visitor_id' => 'ID',
			'visitor_ip' => 'IP',
			'visit_page' => 'Page',
			'page_link' => 'Url',
			'load_time' => 'LT',
			'user_agent' => 'User Agent',
			'referer' => 'Referer',
			'status' => 'Status',
			'request_method' => 'Method'
		], "selected" => [
			'request_time' => 'Time',
			'visitor_name' => "Visitor's Name",
			// 'visitor_id' => 'ID',
			'visitor_ip' => 'IP',
			'visit_page' => 'Page',
			'page_link' => 'Url',
			// 'load_time' => 'LT',
			// 'user_agent' => 'User Agent',
			// 'referer' => 'Referer',
			// 'status' => 'Status',
			'request_method' => 'Method'
		]]);
}

function users_filter() {
	global $post_meta;
	if(current_user_can("see_from_software_engineers")) {
		$system_roles = get_system_roles();
	} 
	else if (current_user_can("see_from_super_admin_tech")) {
		$system_roles = get_system_roles(["software_engineer"]);
	}
	else if (current_user_can("see_from_super_admin")) {
		$system_roles = get_system_roles(["software_engineer", "super_admin_tech"]);
	}
	else if (current_user_can("see_from_admin_tech")) {
		$system_roles = get_system_roles(["software_engineer", "super_admin_tech", "super_admin"]);
	}
	else if (current_user_can("see_from_admin")) {
		$system_roles = get_system_roles(["software_engineer", "super_admin_tech", "super_admin", "admin_tech"]);
	} else return;
	$system_roles = apply_filters('users_filter_system_roles_filter', $system_roles);
	$system_roles = array_merge(["All" => "_all"], $system_roles);
	
  ?>
	<div class="col-md-12">
	<?php do_action("users_columns_before"); ?>
  	<?php 
  		$selected = "";
  		$filters = apply_filters("users_columns_filter", ["all" => [
  	  		'fullname' => 'Fullname',
  			'username' => 'Username',
  			'email' => "Email",
  			'role' => 'Role',
  			'phone' => 'Phone',
  			'address' => 'Address',
  			'profile_picture' => 'Profile Picture',
  			'dob' => 'Date of Birth'
  		],
  		"selected" => [
  			'fullname' => true,
  			'email' => true,
  			'phone' => true,
  			'profile_picture' => true,
  		]]);
  		do_action("build_filter_columns", $filters); 
  	?>
  	<?php do_action("users_columns_after"); ?>
	</div>
      <hr>
      	<?php do_action("users_filter_before"); ?>
        <div class="col-md-3">
          <?php select(['label' => 'Select Role', 'id' => 'role', 'data-form' => 'search-form', 'required' => 'required'], $system_roles, "_all"); ?>
        </div>
        <div class="col-md-3">
          <?php select(['label' => 'Select Status', 'id' => 'status', 'data-form' => 'search-form', 'required' => 'required'], ['Active Account' => 'active', 'Inactive Account' => 'inactive'], 'active'); ?>
        </div>
        <div class="col-md-3">
          <?php input(['label' => 'Surname', 'id' => 'surname_keyword', 'data-form' => 'search-form', 'placeholder' => 'Search by Surname...']); ?>
        </div>
        <div class="col-md-3">
          <?php input(['label' => 'Other Names', 'id' => 'othernames_keyword', 'data-form' => 'search-form', 'placeholder' => 'Search by Other Names...']); ?>
        </div>
        <?php do_action("users_filter_after"); ?>
        <div class="col-md-3">
          <?php do_action("add_db_section_to_form", ['all' => false, 'label' => 'Select State', 'selected' => $selected, 'attr' => ['data-form' => 'search-form']]); ?>
        </div><?php do_action("users_filter_after_after"); ?>
  <?php
}

function default_registration_form() {
	add_page_javascript('app/elements/form.js');
	?>
	<?php do_action("registration_before_names"); ?>
	<style type="text/css">
		.country-code .chosen-container-single .chosen-single {
			padding: 0.5px 0 0.5px 8px;
			height: 26px;
			border: 1px solid #aaa;
			border-radius: 5px 0 0 5px;
			/* margin-right: 10px; */
			border-right: none;
		}
		.country-code .chosen-container-single .chosen-single div b {
			background: none;
		}
		.country-code .chosen-container-single .chosen-single span {
			margin-right: 0;
		}
		#phone {
			border-radius: 0 5px 5px 0;
			/*border-left: none;*/
		}
	</style>
	<div class="row">
		<div class="col-md-6">
			<?php input(['label' => apply_filters("registration_surname_text", 'Surname'), 'id' => 'surname', 'required' => 'required']); ?>
		</div>
		<div class="col-md-6">
			<?php input(['label' => apply_filters("registration_other_names_text", 'Other Names'), 'id' => 'other_names', 'required' => 'required']); ?>
		</div>
	</div>
	<?php do_action("registration_after_names"); ?>
	<div class="row">
		<div class="col-md-6">
			<?php input(['label' => apply_filters("registration_email_text", 'Email'), 'id' => 'email', 'type' => 'email', 'required' => 'required']); ?>
		</div>
		<div class="col-md-6">
			<div class="row">
				<div class="col-xs-4 pr-0">
					<label>...</label>
					<span class="country-code"></span>
				</div>
				<div class="col-xs-8 pl-0">
					<?php input(['label' => apply_filters("registration_phone_text", 'Phone Number'), 'id' => 'phone', 'required' => 'required']); ?>
				</div>
			</div>
		</div>
	</div>
	<?php
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/country-code.js');
}

function default_registration_password_field() {
	$args = ['label' => apply_filters("password_text", 'Password'), 'id' => 'password', 'type' => 'password', 'required' => 'required'];
	$password_rules = get_option("password_rules");
	if($password_rules) {
		if(in_array("numeric", $password_rules)) {
			$args['data-numeric'] = true;
		}
		if(in_array("lowercase", $password_rules)) {
			$args['data-lower-case'] = true;
		}
		if(in_array("uppercase", $password_rules)) {
			$args['data-upper-case'] = true;
		}
		if(in_array("specialchar", $password_rules)) {
			$args['data-special-char'] = true;
		}
	}
	if($option = get_option("min_password_length")) {
		$args['data-password-len'] = $option;
	}
	?>
	<div class="row">
		<div class="col-md-6">
			<?php input($args); ?>
		</div>
		<div class="col-md-6">
			<?php input(['label' => apply_filters("confirm_password_text", 'Confirm Password'), 'id' => 'c_password', 'type' => 'password', 'required' => 'required']); ?>
		</div>
	</div>
	<?php
}

function public_registration_settings() {
	$system_roles = get_system_roles();
	$username_prefix = ($option = get_option('username_prefix')) ? $option : '';
	$default_user_role = ($option = get_option('default_user_role')) ? $option : '';
	label("User Registration Settings", "h3");
	input(['label' => 'Username Prefix', 'id' => 'username_prefix', 'value' => $username_prefix]);
	select(['label' => 'Default User Role', 'id' => 'default_user_role'], $system_roles, $default_user_role);
	checkBox(['label' => 'Pre-activate all registration', 'id' => 'preactivate_all_registration'], get_option('preactivate_all_registration'));
	checkBox(['label' => 'Use Unverified Role', 'id' => 'use_unverified_role'], get_option('use_unverified_role'));
	checkBox(['label' => 'Confirm via Email', 'id' => 'confirm_via_email'], get_option('confirm_via_email'));
	checkBox(['label' => 'Confirm via SMS', 'id' => 'confirm_via_sms'], get_option('confirm_via_sms'));
}

function save_public_registration_settings($args) {
	// $confirm_via_email = ($args['confirm_via_email'] == 1) ? 1 : 0;
	update_option('confirm_via_email', $args['confirm_via_email'] ?? 0);
	// $confirm_via_sms = ($args['confirm_via_sms'] == 1) ? 1 : 0;
	update_option('confirm_via_sms', $args['confirm_via_sms'] ?? 0);
	// $use_unverified_role = ($args['use_unverified_role'] == 1) ? 1 : 0;
	update_option('use_unverified_role', $args['use_unverified_role'] ?? 0);
	// $preactivate_all_registration = ($args['preactivate_all_registration'] == 1) ? 1 : 0;
	update_option('preactivate_all_registration', $args['preactivate_all_registration'] ?? 0);
	update_option('username_prefix', $args['username_prefix']);
	update_option('default_user_role', $args['default_user_role']);
}


/* 
18th Sept 2019
loading large query through ajax
accepts args:
query_function:::	Function that prepares the query
query_size:::		The size of each load
container:::		The element that holds the result
script:::			The script used to generate the fetched items
js_callback:::		Callback function after load
*/

function load_more_api() {
	global $post_meta;
	add_page_javascript('app/assets/jquery-serialize-object-master/jquery.serialize-object.js');
	add_page_javascript('app/assets/js/page-scripts/load-more-api.js');
	add_page_javascript("app/bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js");
	add_page_stylesheet("app/bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css", "bs_datepicker");
	add_page_stylesheet("app/assets/css/table.css", "table-actions");
	add_datatable_scripts();
}


function unverified_dashboard($args) {
	if(!current_user_can("unverified_only"))
		return;
	$msg = 'You are yet to verify your account. Check your '.$args['email'].' email inbox for link to verify your account. If you did not receive the email, please <a data-id="'.$args['id'].'" href="javaScript:void(0)" data-action-callback="resend_verification_link" class="btn bg-red ajax-action">click here</a> to resend the verification link. To change your email visit your <a href="profile">profile</a> to edit.';
	do_action("_display_message", ["msg" => $msg]);
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}

function comments_api($args) {
	if(!isset($args['obj'])) {
		echo "you have not set value for obj";
		return;
	}
	$post = new post;
	if(!$post->get_post_meta($args['obj'])) {
		echo "Obj item does not exist";
		return;
	}
	add_page_javascript(get_option("home_url")."app/assets/js/page-scripts/comment.js");
	$comment = new comment($args['obj']);
	if(isset($args['key'])) {
		$comment->key = $args['key'];
	}
	if(isset($args['label_plu'])) {
		$comment->dictionary->label_plu = $args['label_plu'];
	}
	if(isset($args['label'])) {
		$comment->dictionary->label = $args['label'];
	}
	if(isset($args['label_tag'])) {
		$comment->dictionary->label_tag = $args['label_tag'];
	}
	if(isset($args['notice_subject'])) {
		$comment->dictionary->notice_subject = $args['notice_subject'];
	}
	if(isset($args['submit_text'])) {
		$comment->dictionary->submit_text = $args['submit_text'];
	}
	if(isset($args['editor'])) {
		$comment->appearance->editor = $args['editor'];
	}
	if(isset($args['people'])) {
		$comment->people = $args['people'];
	}
	if(isset($args['notice_action'])) {
		$comment->notice_action = $args['notice_action'];
	}
	$comment->set_comments();
	$comment->close_comments = isset($args['close_comments']) ? $args['close_comments'] : false;
	$comment->send_notice = isset($args['send_notice']) ? $args['send_notice'] : NEVER;
	$count = _count($comment->comments);
	?>
	<div>
		
		<div id="comment-container-<?php echo $args['obj']; ?>">
			<?php echo $comment->display_comment(); ?>
		</div>
	</div>
	<?php
	echo $comment->comment_form();
	
}

function reviews_api($args) {
	if(!isset($args['obj'])) {
		echo "you have not set value for obj";
		return;
	}
	$post = new post;
	// if(!$post->get_post_meta($args['obj'])) {
	// 	echo "Obj item does not exist";
	// 	return;
	// }
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/comment.js');
	$review = new review($args['obj']);
	if(isset($args['key'])) {
		$review->key = $args['key'];
	}
	if(isset($args['label_plu'])) {
		$review->dictionary->label_plu = $args['label_plu'];
	}
	if(isset($args['label'])) {
		$review->dictionary->label = $args['label'];
	}
	if(isset($args['label_tag'])) {
		$review->dictionary->label_tag = $args['label_tag'];
	}
	if(isset($args['notice_subject'])) {
		$review->dictionary->notice_subject = $args['notice_subject'];
	}
	if(isset($args['submit_text'])) {
		$review->dictionary->submit_text = $args['submit_text'];
	}
	if(isset($args['rating_text'])) {
		$review->dictionary->rating = $args['rating_text'];
	}
	if(isset($args['editor'])) {
		$review->appearance->editor = $args['editor'];
	}
	$review->set_reviews();
	$review->close_reviews = isset($args['close_reviews']) ? $args['close_reviews'] : false;
	$review->send_notice = isset($args['send_notice']) ? $args['send_notice'] : NEVER;
	$count = _count($review->reviews);
	$hide_form = (isset($args['hide_form']) && $args['hide_form'] == true) ? true : false;
	if(!$hide_form) {
		echo $review->review_form();
	}
	?>
	<div class="row">
		<div class="col-xs-12" id="review-container-<?php echo $args['obj']; ?>">
			<?php echo $review->display_review(); ?>
		</div>
	</div>
	<?php
}

function default_add_user() {
	?>
	<div class="row">
		<div class="col-md-6">
			<?php 
				$exclude = ['software_engineer', 'unverified'];
				if(current_user_can("change_user_role")) {
	        		$roles = get_system_roles($exclude);
					select(['label' => 'Role', 'id' => 'role', 'required' => 'required'], $roles, $user_meta['role']);
	        	}
			 ?>
		</div>
		<div class="col-md-6">
			<?php 
				checkBox(['label' => 'Pre-activate account', 'id' => 'preactivate_account'], true);
				checkBox(['label' => 'User must verify account', 'id' => 'unverified_role'], get_option("use_unverified_role"));
			 ?>
		</div>
	</div>
	<?php
}


function display_search_box($args) {
  global $post_meta;
  add_datatable_scripts();
  add_page_stylesheet("app/assets/css/table.css", "table-actions");
  add_page_javascript('app/assets/jquery-serialize-object-master/jquery.serialize-object.js');
  add_page_javascript('app/assets/js/page-scripts/search-query.js');
  add_page_javascript('app/assets/js/page-scripts/js-functions.js');
  add_page_javascript('app/assets/js/page-scripts/init-datatable.js');
  add_page_stylesheet("app/assets/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css", "datatable");
  add_page_stylesheet("app/assets/plugins/datatables-buttons/css/buttons.bootstrap4.min.css", "datatable-buttons");
  if(!isset($args['query_function'])) {
  	echo "You must specify the query function";
  	return;
  }
  if(!isset($args['id'])) {
  	echo "You must specify the ID";
  	return;
  }
  $cache = "cache";
  $cache = isset($args['cache']) ? $args['cache'] : $cache;
  $cache = $cache === false ? "no_cache" : $cache;
  $id = $args['id'];
  unset($args['id']);
  ?>
  <div class="panel panel-custom">
    <div class="panel-heading">
      <h1>Search</h1>
    </div>
    <div class="panel-body instruction">
      <form id="search-form" method="post">
      <div class="row">
      	<input type="hidden" name="cache" value=<?=$cache; ?>>
      	<input type="hidden" name="query_function" value="<?=$args['query_function']; ?>">
      	<input type="hidden" name="action" value="search_query_get_result">
      	<?php do_action("search_query_filter_".$id, $args); ?>
      </div> 
      <div class="row">
      	<hr>
        <div class="col-md-3">
        	<button id="search-query-btn" class="btn btn-primary">Search Now</button>
        </div>
      </div>
      </form>
      <div class="mt-4" id="display-result"></div>
    </div>
  </div>
  <?php
}

function display_qf_search_box($args) {
  global $post_meta;
  add_datatable_scripts();
  add_page_stylesheet("app/assets/css/table.css", "table-actions");
  add_page_javascript('app/assets/jquery-serialize-object-master/jquery.serialize-object.js');
  add_page_javascript('app/assets/js/page-scripts/query-loader-api.js');
  add_page_javascript('app/assets/js/page-scripts/js-functions.js');
  add_page_javascript('app/assets/js/page-scripts/init-datatable.js');
  add_page_stylesheet("app/assets/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css", "datatable");
  add_page_stylesheet("app/assets/plugins/datatables-buttons/css/buttons.bootstrap4.min.css", "datatable-buttons");
  if(!isset($args['query_function'])) {
  	echo "You must specify the query function";
  	return;
  }
  if(!isset($args['default_query_function'])) {
  	$args['default_query_function'] = $args['query_function'];
  }
  if(!isset($args['id'])) {
  	echo "You must specify the ID";
  	return;
  }
  $settings = [
  	"limit" => 20,
  	"autostart" => false,
  	"cache" => true
  ];

  if(isset($args['settings']) && is_array($args['settings']) && _count($args['settings'])) {
  	$settings = array_merge($settings, $args['settings']);
  }

  // if(_count($_GET)) {
  // 	$settings['autostart'] = true;
  // 	$settings['cache'] = false;
  // 	$args['default'] = array_merge($args['default'], $_GET);
  // }
  
  ?>
  <style type="text/css">
  	.queryloader {
  		position: relative;
  	}
  	.koweb-refresh-btn {
		height: 36px;
		float: right;
	}
	.koweb-refresh-btn + div {
		clear: right;
	}
	.koweb-refresh-waiting {
		cursor: pointer;
	}
	.koweb-refresh-refreshing {
		cursor: wait;
	}
  </style>
    <div class="row">
		<div class="col-md-12">
			<div class="panel panel-custom qf-loader" id="panel-<?php echo $args['id']; ?>" data-id="<?php echo $args['id']; ?>">
				<div class="panel-body">
					<form id="form-<?php echo $args['id']; ?>" method="post">
						<div class="row">
							<?php $actions = do_action("search_query_qf_filter_".$args['id'], $args); ?>
						</div>
						<?php if ($actions): ?>
							<div class="row">
						      	<hr>
						        <div class="col-md-3">
						        	<button id="btn-<?php echo $args['id']; ?>" data-formid="<?php echo $args['id'] ?>" class="mb-4 search-query-btn btn btn-primary"
						        		<?php 
											foreach ($settings as $key => $value): 
												echo " data-".$key."=\"".$value."\" ";
											endforeach;
											echo "data-query_function=\"" .$args['query_function']. "\"";
											if (isset($args['default'])):
												echo " data-args='".json_encode($args["default"])."'";
											endif;
											?>
						        		>Search Now</button>
						        </div>
						    </div>
						<?php else: 
							$settings['autostart'] = true;
						 endif ?>
					</form>
					<div class="queryloader publications-timeline mt-4" id="result-<?php echo $args['id']; ?>" data-formid="<?php echo $args['id']; ?>"
					<?php 
					foreach ($settings as $key => $value): 
						echo " data-".$key."=\"".$value."\" ";
					endforeach;
					echo "data-query_function=\"" .$args['default_query_function']. "\"";
					if (isset($args['default'])):
						echo " data-args='".json_encode($args["default"])."'";
					endif;
					?>
					 >
					 <?php if (function_exists($args['default_query_function']) && $settings['autostart']): 
					 	$options = [
					 		"offset" => 0,
					 		"count" => 0,
					 		"container" => "result-".$args['id'],
					 		"args" => $args["default"]
					 	];
					 	$options = array_merge($options, $settings);
					 	$response = $args['default_query_function']($options);
					 	echo $response['html'];
					 endif ?>
					</div>
				</div>
			</div>
		</div>
	</div>
  <?php
}

function search_query_date_range($args) {
	$from = ['label' => 'Date: From', 'id' => 'from', 'required' => 'required', 'data-form' => 'form'];
	$to = ['label' => 'Date: To', 'id' => 'to', 'required' => 'required', 'data-form' => 'form'];
	$from['value'] = (isset($args['from'])) ? date("m/d/Y", $args['from']) : date("m/d/Y", strtotime("first day of this month")); 
	$to['value'] = (isset($args['to'])) ? date("m/d/Y", $args['to']) : date("m/d/Y", strtotime("last day of this month")); 
	$class = !isset($args['class']) ? "col-md-3" : $args['class'];
	add_page_stylesheet("app/assets/plugins/jquery-ui/jquery-ui.css", "jquery-ui");
	add_page_javascript("app/assets/plugins/jquery-ui/jquery-ui.js");
	add_page_javascript("app/assets/js/page-scripts/date-range-filter.js");
	?>
	<div class="<?php echo $class ?>">
      <?php input($from); ?>
    </div>
    <div class="<?php echo $class ?>">
      <?php input($to); ?>
    </div>
	<?php
}

function search_query_date_range_2($args) {
	add_page_stylesheet("app/assets/plugins/jquery-ui/jquery-ui.css", "jquery-ui");
	add_page_javascript("app/assets/plugins/jquery-ui/jquery-ui.js");
	add_page_javascript("app/assets/js/page-scripts/date-range-filter2.js");
	$options = [
		'Today' => 'today',
		'Yesterday' => 'yesterday',
		'This Week' => 'this_week',
		'Last Week' => 'last_week',
		'This Month' => 'this_month',
		'Last Month' => 'last_month',
		'Date Range' => 'date_range',
	];
	?>
	<style type="text/css"> .range { display: none; } </style>
	<div class="col-md-3">
      <?php select(['label' => 'Date', 'id' => 's_date', 'required' => 'required', 'data-form' => 'form'], $options, 'today'); ?>
    </div>
	<div class="col-md-3 range">
      <?php input(['label' => 'Date: From', 'id' => 'from', 'required' => 'required', 'data-form' => 'form']); ?>
    </div>
    <div class="col-md-3 range">
      <?php input(['label' => 'Date: To', 'id' => 'to', 'required' => 'required', 'data-form' => 'form']); ?>
    </div>
	<?php
}

function search_query_db_section($args) {
	?>
	<div class="col-md-3">
      <?php do_action("add_db_section_to_form", ['all' => true, 'label' => 'Select State', 'selected' => "all", 'attr' => ['data-form' => 'search-form']]); ?>
    </div>
	<?php
}

function viewerjs_from_library($args) {
	if(!isset($args['ref'])) {
		echo "You have not provided the library ref!";
		return;
	}
	$files = get_upload_library_files( $args['ref'] );
	if(!$files) {
		echo "No file to preview!";
		return;
	}
	$options = [
		'height' => 200,
		'width' => 400,
		'download_only' => false
	];
	$args = array_merge($options, $args);
	global $post_meta;
	add_page_javascript('app/assets/js/magnify/dist/jquery.magnify.js');
	add_page_javascript('app/assets/js/magnify/init.js');
	?>
	<link href="app/assets/js/magnify/dist/jquery.magnify.css" rel="stylesheet">
	<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 4px; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .img-embed-container { position: relative; overflow: hidden; max-width: 100%; min-width: 32.7%; display: inline flow-root list-item; height: 200px; margin-bottom: 2px; border: #4f4f4f solid 3px; } .img-embed-container img { position: absolute; top: 0; left: 0; width: 100%; height: auto; }.viewerjs-filelist {margin-bottom: 4px;background: #fdfdfd;border-radius: 4px;box-shadow: 1px -1px 3px #ffffffab;border: 1px solid #e3e3e3;display: grid;}.viewerjs-filelist a {color: #142959;display: block;padding: 4px 5px;text-decoration: none;}</style>
	<?php
	$sorting = new sorting;
	$files = $sorting->sort_by_meta_value($files['files'], 'file_type');
	foreach ($files as $file => $meta): ?>
		<?php if ($args['download_only'] == true): ?>
			<div class="viewerjs-filelist">
				<a href="<?php echo $meta['file_name']; ?>" title="Click Here to Download" download><?php echo $meta['title']; ?> <span class="pull-right"><span class="badge badge-primary"><?php echo $meta['file_type'] ?></span>&nbsp;<span class=""><?php echo FileSizeConvert($meta['size']) ?></span></span></a>
			</div>
		<?php else: ?>
			<?php if (strpos($meta['file_type'], "application") !== false): ?>
				<div class='embed-container'><iframe src="<?php echo get_option('home_url') ?>/ViewerJS/#../<?php echo $meta['file_name'] ?>" frameborder='0' allowfullscreen webkitallowfullscreen></iframe></div>
			<?php elseif(strpos($meta['file_type'], "image") !== false): ?>
				<div class='img-embed-container'><a href="<?php echo get_option('home_url') . $meta['file_name'] ?>" data-magnify=true><img src="<?php echo get_option('home_url') . $meta['file_name'] ?>"></a></div>
			<?php else: ?>
				<div class="viewerjs-filelist">
					<a href="<?php echo $meta['file_name']; ?>" title="Click Here to Download" download><?php echo $meta['title']; ?> <span class="pull-right"><span class="badge badge-primary"><?php echo $meta['file_type'] ?></span>&nbsp;<span class=""><?php echo FileSizeConvert($meta['size']) ?></span></span></a>
				</div>
			<?php endif ?>
		<?php endif ?>
	<?php endforeach;
}

function search_form($args = array()) {
	$default = [
		'input-class' => 'form-control',
		'container-class' => 'search-container',
		'form-class' => 'search-form',
		'addon-class' => 'input-group-addon',
		'icon-class' => 'fa fa-search',
		'search-results-container-class' => '',
		'id' => md5(rand(0, 2000)),
		'placeholder' => "Search...",
		'search_term' => "",
		'template' => "default_search_template",
		"search-result-container-open" => "",
		"search-result-container-close" => "",
	];
	
	$args = array_merge($default, $args);
	$args['search-result-container-open'] = base64_encode($args['search-result-container-open']);
	$args['search-result-container-close'] = base64_encode($args['search-result-container-close']);
	if(!function_exists($args['template'])) {
		echo "You have not created the function \"".$args['template']."\" to handle the template";
		return;
	}
	if(!isset($args['search_in'])) {
		echo "You have not set a value for \"search in\"";
		return;
	}
	$args['search_in'] = is_array($args['search_in']) ? serialize($args['search_in']) : $args['search_in'];
	$args['search_in'] = base64_encode($args['search_in']);
	if(isset($args['where'])) {
		$args['where'] = is_array($args['where']) ? serialize($args['where']) : $args['where'];
		$args['where'] = base64_encode($args['where']);
	}
	if(isset($args['compare'])) {
		$args['compare'] = is_array($args['compare']) ? serialize($args['compare']) : $args['compare'];
		$args['compare'] = base64_encode($args['compare']);
	}
	?>
	<!-- <div class="row"> -->
		<div class="<?php echo $args['container-class'] ?>" id="<?php echo $args['id'] ?>">
			<form class="<?php echo $args['form-class'] ?>" id="search-form-<?php echo $args['id'] ?>" method="post" action="?content=search-results">
				<?php foreach ($args as $key => $value): ?>
					<input type="hidden" name="<?php echo $key ?>" value="<?php echo $value ?>">
				<?php endforeach ?>
				<!-- <div class="input-group"> -->
				
				
				<div class="input-group">							
					<input class="<?php echo $args['input-class'] ?>" name="search_term" placeholder="<?php echo $args['placeholder'] ?>" type="search" id="search-term-<?php echo $args['id'] ?>" value="<?php echo $args['search_term'] ?>" required>
					<span class="<?php echo $args['addon-class'] ?>" id="search-btn-<?php echo $args['id'] ?>" tabindex=0 role="button" onclick="if(document.getElementById('search-term-<?php echo $args['id'] ?>').value !== '') {document.getElementById('search-form-<?php echo $args['id'] ?>').submit()} else return false;">
						<i class="<?php echo $args['icon-class'] ?>"></i>
					</span>
				</div>
			</form>
		</div>
		
	<!-- </div> -->
	<?php
}

function render_custom_css() {
	?>
	<style type="text/css">
		<?php echo base64_decode(get_option("custom_css")) ?>
	</style>
	<?php
}

function search_users($args = ["settings" => array(), "default_options" => array(), "query_settings" => array()]) {
	$default = [
		"label" => "Select Users",
		"id" => "user_id",
		"name" => "user_id[]",
		"multiple" => "multiple"
	];
	$args['settings'] = array_merge($default, $args['settings']);
	if($args['settings']["multiple"] !== "multiple") {
		$args['settings']["data-multiple"] = "no";
	} else {
		$args['settings']["data-multiple"] = "yes";
	}
	?>
	<div class="search-users" id="container_<?php echo $args['settings']['id']; ?>" data-default-options='<?php echo json_encode($args['default_options']) ?>' data-query-settings='<?php echo json_encode($args['query_settings']) ?>'>
	<?php
	select($args['settings'], ($args['default_options'] ?? []));
	?>
	</div>
	<?php
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/search-users.js');
}

function country_directories($args = array()) {
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('app/assets/js/page-scripts/country-directories.js');
	$default = [
		"ref" => "directory-".rand(9, 99).rand(9, 99).rand(9, 99),
		"enable_country" => true,
		"country_args" => ["label" => "Country", "id" => "country", "name" => "country"],
		"default_country" => false,
		"enable_state" => true,
		"state_args" => ["label" => "State/Region", "id" => "state", "name" => "state"],
		"default_state" => false,
		"container_attr" => "class=\"col-md-6\""
	];
	$args = array_merge($default, $args);
	$args['country_args']['id'] = "country-".$args['ref'];
	$args['state_args']['id'] = "state-".$args['ref'];
	$args['city_args']['id'] = "city-".$args['ref'];
	$args['country_args']['class'] = isset($args['country_args']['class']) ? $args['country_args']['class']." form-control standardSelect dir-country ".$args['ref'] : "form-control dir-country standardSelect ".$args['ref'];
	$args['state_args']['class'] = isset($args['state_args']['class']) ? $args['state_args']['class']." form-control standardSelect dir-state ".$args['ref'] : "form-control dir-state standardSelect ".$args['ref'];
	$args['country_args']['data-ref'] = $args['state_args']['data-ref'] = $args['city_args']['data-ref'] = $args['ref'];
	$args['country_args']['data-default'] = $args['default_country'];
	$args['state_args']['data-default'] = $args['default_state'];
	?>
	<?php if ($args['enable_country']): ?>
		<div <?php echo $args['container_attr'] ?>>
			<?php select($args['country_args']) ?>
		</div>
		<?php 
		else: 
			if(!$args['default_country']) {
				echo "Please specify default_country"; return;
			}
		endif ?>
	<?php if ($args['enable_state']): ?>
		<div <?php echo $args['container_attr'] ?>>
			<?php select($args['state_args']) ?>
		</div>
	<?php endif ?>
	<?php
}

function menu_search($args = array()) {
	$default  = [
		'name' => 'SidebarSearch',
		'key' => 'lte.sidebar-search',
		'cn_open' => 'sidebar-search-open',
		'cn_icon_search' => 'fa-search',
		'cn_icon_close' => 'fa-times',
		'cn_header' => 'nav-header',
		'cn_search_results' => 'sidebar-search-results',
		'cn_list_group' => 'list-group',
		'selector_data_widget' => '[data-widget="sidebar-search"]',
		'selector_sidebar' => '.main-sidebar .nav-sidebar',
		'selector_nav_link' => '.nav-link',
		'selector_nav_treeview' => '.nav-treeview'
	];
	$args = array_merge($default, $args);
	?>
	<div class="navbar-search-block">
      <form class="form-inline">
        <div class="input-group input-group-sm">
          <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">
          <div class="input-group-append">
            <button class="btn btn-navbar" type="submit">
              <i class="fas fa-search"></i>
            </button>
            <button class="btn btn-navbar" type="button" data-widget="navbar-search">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>
      </form>
    </div>
	<?php
	add_page_javascript("app/assets/js/SidebarSearch.js");
}