<?php 

function view_ticket_cb() {
	$post = new post;
	if(!isset($_GET['id'])) {
		return_method("support-ticket");
	}
	$ticket_meta = $post->get_post_meta($_GET['id']);
	
	if(!current_user_can("respond_to_others_support_tickets")) {
		$session = Session::getInstance();
		if($ticket_meta['user_id'] != $session->id)
			return_method("support-ticket");
	}
	$status = ($ticket_meta['status'] == 'active') ? "<span class=\"badge badge-success\">Open</span>" : "<span class=\"badge badge-secondary\">Closed</span>";
	?>
	<div class="row">
		<div class="col-md-8 col-offset-md-2">
			<div class="card card-primary">
				<div class="card-header">
					<h3>Ticket: <?php echo $ticket_meta['ticket_number']; ?></h3>
				</div>
				<div class="card-body">
					<table class="table">
						<tbody>
							<tr>
								<td class="pt-0 pb-0">Status</td>
								<td class="pt-0 pb-0"><?php echo $status; ?>&nbsp;
									<?php if ($ticket_meta['status'] == 'active'): ?>
										<a href="javaScript:void(0);" class="red ajax-action" data-action-callback="msg_close_ticket" data-id="<?php echo $ticket_meta['id'] ?>" data-callback="refreshPage">Close Ticket</a>
									<?php else: ?>
										<a href="javaScript:void(0);" class="green ajax-action" data-action-callback="msg_open_ticket" data-id="<?php echo $ticket_meta['id'] ?>" data-callback="refreshPage">Open Ticket</a>
									<?php endif ?>
								</td>
							</tr>
							<tr>
								<td class="pt-0 pb-0">Subject</td>
								<td class="pt-0 pb-0"><?php echo $ticket_meta['subject']; ?></td>
							</tr>
							<tr>
								<td class="pt-0 pb-0">Date Opened</td>
								<td class="pt-0 pb-0"><?php echo date(_DATE_FORMAT_, $ticket_meta['posted_on']); ?></td>
							</tr>
							<tr>
								<td class="pt-0 pb-0">User Account</td>
								<td class="pt-0 pb-0"><?php echo get_fullname($ticket_meta['user_id'], true); ?></td>
							</tr>
							<tr>
								<td class="pt-0 pb-0">Opened By</td>
								<td class="pt-0 pb-0"><?php echo get_fullname($ticket_meta['posted_by'], true); ?></td>
							</tr>
							<tr>
								<td class="pt-0 pb-0">Message</td>
								<td class="pt-0 pb-0"><?php echo $ticket_meta['message']; ?></td>
							</tr>
						</tbody>
					</table>
					<?php do_action("comments_api", ["obj" => $ticket_meta['id'], 'key' => 'support_ticket']) ?>
				</div>
			</div>
		</div>
	</div>
	<?php
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}