<?php

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

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

	    $totalRecord = $db->from('tasks')
		->select('count(task_id) as total')
		->total();

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

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

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

				$tasks = $db->from('tasks')
			    ->join('orders', 'orders.order_id = tasks.order_id')
			    ->leftjoin('users', 'users.user_id = tasks.user_id')
			    ->where('tasks.task_id', $_GET['search'])
			    ->orderby('task_id', 'DESC')
			    ->limit($pagination['start'], $pagination['limit'])
			    ->all();

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

				$tasks = $db->from('tasks')
			    ->join('orders', 'orders.order_id = tasks.order_id')
			    ->leftjoin('users', 'users.user_id = tasks.user_id')
			    ->where('orders.order_link', $_GET['search'])
			    ->orderby('task_id', 'DESC')
			    ->limit($pagination['start'], $pagination['limit'])
			    ->all();

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

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

				$tasks = $db->from('tasks')
			    ->join('orders', 'orders.order_id = tasks.order_id')
			    ->leftjoin('users', 'users.user_id = tasks.user_id')
			    ->where('tasks.user_id', $user['user_id'])
			    ->orderby('task_id', 'DESC')
			    ->limit($pagination['start'], $pagination['limit'])
			    ->all();

			}

		}else{

			$tasks = $db->from('tasks')
		    ->join('orders', 'orders.order_id = tasks.order_id')
		    ->leftjoin('users', 'users.user_id = tasks.user_id')
		    ->orderby('task_id', 'DESC')
		    ->limit($pagination['start'], $pagination['limit'])
		    ->all();

		}

	    $tempTasks = [];

	    foreach($tasks as $task){

	    	if($task['service_id']){

	    		$service = $db->from('services')->where('service_id', $task['service_id'])->first();

	    		if($service){

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

	    			foreach($service_name as $details){

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

	    					$name = $details['name'];

	    				}

	    			}

	    			$task['service_name'] = $name;

	    		}

	    	}

	    	if($task['task_status'] == 0){

	    		$task['task_status'] = "Onay Bekliyor";
	    		$task['status'] = 0;

	    	}elseif($task['task_status'] == 1){

	    		$task['task_status'] = "Onaylanmış";
	    		$task['status'] = 1;

	    	}elseif($task['task_status'] == 2){

	    		$task['task_status'] = "Reddedilmiş";
	    		$task['status'] = 2;

	    	}

	    	if($task['task_type'] == 2){

	    		$task['task_type'] = "İptal";
	    		$task['type'] = 2;

	    	}elseif($task['task_type'] == 1){

	    		$task['task_type'] = "Telafi";
	    		$task['type'] = 1;

	    	}

	    	$tempTasks[] = $task;
	    }

	    $tasks = $tempTasks;

		$title = "Sipariş İşlemleri";
		$page_title = "Sipariş İşlemleri";

		require admin_view('tasks');

	}else{

		require controller("404");

	}

}else{

	require controller("404");
	
}