<?php 

function query_qf_get_users($args) {
	$user = new user;
	$query = $args['args'];
	if($query['status'] == '') {
		unset($query['status']);
	}
	if(isset($args['args']['from']) && $args['args']['from'] !== "") {
		$query["_range_from"] = strtotime($args['args']['from']);
	}
	if(isset($args['args']['to']) && $args['args']['to'] !== "") {
		$query["_range_to"] = strtotime("11.59 PM ". $args['args']['to']);
	}
	if(isset($args['args']['db_section_id']) && $args['args']['db_section_id'] !== "") {
		$query['db_section_id'] = $args['args']['db_section_id'];
		$query['apply_db_session'] = 1;
	} else {
		unset($query['db_section_id']);
	}
	if(isset($args['args']['role']) && $args['args']['role'] !== "") {
		$query['role'] = $args['args']['role'];
	} else {
		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 $system_roles = get_system_roles(["software_engineer", "super_admin_tech", "super_admin", "admin_tech", "member"]);
		$system_roles = apply_filters('users_filter_system_roles_filter', $system_roles);
		$query['role'] = array_values($system_roles);
	}
	if(isset($args['args']['surname_keyword']) && $args['args']['surname_keyword'] !== "") {
		$query['surname'] = "%".$args['args']['surname_keyword']."%";
	}
	if(isset($args['args']['othernames_keyword']) && $args['args']['othernames_keyword'] !== "") {
		$query['other_names'] = "%".$args['args']['othernames_keyword']."%";
	}
	$query = apply_filters("filter_user_args", $query);
	unset($query['from']);
	unset($query['to']);
	unset($query['column']);
	if(isset($query['surname_keyword'])) unset($query['surname_keyword']);
	if(isset($query['othernames_keyword'])) unset($query['othernames_keyword']);
	
	$users = $user->get_user_by_meta_data($query, "LIKE", false, $args['limit'], $args['offset']);
	
	// return $table->table_html();

	if(_count($users) && is_array($users)) {
		$table = new table("users_table", $users);
		$columns = [
			"sn" => "SN"
		];
		$columns = array_merge($columns, $args['args']['column']);
		if($args['args']['status'] === "approved") {
			$columns['approved_by'] = "Approved By";
		}
		$table->set_display_columns($columns);
		$table->set_action_callback([
			"Suspend User" => "deactivate_user",
			"Recall User" => "activate_user",
			"Delete User" => "delete_user"
		]);
		$table->set_action_links([
			"Profile" => "profile",
			"Suspend User" => false,
			"Recall User" => false,
			"Delete User" => false
		]);
		$table->set_action_access([
			"Profile" => "profile",
			"Suspend User" => "deactivate_user",
			"Recall User" => "activate_user",
			"Delete User" => "delete_user"
		]);
		$table->set_multirow_actions([
			// "Profile" => "profile",
			"Suspend User" => "Suspend Selected",
			"Recall User" => "Recall Selected",
			"Delete User" => "Delete Selected"
		]);
		$table->set_action_popup([
			"Profile" => ["width" => 1200, "height" => 700],
		]);
		$table->set_action_args([
			"Profile" => ["id" => "id"],
			"Suspend User" => ["id" => "id", "callback" => "removeTableRow"],
			"Recall User" => ["id" => "id", "callback" => "removeTableRow"],
			"Delete User" => ["id" => "id", "callback" => "removeTableRow"]
		]);
		if($args['args']["status"] === "active") {
			unset($table->action_links['Recall User']);
			unset($table->action_access['Recall User']);
			unset($table->action_callback['Recall User']);
			unset($table->action_args['Recall User']);
			unset($table->action_links['Delete User']);
			unset($table->action_access['Delete User']);
			unset($table->action_callback['Delete User']);
			unset($table->action_args['Delete User']);
		} else {
			unset($table->action_links['Suspend User']);
			unset($table->action_access['Suspend User']);
			unset($table->action_callback['Suspend User']);
			unset($table->action_args['Suspend User']);
		}
		$table = apply_filters("before_users_table_display", $table, $args['args']);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "users_table tbody";
	return $response;
}

function query_qf_get_visitors($args) {
	$user = new user;

	global $app;
	$visitor = new visitor($app);
	
	if($args['args']['s_date'] === 'date_range') {
		$range = [
			'start' => false,
			'stop' => false
		];
		if(isset($args['args']['from']) && $args['args']['from'] !== "") {
			$range["start"] = strtotime($args['args']['from']);
		}
		if(isset($args['args']['to']) && $args['args']['to'] !== "") {
			$range["stop"] = strtotime("11.59 PM ". $args['args']['to']);
		}
	} else {
		$range = date_limits($args['args']['s_date']);
	}
	$visits = $visitor->get_visits($range['start'], $range['stop'], false, ARRAY_A, 'visitors', $args['limit'], $args['offset']);

	if(_count($visits) && is_array($visits)) {
		$table = new table("visitors_table", $visits);
		$columns = [
			"sn" => "SN"
		];
		$columns = array_merge($columns, $args['args']['column']);

		$table->set_display_columns($columns);
		$table->set_dynamic_columns([
			'request_time' => '_tf_get_time_date'
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "visitors_table tbody";
	return $response;
}

function query_qf_get_external_visits($args) {
	$user = new user;

	global $app;
	$visitor = new visitor($app);
	
	if($args['args']['s_date'] === 'date_range') {
		$range = [
			'start' => false,
			'stop' => false
		];
		if(isset($args['args']['from']) && $args['args']['from'] !== "") {
			$range["start"] = strtotime($args['args']['from']);
		}
		if(isset($args['args']['to']) && $args['args']['to'] !== "") {
			$range["stop"] = strtotime("11.59 PM ". $args['args']['to']);
		}
	} else {
		$range = date_limits($args['args']['s_date']);
	}
	$visits = $visitor->get_visits($range['start'], $range['stop'], false, ARRAY_A, 'externals', $args['limit'], $args['offset']);

	if(_count($visits) && is_array($visits)) {
		$table = new table("external_visits_table", $visits);
		$columns = [
			"sn" => "SN"
		];
		$columns = array_merge($columns, $args['args']['column']);

		$table->set_display_columns($columns);
		$table->set_dynamic_columns([
			'request_time' => '_tf_get_time_date'
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "external_visits_table tbody";
	return $response;
}

function query_qf_outward_binding($args) {
	$post = new post;
	
	$results = $post->get_post_by_meta_data($args['args'], "LIKE", false, $args['limit'], $args['offset'], 'post_id', 'DESC');

	if(_count($results) && is_array($results)) {
		$table = new table("outward_table", $results);
		$columns = [
			"sn" => "SN",
			"site_name" => "Site Name",
			"binding_id" => "Binding ID"
		];
		$table->set_display_columns($columns);
		$table->set_action_callback([
			"Delete" => "delete_outward_site_binding"
		]);
		$table->set_action_links([
			"Delete" => false
		]);
		$table->set_action_access([
			"Delete" => "delete_post"
		]);
		$table->set_action_args([
			"Delete" => ["id" => "id"]
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "outward_table tbody";
	return $response;
}

function query_qf_inward_binding($args) {
	$post = new post;
	
	$results = $post->get_post_by_meta_data($args['args'], "LIKE", false, $args['limit'], $args['offset'], 'post_id', 'DESC');

	if(_count($results) && is_array($results)) {
		$table = new table("inward_table", $results);
		$table->table_id = "inward";
		$columns = [
			"sn" => "SN",
			"site_name" => "Site Name",
			"status" => "Status",
			"binding_id" => "Binding ID"
		];
		$table->set_display_columns($columns);
		$table->set_action_callback([
			"Delete" => "delete_inward_site_binding",
			"Disallow" => "disallow_inward_site_binding",
			"Approve" => "approve_inward_site_binding"
		]);
		$table->set_action_links([
			"Delete" => false,
			"Disallow" => false,
			"Approve" => false
		]);
		$table->set_action_access([
			"Delete" => "delete_post",
			"Disallow" => "approve_inward_site_binding",
			"Approve" => "approve_inward_site_binding"
		]);
		$table->set_action_args([
			"Delete" => ["id" => "id"],
			"Disallow" => ["id" => "id"],
			"Approve" => ["id" => "id"]
		]);
		$table->set_action_condition([
			"Disallow" => ["status" => "disallowed"],
			"Approve" => ["status" => "active"]
		]);
		$table->set_action_conditioner([
			"Disallow" => ["status" => "!="],
			"Approve" => ["status" => "!="]
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "inward_table tbody";
	return $response;
}

function query_qf_cron_tabs($args) {
	$query = new query;
	$res = $query->quick_select("crontabs", [
		'limit' => $args['limit'],
		'offset' => $args['offset'],
		'order_by' => 'next_runtime',
		'order' => 'DESC'
	], ARRAY_A);
	if(_count($res) && is_array($res)) {
		$table = new table("cron_tabs_table", $res);
		$columns = [
			"sn" => "SN",
			"callback" => "Callback",
			"exact_last_runtime" => "Last Execution",
			"next_runtime" => "Next Execution",
			"args" => "Arguments",
			"status" => "Status"
		];
		$table->set_display_columns($columns);
		$table->set_dynamic_columns([
			"exact_last_runtime" => "_tf_get_cron_time_date",
			"next_runtime" => "_tf_get_cron_time_date",
			"args" => "_tf_view_array_data"
		]);
		$table->set_action_links([
			'Cancel' => false,
			'Delete' => false,
			'Make Active' => false,
			'Run Now' => false,
			'History' => 'cron-logs'
		]);
		$table->set_action_access([
			'Cancel' => 'cron_tabs',
			'Delete' => 'cron_tabs',
			'Make Active' => 'cron_tabs',
			'Run Now' => 'cron_tabs',
			'History' => 'cron_tabs'
		]);
		$table->set_action_callback([
			'Cancel' => 'cancel_cron',
			'Delete' => 'delete_cron',
			'Make Active' => 'make_cron_active',
			'Run Now' => 'run_cron_now'
		]);
		$table->set_action_condition([
			'Cancel' => ["status" => ["active", "running"]],
			'Delete' => ["status" => "cancelled"],
			'Make Active' => ["status" => ["running", "cancelled"], 'next_runtime' => (time() + 300)],
			'Run Now' => ["status" => "active"],
			'History' => ["exact_last_runtime" => 0]
		]);
		$table->set_action_popup([
			'History' => ["width" => 700, "height" => 500]
		]);
		$table->set_multirow_actions([
			'Cancel' => "Cancel Selected",
			'Delete' => "Delete Selected",
			'Make Active' => "Make Active",
			'Run Now' => "Run Selected"
		]);
		$table->set_action_conditioner([
			'Make Active' => ['next_runtime' => "<"],
			'History' => ["exact_last_runtime" => "!="]
		]);
		$table->set_action_args([
			'Cancel' => ['id' => 'id', "callback" => "reRunQuery", "callbackid" => "#result-cron_tabs"],
			'Delete' => ['id' => 'id', "callback" => "removeTableRow"],
			'Make Active' => ['id' => 'id', "callback" => "reRunQuery", "callbackid" => "#result-cron_tabs"],
			'Run Now' => ['id' => 'id'],
			'History' => ['id' => 'id']
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "cron_tabs_table tbody";
	return $response;
}

function query_qf_cron_logs($args) {
	$query = new query;
	$res = $query->quick_select("cron_logs", [
		'limit' => $args['limit'],
		'offset' => $args['offset'],
		'order_by' => 'exact_runtime',
		'order' => 'DESC',
		'where' => [
			'crontab_id' => $args['args']['crontab_id']
		]
	], ARRAY_A);
	if(_count($res) && is_array($res)) {
		$table = new table("cron_tabs_table", $res);
		$columns = [
			"sn" => "SN",
			"status" => "Status",
			"exact_runtime" => "Runtime",
			"feedback" => "Feedback"
		];
		$table->set_display_columns($columns);
		$table->set_dynamic_columns([
			"exact_runtime" => "_tf_get_cron_time_date"
		]);
		if(isset($args['temp'])) {
			$table->temp = $args['temp'];
		}
		
		if($args['count'] > 0) {
			$response['html'] = $table->table_rows();
		} else $response['html'] = $table->table_html();
		
		$response['temp'] = $table->temp;
		$response['continue'] = true;
	}
	else {
		if($args['count'] > 0) {
			$response['html'] = "";
		} else $response['html'] = "no result found";
		$response['continue'] = false;
	}
	$response['container'] = "cron_tabs_table tbody";
	return $response;
}