<?php
$default_deep_link = stripslashes( eh_crm_get_settingsmeta( '0', 'default_deep_link' ) );
if ( $default_deep_link && ! isset( $_GET['filter'] ) && ! isset( $_GET['tid'] ) ) {
	$queries         = [];
	$queries['page'] = 'wsdesk_tickets';
	parse_str( $default_deep_link, $queries );

	$queries = array_merge( array( 'page' => 'wsdesk_tickets' ), $queries );

	$url = add_query_arg( $queries, admin_url( 'admin.php' ) );

	?>
	<script>
		const [url] = <?php echo wp_json_encode( [ $url ] ); ?>;
		window.location = url;
</script>
<?php
	die;
}
echo '<div class="wsdesk_wrapper">';
$plugin_name = 'wsdesk';
$tab_head    = '';
$tab_content = '';
$t_id        = ( isset( $_GET['tid'] ) ? sanitize_text_field( $_GET['tid'] ) : false );
if ( $t_id ) {
	$data = eh_crm_get_ticket(
		array(
			'ticket_id'     => $t_id,
			'ticket_parent' => 0,
		)
	);
	if ( $data ) {
		$all_section_ids = eh_crm_get_ticket_value_count( 'ticket_parent', 0, false, '', '', 'ticket_updated', 'DESC', '', 0 );
		$pagination_ids  = array();
		foreach ( $all_section_ids as $tic ) {
			array_push( $pagination_ids, $tic['ticket_id'] );
		}
		$tab_head    = '<li role="presentation" class="active visible_tab" id="tab_' . $t_id . '" style="min-width:200px;">' . CRM_Ajax::eh_crm_ticket_single_view_gen_head( $t_id ) . '</li>';
		$tab_content = '<div class="tab-pane new-style-tab-pane active direct_load_tid" id="tab_content_' . $t_id . '">' . CRM_Ajax::eh_crm_ticket_single_view_gen( $t_id, $pagination_ids ) . '</div>';
	} else {
		?>
		<div class="row" id="alert_for_activation">
			<div class="col-md-12">
				<div class="alert alert-danger aw_wsdesk_activate_alert" role="alert">
		<?php esc_html_e( 'Invalid Ticket Number', 'wsdesk' ); ?> [ <b>#<?php echo esc_html( $t_id ); ?></b> ]
				</div>
			</div>
		</div>
		<?php
	}
}
?>
<div class="container wrapper" id="tickets_page_view">
	<div class="row">
		<div class="col-md-12">
			<div class="panel with-nav-tabs panel-default">
				<div class="panel-heading col-md-12">
					<ul class="nav nav-tabs col-md-11 elaborate" role="tablist" style="margin-left: 0em;">
						<li class="<?php echo ( '' === $tab_head ) ? 'active' : ''; ?> all_tickets" role="presentation">
							<a id="all_tickets_tab_header" onclick=" ('tickets')" href="#all_tickets_tab" aria-controls="all" style="text-align: center;padding: 13px 15px;margin-right:0px !important;" data-toggle="tab" class="tab_a">
								<?php esc_html_e( 'All Tickets', 'wsdesk' ); ?>
							</a>
						</li>
						<?php echo wp_kses_post( $tab_head ); ?>
						<li role="presentation" class="dropdown" style="display: none;">
							<a href="#" id="myTabDrop1" class="dropdown-toggle tab_a" data-toggle="dropdown" aria-controls="myTabDrop1-contents" aria-expanded="true" style="text-align: center;padding: 13px 15px;margin-right:0px !important;">
								<span class="caret"></span> <?php esc_html_e( 'More', 'wsdesk' ); ?></a>
							<ul class="dropdown-menu collapse_ul" aria-labelledby="myTabDrop1" id="myTabDrop1-contents"style="overflow-x: hidden">
							</ul>
						</li>
					</ul>
					<div class="col-md-1 nav-tabs">
						<div class="pull-right side_bar">
							<a href="#" class="add-ticket tab_a" style="text-align: center;" data-placement="bottom" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'New Ticket', 'wsdesk' ); ?>" data-container="body">
								<span class="glyphicon glyphicon-plus"></span>
							</a>
						</div>
						<div class="pull-right ">
							<div class="input-group stylish-input-group" style="margin: 3px 5px;">
								<input type="text" class="form-control" id="search_ticket_input"  placeholder="Search" data-placement="bottom" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Search Ticket', 'wsdesk' ); ?>" data-container="body">
								<span class="glyphicon glyphicon-search clickable form-control-feedback" id="search_ticket_icon"></span>
							</div>
						</div>
					</div>
				</div>
				<div class="panel-body">
					<div class="tab-content">
						<div class="alert alert-success" style="z-index: 1100;display: none;" role="alert">
							<div id="success_alert_text"></div>
						</div>
						<div class="alert alert-warning" style="z-index: 1100;display: none;" role="alert">
							<div id="warning_alert_text"></div>
						</div>
						<div class="tab-pane <?php echo ( '' === $tab_content ) ? 'active' : ''; ?>" id="all_tickets_tab">
							<?php require EH_CRM_MAIN_VIEWS . 'tickets/crm_tickets_v2_all.php'; ?>
						</div>
						<?php echo wp_kses_post( $tab_content ); ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</div>
