<?php

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

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

	    if(route(2)){

	    	$totalRecord = $db->from('auto_orders')
			->select('count(auto_id) as total')
			->total();

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

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

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

					$autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_status', route(2))
			    	->where('auto_orders.auto_id', $_GET['search'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();
			    	
				}elseif($_GET['search_type'] == "link"){

				    $autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_status', route(2))
			    	->where('auto_orders.auto_link', $_GET['search'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();

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

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

					$autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_status', route(2))
			    	->where('auto_orders.user_id', $user['user_id'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();

				}

			}else{

				$autos = $db->from('auto_orders')
		    	->join('services', 'services.service_id = auto_orders.service_id')
		    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
	    		->where('auto_status', route(2))
		    	->orderby('auto_id', 'DESC')
		    	->limit($pagination['start'], $pagination['limit'])
		    	->all();

			}

			$tempAutos = [];

			foreach($autos as $auto) {

				if($auto["service_name"] != null) {

					$service_name = json_decode($auto["service_name"], true);
					$name = null;

					foreach($service_name as $details) {

						if($details["lang"] == 1) {

							$name = $details["name"];

						}

					}

					$auto["service_name"] = $name;

				}

				$tempAutos[] = $auto;
				
			}

			$autos = $tempAutos;
			$title = "Otomatik Siparişler";
			$page_title = "Otomatik Siparişler";

	    }else{

	    	$totalRecord = $db->from('auto_orders')
			->select('count(auto_id) as total')
			->total();

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

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

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

					$autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_orders.auto_id', $_GET['search'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();

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

				    $autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_orders.auto_link', $_GET['search'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();

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

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

					$autos = $db->from('auto_orders')
			    	->join('services', 'services.service_id = auto_orders.service_id')
			    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
			    	->where('auto_orders.user_id', $user['user_id'])
			    	->orderby('auto_id', 'DESC')
			    	->limit($pagination['start'], $pagination['limit'])
			    	->all();

				}

			}else{

				$autos = $db->from('auto_orders')
		    	->join('services', 'services.service_id = auto_orders.service_id')
		    	->leftjoin('users', 'users.user_id = auto_orders.user_id')
		    	->orderby('auto_id', 'DESC')
		    	->limit($pagination['start'], $pagination['limit'])
		    	->all();

			}

	    	

			$tempAutos = [];

			foreach($autos as $auto) {

				if($auto["service_name"] != null) {

					$service_name = json_decode($auto["service_name"], true);
					$name = null;

					foreach($service_name as $details) {

						if($details["lang"] == 1) {

							$name = $details["name"];

						}

					}

					$auto["service_name"] = $name;

				}

				$tempAutos[] = $auto;
				
			}

			$autos = $tempAutos;
			$title = "Otomatik Siparişler";
			$page_title = "Otomatik Siparişler";

	    }

		require admin_view('autos');

	}else{

		require controller("404");

	}

}else{

	require controller("404");
	
}