<?php

	$extra = null;

	if(login_control()) {

		$title = "{ACCOUNT_PAGE_TITLE}";



		$deposits = '<tr class="text-center"><td colspan="3">---</td></tr>';

		$query = $db->prepare("SELECT * FROM `deposits` WHERE user_id = ? ORDER BY deposit_id DESC");

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

		$total_deposited = 0;

		if($query->rowCount()) {

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

			$text = "";

			foreach($deposits as $deposit) {

				if($deposit["deposit_status"] == 2) {

					$deposit["deposit_status"] = "{PAYMENT_WAIT}";

				} elseif($deposit["deposit_status"] == 1) {

					$deposit["deposit_status"] = "{PAYMENT_SUCCESS}";

					$total_deposited += $deposit["deposit_amount"];

				} else {

					$deposit["deposit_status"] = "{PAYMENT_FAILED}";

				}

				$text .= "<tr><td>".date("d.m.y H:i:s", $deposit["deposit_date"])."</td><td>".$deposit["deposit_amount"]."</td><td>".$deposit["deposit_status"]."</td></tr>";

			}

		}

		$references = null;

		$query = $db->prepare("SELECT * FROM `user_references` INNER JOIN `users` ON users.user_id = user_references.user_id WHERE reference_user_id = ? ORDER BY reference_earnings DESC");

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

		if($query->rowCount()) {

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

			$referencesTemp = "";

			foreach($references as $reference) {

				$referencesTemp .= '<tr><td>'.$reference["user_username"].'</td><td>'.$reference["reference_earnings"].'</td></tr>';

			}

			$references = $referencesTemp;

		}

		$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"];

				}

			}

		}

		@$deposits = replace($text);



		@$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'] == 0){ // Tüm Sayfalar

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

				}elseif(@$data['visibility'] == 1){ // Giriş Yapanlar

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

				}



			}



		}



		$extra = [

			"{INTEGRATIONS}" => $integration,

			"{DEPOSITS}" => $deposits,

			"{TOTAL_DEPOSITED}" => $total_deposited,

			"{TOTAL_SPENT}" => $total_spent,

			"{USER_REFERENCE_LINK}" => site($siteLinks['register']['url']."?r=".$user["user_id"]),

			"{REFERENCES}" => $references

		];

		

		require view("account");

		

		$output = ob_get_clean();

		echo replace($output, $extra);



	} else {



		require controller("404");



	}