<?php
$time = microtime( true );
require_once EH_CRM_MAIN_PATH . '/vendor/autoload.php';

use Illuminate\Support\Arr;
use WSDesk\Settings\SettingsRepository;


if ( ! defined( 'ABSPATH' ) ) {
	 exit;
}
ob_start();
$settings = new SettingsRepository();

$avail_labels_wf = $settings->getLabels();
$avail_labels_f  = $settings->getFilterableLabels();

$avail_tags_wf = $settings->getTags();
$avail_tags_f  = $settings->getFilterableTags();

$avail_templates = $settings->getTemplates();
$avail_fields    = $settings->getFields();

$tickets_display = eh_crm_get_settingsmeta( '0', 'tickets_display' );
if ( ! $avail_templates ) {
$avail_templates = array();
}
$user_roles_default = array( 'WSDesk_Agents', 'WSDesk_Supervisor', 'administrator' );
$user_caps_default  = array( 'reply_tickets', 'delete_tickets', 'manage_tickets' );
$users_data         = get_users( array( 'role__in' => $user_roles_default ) );

$users_data = array_map(
	function ( $user ) {
	return array(
		'id'    => $user->ID,
		'name'  => $user->display_name,
		'caps'  => $user->caps,
		'email' => $user->user_email,
	);
	},
	$users_data
);
$users_data = array_key_by( $users_data, 'id' );

$table_title = 'Archived Tickets';
$ticket_rows = eh_crm_get_settingsmeta( 0, 'ticket_rows' );
if ( '' == $ticket_rows ) {
	 $ticket_rows = 25;
}
$current_page = Arr::get( $_GET, 'page_no', 0 );

$offset              = ( $current_page ) * $ticket_rows;
$section_tickets_id  = eh_crm_get_ticket_value_count_archive( 'ticket_parent', 0, false, '', '', 'ticket_updated', 'DESC', $ticket_rows, $offset );
$archived_ticket_ids = Arr::pluck( $section_tickets_id, 'ticket_id' );
$archived_tickets    = eh_crm_get_ticket_archive( [ 'ticket_id' => $archived_ticket_ids ] );
$archived_tickets    = false === $archived_tickets ? array() : $archived_tickets;
$archived_tickets    = array_key_by( $archived_tickets, 'ticket_id' );

$archived_tickets_meta = eh_crm_get_ticketmeta_archive( $archived_ticket_ids );
//dd($archived_tickets_meta);
$total_no_of_tickets = wpElexFluent()->table( 'wsdesk_archived_tickets' )->where( 'ticket_parent', 0 )->count();

$total_no_of_voices = wpElexFluent()->table( 'wsdesk_archived_tickets' )
							->where( 'ticket_trash', 0 )
							->whereIn( 'ticket_parent', $archived_ticket_ids )
							->select( \DB::raw( 'ticket_parent, count(ticket_id) as count, ticket_category' ) )
							->groupBy( [ 'ticket_category', 'ticket_parent' ] )
							->get();
$total_no_of_voices = array_map(
	function ( $status ) {
	return array(
		'ticket_id'                => $status['ticket_parent'],
		$status['ticket_category'] => $status['count'],
	);
	},
	$total_no_of_voices
);
$total_no_of_voices = array_group_by( $total_no_of_voices, 'ticket_id' );

$total_no_of_voices = array_map(
	function ( $replies ) {
	return Arr::collapse( $replies );
	},
	$total_no_of_voices
);

$eye_colors = wpElexFluent()->table( 'wsdesk_settingsmeta' )
			->whereIn( 'settings_id', Arr::pluck( $avail_labels_wf, 'settings_id' ) )
			->where( 'meta_key', 'label_color' )
			->get();

$eye_colors = Arr::pluck( $eye_colors, 'meta_value', 'settings_id' );

foreach ( $avail_labels_wf as $i => $label ) {
	 $avail_labels_wf[ $i ]['eye_color'] = Arr::get( $eye_colors, $label['settings_id'], '' );
}

//$all_section_ids = eh_crm_get_ticket_value_count_archive("ticket_parent",0,false,"","","ticket_updated","DESC","",0);
$pagination_ids = array();
//foreach ($all_section_ids as $tic) {
//    array_push($pagination_ids,$tic['ticket_id']);
//}
$avail_caps       = array( 'reply_tickets', 'delete_tickets', 'manage_tickets', 'manage_templates' );
$access           = array();
$logged_user      = wp_get_current_user();
$logged_user_caps = array_keys( $logged_user->caps );
if ( ! in_array( 'administrator', $logged_user->roles ) ) {
	for ( $i = 0;$i < count( $logged_user_caps );$i++ ) {
		if ( ! in_array( $logged_user_caps[ $i ], $avail_caps ) ) {
			unset( $logged_user_caps[ $i ] );
		}
	}
	 $access = $logged_user_caps;
} else {
	 $access = $avail_caps;
}
if ( ! isset( $_COOKIE['collapsed_views'] ) ) {
	 $collapsed_views = array();
} else {
	 $collapsed_views = stripslashes( sanitize_text_field( $_COOKIE['collapsed_views'] ) );
	 $collapsed_views = str_replace( '"', '', $collapsed_views );
	 $collapsed_views = str_replace( '[', '', $collapsed_views );
	 $collapsed_views = str_replace( ']', '', $collapsed_views );
	 $collapsed_views = explode( ',', $collapsed_views );
}

$settings_meta_data     = [];
$all_ticket_field_views = eh_crm_get_settingsmeta( '0', 'all_ticket_page_columns' );
$custom_table_headers   = array();
$default_columns        = array( 'id', 'requestor', 'subject', 'requested', 'assignee', 'feedback' );
if ( false === $all_ticket_field_views ) {
	 $all_ticket_field_views = $default_columns;
	 eh_crm_update_settingsmeta( '0', 'all_ticket_page_columns', $default_columns );
}
if ( ! empty( $all_ticket_field_views ) ) {
	foreach ( $all_ticket_field_views as  $all_ticket_field ) {
		if ( in_array( $all_ticket_field, $default_columns ) ) {
			switch ( $all_ticket_field ) {
				case 'id':
					array_push( $custom_table_headers, '<div class="row" style="margin-left: 0px; "># <span class="dashicons dashicons-sort sort-icon" id="id" style="margin-left: 5px;"></span></div>' );
					break;
				case 'subject':
					array_push( $custom_table_headers, '<div class="row">' . ucfirst( $all_ticket_field ) . '<span class="dashicons dashicons-sort sort-icon" id="subject" style="margin-left: 5px"></span></div>' );
					break;
				default:
					array_push( $custom_table_headers, ucfirst( $all_ticket_field ) );
			}
		} else {
			$fields = eh_crm_get_settings( array( 'slug' => $all_ticket_field ), 'title' );
			if ( isset( $fields[0] ) ) {
				array_push( $custom_table_headers, $fields[0]['title'] );
			}
		}
	}
}
?>

<div class="container">
	<div class="row" style="margin-top: 10px;">
		<div class="col-md-9" style="padding-right:0px;">
			<div class="full_row filter_div" id="dev-table-action-bar" >
				<div class="filter-each"><input type="checkbox" class="ticket_select_all"></div>
				<div class="filter-each" id="refresh_tickets" style="cursor:pointer;">
					<div  class="ticket-refresh-button" data-placement="top" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Refresh', 'wsdesk' ); ?>">
						<span class="glyphicon glyphicon-refresh"></span>
					</div>
				</div>
				<?php
				if ( in_array( 'delete_tickets', $access ) ) {
					echo '<div class="filter-each ticket-delete-btn multiple_ticket_unarchive_action" id="delete_tickets" style="display: none;"><div  class="multiple_ticket_unarchive_action ticket-delete-button" data-placement="top" data-toggle="wsdesk_tooltip" title="' . esc_html__( 'Restore the ticket(s)', 'wsdesk' ) . '"><span class="glyphicon glyphicon-folder-open"></span></div></div>';
				}
				?>

			</div>
		</div>
		<div id="update_template_model_display"></div>
		<div id="preview_template_model_display"></div>
		<div class="col-md-9" id="right_bar_all_tickets" style="overflow: scroll;padding-right: 0px;width:1250px;">
			<input type="hidden" id="pagination_ids_traverse" value="<?php echo esc_html( json_encode( $pagination_ids ) ); ?>">
			<div class="panel panel-default tickets_panel" style="background-color: white;color: black">
				<div class="panel-heading" style="background-color:white;color:black">
					<h3 class="panel-title"><?php esc_html_e( $table_title, 'wsdesk' ); ?>
						<span class="spinner_loader table_loader">
							<span class="bounce1"></span>
							<span class="bounce2"></span>
							<span class="bounce3"></span>
						</span>
					</h3>
					<div class="pull-right">
						<span class="clickable filter" data-toggle="wsdesk_tooltip" title="<?php esc_html_e( 'Tickets Filter', 'wsdesk' ); ?>" data-container="body">
							<i class="glyphicon glyphicon-filter"></i>
						</span>
					</div>
					<div class="pull-right" style="margin: -25px 0px 0px 0px;">
						<span class="text-muted"><b>
						<?php
						$page_number = $current_page;
echo esc_html( ( $current_page > 0 ) && ( $current_page * $ticket_rows ) <= $total_no_of_tickets ) ? esc_html( ( ( $current_page ) * $ticket_rows ) + 1 ) : esc_html( '1' );
						?>
						</b>–<b><?php echo esc_html( ( $current_page > 0 ) && ( $current_page * $ticket_rows ) <= $total_no_of_tickets ) ? esc_html( ( $current_page * $ticket_rows ) + count( $section_tickets_id ) ) : esc_html( $ticket_rows ); ?></b> of <b><?php echo esc_html( $total_no_of_tickets ); ?></b></span>
						<?php
						if ( $page_number >= 0 ) {
							$page_number = $current_page + 1;

							$current_page = $current_page;
						}
						?>
						<input type="number" oninput="this.value = !!this.value && Math.abs(this.value) >= 0 ? Math.abs(this.value) : null" min="0" name="cur" id="current_page_n" class="btn btn-default pagination_tic" placeholder="<?php esc_html_e( $page_number, 'wsdesk' ); ?>"min=1 title="<?php esc_html_e( 'Page Number', 'wsdesk' ); ?> "
						oninput="validity.valid||(value='');" style="width:65px;height:30px" />
						<div class="btn-group btn-group-sm" style="margin:1px 0px 0px 0px;">
							<?php
							if ( 0 != $current_page ) {
								?>
											<button type="button"  class="btn btn-default pagination_tickets" id="prev" title="<?php esc_html_e( 'Previous', 'wsdesk' ); ?> <?php echo esc_html( $ticket_rows ); ?>" data-container="body">
												<span class="glyphicon glyphicon-chevron-left"></span>
											</button>
										<?php
							}
							?>
							<?php
							if ( 0 == $current_page ) {
								?>
											<button type="button"  class="btn btn-default pagination_tick" id="pre"  title="<?php esc_html_e( 'Beginning of the Page', 'wsdesk' ); ?> "style="color:#AFAFAF; " data-container="body">
												<span class="glyphicon glyphicon-chevron-left"></span>
											</button>
										<?php
							}
							?>
							<input type="hidden" id="current_page_no" value="<?php echo esc_html( $current_page ); ?>">
							<?php
									//To Hide the preview and next buttons for first and lastpages of tickets
							if ( ( ( $current_page * $ticket_rows ) + count( $section_tickets_id ) ) != $total_no_of_tickets ) {
								?>
											<button type="button"  class="btn btn-default pagination_tickets" id="next"  title="<?php esc_html_e( 'Next', 'wsdesk' ); ?> <?php echo esc_html( $ticket_rows ); ?>" data-container="body">
												<span class="glyphicon glyphicon-chevron-right"></span>
											</button>
										<?php
							}
							?>
							<?php
							if ( ( ( $current_page * $ticket_rows ) + count( $section_tickets_id ) ) == $total_no_of_tickets ) {
								?>
											<button type="button"  class="btn btn-default pagination_tick" id="nex" title="<?php esc_html_e( 'End of the Page', 'wsdesk' ); ?> "style="color:#AFAFAF; " data-container="body">
												<span class="glyphicon glyphicon-chevron-right"></span>
											</button>
										<?php
							}
							?>
						</div>
					</div>
				</div>
				<div class="panel-body">
					<input type="text" class="form-control" id="dev-table-filter" data-action="filter" data-filters="#dev-table" placeholder="<?php esc_html_e( 'Filter Tickets', 'wsdesk' ); ?>" />
				</div>
				<table class="table table-hover" id="dev-table">
					<thead>
						<tr class="except_view">
							<th style="width: 1%;"></th>
							<th style="width: 2%;"><?php esc_html_e( 'View', 'wsdesk' ); ?></th>
							<?php
							foreach ( $custom_table_headers as  $value ) {
								echo '<th>' . esc_html( $value ) . '</th>';
							}
							?>
						</tr>
					</thead>
					<tbody>
						<?php
						if ( empty( $section_tickets_id ) ) {
							echo '<tr class="except_view">
                                    <td colspan="12"> ' . esc_html__( 'No Tickets', 'wsdesk' ) . ' </td></tr>';
						} else {
							for ( $i = 0;$i < count( $section_tickets_id );$i++ ) {
								//$current = eh_crm_get_ticket_archive(array("ticket_id"=>$section_tickets_id[$i]['ticket_id']));
								$current        = [ $archived_tickets[ $section_tickets_id[ $i ]['ticket_id'] ] ];
								$current_meta   = $archived_tickets_meta[ $section_tickets_id[ $i ]['ticket_id'] ];
								$action_value   = '';
								$assignee_value = '';
								$eye_color      = '';
								for ( $j = 0;$j < count( $avail_labels_wf );$j++ ) {
									if ( in_array( 'manage_tickets', $access ) ) {
										$action_value .= '<li id="' . $current[0]['ticket_id'] . '"><a href="#" class="single_ticket_action" id="' . $avail_labels_wf[ $j ]['slug'] . '">' . esc_html__( 'Mark as', 'wsdesk' ) . ' ' . $avail_labels_wf[ $j ]['title'] . '</a></li>';

									}
									if ( $avail_labels_wf[ $j ]['slug'] == $current_meta['ticket_label'] ) {
										$eye_color = $avail_labels_wf[ $j ]['eye_color'];
									}
								}
								foreach ( $users_data as $user ) {
									if ( in_array( 'manage_tickets', $access ) ) {
										$assignee_value .= '<li id="' . $current[0]['ticket_id'] . '"><a href="#" class="single_ticket_assignee" id="' . $user['id'] . '">' . $user['name'] . '</a></li>';
									}
								}
								$ticket_raiser       = $current[0]['ticket_email'];
								$ticket_raiser_email = $current[0]['ticket_email'];
								if ( 0 != $current[0]['ticket_author'] ) {
									$wcurrent_user = new WP_User( $current[0]['ticket_author'] );
									$ticket_raiser = $wcurrent_user->display_name;
								}
								$ticket_assignee_name  = array();
								$ticket_assignee_email = array();
								if ( isset( $current_meta['ticket_assignee'] ) ) {
									$current_assignee = array_values( $current_meta['ticket_assignee'] );
									foreach ( $current_assignee as $assignee_id ) {
										if ( isset( $users_data[ $assignee_id ] ) ) {
											$ticket_assignee_name[]  = $users_data[ $assignee_id ]['name'];
											$ticket_assignee_email[] = $users_data[ $assignee_id ]['email'];
										}
									}
								}
								$ticket_assignee_name = empty( $ticket_assignee_name ) ? esc_html__( 'No Assignee', 'wsdesk' ) : implode( ', ', $ticket_assignee_name );
								$latest_reply_id      = '';
								//$latest_reply_id = eh_crm_get_ticket_value_count_archive("ticket_category","agent_note" ,true,"ticket_parent",$current[0]['ticket_id'],'ticket_id','DESC','1');
								$latest_content = array();
								$attach         = '';
								if ( ! empty( $latest_reply_id ) ) {
									$latest_ticket_reply            = eh_crm_get_ticket_archive( array( 'ticket_id' => $latest_reply_id[0]['ticket_id'] ) );
									$latest_content['content']      = $latest_ticket_reply[0]['ticket_content'];
									$latest_content['author_email'] = $latest_ticket_reply[0]['ticket_email'];
									$latest_content['reply_date']   = $latest_ticket_reply[0]['ticket_date'];
									if ( 0 != $latest_ticket_reply[0]['ticket_author'] ) {
										$reply_user                    = new WP_User( $latest_ticket_reply[0]['ticket_author'] );
										$latest_content['author_name'] = $reply_user->display_name;
									} else {
										$latest_content['author_name'] = esc_html__( 'Guest', 'wsdesk' );
									}
									$latest_reply_meta = eh_crm_get_ticketmeta_archive( $latest_reply_id[0]['ticket_id'] );
									if ( isset( $latest_reply_meta['ticket_attachment'] ) ) {
										$attach = ' | <small class="glyphicon glyphicon-pushpin"></small> <small style="opacity:0.7;"> ' . count( $latest_reply_meta['ticket_attachment'] ) . ' ' . esc_html__( 'Attachment', 'wsdesk' ) . '</small>';
									}
								} else {
									$latest_content['content']      = $current[0]['ticket_content'];
									$latest_content['author_email'] = $current[0]['ticket_email'];
									$latest_content['reply_date']   = $current[0]['ticket_date'];
									if ( 0 != $current[0]['ticket_author'] ) {
										$wcurrent_user                 = new WP_User( $current[0]['ticket_author'] );
										$latest_content['author_name'] = $wcurrent_user->display_name;
									} else {
										$latest_content['author_name'] = esc_html__( 'Guest', 'wsdesk' );
									}
									if ( isset( $current_meta['ticket_attachment'] ) ) {
										$attach = ' | <small class="glyphicon glyphicon-pushpin"></small> <small style="opacity:0.7;"> ' . count( $current_meta['ticket_attachment'] ) . ' ' . esc_html__( 'Attachment', 'wsdesk' ) . '</small>';
									}
								}
								$input_data                = ( 'text' != $tickets_display ) ? html_entity_decode( stripslashes( $latest_content['content'] ) ) : stripslashes( $latest_content['content'] );
								$input_array[0]            = '/<((html)[^>]*)>(.*)\<\/(html)>/Us';
								$input_array[1]            = '/<((head)[^>]*)>(.*)\<\/(head)>/Us';
								$input_array[2]            = '/<((style)[^>]*)>(.*)\<\/(style)>/Us';
								$input_array[3]            = '/<((body)[^>]*)>(.*)\<\/(body)>/Us';
								$input_array[4]            = '/<((form)[^>]*)>(.*)\<\/(form)>/Us';
								$input_array[5]            = '/<((input)[^>]*)>(.*)\<\/(input)>/Us';
								$input_array[6]            = '/<((input)[^>]*)>/Us';
								$input_array[7]            = '/<((button)[^>]*)>(.*)\<\/(button)>/Us';
								$input_array[8]            = '/<((iframe)[^>]*)>(.*)\<\/(iframe)>/Us';
								$input_array[9]            = '/<((script)[^>]*)>(.*)\<\/(script)>/Us';
								$input_array[10]           = '/<((ins)[^>]*)>(.*)\<\/(ins)>/Us';
								$output_array[0]           = '&lt;$1&gt;$3&lt;/html&gt;';
								$output_array[1]           = '&lt;$1&gt;$3&lt;/head&gt;';
								$output_array[2]           = '&lt;$1&gt;$3&lt;/style&gt;';
								$output_array[3]           = '&lt;$1&gt;$3&lt;/body&gt;';
								$output_array[4]           = '&lt;$1&gt;$3&lt;/form&gt;';
								$output_array[5]           = '&lt;$1&gt;$3&lt;/input&gt;';
								$output_array[6]           = '&lt;$1&gt;$3&lt;/input&gt;';
								$output_array[7]           = '&lt;$1&gt;$3&lt;/button&gt;';
								$output_array[8]           = '&lt;$1&gt;$3&lt;/iframe&gt;';
								$output_array[9]           = '&lt;$1&gt;$3&lt;/script&gt;';
								$output_array[10]          = '&lt;$1&gt;$3&lt;/ins&gt;';
								$latest_content['content'] = preg_replace( $input_array, $output_array, $input_data );
								$latest_content['content'] = str_replace( '<script>', '&lt;script&gt;', $latest_content['content'] );
								$ticket_tags               = '';
								if ( ! empty( $avail_tags_wf ) ) {
									for ( $j = 0;$j < count( $avail_tags_wf );$j++ ) {
										$current_ticket_tags = ( isset( $current_meta['ticket_tags'] ) ? array_values( $current_meta['ticket_tags'] ) : array() );
										for ( $k = 0;$k < count( $current_ticket_tags );$k++ ) {
											if ( $avail_tags_wf[ $j ]['slug'] == $current_ticket_tags[ $k ] ) {
												$ticket_tags .= '<span class="label label-info">#' . $avail_tags_wf[ $j ]['title'] . '</span>';
											}
										}
									}
								}
								if ( isset( $current_meta['ticket_rating'] ) ) {
									if ( 'great' == $current_meta['ticket_rating'] ) {
										$ticket_rating = '<span class="glyphicon glyphicon-thumbs-up" style="color: green"></span>';
									} else {
										$ticket_rating = '<span class="glyphicon glyphicon-thumbs-down" style="color: red"></span>';
									}
								} else {
									$ticket_rating = '<span class="glyphicon glyphicon-time"></span>';
								}
								$reply_count = Arr::get( $total_no_of_voices, $section_tickets_id[ $i ]['ticket_id'], [] );

								$raiser_voice = Arr::get( $reply_count, 'raiser_reply', 0 );
								$agent_voice  = Arr::get( $reply_count, 'agent_reply', 0 );
								echo '
                                    <tr data-time="' . esc_html( microtime( true ) - $time ) . '" class="clickable ticket_row" id="' . esc_attr( $current[0]['ticket_id'] ) . '">
                                        <td class="except_view"><input type="checkbox" class="ticket_select_t" id="ticket_select_t" value="' . esc_html( $current[0]['ticket_id'] ) . '"></td>
                                        <td class="except_view"><button class="btn btn-default btn-xs accordion-toggle quick_view_ticket" style="background-color: ' . esc_html( $eye_color ) . ' !important" data-toggle="collapse" data-target="#expand_' . esc_attr( $current[0]['ticket_id'] ) . '" ><span class="glyphicon glyphicon-eye-open"></span></button></td>';
								if ( ! empty( $all_ticket_field_views ) ) {
									foreach ( $all_ticket_field_views as  $all_ticket_field ) {
										switch ( $all_ticket_field ) {
											case 'id':
												echo '<td>' . esc_html( $current[0]['ticket_id'] ) . '</td>';
												break;
											case 'requestor':
												echo '<td>' . esc_html( $ticket_raiser ) . '</td>';
												break;
											case 'subject':
												echo '<td class="wrap_content" data-toggle="wsdesk_tooltip" title="' . esc_html( $current[0]['ticket_title'] ) . '" data-container="body">' . esc_html( $current[0]['ticket_title'] ) . '</td>';
												break;
											case 'requested':
												echo '<td>' . esc_html( eh_crm_get_formatted_date( $current[0]['ticket_date'] ) ) . '</td>';
												break;
											case 'assignee':
												echo '<td>' . esc_html( $ticket_assignee_name ) . '</td>';
												break;
											case 'feedback':
												echo '<td>' . esc_html( $ticket_rating ) . '</td>';
												break;
											default:
												if ( ! isset( $settings_meta_data[ $all_ticket_field ]['settings'] ) ) {
													$settings_meta_data[ $all_ticket_field ]['settings'] = eh_crm_get_settings( array( 'slug' => $all_ticket_field ), 'settings_id' );
												}
												$current_settings_id = Arr::get( $settings_meta_data, $all_ticket_field . '.settings', [] );
												if ( ! isset( $current_settings_id[0]['settings_id'] ) ) {
													break;
												}
												if ( ! isset( $settings_meta_data[ $all_ticket_field ]['meta'] ) ) {
													$settings_meta_data[ $all_ticket_field ]['meta'] = eh_crm_get_settingsmeta( $current_settings_id[0]['settings_id'] );
												}
												$current_settings_meta = Arr::get( $settings_meta_data, $all_ticket_field . '.meta', [] );
												if ( 'select' == $current_settings_meta['field_type'] ) {
													if ( 'woo_order_id' == $all_ticket_field ) {
													   $current_settings_meta['field_type'] = 'text';
													}
												}
												if ( 'file' != $current_settings_meta['field_type'] && 'google_captcha' != $current_settings_meta['field_type'] ) {
													switch ( $current_settings_meta['field_type'] ) {
														case 'select':
														case 'radio':
														case 'checkbox':
															$field_values = $current_settings_meta['field_values'];
															if ( isset( $current_meta[ $all_ticket_field ] ) ) {
																echo '<td>' . esc_html( $field_values[ $current_meta[ $all_ticket_field ] ] ) . '</td>';
															} else {
echo '<td>-</td>';
															}
															break;
														default:
															if ( isset( $current_meta[ $all_ticket_field ] ) ) {
																echo '<td>' . esc_html( $current_meta[ $all_ticket_field ] ) . '</td>';
															} else {
echo '<td>-</td>';
															}
															break;
													}
												}
												break;
										}
									}
								}
								echo '</tr>
                                    <tr class="except_view">
                                        <td colspan="12" class="hiddenRow">
                                            <div class="accordian-body collapse" id="expand_' . esc_html( $current[0]['ticket_id'] ) . '">
                                                <table class="table table-striped" style="margin-bottom: 0px !important">
                                                    <thead>
                                                        <tr>
                                                            <td colspan="12" style="white-space: normal;">
                                                            <div style="padding:5px 0px;">
                                                                <small class="glyphicon glyphicon-user"></small> <small style="opacity:0.7;">' . esc_html( $latest_content['author_name'] ) . '</small>
                                                                | <small class="glyphicon glyphicon-envelope"></small> <small style="opacity:0.7;">' . esc_html( $latest_content['author_email'] ) . '</small>
                                                                | <small class="glyphicon glyphicon-calendar"></small> <small style="opacity:0.7;">' . esc_html( eh_crm_get_formatted_date( $latest_content['reply_date'] ) ) . '</small>
                                                                ' . esc_html( $attach ) . '
                                                            </div>
                                                            <hr>
                                                            <p>
                                                                ' . esc_html( $latest_content['content'] ) . '
                                                            </p>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <th>' . esc_html__( 'Actions', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Assignee', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Reply Requester', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Raiser Voices', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Agent Voices', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Tags', 'wsdesk' ) . '</th>
                                                            <th>' . esc_html__( 'Source', 'wsdesk' ) . '</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td>
                                                                <div class="btn-group">
                                                                    <button type="button" class="btn btn-default dropdown-toggle single_ticket_action_button_' . esc_html( $current[0]['ticket_id'] ) . '" data-toggle="dropdown">
                                                                        ' . esc_html__( 'Actions', 'wsdesk' ) . ' <span class="caret"></span>
                                                                    </button>
                                                                    <ul class="dropdown-menu" role="menu">
                                                                        ' . ( ( '' != $action_value ) ? esc_html( $action_value ) : '<li style="padding: 3px 20px;">' . esc_html__( 'No Actions', 'wsdesk' ) . '</li>' ) . '
                                                                        <li class="divider"></li>
                                                                        <li class="text-center">
                                                                            <small class="text-muted">
                                                                                ' . esc_html__( 'Select label to assign', 'wsdesk' ) . '
                                                                            </small>
                                                                        </li>
                                                                    </ul>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div class="btn-group">
                                                                    <button type="button" class="btn btn-default dropdown-toggle single_ticket_assignee_button_' . esc_html( $current[0]['ticket_id'] ) . '" data-toggle="dropdown">
                                                                        ' . esc_html__( 'Assignee', 'wsdesk' ) . ' <span class="caret"></span>
                                                                    </button>
                                                                    <ul class="dropdown-menu" role="menu">
                                                                        ' . ( ( '' != $assignee_value ) ? esc_html( $assignee_value ) : '<li style="padding: 3px 20px;">' . esc_html__( 'No Assignee', 'wsdesk' ) . '</li>' ) . '
                                                                        <li class="divider"></li>
                                                                        <li class="text-center">
                                                                            <small class="text-muted">
                                                                                ' . esc_html__( 'Select assignee to assign', 'wsdesk' ) . '
                                                                            </small>
                                                                        </li>
                                                                    </ul>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <a href="#reply_' . esc_html( $current[0]['ticket_id'] ) . '" data-toggle="modal"  title="' . esc_html__( 'Compose Reply', 'wsdesk' ) . '">
                                                                    ' . esc_html( $current[0]['ticket_email'] ) . '
                                                                </a>
                                                            </td>
                                                            <td>' . esc_html( $raiser_voice ) . '</td>
                                                                <td>' . esc_html( $agent_voice ) . '</td>
                                                            <td>' . ( ( '' != $ticket_tags ) ? esc_html( $ticket_tags ) : esc_html__( 'No Tags', 'wsdesk' ) ) . '</td>
                                                            <td>' . ( ( isset( $current_meta['ticket_source'] ) ) ? esc_html( $current_meta['ticket_source'] ) : esc_html__( 'Unknown', 'wsdesk' ) ) . '</td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                                <!-- Modal -->
                                                <div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="reply_' . esc_attr( $current[0]['ticket_id'] ) . '" class="modal fade" style="display: none;">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                                                                <h4 class="modal-title">' . esc_html__( 'Ticket', 'wsdesk' ) . ' #' . esc_html( $current[0]['ticket_id'] ) . ' ' . esc_html__( 'Compose Reply', 'wsdesk' ) . '</h4>
                                                            </div>
                                                            <div class="modal-body">
                                                                <p style="margin-top: 5px;font-size: 16px;">
                                                                ';
								if ( in_array( 'manage_tickets', $access ) ) {
			echo '<input type="text" value="' . esc_html( $current[0]['ticket_title'] ) . '" id="direct_ticket_title_' . esc_html( $current[0]['ticket_id'] ) . '" class="ticket_title_editable">';
								} else {
				echo esc_html( $current[0]['ticket_title'] );
								}
								if ( in_array( 'reply_tickets', $access ) ) {
									?>
																	</p>
																	<div class="row" style="margin-bottom: 20px;">
																		<div class="col-md-12">
																			<div class="widget-area no-padding blank">
																				<div class="status-upload">
																					<div rows="10" cols="30" class="textarea form-control direct_reply_textarea" id="direct_reply_textarea_<?php echo esc_html( $current[0]['ticket_id'] ); ?>" name="reply_textarea_<?php echo esc_html( $current[0]['ticket_id'] ); ?>"></div>
																					<div class="form-group">
																						<div class="input-group col-md-12">
																							<span class="btn btn-primary fileinput-button">
																								<i class="glyphicon glyphicon-plus"></i>
																								<span><?php esc_html_e( 'Attachment', 'wsdesk' ); ?></span>
																								<input type="file" name="direct_files" id="direct_files_<?php echo esc_html( $current[0]['ticket_id'] ); ?>" class="direct_attachment_reply" multiple="">
																							</span>
																							<div class="btn-group pull-right">
																								<button type="button" class="btn btn-primary dropdown-toggle direct_ticket_reply_action_button_<?php echo esc_html( $current[0]['ticket_id'] ); ?>" data-toggle="dropdown" aria-haswsdesk-popup="true" aria-expanded="false">
												  <?php esc_html_e( 'Submit as', 'wsdesk' ); ?> <span class="caret"></span>
																								</button>
																								<ul class="dropdown-menu">
													<?php
													for ( $j = 0;$j < count( $avail_labels_wf );$j++ ) {
																								echo '<li id="' . esc_html( $current[0]['ticket_id'] ) . '"><a href="#" class="direct_ticket_reply_action" id="' . esc_html( $avail_labels_wf[ $j ]['slug'] ) . '">' . esc_html__( 'Submit as', 'wsdesk' ) . ' ' . esc_html( $avail_labels_wf[ $j ]['title'] ) . '</a></li>';
													}
													?>
																									<li role="separator" class="divider"></li>
																									<li id="<?php echo esc_html( $current[0]['ticket_id'] ); ?>"><a href="#" class="direct_ticket_reply_action" id="note"><?php esc_html_e( 'Submit as Note', 'wsdesk' ); ?></a></li>
																									<li class="text-center"><small class="text-muted"><?php esc_html_e( 'Notes visible to Agents and Supervisors', 'wsdesk' ); ?></small></li>
																								</ul>
																							  </div>
																						</div>
																						<div class="direct_upload_preview_files_<?php echo esc_html( $current[0]['ticket_id'] ); ?>"></div>
																					</div>
																				</div><!-- Status Upload  -->
																			</div><!-- Widget Area -->
																		</div>
																	</div>
																	<?php
								} else {
										echo '<p>' . esc_html__( "You don't Have permisson to Reply this ticket", 'wsdesk' ) . '</p>';
								}
														echo'
                                                        </div><!-- /.modal-content -->
                                                    </div><!-- /.modal-dialog -->
                                                </div><!-- /.modal -->
                                            </div>
                                        </td>
                                    </tr >
                                    ';
							}
						}
						?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
<script>deepview();</script>
<div id="wsdesk-template-wsdesk-popup" class="wsdesk-overlay">
	<div class="wsdesk-popup">
		<h4>Available Templates</h4>
		<a class="close" href="#">&times;</a>
		<div class="content">
			<div class="wsdesk-overlay-success" style="display: none;">
				<?php esc_html_e( 'Template Added !', 'wsdesk' ); ?>
			</div>
			<?php
			if ( ! empty( $avail_templates ) ) {
				for ( $i = 0;$i < count( $avail_templates );$i++ ) {
					echo '<li class="list-group-item available_template ' . esc_html( $avail_templates[ $i ]['slug'] ) . '_li" title="' . esc_html( $avail_templates[ $i ]['title'] ) . '"> <span class="truncate multiple_template_action ' . esc_html( $avail_templates[ $i ]['slug'] ) . '_head" based="bulk" id="' . esc_html( $avail_templates[ $i ]['slug'] ) . '">' . esc_html( $avail_templates[ $i ]['title'] ) . '</span>';
					if ( in_array( 'manage_templates', $access ) ) {
						echo '<span class="pull-right"> <span class="glyphicon glyphicon-pencil ticket_template_edit_type" id="' . esc_html( $avail_templates[ $i ]['slug'] ) . '" data-toggle="wsdesk_tooltip" data-container="body" title="' . esc_html__( 'Edit Template', 'wsdesk' ) . '" style="margin-right:5px;cursor:pointer;font-size: large;"></span></span>';
					}
					echo '</li>';
				}
			}
			?>
		</div>
	</div>
</div>
<?php
return ob_get_clean();
