<?php
/**
 * Answyr Desk — admin inbox (list + ticket detail router).
 *
 * Detail view drives the draft-and-vet flow: "Suggest reply" → /suggest,
 * editable draft + cited sources, "Send" → /reply. The AI never sends itself.
 *
 * @package Answyr
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$ticket_id = isset( $_GET['ticket'] ) ? (int) $_GET['ticket'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( $ticket_id > 0 ) {
	$ticket = Answyr_Desk_Store::get_ticket( $ticket_id );
	if ( ! $ticket ) {
		echo '<div class="wrap"><h1>' . esc_html__( 'Ticket not found', 'answyr' ) . '</h1></div>';
		return;
	}
	$thread   = Answyr_Desk_Store::thread( $ticket_id );
	$back_url = admin_url( 'admin.php?page=answyr' );
	?>
	<div class="wrap answyr-desk" data-ticket="<?php echo (int) $ticket_id; ?>">
		<a class="answyr-back" href="<?php echo esc_url( $back_url ); ?>">&larr; <?php esc_html_e( 'Back to inbox', 'answyr' ); ?></a>
		<h1><?php echo esc_html( $ticket->subject ); ?></h1>
		<?php
		$ss_statuses   = Answyr_Desk_Store::statuses();
		$ss_priorities = Answyr_Desk_Store::priorities();
		$ss_tags       = Answyr_Desk_Store::tags_of( $ticket );
		?>
		<div class="answyr-ticket-bar">
			<p class="answyr-ticket-meta">
				<?php if ( $ticket->customer_name ) : ?>
					<span><?php echo esc_html( $ticket->customer_name ); ?></span> ·
				<?php endif; ?>
				<span><?php echo esc_html( $ticket->customer_email ); ?></span>
				<span class="answyr-pill answyr-pill-<?php echo esc_attr( $ticket->status ); ?>" id="answyr-status-pill"><?php echo esc_html( $ss_statuses[ $ticket->status ] ?? $ticket->status ); ?></span>
				<?php
				$ss_waiting = in_array( $ticket->status, array( 'resolved', 'closed' ), true )
					? ''
					: Answyr_Desk_Store::waiting_on( (int) $ticket->id );
				?>
				<?php if ( 'agent' === $ss_waiting ) : ?>
					<span class="answyr-pill answyr-pill-waiting-agent"><?php esc_html_e( 'Pending agent response', 'answyr' ); ?></span>
				<?php elseif ( 'customer' === $ss_waiting ) : ?>
					<span class="answyr-pill answyr-pill-waiting-customer"><?php esc_html_e( 'Pending customer response', 'answyr' ); ?></span>
				<?php endif; ?>
				<span class="answyr-pri answyr-pri-<?php echo esc_attr( $ticket->priority ); ?>" id="answyr-priority-pill"><?php echo esc_html( $ss_priorities[ $ticket->priority ] ?? $ticket->priority ); ?></span>
				<?php if ( isset( $ticket->rating ) && null !== $ticket->rating ) : ?>
					<span class="answyr-rating" title="<?php esc_attr_e( 'Customer satisfaction rating', 'answyr' ); ?>"><?php echo (int) $ticket->rating >= 4 ? '👍' : '👎'; ?></span>
				<?php endif; ?>
			</p>
			<p class="answyr-status-control">
				<label for="answyr-status-select"><?php esc_html_e( 'Status', 'answyr' ); ?></label>
				<select id="answyr-status-select">
					<?php foreach ( $ss_statuses as $ss_key => $ss_label ) : ?>
						<option value="<?php echo esc_attr( $ss_key ); ?>" <?php selected( $ss_key, $ticket->status ); ?>><?php echo esc_html( $ss_label ); ?></option>
					<?php endforeach; ?>
				</select>
				<label for="answyr-priority-select"><?php esc_html_e( 'Priority', 'answyr' ); ?></label>
				<select id="answyr-priority-select">
					<?php foreach ( $ss_priorities as $ss_key => $ss_label ) : ?>
						<option value="<?php echo esc_attr( $ss_key ); ?>" <?php selected( $ss_key, $ticket->priority ); ?>><?php echo esc_html( $ss_label ); ?></option>
					<?php endforeach; ?>
				</select>
				<span id="answyr-status-result" class="description"></span>
			</p>
		</div>

		<p class="answyr-tags-control">
			<label for="answyr-tags-input"><?php esc_html_e( 'Tags', 'answyr' ); ?></label>
			<input type="text" id="answyr-tags-input" class="regular-text" list="answyr-tags-list" value="<?php echo esc_attr( implode( ', ', $ss_tags ) ); ?>" placeholder="<?php esc_attr_e( 'billing, bug, feature-request', 'answyr' ); ?>">
			<datalist id="answyr-tags-list">
				<?php foreach ( Answyr_Desk_Store::all_tags() as $ss_known_tag ) : ?>
					<option value="<?php echo esc_attr( $ss_known_tag ); ?>"></option>
				<?php endforeach; ?>
			</datalist>
			<button type="button" class="button" id="answyr-tags-save"><?php esc_html_e( 'Save tags', 'answyr' ); ?></button>
			<span id="answyr-tags-result" class="description"></span>
		</p>

		<?php
		// Customer context captured by the intake fields (website, versions, setup, …).
		$ss_context = Answyr_Desk_Store::get_context( $ticket_id );
		if ( $ss_context ) :
			?>
			<div class="answyr-context">
				<h2><span class="dashicons dashicons-info-outline" aria-hidden="true"></span><?php esc_html_e( 'Customer context', 'answyr' ); ?></h2>
				<dl class="answyr-context-list">
					<?php foreach ( $ss_context as $ss_pair ) : ?>
						<dt><?php echo esc_html( $ss_pair['label'] ); ?></dt>
						<dd>
							<?php
							if ( preg_match( '#^https?://#i', $ss_pair['value'] ) ) {
								printf(
									'<a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
									esc_url( $ss_pair['value'] ),
									esc_html( $ss_pair['value'] )
								);
							} else {
								echo esc_html( $ss_pair['value'] );
							}
							?>
						</dd>
					<?php endforeach; ?>
				</dl>
			</div>
		<?php endif; ?>

		<?php if ( answyr_is_pro() && class_exists( 'Answyr_Desk_AI' ) ) : ?>
			<div class="answyr-ai-tools">
				<button type="button" class="button" id="answyr-ai-triage"><span class="dashicons dashicons-tag" aria-hidden="true"></span> <?php esc_html_e( 'Suggest triage (AI)', 'answyr' ); ?></button>
				<button type="button" class="button" id="answyr-ai-summarize"><span class="dashicons dashicons-media-text" aria-hidden="true"></span> <?php esc_html_e( 'Summarize thread (AI)', 'answyr' ); ?></button>
				<?php if ( in_array( $ticket->status, array( 'resolved', 'closed' ), true ) ) : ?>
					<button type="button" class="button" id="answyr-ai-kb"><span class="dashicons dashicons-book" aria-hidden="true"></span> <?php esc_html_e( 'Turn into help article', 'answyr' ); ?></button>
				<?php endif; ?>
				<span id="answyr-ai-status" class="description"></span>
				<div id="answyr-ai-triage-result" class="answyr-ai-result" hidden></div>
				<div id="answyr-ai-summary" class="answyr-ai-result" hidden></div>
			</div>
		<?php endif; ?>

		<?php
		// Render a message body, promoting an attached chat transcript ("Customer:" /
		// "Assistant:" lines under the "— chat transcript —" marker) into styled turns
		// so the two sides read distinctly. Falls back to the plain body otherwise.
		$ss_cust_label  = __( 'Customer', 'answyr' );
		$ss_marker      = '— chat transcript —';
		$ss_render_body = static function ( string $body ) use ( $ss_marker, $ss_cust_label ) {
			$pos = strpos( $body, $ss_marker );
			if ( false === $pos ) {
				return wp_kses_post( wpautop( $body ) );
			}
			$intro      = trim( substr( $body, 0, $pos ) );
			$transcript = trim( substr( $body, $pos + strlen( $ss_marker ) ) );
			$html       = '' !== $intro ? wp_kses_post( wpautop( $intro ) ) : '';
			$turns      = array();
			foreach ( preg_split( '/\r?\n/', $transcript ) as $line ) {
				if ( '' === trim( $line ) ) {
					continue;
				}
				if ( preg_match( '/^([^:]{1,40}):\s?(.*)$/', $line, $m ) ) {
					$turns[] = array(
						'who'  => trim( $m[1] ),
						'role' => trim( $m[1] ) === $ss_cust_label ? 'customer' : 'agent',
						'text' => $m[2],
					);
				} elseif ( $turns ) {
					// Continuation of the previous speaker's message.
					$turns[ count( $turns ) - 1 ]['text'] .= "\n" . $line;
				}
			}
			if ( $turns ) {
				$html .= '<div class="answyr-transcript">';
				foreach ( $turns as $turn ) {
					$html .= sprintf(
						'<div class="answyr-transcript-turn is-%1$s"><span class="answyr-transcript-who">%2$s</span><span class="answyr-transcript-text">%3$s</span></div>',
						esc_attr( $turn['role'] ),
						esc_html( $turn['who'] ),
						nl2br( esc_html( trim( $turn['text'] ) ) )
					);
				}
				$html .= '</div>';
			}
			return $html;
		};
	?>
		<div class="answyr-thread">
			<?php foreach ( $thread as $msg ) : ?>
				<?php
				if ( 1 === (int) $msg->is_draft ) :
					continue;
endif;
				$is_note = 'note' === $msg->author_type;
				if ( $is_note ) {
					$ss_name = __( 'Internal note', 'answyr' );
					$ss_init = '✎';
				} elseif ( 'agent' === $msg->author_type ) {
					$ss_name = __( 'Agent', 'answyr' );
					$ss_init = strtoupper( mb_substr( $ss_name, 0, 1 ) );
				} else {
					$ss_name = $ticket->customer_name ? $ticket->customer_name : $ticket->customer_email;
					$ss_init = strtoupper( mb_substr( trim( (string) $ss_name ), 0, 1 ) );
				}
				?>
				<div class="answyr-msg answyr-msg-<?php echo esc_attr( $msg->author_type ); ?>">
					<div class="answyr-avatar" aria-hidden="true"><?php echo esc_html( $ss_init ); ?></div>
					<div class="answyr-bubble">
						<div class="answyr-msg-meta"><?php echo esc_html( $ss_name ); ?> · <?php echo esc_html( $msg->created_at ); ?></div>
						<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $ss_render_body builds output from esc_html/esc_attr/wp_kses_post. ?>
						<div class="answyr-msg-body"><?php echo $ss_render_body( (string) $msg->body ); ?></div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>

		<?php
		// Attachments (Pro) — premium-only; guarded so the free build has no panel.
		if ( answyr_is_pro() && class_exists( 'Answyr_Attachments' ) ) :
			$ss_atts = Answyr_Attachments::for_ticket( $ticket_id );
			?>
			<div class="answyr-attachments" id="answyr-attachments" data-ticket="<?php echo (int) $ticket_id; ?>">
				<h2><span class="dashicons dashicons-paperclip" aria-hidden="true"></span><?php esc_html_e( 'Attachments', 'answyr' ); ?></h2>
				<ul class="answyr-attach-list">
					<?php foreach ( $ss_atts as $ss_att ) : ?>
						<li>
							<a href="<?php echo esc_url( Answyr_Attachments::download_url( $ss_att ) ); ?>"><?php echo esc_html( $ss_att->orig_name ); ?></a>
							<span class="answyr-attach-size"><?php echo esc_html( Answyr_Attachments::human_size( (int) $ss_att->size ) ); ?></span>
						</li>
					<?php endforeach; ?>
				</ul>
				<p class="answyr-attach-add">
					<input type="file" id="answyr-attach-file">
					<span id="answyr-attach-status" class="description"></span>
				</p>
			</div>
		<?php endif; ?>

		<?php $ss_canned = (array) Answyr_Settings::get( 'canned', array() ); ?>
		<div class="answyr-compose">
			<h2><?php esc_html_e( 'Reply', 'answyr' ); ?></h2>
			<p class="answyr-compose-tools">
				<button type="button" class="button button-secondary" id="answyr-suggest"><?php esc_html_e( 'Suggest reply (AI)', 'answyr' ); ?></button>
				<?php if ( answyr_is_pro() && class_exists( 'Answyr_Desk_AI' ) ) : ?>
					<button type="button" class="button button-secondary" id="answyr-polish"><span class="dashicons dashicons-admin-customizer" aria-hidden="true"></span> <?php esc_html_e( 'Polish reply (AI)', 'answyr' ); ?></button>
				<?php endif; ?>
				<?php if ( $ss_canned ) : ?>
					<select id="answyr-canned" aria-label="<?php esc_attr_e( 'Insert a saved reply', 'answyr' ); ?>">
						<option value=""><?php esc_html_e( 'Insert saved reply…', 'answyr' ); ?></option>
						<?php foreach ( $ss_canned as $ss_i => $ss_snip ) : ?>
							<option value="<?php echo esc_attr( (string) $ss_i ); ?>"><?php echo esc_html( $ss_snip['title'] ?? '' ); ?></option>
						<?php endforeach; ?>
					</select>
				<?php endif; ?>
				<?php
				// Macros (Pro) — premium-only; renders a picker when macros exist.
				if ( answyr_is_pro() && class_exists( 'Answyr_Macros' ) ) {
					Answyr_Macros::render_composer_control();
				}
				?>
				<span id="answyr-suggest-status" class="description"></span>
			</p>
			<div id="answyr-sources" class="answyr-sources" hidden>
				<strong><?php esc_html_e( 'Sources used:', 'answyr' ); ?></strong>
				<ul></ul>
			</div>
			<?php
			// The reply box is the full WordPress editor (Visual/Text). The underlying
			// textarea keeps its id so the draft-and-vet JS can read/fill it in either mode.
			wp_editor(
				'',
				'answyr-reply-body',
				array(
					'media_buttons' => false,
					'textarea_rows' => 10,
					'teeny'         => true,
					'quicktags'     => true,
					'tinymce'       => array(
						'toolbar1' => 'bold,italic,bullist,numlist,link,undo,redo',
						'toolbar2' => '',
					),
				)
			);
			?>
			<p>
				<button type="button" class="button button-primary" id="answyr-send"><?php esc_html_e( 'Send reply', 'answyr' ); ?></button>
				<span id="answyr-send-status" class="description"></span>
			</p>
			<p class="description answyr-vet-note"><span class="dashicons dashicons-shield" aria-hidden="true"></span><?php esc_html_e( 'Drafts are AI-generated and grounded in your published docs. You are the one who sends. Tip: ⌘/Ctrl+Enter sends.', 'answyr' ); ?></p>
		</div>

		<div class="answyr-compose answyr-note-box">
			<h2><?php esc_html_e( 'Internal note', 'answyr' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Visible only to your team — never emailed or shown to the customer.', 'answyr' ); ?></p>
			<textarea id="answyr-note-body" rows="3" class="large-text" placeholder="<?php esc_attr_e( 'Leave a note for your team…', 'answyr' ); ?>"></textarea>
			<p>
				<button type="button" class="button" id="answyr-note-add"><?php esc_html_e( 'Add note', 'answyr' ); ?></button>
				<span id="answyr-note-status" class="description"></span>
			</p>
		</div>

		<?php if ( $ss_canned ) : ?>
			<script type="application/json" id="answyr-canned-data"><?php echo wp_json_encode( array_values( wp_list_pluck( $ss_canned, 'body' ) ) ); ?></script>
		<?php endif; ?>
	</div>
	<?php
	return;
}

// ── List view ───────────────────────────────────────────────────────────────────
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only admin filters (status/search/paged) from GET.
$ss_statuses   = Answyr_Desk_Store::statuses();
$ss_priorities = Answyr_Desk_Store::priorities();
$ss_status     = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : 'open';
if ( 'all' !== $ss_status && ! Answyr_Desk_Store::is_status( $ss_status ) ) {
	$ss_status = 'open';
}
$ss_priority = isset( $_GET['priority'] ) ? sanitize_key( wp_unslash( $_GET['priority'] ) ) : '';
if ( '' !== $ss_priority && 'all' !== $ss_priority && ! Answyr_Desk_Store::is_priority( $ss_priority ) ) {
	$ss_priority = '';
}
$ss_tag      = isset( $_GET['tag'] ) ? Answyr_Desk_Store::tag_slug( wp_unslash( $_GET['tag'] ) ) : '';
$ss_search   = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
$ss_per_page = 25;
$ss_paged    = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1;
// phpcs:enable WordPress.Security.NonceVerification.Recommended

// Filters shared across count + tabs (status varies per tab, the rest are sticky).
$ss_filter = array(
	'search'   => $ss_search,
	'priority' => $ss_priority,
	'tag'      => $ss_tag,
);

$ss_total   = Answyr_Desk_Store::count_tickets( array( 'status' => $ss_status ) + $ss_filter );
$ss_pages   = max( 1, (int) ceil( $ss_total / $ss_per_page ) );
$ss_paged   = min( $ss_paged, $ss_pages );
$tickets    = Answyr_Desk_Store::query_tickets(
	array(
		'status' => $ss_status,
		'limit'  => $ss_per_page,
		'offset' => ( $ss_paged - 1 ) * $ss_per_page,
	) + $ss_filter
);
$ss_base    = admin_url( 'admin.php?page=answyr' );
$ss_tab_url = static function ( $status ) use ( $ss_base, $ss_search, $ss_priority, $ss_tag ) {
	$args = array( 'status' => $status );
	if ( '' !== $ss_search ) {
		$args['s'] = $ss_search;
	}
	if ( '' !== $ss_priority ) {
		$args['priority'] = $ss_priority;
	}
	if ( '' !== $ss_tag ) {
		$args['tag'] = $ss_tag;
	}
	return add_query_arg( $args, $ss_base );
};
?>
<div class="wrap answyr-desk-inbox">
	<div class="answyr-inbox-head">
		<?php Answyr_Plugin::admin_header( __( 'Tickets', 'answyr' ) ); ?>
		<span class="answyr-count"><?php echo (int) $ss_total; ?></span>
	</div>

	<ul class="subsubsub answyr-status-tabs">
		<?php
		$ss_tabs = array( 'all' => __( 'All', 'answyr' ) ) + $ss_statuses;
		$ss_i    = 0;
		$ss_last = count( $ss_tabs ) - 1;
		foreach ( $ss_tabs as $ss_key => $ss_label ) :
			$ss_n = Answyr_Desk_Store::count_tickets( array( 'status' => $ss_key ) + $ss_filter );
			?>
			<li>
				<a href="<?php echo esc_url( $ss_tab_url( $ss_key ) ); ?>" <?php echo $ss_key === $ss_status ? 'class="current" aria-current="page"' : ''; ?>>
					<?php echo esc_html( $ss_label ); ?> <span class="count">(<?php echo (int) $ss_n; ?>)</span>
				</a><?php echo $ss_i < $ss_last ? ' |' : ''; ?>
			</li>
			<?php
			++$ss_i;
		endforeach;
		?>
	</ul>

	<form method="get" class="answyr-inbox-filters">
		<input type="hidden" name="page" value="answyr">
		<input type="hidden" name="status" value="<?php echo esc_attr( $ss_status ); ?>">
		<?php if ( '' !== $ss_tag ) : ?>
			<input type="hidden" name="tag" value="<?php echo esc_attr( $ss_tag ); ?>">
		<?php endif; ?>
		<select name="priority" onchange="this.form.submit()" aria-label="<?php esc_attr_e( 'Filter by priority', 'answyr' ); ?>">
			<option value=""><?php esc_html_e( 'Any priority', 'answyr' ); ?></option>
			<?php foreach ( $ss_priorities as $ss_pk => $ss_pl ) : ?>
				<option value="<?php echo esc_attr( $ss_pk ); ?>" <?php selected( $ss_pk, $ss_priority ); ?>><?php echo esc_html( $ss_pl ); ?></option>
			<?php endforeach; ?>
		</select>
		<span class="search-box">
			<label class="screen-reader-text" for="answyr-ticket-search"><?php esc_html_e( 'Search tickets', 'answyr' ); ?></label>
			<input type="search" id="answyr-ticket-search" name="s" value="<?php echo esc_attr( $ss_search ); ?>" placeholder="<?php esc_attr_e( 'Search subject, name, email…', 'answyr' ); ?>">
			<?php submit_button( __( 'Search', 'answyr' ), 'button', '', false ); ?>
		</span>
		<?php if ( '' !== $ss_tag ) : ?>
			<a class="answyr-tag-active" href="<?php echo esc_url( remove_query_arg( 'tag', $ss_tab_url( $ss_status ) ) ); ?>">#<?php echo esc_html( $ss_tag ); ?> &times;</a>
		<?php endif; ?>
	</form>

	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'ID', 'answyr' ); ?></th>
				<th><?php esc_html_e( 'Subject', 'answyr' ); ?></th>
				<th><?php esc_html_e( 'Customer', 'answyr' ); ?></th>
				<th><?php esc_html_e( 'Priority', 'answyr' ); ?></th>
				<th><?php esc_html_e( 'Status', 'answyr' ); ?></th>
				<th><?php esc_html_e( 'Updated', 'answyr' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $tickets ) ) : ?>
				<tr><td colspan="6" class="answyr-empty">
					<span class="answyr-empty-icon"><span class="dashicons dashicons-tickets-alt" aria-hidden="true"></span></span>
					<?php
					echo '' !== $ss_search
						? esc_html__( 'No tickets match your search.', 'answyr' )
						: esc_html__( 'No tickets here. New requests from your intake form and chat will land in Open.', 'answyr' );
					?>
				</td></tr>
			<?php else : ?>
				<?php $ss_wait_map = Answyr_Desk_Store::waiting_on_map( wp_list_pluck( $tickets, 'id' ) ); ?>
				<?php foreach ( $tickets as $t ) : ?>
					<?php
					$url      = admin_url( 'admin.php?page=answyr&ticket=' . (int) $t->id );
					$ss_stamp = strtotime( (string) $t->updated_at );
					$ss_rtags = Answyr_Desk_Store::tags_of( $t );
					?>
					<tr>
						<td class="answyr-id">#<?php echo (int) $t->id; ?></td>
						<td>
							<a class="answyr-subject" href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $t->subject ); ?></a>
							<?php if ( $ss_rtags ) : ?>
								<span class="answyr-row-tags">
									<?php foreach ( $ss_rtags as $ss_rtag ) : ?>
										<a class="answyr-chip" href="<?php echo esc_url( add_query_arg( 'tag', $ss_rtag, $ss_base ) ); ?>">#<?php echo esc_html( $ss_rtag ); ?></a>
									<?php endforeach; ?>
								</span>
							<?php endif; ?>
						</td>
						<td class="answyr-customer"><?php echo esc_html( $t->customer_email ); ?></td>
						<td><span class="answyr-pri answyr-pri-<?php echo esc_attr( $t->priority ); ?>"><?php echo esc_html( $ss_priorities[ $t->priority ] ?? $t->priority ); ?></span></td>
						<td>
							<span class="answyr-pill answyr-pill-<?php echo esc_attr( $t->status ); ?>"><?php echo esc_html( $ss_statuses[ $t->status ] ?? $t->status ); ?></span>
							<?php
							$ss_row_wait = in_array( $t->status, array( 'resolved', 'closed' ), true )
								? ''
								: ( $ss_wait_map[ (int) $t->id ] ?? '' );
							?>
							<?php if ( 'agent' === $ss_row_wait ) : ?>
								<span class="answyr-wait answyr-wait-agent"><?php esc_html_e( 'Needs reply', 'answyr' ); ?></span>
							<?php elseif ( 'customer' === $ss_row_wait ) : ?>
								<span class="answyr-wait answyr-wait-customer"><?php esc_html_e( 'Awaiting customer', 'answyr' ); ?></span>
							<?php endif; ?>
						</td>
						<td title="<?php echo esc_attr( $t->updated_at ); ?>">
							<?php
							echo $ss_stamp
								/* translators: %s: human-readable time difference, e.g. "2 hours". */
								? esc_html( sprintf( __( '%s ago', 'answyr' ), human_time_diff( $ss_stamp ) ) )
								: esc_html( (string) $t->updated_at );
							?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>

	<?php if ( $ss_pages > 1 ) : ?>
		<div class="tablenav"><div class="tablenav-pages">
			<?php
			echo wp_kses_post(
				paginate_links(
					array(
						'base'      => add_query_arg( 'paged', '%#%' ),
						'format'    => '',
						'current'   => $ss_paged,
						'total'     => $ss_pages,
						'prev_text' => '‹',
						'next_text' => '›',
					)
				)
			);
			?>
		</div></div>
	<?php endif; ?>
</div>
