<?php

	if(login_control()) {

		$title = "{AUTOS_PAGE_TITLE}";

		$query = $db->prepare("SELECT * FROM `auto_orders` INNER JOIN `services` ON services.service_id = auto_orders.service_id WHERE auto_orders.user_id = ? ORDER BY auto_id DESC");

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

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

		$autos_text = "";

		foreach($autos as $auto) {

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

			foreach($service_name as $details) {

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

					$name = $details["name"];

				}

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

					$name = $details["name"];

				}

			}

			$status = '<span class="badge badge-success">{AUTO_STATUS_COMPLETED}</span>';

			$edit = '-';

			if($auto["auto_status"] == 1) {

				$status = '<span class="badge badge-warning">{AUTO_STATUS_PROCESSING}</span>';

				$edit = '<button class="btn btn-danger btn-sm" onclick="stop_auto('.$auto["auto_id"].')">{STOP_AUTO}</button>';

			} elseif($auto["auto_status"] == 2) {

				$status = '<span class="badge badge-danger">{AUTO_STATUS_CANCELED}</span>';

			}

			$autos_text .= "<tr id='auto_$auto[auto_id]'><td>$auto[auto_id]</td><td>$name</td><td>$auto[auto_link]</td><td>$auto[auto_quantity]</td><td>".date("d.m.y H:i:s", $auto["auto_date"])."</td><td>$auto[auto_completed]/$auto[auto_limit]</td><td>$status</td><td>$edit</td></tr>";

		}

		$autos_text = replace($autos_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("autos");

		$output = ob_get_clean();

		$extra = [

			"{INTEGRATIONS}" => $integration,

			"{AUTOS_LIST}" => $autos_text

		];

		echo replace($output, $extra);

	} else {

		require controller("404");

	}