<?php

if (login_control()) {

	function tarih_farki($hesaplanacak_tarih)
	{
		date_default_timezone_set("Europe/Istanbul");
		$datetime_now = time();
		$hesaplanacak_tarih = strtotime($hesaplanacak_tarih);
		$fark = $hesaplanacak_tarih - $datetime_now;
		$dakika = $fark / 60;
		$saniye_farki = floor($fark - (floor($dakika) * 60));

		$saat = $dakika / 60;
		$dakika_farki = floor($dakika - (floor($saat) * 60));

		$gun = $saat / 24;
		$saat_farki = floor($saat - (floor($gun) * 24));

		$yil = floor($gun / 365);
		$gun_farki = floor($gun - (floor($yil) * 365));

		$array = array(
			'gun' => $gun_farki,
			'saat' => $saat_farki,
			'dakika' => $dakika_farki,
			'saniye' => $saniye_farki,
		);


		return $array;
	}

	$extra_sql = "";

	$status_list = [

		"completed" => "Completed",

		"canceled" => "Canceled",

		"partial" => "Partial",

		"pending" => "Pending",

		"inprogress" => "In progress",

		"processing" => "Processing"

	];

    $uri = explode("/",$_SERVER['REQUEST_URI']);

	if (array_key_exists(2,$uri)) {
	    

		if (array_key_exists($uri[2], $status_list)) {

			$extra_sql = "AND order_status = '" . $status_list[$uri[2]] . "'";
				$orders = $db->from('orders')

		->join('services', 'services.service_id = orders.service_id')

		->where('orders.user_id', session('user_id'))
		->where('orders.order_status', $status_list[$uri[2]])

		->orderby('order_id', 'DESC')

		->all();

		}
	}else{
	    	$orders = $db->from('orders')

		->join('services', 'services.service_id = orders.service_id')

		->where('orders.user_id', session('user_id'))

		->orderby('order_id', 'DESC')

		->all();
	}

	$title = "{ORDERS_PAGE_TITLE}";







	$orders_text = "";

	foreach ($orders as $order) {



		$task = $db->from('tasks')

			->where('order_id', $order['order_id'])

			->orderby('task_date', 'DESC')

			->first();



		if ($task) {

			if ($task['task_status'] == 0) {
				$task = "<td>Onay Bekliyor</td>";
				
			} else {
				
				if ($task['task_type'] == 1) {


					$is_refill_gun_sinir = 0;
					$refill_gun_sinir = $order['refill_day'];
					if ($refill_gun_sinir > 0) {
						$date_now = date("Y-m-d H:i:s");
						$refill_gun_trh = strtotime('+' . $refill_gun_sinir . ' days', $order['order_date']);
						$refill_gun_trh = date('Y-m-d H:i:s', $refill_gun_trh);
						if ($date_now > $refill_gun_trh) {
							$is_refill_gun_sinir = false;
						} else {
							$is_refill_gun_sinir = true;
						}
					} else {
						$is_refill_gun_sinir = false;
					}

					if ($is_refill_gun_sinir) {

						$is_refill_bekleme = 0;
						$refill_bekleme_suresi = $order['refill_bekleme'];
						if ($refill_bekleme_suresi > 0) {
							$date_now = date("Y-m-d H:i:s");
							$refill_bekleme_trh = strtotime('+' . $refill_bekleme_suresi . ' hours', strtotime($task['task_updatedate']));
							$refill_bekleme_trh = date('Y-m-d H:i:s', $refill_bekleme_trh);
							if ($date_now > $refill_bekleme_trh) {
								$is_refill_bekleme = true;
							} else {
								$is_refill_bekleme = false;
							}
						} else {
							$is_refill_bekleme = true;
						}

						if ($is_refill_bekleme) {
							$task = "<td><button onclick='refill_order(" . $order['order_id'] . ")' class='btn btn-success btn-xs'>Telafi Başlat</button></td>";
						} else {
							if ($task['task_updatedate'] != null) {

								$refilkalantrh = tarih_farki($refill_bekleme_trh);

								$refill_status_msg = "";
								$refill_status = $task['task_status'];
								if($refill_status == 1){
									$refill_status_msg = "<span class=\"text-success\"><i class=\"fa fa-check\">Onaylandı</i> </span><br>";
								}else if($refill_status == 2){
									$refill_status_msg = "<span class=\"text-danger\"><i class=\"fa fa-times\">Reddedildi</i> </span><br>";
								}
								
								$task = "
								<td>
									".$refill_status_msg."
									<a href=\"#\" data-toggle=\"modal\" id=\"refill-time-btn\"data-time=\"Yeniden refill yapabilmek için kalan süre: " . $refilkalantrh['saat'] . " saat " . $refilkalantrh['dakika'] . " dakika " . $refilkalantrh['saniye'] . " saniye\" ><i class=\"fa fa-clock\"></i> Telafi</a>
								
								</td>";
							} else {
								$task = "<td><span class=\"text-success\"><i class=\"fa fa-check\"></i> İstek Gönderildi </span></td>";
							}
						}
					} else {
						$task = "<td></td>";
					}
				} elseif ($task['task_type'] == 2) {
					$task = "<td><button onclick='customer_cancel_order(" . $order['order_id'] . ")' class='btn btn-danger btn-xs'>İptal Et</button></td>";
				}
			}

		} else {

			if ($order['cancel_button'] == 1) {

				$task = "<td><button onclick='customer_cancel_order(" . $order['order_id'] . ")' class='btn btn-danger btn-xs'>İptal Et</button></td>";
			} elseif ($order['refill_button'] == 1) {


				$refill_bekleme_trh = "";
				$is_refill_bekleme = 0;
				$refill_bekleme_suresi = $order['refill_bekleme'];
				if ($refill_bekleme_suresi > 0) {
					$date_now = date("Y-m-d H:i:s");
					$refill_bekleme_trh = strtotime('+' . $refill_bekleme_suresi . ' hours', $order['update_date']);
					$refill_bekleme_trh = date('Y-m-d H:i:s', $refill_bekleme_trh);
					if ($date_now > $refill_bekleme_trh) {
						$is_refill_bekleme = true;
					} else {
						$is_refill_bekleme = false;
					}
				} else {
					$is_refill_bekleme = true;
				}

				if ($is_refill_bekleme) {
					$task = "<td><button onclick='refill_order(" . $order['order_id'] . ")' class='btn btn-success btn-xs'>Telafi Başlat</button></td>";
				} else {
					if ($order['order'] != "0") {

						$refilkalantrh = tarih_farki($refill_bekleme_trh);

						$task = "
							<td>
								<a href=\"#\" data-toggle=\"modal\" id=\"refill-time-btn\"data-time=\"Telafi yapabilmek için kalan süre: " . $refilkalantrh['saat'] . " saat " . $refilkalantrh['dakika'] . " dakika " . $refilkalantrh['saniye'] . " saniye\" ><i class=\"fa fa-clock\"></i> Telafi </a>
							
							</td>";
					} else {
						$task = "<td><span class=\"text-success\"><i class=\"fa fa-check\"></i> İstek Gönderildi </span></td>";
					}
				}
			} else {

				$task = "<td></td>";
			}
		}



		$service_name = json_decode($order["service_name"], true);

		foreach ($service_name as $details) {

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

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

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

				$name = $details["name"];

				break;
			}
		}

		if ($order["order_status"] == "Completed") {

			$order["order_remains"] = str_replace("-", "+", $order["order_remains"]);
		}



		if ($order['order_status'] == "Completed") {

			$orderstatus = "{ORDERS_COMPLETED}";
		} elseif ($order['order_status'] == "Canceled") {

			$orderstatus = "{ORDERS_CANCELED}";
		} elseif ($order['order_status'] == "Partial") {

			$orderstatus = "{ORDERS_PARTIAL}";
		} elseif ($order['order_status'] == "Pending") {

			$orderstatus = "{ORDERS_PENDING}";
		} elseif ($order['order_status'] == "In progress") {

			$orderstatus = "{ORDERS_INPROGRESS}";
		} elseif ($order['order_status'] == "Processing") {

			$orderstatus = "{ORDERS_PROCESSING}";
		}



		$orders_text .= "

			<tr>

				<td>" . $order['order_id'] . "</td>

				<td>" . str_date(date("y-m-d H:i:s", $order["order_date"])) . "</td>

				<td>" . $order['order_link'] . " </td>

				<td>{CURRENCY_SYMBOL}" . $order['order_amount'] . "</td>

				<td>" . $order['order_start_count'] . "</td>

				<td>" . $order['order_quantity'] . "</td>

				<td>$name</td>

				<td>" . $orderstatus . "</td>

				<td>" . $order['order_remains'] . "</td>";

		if ($order['cancel_button'] == 1) :

			if ($order['order_status'] == "Pending") :

				$orders_text .= $task;

			elseif ($order['order_status'] == "Processing") :

				$orders_text .= $task;

			else :

				$orders_text .= "<td></td>";

			endif;

		elseif ($order['refill_button'] == 1) :

			if ($order['order_status'] == "Completed") :

				$orders_text .= $task;

			else :

				$orders_text .= "<td></td>";

			endif;

		else :

			$orders_text .= "<td></td>";

		endif;

		$orders_text .= "</tr>";

		$orders_text2 .= "

			<div class='well mo'>

				<li style='background:#fff;padding:1px 4px;'>ID: <span>" . $order['order_id'] . "</span></li>

				<li style='background:#e4e4e4;padding:1px 4px;'>Tarih: <span>" . str_date(date("y-m-d H:i:s", $order["order_date"])) . "</span></li>

				<li style='background:#fff;padding:1px 4px;'>Bağlantı: <span><button style='background:#dcdcdc;border-color:#e4e4e4;' class='btn btn-primary btn-xs' data-toggle='collapse' id='link_$order[order_id]' onclick='openLink($order[order_id])' data-link='" . $order['order_link'] . "'>&#128065;</button></span></li>

				<li style='background:#e4e4e4;padding:1px 4px;'>Tutar: <span>" . CURRENCY_SYMBOL . $order['order_amount'] . "</span></li>

				<li style='background:#fff;padding:1px 4px;'>Başlangıç Sayısı: <span>" . $order['order_start_count'] . "</span></li>

				<li style='background:#e4e4e4;padding:1px 4px;'>Miktar: <span>" . $order['order_quantity'] . "</span></li>

				<li class='service' style='background:#fff;padding:1px 4px;'>Servis: <span><button style='background:#dcdcdc;border-color:#e4e4e4;' class='btn btn-primary btn-xs' data-toggle='collapse' id='service_$order[order_id]' onclick='openService($order[order_id])' data-service='$name'>&#128065;</button></span></li>

				<li style='background:#e4e4e4;padding:1px 4px;'>Durum: <span>" . $orderstatus . "</span></li>

				<li style='background:#fff;padding:1px 4px;'>Kalan: <span>" . $order['order_remains'] . "</span></li>";


		$orders_text2 .= "</div>";
	}

	$orders_text = replace($orders_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'];
			}
		}
	}



	require view("orders");

	$output = ob_get_clean();

	$extra = [

		"{INTEGRATIONS}" => $integration,

		"{LINK_ALL}" => site("orders"),

		"{LINK_COMPLETED}" => site($siteLinks['orders']['url'] . "/completed"),

		"{LINK_CANCELED}" => site($siteLinks['orders']['url'] . "/canceled"),

		"{LINK_PARTIAL}" => site($siteLinks['orders']['url'] . "/partial"),

		"{LINK_PENDING}" => site($siteLinks['orders']['url'] . "/pending"),

		"{LINK_INPROGRESS}" => site($siteLinks['orders']['url'] . "/inprogress"),

		"{LINK_PROCESSING}" => site($siteLinks['orders']['url'] . "/processing"),

		"{ORDERS_LIST}" => $orders_text,

		"{ORDERS_LIST2}" => $orders_text2

	];

	echo replace($output, $extra);
} else {

	require controller("404");
}
