<?php

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

		if(!permission('access', 'services')){
			$page_title = "Yetkisiz Erişim";
			require admin_view("unauthorized");
			exit();
		}

		if(route(2)) {

			header("Location:".admin_url());
			die();

		} else {

			$services = $db->from('services')
			->join('categories', 'categories.category_id = services.category_id')
			->leftjoin('apis', 'apis.api_id = services.api_id')
			->orderby('service_number', 'ASC')
			->all();

			$serviceList    = array_group_by($services, 'category_name');
			$apis = $db->from('apis')->orderby('api_id', 'ASC')->all();
			$categories = $db->from('categories')->orderby('category_number', 'ASC')->all();
			

			$tempCategories = [];
			foreach($categories as $category) {
				$category_name = json_decode($category["category_name"], true);
				$name = null;
				foreach($category_name as $details) {
					if($details["lang"] == 1) {
						$name = $details["name"];
					}
				}
				$category["category_name"] = $name;
				$tempCategories[] = $category;
				
			}
			$categories = $tempCategories;


			$tempServices = [];
			foreach($services as $service) {

				$category_name = json_decode($service["category_name"], true);
				$name = null;
				foreach($category_name as $details) {
					if($details["lang"] == 1) {
						$name = $details["name"];
					}
				}
				$service["category_name"] = $name;

				if($service['api_detail']){

					$api = json_decode($service["api_detail"], true);
					$service['api_min'] = '<div class="provider-value">'.$api['min'].'</div>';
					$service['api_max'] = '<div class="provider-value">'.$api['max'].'</div>';
					if($api['currency'] == "USD"){
					    $service['api_rate'] = '<div class="provider-value">'.'~'.round($api['rate']*$settings['dolar'],2) . ' TL</div>';
					}else{
					    $service['api_rate'] = '<div class="provider-value">'.$api['rate'].' '.$api['currency'].'</div>';
					}

				}else{

					$service['api_min'] = null;
					$service['api_max'] = null;
					$service['api_rate'] = null;

				}

				$service_name = json_decode($service["service_name"], true);
				$name = null;
				foreach($service_name as $details) {
					if($details["lang"] == 1) {
						$name = $details["name"];
					}
				}
				$service["service_name"] = $name;
				$tempServices[] = $service;

			}

			$services = $tempServices;
			$title = "Servisler";
			$page_title = "Servisler";

		}



		require admin_view('services');
		

	}else{

		require controller("404");

	}

}else{

	require controller("404");
	
}