<?php

if(login_control()) {
	
	
	echo reference($db,2, 50);
	
	
	if($user["user_authority"] == 2) {

		if(!permission('access', 'users')){
			require admin_view("unauthorized");
			exit();
		}

		$totalRecord = $db->from('users')
		->select('count(user_id) as total')
		->total();

		$pageLimit = 1000000;
		$pageParam = 'page';
		$pagination = $db->pagination($totalRecord, $pageLimit, $pageParam);

		if(@$_GET['search']){

			if($_GET['search_type'] == "id"){

				$users = $db->from('users')
				->where('user_id', $_GET['search'])
				->orderby('user_id', 'DESC')
				->limit($pagination['start'], $pagination['limit'])
				->all();

			}elseif($_GET['search_type'] == "email"){

				$users = $db->from('users')
				->where('user_email', $_GET['search'])
				->orderby('user_id', 'DESC')
				->limit($pagination['start'], $pagination['limit'])
				->all();

			}elseif($_GET['search_type'] == "username"){

				$user = ($db->from('users')->where('user_username', $_GET['search'])->all())[0];
				

				$user_username = isset($user['user_username']) ? $user['user_username'] : '0';

				$users = $db->from('users')
				->where('user_username', $user_username)
				->orderby('user_id', 'DESC')
				->limit($pagination['start'], $pagination['limit'])
				->all();

			}

		}else{

			$users = $db->from('users')
			->orderby('user_id', 'DESC')
			->limit($pagination['start'], $pagination['limit'])
			->all();

		}

		$title = "Üyeler";
		$page_title = "Üyeler";

		require admin_view('users');

	}else{

		require controller("404");

	}

}else{

	require controller("404");
	
}