<?php

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

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

	    $allOrders = $db->from('orders')->select('count(order_id) as total')->total();
	    $pendingOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'Pending')->select('count(order_id) as total')->total();
	    $processingOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'Processing')->select('count(order_id) as total')->total();
	    $inprogressOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'In progress')->select('count(order_id) as total')->total();
	    $completedOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'Completed')->select('count(order_id) as total')->total();
	    $partialOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'Partial')->select('count(order_id) as total')->total();
	    $canceledOrders = $db->from('orders')->where('is_failed', '0')->where('order_status', 'Canceled')->select('count(order_id) as total')->total();
	    $failedOrders = $db->from('orders')->where('is_failed', 1)->select('count(order_id) as total')->total();

	
		$page_order_status = route(2);


	    if($page_order_status == "failed"){ 
		

	    	$status_list = [
				"Completed" => "Tamamlandı",
				"Canceled" => "İptal edildi",
				"Partial" => "Kısmi Tamamlanmış",
				"Pending" => "Beklemede",
				"In progress" => "Yükleniyor",
				"Processing" => "İşlemde"
			]; 

			$orders = $db->from('orders')
			->join('users', 'users.user_id = orders.user_id')
			->where('orders.is_failed', '1')
			->orderby('order_id', 'DESC')
			->all();

			$tempOrders = [];
			foreach($orders as $order) {

				$services = $db->from('services')->where('service_id', $order['service_id'])->first();
				if($services):
					$order['service_name'] = $services['service_name'];
					$order['service_id'] = $services['service_id'];
				else:
					$order['service_name'] = null;
					$order['service_id'] = null;
				endif;

				$key = array_search($order["order_status"], $status_list);
				if($key !== false) {
					$order["order_status"] = "{ORDERS_STATUS_".str_replace(" ", "", strtoupper($status_list[$key]))."}";
				}
				if($order["service_name"] != null) {
					$service_name = json_decode($order["service_name"], true);
					$name = null;
					foreach($service_name as $details) {
						if($details["lang"] == 1) {
							$name = $details["name"];
						}
					}
					$order["service_name"] = $name;
				}
				$tempOrders[] = $order;
			}

			$orders = $tempOrders;
			$title = "Başarısız Siparişler";
			$page_title = "Başarısız Siparişler";

	    }elseif($page_order_status){
		
	    	$status_list = [
				"Completed" => "Tamamlandı",
				"Canceled" => "İptal edildi",
				"Partial" => "Kısmi Tamamlanmış",
				"Pending" => "Beklemede",
				"In progress" => "Yükleniyor",
				"Processing" => "İşlemde"
			];
			$route = $page_order_status;
			if( $route == "inprogress"){
				$route = "In progress";
			}
			$totalRecord = $db->from('orders')
			->select('count(order_id) as total')
			->total();

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

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

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

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

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

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

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

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

					$orders = $db->from('orders')
					->join('users', 'users.user_id = orders.user_id')
					->where('orders.is_failed', '0')
					->where('orders.order_status', $route)
					->where('orders.user_id', $user['user_id'])
					->orderby('order_id', 'DESC')
					->limit($pagination['start'], $pagination['limit'])
					->all();

				}

			}else{

				$orders = $db->from('orders')
				->join('users', 'users.user_id = orders.user_id')
				->where('orders.is_failed', '0')
				->where('orders.order_status', $route)
				->orderby('order_id', 'DESC')
				->limit($pagination['start'], $pagination['limit'])
				->all();

			}

			

			$tempOrders = [];
			foreach($orders as $order) {
				$services = $db->from('services')->where('service_id', $order['service_id'])->first();
				if($services):
					$order['service_name'] = $services['service_name'];
					$order['service_id'] = $services['service_id'];
				else:
					$order['service_name'] = null;
					$order['service_id'] = null;
				endif;
				$key = array_search($order["order_status"], $status_list);
				if($key !== false) {
					$order["order_status"] = "{ORDERS_STATUS_".str_replace(" ", "", strtoupper($status_list[$key]))."}";
				}
				if($order["service_name"] != null) {
					$service_name = json_decode($order["service_name"], true);
					$name = null;
					foreach($service_name as $details) {
						if($details["lang"] == 1) {
							$name = $details["name"];
						}
					}
					$order["service_name"] = $name;
				}
				$tempOrders[] = $order;
			}

			$orders = $tempOrders;
			$title = "Siparişler";
			$page_title = "Siparişler";
			
	    }else{
			
	    	$status_list = [
				"Completed" => "Tamamlandı",
				"Canceled" => "İptal edildi",
				"Partial" => "Kısmi Tamamlanmış",
				"Pending" => "Beklemede",
				"In progress" => "Yükleniyor",
				"Processing" => "İşlemde"
			];

			$totalRecord = $db->from('orders')
			->select('count(order_id) as total')
			->total();

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

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

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

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

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

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

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

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

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

				}

			}else{

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

			}

			$tempOrders = [];
			foreach($orders as $order) {
				$services = $db->from('services')->where('service_id', $order['service_id'])->first();
				if($services):
					$order['service_name'] = $services['service_name'];
					$order['service_id'] = $services['service_id'];
				else:
					$order['service_name'] = null;
					$order['service_id'] = null;
				endif;
				$key = array_search($order["order_status"], $status_list);
				if($key !== false) {
					$order["order_status"] = "{ORDERS_STATUS_".str_replace(" ", "", strtoupper($status_list[$key]))."}";
				}
				if($order["service_name"] != null) {
					$service_name = json_decode($order["service_name"], true);
					$name = null;
					foreach($service_name as $details) {
						if($details["lang"] == 1) {
							$name = $details["name"];
						}
					}
					$order["service_name"] = $name;
				}
				$tempOrders[] = $order;
			}

			$orders = $tempOrders;
			$title = "Siparişler";
			$page_title = "Siparişler";

	    }


	
		require admin_view('orders');

	}else{

		require controller("404");

	}

}else{

	require controller("404");
	
}