<?php



if(login_control()) {

	

	if($user["user_authority"] == 2) {



		if(!permission('access', 'support')){

			$page_title = "Yetkisiz Erişim";

			require admin_view("unauthorized");

			exit();

		}



		if(route(2) == "unread") {



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

				->join('users', 'users.user_id = tickets.user_id')

				->where('ticket_read', 0)

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

				->orderby('ticket_status', 'ASC')

				->all();



			$pageParam = false;

			$title = "Destek Talepleri";

			$page_title = "Destek Talepleri";



		}elseif(route(2)){



			$id = route(2);



			$ticket = $db->from('tickets')

			->join('users', 'users.user_id = tickets.user_id')

			->where('tickets.ticket_id', $id)

			->first();



			if($ticket) {



				$title = "Destek Talepleri";

				$page_title = "Destek Talepleri";

				$messages = $db->from('ticket_messages')->where('ticket_id', $id)->orderby('message_id', 'DESC')->all();

				$update = $db->update('tickets')->where('ticket_id', $id)->set(array('ticket_read' => 1));



			} else {



				header("Location:".admin_url("support"));

				die();



			}



		}else{



			$totalRecord = $db->from('tickets')

			->select('count(ticket_id) as total')

			->total();



			$pageLimit = 25;

			$pageParam = 'page';

			$pagination = $db->pagination($totalRecord, $pageLimit, $pageParam);


			if(@$_GET['status']){

				if($_GET['status'] == 0){$_GET['status'] = '0';}
				$tickets = $db->from('tickets')
					->join('users', 'users.user_id = tickets.user_id')
					->where('tickets.ticket_status', $_GET['status'])
					->orderby('ticket_id', 'DESC')
					->orderby('ticket_status', 'ASC')
					->limit($pagination['start'], $pagination['limit'])
					->all();

			}elseif(@$_GET['search']){



				if($_GET['search_type'] == "id"){



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

					->join('users', 'users.user_id = tickets.user_id')

					->where('tickets.ticket_id', $_GET['search'])

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

					->orderby('ticket_status', 'ASC')

					->limit($pagination['start'], $pagination['limit'])

					->all();



				}elseif($_GET['search_type'] == "link"){



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

					->join('users', 'users.user_id = tickets.user_id')

					->where('tickets.ticket_title', $_GET['search'])

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

					->orderby('ticket_status', 'ASC')

					->limit($pagination['start'], $pagination['limit'])

					->all();



				}elseif($_GET['search_type'] == "username"){



					$user = $db->from('users')->where('user_username', $_GET['search'])->first();



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

					->join('users', 'users.user_id = tickets.user_id')

					->where('tickets.user_id', $user['user_id'])

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

					->orderby('ticket_status', 'ASC')

					->limit($pagination['start'], $pagination['limit'])

					->all();



				}



			}else{



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

				->join('users', 'users.user_id = tickets.user_id')

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

				->orderby('ticket_status', 'ASC')

				->limit($pagination['start'], $pagination['limit'])

				->all();



			}



			$title = "Destek Talepleri";

			$page_title = "Destek Talepleri";



		}







		require admin_view('support');

		



	}else{



		require controller("404");



	}



}else{



	require controller("404");

	

}