<?php



if(login_control()) {

	

	if($user["user_authority"] == 2) {



		if(!permission('access', 'logs')){

	        require admin_view("unauthorized");

	        exit();

	    }



	    if(route(2) == "provider"){

	    	$totalRecord = $db->from('api_report')
			->select('count(report_id) as total')
			->total();

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

			$logs = $db->from('api_report')
			->orderby('report_id', 'DESC')
			->limit($pagination['start'], $pagination['limit'])
			->all();

			$title = "Sağlayıcı Logları";
			$page_title = "Sağlayıcı Logları";

	    }elseif(route(2) == "automation"){

	    	$totalRecord = $db->from('auto_report')
			->select('count(report_id) as total')
			->total();

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

			$logs = $db->from('auto_report')
			->orderby('report_id', 'DESC')
			->limit($pagination['start'], $pagination['limit'])
			->all();

			$title = "Otomasyon Logları";
			$page_title = "Otomasyon Logları";

	    }elseif(route(2) == "crons"){

	    	$totalRecord = $db->from('crons_report')
			->select('count(crons_id) as total')
			->total();

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

			$logs = $db->from('crons_report')
			->orderby('crons_id', 'DESC')
			->limit($pagination['start'], $pagination['limit'])
			->all();

			$title = "Cron Logları";
			$page_title = "Cron Logları";

	    }else{

		    $totalRecord = $db->from('user_logs')
			->select('count(log_id) as total')
			->total();

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

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

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

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

					@$logs = $db->from('user_logs')
					->join('users', '%s.user_id = %s.user_id')
					->where('users.user_id', $user['user_id'])
					->orderby('user_logs.log_id', 'DESC')
					->limit($pagination['start'], $pagination['limit'])
					->all();

				}

			}else{

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

			}

			$title = "Üye Logları";
			$page_title = "Üye Logları";

	    }

		require admin_view('logs');

	}else{

		require controller("404");

	}

}else{

	require controller("404");	

}