<?php

$extra = null;


if (login_control()) {

	$title = "{INDEX_PAGE_TITLE}";



	$categories = $db->from('categories')

		->join('services', 'services.category_id = categories.category_id')

		->where('services.service_auto', 0)

		->where('categories.category_status', 1)

		->groupby('services.category_id')

		->orderby('categories.category_number', 'ASC')

		->all();



	$categories_text = "";

	foreach ($categories as $category) {

		$category_name = json_decode($category["category_name"], true);

		$default_lang = "";

		$control = false;

		foreach ($category_name as $details) {

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

				$default_lang = $details["name"];
			}

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

				$control = true;

				$categories_text .= '<option value="' . $category["category_id"] . '">' . $details["name"] . '</option>';
			}
		}

		if (!$control) {

			$categories_text .= '<option value="' . $category["category_id"] . '">' . $default_lang . '</option>';
		}
	}

	$query = $db->prepare("SELECT * FROM `orders` WHERE user_id = ? AND (order_status = 'Completed' OR order_status = 'Partial')");

	$query->execute([$user["user_id"]]);

	$total_spent = 0;

	if ($query->rowCount()) {

		$orders = $query->fetchAll(PDO::FETCH_ASSOC);

		$text = "";
		foreach ($orders as $order) {
		   
			if ($order["order_status"] == "Partial") {

				if ($order["order_remains"] > $order["order_quantity"]) {

					$order["order_remains"] = $order["order_quantity"];
				}
                
				$total_spent += $order["order_amount"] - ($order["order_amount"] / $order["order_quantity"]) * $order["order_remains"];
				
			} elseif ($order["order_status"] == "Completed") {
				$total_spent += $order["order_amount"];
				
			
			}
		}
	}




	$duyurular = $db->from('duyuru')->orderby('id', 'DESC')->all();

	$page_content = $db->from('pages')->where('id', 3)->first();
	$seo = json_decode($page_content['page_seo'], true);

	@$meta = [
		"title" => $seo['title'],
		"desc" 	=> $seo['desc'],
		"keyw" 	=> $seo['keyw']
	];




	@$integration = "";

	foreach ($plugins as $plugin) {



		if ($plugin['app_id'] !== 1 && $plugin['app_id'] !== 2 && $plugin['app_id'] !== 3 && $plugin['app_id'] !== 4 && $plugin['app_id'] !== 5) {



			@$data = json_decode($plugin['app_data'], true);



			if (@$data['visibility'] == 1) {

				@$integration .= $data['code'];
			} elseif (@$data['visibility'] == 0) {

				@$integration .= $data['code'];
			}
		}
	}



	require view("index");

	$extra = [

		"{INTEGRATIONS}" => $integration,

		"{ORDER_FORM_CATEGORIES}" => $categories_text,

		"{TOTAL_SPENT}" => $total_spent

	];

	$output = ob_get_clean();

	echo replace($extra);
} else {

	$title = "{LOGIN_PAGE_TITLE}";

	$page_content = $db->from('pages')->where('id', 8)->first();

	$seo = json_decode($page_content['page_seo'], true);

	@$meta = [
		"title" => $seo['title'],
		"desc" 	=> $seo['desc'],
		"keyw" 	=> $seo['keyw']
	];

	$uye_text = "";
	$dcq = $db->from('integrations')->where('app_id', 1)->first();
	$recaptcha = json_decode($dcq['app_data'], true);
	$recaptcha_text = "";
	if ($recaptcha["login_page"]) {
		$recaptcha_text = '<div class="form-group"><div class="g-recaptcha" data-sitekey="' . $recaptcha["site_key"] . '"></div></div>';
	}

	@$integration = "";
	foreach ($plugins as $plugin) {
		if ($plugin['app_id'] !== 1 && $plugin['app_id'] !== 2 && $plugin['app_id'] !== 3 && $plugin['app_id'] !== 4 && $plugin['app_id'] !== 5) {
			@$data = json_decode($plugin['app_data'], true);
			if (@$data['visibility'] == 2) {
				@$integration .= $data['code'];
			} elseif (@$data['visibility'] == 0) {
				@$integration .= $data['code'];
			}
		}
	}

	require view("login");

	$extra = [
		"{INTEGRATIONS}" => $integration,
		"{reCAPTCHA}" => $recaptcha_text,
		"{Uyeler}" => $uye_text
	];
}

$output = ob_get_clean();
echo replace($output, $extra);
