<?php

	$extra = [];

	if(login_control()) {



		if($settings['support_system'] == 0){

			header('Location:'.site());

			exit();

		}



		if(route(1)) {

			$id = route(1);

			$query = $db->prepare("SELECT * FROM `tickets` WHERE ticket_id = ? AND user_id = ?");

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

			$ticket = $query->fetch(PDO::FETCH_ASSOC);

			if($ticket) {

				$title = $ticket["ticket_title"];

				$query = $db->prepare("SELECT * FROM `ticket_messages` WHERE ticket_id = ? ORDER BY message_id ASC");

				$query->execute([$id]);

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

				$messages_text = "";

				foreach($messages as $message) {

					$query = $db->prepare("UPDATE tickets SET client_read = 1 WHERE ticket_id = ?");	
					$query->execute([$id]);				

					if($message["message_author"] == 1) {

						$messages_text .= '

							<div class="col-md-10 offset-md-2 mb-2">

								<pre class="bg-primary small text-white rounded p-2 mb-0" style="white-space: pre-line;">'.$message["message_text"].'</pre>

								<div class="text-right small">'.date("d.m.Y H:i:s", $message["message_time"]).'</div>

							</div>';

					} else {

						$messages_text .= '

							<div class="col-md-10 mb-2">

								<pre class="bg-dark text-light small text-white rounded p-2 mb-0" style="white-space: pre-line;">'.htmlspecialchars_decode($message["message_text"]).'</pre>

								<div class="small">'.date("d.m.Y H:i:s", $message["message_time"]).'</div>

							</div>';

					}

				}

				$form = '<div class="alert alert-danger">Destek Talebi Kapalı</div>';

				if($ticket["ticket_status"] == 1) {

					$form = '<form id="send_message" onsubmit="return false;">

							<input type="hidden" name="id" value="{TICKET_ID}" />

							<div class="form-group">

								<label>Mesaj:</label>

								<textarea name="message" rows="6" class="form-control"></textarea>

							</div>

							<div class="form-group">

								<button type="submit" class="btn btn-primary">Gönder</button>

							</div>

						</form>';

				}



				$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,

					"{TICKET_SEND_FORM}" => $form,

					"{MESSAGES}" => $messages_text,

					"{TICKET_ID}" => $id

				];

				$form = replace($form, $extra);

				require view("ticket");

			} else {

				require controller("404");

			}

		} else {

			$title = "{TICKETS_PAGE_TITLE}";

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

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


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

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

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

			$tickets_text = "";

			if($tickets) {

				foreach($tickets as $ticket) {



					$status = "Beklemede";



					if($ticket['ticket_status'] == 1){



						if($ticket['ticket_ans'] == 1){

							$status = "{TICKET_ANS_ONE}";

						}elseif($ticket['ticket_ans'] == 2){

							$status = "{TICKET_ANS_TWO}";

						}

						

					}elseif($ticket["ticket_status"] == 0){

						$status = "{TICKET_CLOSED}";

					}elseif($ticket["ticket_status"] == 2){

						$status = "{TICKET_CLOSED}";

					}

					

					$link = site("tickets/$ticket[ticket_id]");

					$tickets_text .= "<tr><td>{$ticket["ticket_id"]}</td><td><a href=\"{$link}\">{$ticket["ticket_title"]}</a></td>

					<td>{$status}</td></tr>";

				}

				$tickets_text = replace($tickets_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,

				"{TICKETS}" => $tickets_text

			];

			require view("tickets");

		}

	} else {

		require controller("404");

	}

	$output = ob_get_clean();

	echo replace($output, $extra);