<?php
echo '<div class="wsdesk_wrapper">';

function eh_crm_reports_page_tabs( $current = 'agent' ) {
	$tabs = array(
		'agent'     => esc_html__( 'Agents Reports', 'wsdesk' ),
		'date_wise' => esc_html__( 'Date wise Reports', 'wsdesk' ),
	);
	if ( EH_CRM_WOO_STATUS ) {
		$tabs['woocommerce'] = esc_html__( 'WooCommerce Reports', 'wsdesk' );
	}
	?>
		<h2 class="nav-tab-wrapper" style="margin-right:20px;">
		<?php
		foreach ( $tabs as $tab => $name ) {
			$class = ( $tab === $current ) ? 'nav-tab-active' : '';
			$style = ( $tab === $current ) ? '' : '';
			?>
				<a style='text-decoration:none !important; <?php echo esc_html( $style ); ?>'  class='nav-tab <?php echo esc_html( $class ); ?>' href='?page=wsdesk_reports&tab= <?php echo esc_html( $tab ); ?>' ></a>
			<?php
		}
		?>
		</h2>
	<?php
}
	eh_crm_reports_page_tabs( ( ! empty( $_GET['tab'] ) ) ? esc_attr( sanitize_text_field( $_GET['tab'] ) ) : 'agent' );
if ( 'agent' == ( ! empty( $_GET['tab'] ) ) ? esc_attr( sanitize_text_field( $_GET['tab'] ) ) : 'agent' ) {
	?>
		<div class="table-box table-box-main" id='agent_section' style="margin-top: 10px;">
	<?php wsdesk_agents_reports_view_section(); ?>
		</div>
	</div>
	<?php
} elseif ( 'date_wise' === ( ! empty( $_GET['tab'] ) ) ? esc_attr( sanitize_text_field( $_GET['tab'] ) ) : 'agent' ) {
	?>
		<div class="table-box table-box-main" id='woocommerce_section' style="margin-top: 10px;">
	   <?php wsdesk_date_wise_view_section(); ?>
		</div>
	<?php
} else {
	?>
		<div class="table-box table-box-main" id='woocommerce_section' style="margin-top: 10px;">
	   <?php wsdesk_woo_reports_view_section(); ?>
		</div>
	</div>
	<?php
}
function wsdesk_agents_reports_view_section() {
	 ob_start();
	$user      = ( isset( $_GET['user'] ) ? sanitize_text_field( $_GET['user'] ) : 'all' );
	$duration  = ( isset( $_GET['duration'] ) ? sanitize_text_field( $_GET['duration'] ) : 'last_7' );
	$from_date = ( isset( $_GET['from_date'] ) ? sanitize_text_field( $_GET['from_date'] ) : gmdate( 'Y-m-d', time() - ( 7 * 86400 ) ) );
	$to_date   = ( isset( $_GET['to_date'] ) ? sanitize_text_field( $_GET['to_date'] ) : gmdate( 'Y-m-d', time() ) );
	if ( strtotime( $to_date ) < strtotime( $from_date ) ) {
		$to_date   = gmdate( 'Y-m-d', time() );
		$from_date = gmdate( 'Y-m-d', time() - ( 7 * 86400 ) );
		echo "<script>alert('From Date cannot be beyond To Date.')</script>";
	}
	$user_name = '';
	$sol       = eh_crm_get_settings( array( 'slug' => 'label_LL02' ), array( 'settings_id' ) );
	$sol_col   = eh_crm_get_settingsmeta( $sol[0]['settings_id'], 'label_color' );
	$color     = array();
	switch ( $duration ) {
		case 'last_7':
			$message = __( ' Last 7 Days ', 'wsdesk' );
			break;
		case 'last_30':
			$message = __( ' Last 30 Days ', 'wsdesk' );
			break;
		case 'custom':
			$message = $from_date . __( ' to ', 'wsdesk' ) . $to_date;
	}
	if ( 'all' != $user ) {
		$user_check = get_user_by( 'ID', $user );
		if ( $user_check ) {
			$user_roles_default = array( 'WSDesk_Agents', 'WSDesk_Supervisor', 'administrator' );
			$role               = array_intersect( $user_roles_default, $user_check->roles );
			if ( ! empty( $role ) ) {
				$user_name                = $user_check->display_name;
				$bar                      = eh_crm_generate_bar_values( $user, $from_date, $to_date );
				$donut_data               = eh_crm_generate_donut_values( $user, $from_date, $to_date );
				$bar_avg_time             = eh_crm_generate_bar_values_Avg( $user, $from_date, $to_date );
				$donut_satisfaction_data  = eh_crm_generate_donut_satisfaction_values( $user, $from_date, $to_date );
				$donut                    = $donut_data['donut'];
				$color                    = $donut_data['color'];
				$donut_satisfaction       = $donut_satisfaction_data['donut'];
				$donut_satisfaction_color = $donut_satisfaction_data['color'];
				$lines                    = eh_crm_generate_line_values( $user, $from_date, $to_date );

			} else {
				wp_die( sprintf( '<center><h1>' . esc_html__( 'Oops', 'wsdesk' ) . ' !</h1><h4>' . esc_html__( 'User is not having any WSDesk Role', 'wsdesk' ) . '</h4><a href="' . esc_url( admin_url( 'admin.php?page=wsdesk_reports' ) ) . '">' . esc_html__( 'Back to Reports', 'wsdesk' ) . '</a></center>' ) );
			}
		} else {
			wp_die( sprintf( '<center><h1>' . esc_html__( 'Oops', 'wsdesk' ) . ' !</h1><h4>' . esc_html__( 'User not found', 'wsdesk' ) . '</h4><a href="' . esc_url( admin_url( 'admin.php?page=wsdesk_reports' ) ) . '">' . esc_html__( 'Back to Reports', 'wsdesk' ) . '</a></center>' ) );
		}
	} else {
		$user_name                = 'All';
		$bar                      = eh_crm_generate_bar_values( $user, $from_date, $to_date );
		$donut                    = eh_crm_generate_donut_values( $user, $from_date, $to_date );
		$bar_avg_time             = eh_crm_generate_bar_values_Avg( $user, $from_date, $to_date );
		$lines                    = eh_crm_generate_line_values( $user, $from_date, $to_date );
		$donut_satisfaction_data  = eh_crm_generate_donut_satisfaction_values( $user, $from_date, $to_date );
		$donut_satisfaction       = $donut_satisfaction_data['donut'];
		$donut_satisfaction_color = $donut_satisfaction_data['color'];
	}
	?>
	<div class="container wrapper" id="reports_page_view">
		<div class="row">
			<div class="col-md-12">
				<div class="panel panel-default reports_panel">
					<div class="panel-heading">
						<h3 class="panel-title"><?php esc_html_e( 'WSDesk - Agents Reports', 'wsdesk' ); ?></h3>
					</div>
					<div class="panel-body" id="reports_panel_body" style="text-align: center">
						<form method="GET">
							<input type="hidden" name="page" id="page" value="wsdesk_reports">
							<span class="help-block"><?php esc_html_e( 'Select the Agents/Supervisors to show report?', 'wsdesk' ); ?></span>
							<select id="user" name="user" style="display: inline !important;" class="form-control" aria-describedby="helpBlock">
								<option value="all" <?php echo ( 'all' === $user ) ? 'selected' : ''; ?> >All</option>
								<?php
									$user_roles_default = array( 'WSDesk_Agents', 'WSDesk_Supervisor', 'administrator' );
									$users              = get_users( array( 'role__in' => $user_roles_default ) );
									$users_data         = array();
								for ( $i = 0; $i < count( $users ); $i++ ) {
									$current                  = $users[ $i ];
									$id                       = $current->ID;
									$user_data                = new WP_User( $id );
									$users_data[ $i ]['id']   = $id;
									$users_data[ $i ]['name'] = $user_data->display_name;
								}
								for ( $i = 0;$i < count( $users_data );$i++ ) {
									$selected = 'selected';
									echo '<option value="' . esc_attr( $users_data[ $i ]['id'] ) . ( ( $user == $users_data[ $i ]['id'] ) ? esc_html( $selected ) : '' ) . '>' . esc_html( $users_data[ $i ]['name'] ) . '</option>';
								}
								?>
							</select>
							<span class="help-block"><?php esc_html_e( 'Select the Duration:', 'wsdesk' ); ?></span>
							<select id="duration" name="duration" style="display: inline !important;" class="form-control" aria-describedby="helpBlock">
								<option value="last_7" <?php echo ( 'last_7' == $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Last 7 Days', 'wsdesk' ); ?></option>
								<option value="last_30" <?php echo ( 'last_30' == $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Last 30 Days', 'wsdesk' ); ?></option>
								<option value="custom" <?php echo ( 'custom' == $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Custom', 'wsdesk' ); ?></option>
							</select>

							<?php
							switch ( $duration ) {
								case 'custom':
									$display = 'inline';
									break;

								default:
									$display = 'none';
									break;
							}
							?>
							<div class = "from_date" style="display: <?php echo esc_html( $display ); ?>;">
								<span class="help-block"><?php esc_html_e( 'Select the From Date:', 'wsdesk' ); ?></span>
								<input style="width: 30%; display: inline !important;" value="<?php echo esc_html( $from_date ); ?>" type="date" name="from_date" id="from_date" class="form-control" aria-describedby="helpBlock">
							</div>
							<div class="to_date" style="display: <?php echo esc_html( $display ); ?>">
								<span class="help-block"><?php esc_html_e( 'Select the To Date:', 'wsdesk' ); ?></span>
								<input style="width: 30%; display: inline !important;" value="<?php echo esc_html( $to_date ); ?>" type="date" name="to_date" id="to_date" class="form-control" aria-describedby="helpBlock">
							</div>
							<br>
							<input type="submit" style="margin-top: 10px" class="btn btn-primary" value="<?php esc_html_e( 'Show Report', 'wsdesk' ); ?>">

						</form>
						<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">

							<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report_full" value="<?php echo esc_html( $from_date ); ?>">
							<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report_full" value="<?php echo esc_html( $to_date ); ?>">
							<input type="hidden" name="action" value="eh_crm_export_ticket_data_report">
							<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
							<input type="hidden" class="user_for_dld_report" name="user_for_dld_report_full" value="<?php echo esc_html( $user ); ?>"></input>
							<input type="submit" style="margin-top: 10px" class="btn btn-primary" value="<?php esc_html_e( 'Download Report', 'wsdesk' ); ?>">
						</form>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-6">
								<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
									<div class="panel-heading" style="background-color: white;border-color: white;">
										<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
											<h3 class="panel-title"><?php esc_html_e( $message . 'Status of New Tickets', 'wsdesk' ); ?> : <?php echo esc_html( $user_name ); ?>
											<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report_new" value="<?php echo esc_html( $from_date ); ?>">
											<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report_new" value="<?php echo esc_html( $to_date ); ?>">
											<input type="hidden" name="action" value="eh_crm_download_status_of_new">
											<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
											<input type="hidden" class="user_for_dld_report" name="user_for_dld_report_new" value="<?php echo esc_html( $user ); ?>"></input>
											<button class="btn btn-primary download_status_of_new" style="color: white;width: 6%;height: 3%;margin-left: 90%;margin-right: 90%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 200%;height: 200%;background-color: white;border-color: white;"></img>
											</button> </h3>
										</form>
									</div>
									<div class="panel-body">
										<div id="7days-ticket" style="height: 250px;"></div>
										<script>
											Morris.Bar({
												element: '7days-ticket',
												data: <?php echo json_encode( $bar ); ?>,
												xkey: 'y',
												ykeys: ['a'],
												labels: ["<?php esc_html_e( 'Tickets', 'wsdesk' ); ?>", "<?php esc_html_e( 'Date', 'wsdesk' ); ?>"],
												resize:true
											});
										</script>
									</div>
								</div>
							</div>

							<div class="col-md-6" div class="col-md-6">
								<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
									<div class="panel-heading" style="background-color: white;border-color: white;">
										<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
											<h3 class="panel-title" ><?php esc_html_e( $message . 'Status of Agent Tickets', 'wsdesk' ); ?> : <?php echo esc_html( $user_name ); ?>
											<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report_new_donut" value="<?php echo esc_html( $from_date ); ?>">
											<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report_new_donut" value="<?php echo esc_html( $to_date ); ?>">
											<input type="hidden" name="action" value="eh_crm_download_status_of_new_donut">
											<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
											<input type="hidden" class="user_for_dld_report" name="user_for_dld_report_new_donut" value="<?php echo esc_html( $user ); ?>">

											</input>

											<button class="btn btn-primary download_status_of_new_donut" style="color: white;margin-left: 90%;margin-right: 90%;width: 6%;height: 3%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 200%;height: 200%;background-color: white;border-color: white;"></img>
											</button> </h3>
										</form>

									</div>
									<div class="panel-body">
										<div class="col-md-12">
											<div class="col-md-6">
												<div id="assignee-contribution" style="height: 250px;"></div>
													<script>
														Morris.Donut({
															element: 'assignee-contribution',
															data: <?php echo json_encode( $donut ); ?>,
															<?php echo ( ( ! empty( $color ) ) ? 'colors: ' . json_encode( $color ) . ',' : '' ); ?>
															resize:true
														});
													</script>
											</div>
											<div class="col-md-6">
												<div class="panel panel-default">
													<table class="table table-hover" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
													<?php
													for ( $i = 0;$i < count( $donut );$i++ ) {
														if ( ! empty( $color ) ) {
															echo "<tr style='background:" . esc_html( $color[ $i ] ) . "'>";
														} else {
															echo '<tr>';
														}
														echo '<td>' . esc_html( $donut[ $i ]['label'] ) . '</td>';
														echo '<td>' . esc_html( $donut[ $i ]['value'] ) . '</td>';
														echo '</tr>';
													}
													?>
													</table>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-12">
								<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
									<div class="panel-heading" style="background-color: white;border-color: white;">


										<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
											<h3 class="panel-title"><?php esc_html_e( $message . 'Status of New and Solved Ticket', 'wsdesk' ); ?> : <?php echo esc_html( $user_name ); ?>
											<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report" value="<?php echo esc_html( $from_date ); ?>">
											<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report" value="<?php echo esc_html( $to_date ); ?>">
											<input type="hidden" name="action" value="eh_crm_download_status_of_new_and_solved">
											<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
											<input type="hidden" class="user_for_dld_report" name="user_for_dld_report" value="<?php echo( esc_html( $user ) ); ?>"></input>

										<button class="btn btn-primary download_status_of_new_and_solved" style="color: white;margin-left: 94%;margin-right: 94%;width: 7%;height: 6%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 30%;height: 60%;background-color: white;border-color: white;"></img>
											</button> </h3>
										</form>

									</div>
									<div class="panel-body">
										<div id="new-solved-status" style="height: 250px;"></div>
										<script>
											Morris.Area({
												element: 'new-solved-status',
												data: <?php echo json_encode( $lines ); ?>,
												xkey: 'y',
												ykeys: ['a', 'b', 'c', 'd'],
												lineColors: ['#0b62a4','<?php echo esc_html( $sol_col ); ?>','#57b755','#e87681'],
												labels: ["<?php esc_html_e( 'New Tickets', 'wsdesk' ); ?>", "<?php esc_html_e( 'Solved Tickets', 'wsdesk' ); ?>", "<?php esc_html_e( 'Good Rating', 'wsdesk' ); ?>", "<?php esc_html_e( 'Bad Rating', 'wsdesk' ); ?>"],
												behaveLikeLine:true,
												resize: true
											});
										</script>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="col-md-12">
						<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
									<div class="panel-heading" style="background-color: white;border-color: white;">
								<h3 class="panel-title">
									<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
										<?php

										esc_html_e( $message . 'First Reply Average Time', 'wsdesk' );
										?>
								  :
									<?php
									echo esc_html( $user_name );
									if ( 'all' == $user ) {
										echo ' ( In Minutes )';
										?>
										<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_export_ticket_data_report_first_reply" value="<?php echo esc_html( $from_date ); ?>">
										<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_export_ticket_data_report_first_reply" value="<?php echo esc_html( $to_date ); ?>">
										<input type="hidden" name="action" value="eh_crm_export_ticket_data_report_first_reply">
										<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
										<input type="hidden" class="user_for_dld_report" name="user_for_dld_export_ticket_data_report_first_reply" value="<?php echo esc_html( $user ); ?>"></input>
										<button class="btn btn-primary export_ticket_data_report_first_reply" style="color: white;margin-left: 94%;margin-right: 94%;width: 7%;height: 6%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 30%;height: 60%;background-color: white;border-color: white;"></img>
										</button></h3>
										 </form>
										<?php
									}
									?>

								</h3>
							</div>
							<div class="panel-body">
								<div class="col-md-12">
									<?php
									$data_need_to_manipulated = eh_crm_first_reply_avg_time( $user, $from_date, $to_date );
									?>
									<div class="panel-body">
										<div id="days-ticket" style="height: 250px;"></div>
										<script>
											Morris.Bar({
												barSizeRatio:0.2,
												element: 'days-ticket',
												data: <?php echo json_encode( $data_need_to_manipulated ); ?>,
												xkey: 'ticket_assignee',
												ykeys: ['first_reply_time'],
												labels: ["<?php esc_html_e( 'Minutes', 'wsdesk' ); ?>", "<?php esc_html_e( 'Agent', 'wsdesk' ); ?>"],
												resize:true
											});
										</script>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-12">
								<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
									<div class="panel-heading" style="background-color: white;border-color: white;">
										 <form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
											<h3 class="panel-title"><?php esc_html_e( $message . 'Satisfaction Survey', 'wsdesk' ); ?> : <?php echo esc_html( $user_name ); ?></h3>
											<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report" value="<?php echo esc_html( $from_date ); ?>">
											<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report" value="<?php echo esc_html( $to_date ); ?>">
											<input type="hidden" name="action" value="eh_crm_download_status_of_good_bad">
											<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
											<input type="hidden" class="user_for_dld_report" name="user_for_dld_report" value="<?php echo esc_html( $user ); ?>"></input>
											<button class="btn btn-primary download_status_of_good_bad" style="color: white;margin-left: 94%;margin-right: 94%;width: 7%;height: 6%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 30%;height: 60%;background-color: white;border-color: white;"></img>
												</button> </h3>
										</form>


									</div>
									<div class="panel-body">
										<div class="col-md-12">
											<div class="col-md-6">
												<div id="assignee-satisfaction" style="height: 250px"></div>
												<script>
													Morris.Donut({
														element: 'assignee-satisfaction',
														data: <?php echo json_encode( $donut_satisfaction ); ?>,
														<?php echo ( ( ! empty( $donut_satisfaction_color ) ) ? 'colors: ' . wp_json_encode( $donut_satisfaction_color ) . ',' : '' ); ?>
														resize:true
													});
												</script>
											</div>
											<div class="col-md-6">
												<div class="panel panel-default">

												<table cellspacing="0" cellpadding="0" class="table table-hover" style="box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);border: none !important;">
													<?php
													$total_rating = 0;
													$count        = count( $donut_satisfaction ) + 1;

													for ( $i = 0;$i < $count;$i++ ) {

														if ( 2 !== $i ) {
															$total_rating = $donut_satisfaction[ $i ]['value'] + $total_rating;
															if ( 'Good' === $donut_satisfaction[ $i ]['label'] ) {
																								 $good_rating = $donut_satisfaction[ $i ]['value'];
															}
															if ( 'Bad' === $donut_satisfaction[ $i ]['label'] ) {
																								 $bad_rating = $donut_satisfaction[ $i ]['value'];
															}
															if ( 0 !== $total_rating ) {
																				  $score = round( ( $good_rating / $total_rating ) * 100, 2 );

															} else {
																			   $score = 0;
															}if ( ! isset( $bad_rating ) ) {
																		 $bad_rating = 0;
															}if ( 0 !== $bad_rating && 0 === $good_rating ) {
																$score = 0;
															}
															if ( $score <= 100 && $score >= 90 ) {
																							  $class = 'progress-bar-success';
															} elseif ( $score < 90 && $score >= 70 ) {
																						$class = 'progress-bar-info';
															} elseif ( $score < 70 && $score >= 50 ) {
																				  $class = 'progress-bar-warning';
															} elseif ( $score < 50 ) {
																			   $class = 'progress-bar-danger';
															}
														}
														if ( ! empty( $color ) ) {
															if ( 2 !== $i ) {
																echo "<tr style='background:" . esc_html( $donut_satisfaction_color[ $i ] ) . "'>";
															}
														} else {
															echo "<tr style='background:white'>";
														}if ( 2 === $i ) {
															?>

																<td style="width:150px;"><b>Total Customer Response</b></td><td> <?php echo esc_html( $good_rating + $bad_rating ); ?></td>
																<td style="width:150px;"><b>Satisfaction Score</b></td>

															<?php
															if ( 0 !== $score ) {
																?>
																		<td>
																			<h4 style="color:green;font-size: 30px;">
																<?php echo( esc_html( $score . ' %' ) ); ?>
																			</h4>
																		</td>
																<?php

															} else {
																?>
																		 <td>
																			<h4 style="color:green;font-size: 10px;">
																<?php esc_html_e( 'No Ratings', 'wsdesk' ); ?>
																			</h4>
																		</td>
																<?php
															}
															echo '</tr>';
														} else {
															echo '<td><b>' . esc_html( $donut_satisfaction[ $i ]['label'] ) . '</td></b>';
															echo '<td>' . esc_html( $donut_satisfaction[ $i ]['value'] ) . '</td>';
															echo '<td></td>';
															echo '<td></td>';
															echo '</tr>';
														}
													}

													?>
													</table>
												</div>

											</div>
										</div>
									</div>
								</div>
							</div>
					   </div>
				   </div>
				   <div class="row" style="margin-right: 0px;margin-left: 0px;">
						<div class="col-md-12">
							<div class="panel panel-default" style="border-color: white;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
							<div class="panel-heading" style="background-color: white;border-color: white;">

									<form id="backup_wsdesk" method="POST" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">

									<?php if ( 'all' === $user ) { ?>
									<h3 class="panel-title"><?php esc_html_e( $message . 'Status of Solved Tickets: All Average Solving Time in Minutes ', 'wsdesk' ); ?>
									<input type="hidden" class="from_date_for_dld_report" name="from_date_for_dld_report_new_satisfaction" value="<?php echo esc_html( $from_date ); ?>">
									<input type="hidden" class="to_date_for_dld_report" name="to_date_for_dld_report_new_satisfaction" value="<?php echo esc_html( $to_date ); ?>">
									<input type="hidden" name="action" value="eh_crm_download_status_of_new_satisfaction">
									<input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'wsdesk_nonce' ) ); ?>">
									<input type="hidden" class="user_for_dld_report" name="user_for_dld_report_new_satisfaction" value="<?php echo esc_html( $user ); ?>"></input>
									<button class="btn btn-primary download_status_of_new_donut" style="color: white;margin-left: 94%;margin-right: 94%;width: 7%;height: 6%;background-color: white;border-color: white;"><img src="<?php echo esc_url( EH_CRM_MAIN_IMG . 'wsdesk_download.png' ); ?>" style="color: white;width: 30%;height: 60%;background-color: white;border-color: white;"></img>
									<?php } ?>
								</form>
								</div>
								<div class="panel-body">
									<?php if ( 'all' === $user ) { ?>
										<div id="avg_resolution" style="height: 250px;"></div>
										<script>
											Morris.Bar({
											barSizeRatio:0.2,
											element: 'avg_resolution',
											data: <?php echo wp_json_encode( $bar_avg_time ); ?>,
											xkey: ['1'],
											ykeys: ['0','2'],
											labels: ["<?php esc_html_e( 'Avg Solving time', 'wsdesk' ); ?>", "<?php esc_html_e( 'Solved Tickets', 'wsdesk' ); ?>"],
											barShape:'soft',
											xLabelAngle:50,
											resize:true
											});
										</script>
										<?php
									}
									if ( 'all' !== $user ) {
										?>

											<h3>
										<?php
										esc_html_e( 'Average Resolution Time of ', 'wsdesk' );
										esc_html_e( $user_name, 'wsdesk' );
										?>
											</h3>
											<h5>
										<?php
										echo esc_html( $bar_avg_time[0] ) . ' ' . esc_html__( 'Days', 'wsdesk' ) . ' ' . esc_html( $bar_avg_time[1] ) . ' ' . esc_html__( 'Hours', 'wsdesk' ) . ' ' . esc_html( $bar_avg_time[2] ) . ' ' . esc_html__( 'Minutes', 'wsdesk' );
										?>
											</h5>
									<?php } ?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	<?php
	$html = ob_get_clean();
	return $html;
}
function wsdesk_woo_reports_view_section() {
	ob_start();
	$pro_bar = eh_crm_woo_products_generate_bar_values();
	$cat_bar = eh_crm_woo_category_generate_bar_values();
	?>
	<div class="container wrapper" id="woo_reports_page_view">
		<div class="row">
			<div class="col-md-12">
				<div class="panel panel-default woo_reports_panel">
					<div class="panel-heading">
						<h3 class="panel-title"><?php esc_html_e( 'WSDesk - WooCommerce Reports', 'wsdesk' ); ?></h3>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-12">
								<div class="panel panel-default">
									<div class="panel-heading">
										<h3 class="panel-title"><?php esc_html_e( 'Last 7 Days status of new tickets', 'wsdesk' ); ?> : <span id="change_product_name"><?php esc_html_e( 'Top Products', 'wsdesk' ); ?></span></h3>
									</div>
									<div class="panel-body" id="reports_panel_body" style="text-align: center">
										<div class="loader" id="woo_product_loader"></div>
										<span class="help-block"><?php esc_html_e( 'Select the Product(s) to show report?', 'wsdesk' ); ?></span>
										<select id="show_product" multiple name="show_product" style="display: inline !important;" class="form-control" aria-describedby="helpBlock">
											<?php
												$field = eh_crm_get_settings( array( 'slug' => 'woo_product' ) );
											if ( $field ) {
												$field_meta = eh_crm_get_settingsmeta( $field[0]['settings_id'], 'field_values' );
												foreach ( $field_meta as $key => $value ) {
													echo '<option value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
												}
											}
											?>
										</select>
										<br>
										<button style="margin-top: 10px" class="btn btn-primary" id="woo_product_report_custom"><?php esc_html_e( 'Show Report', 'wsdesk' ); ?></button>
									</div>
									<div id="7days-woo-product-ticket" style="height: 250px;"></div>
									<script>
										var woo_product_bar_chart = Morris.Bar({
											element: '7days-woo-product-ticket',
											data: <?php echo wp_json_encode( $pro_bar ); ?>,
											xkey: 'y',
											ykeys: ['a'],
											labels: ["<?php esc_html_e( 'Tickets', 'wsdesk' ); ?>", "<?php esc_html_e( 'Product', 'wsdesk' ); ?>"],
											resize:true
										});
									</script>
								</div>
							</div>
						</div>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-12">
								<div class="panel panel-default">
									<div class="panel-heading">
										<h3 class="panel-title"><?php esc_html_e( 'Last 7 Days status of new tickets', 'wsdesk' ); ?> : <span id="change_product_name"><?php esc_html_e( 'Top Categories', 'wsdesk' ); ?></span></h3>
									</div>
									<div class="panel-body" id="reports_panel_body" style="text-align: center">
										<div class="loader" id="woo_category_loader"></div>
										<span class="help-block"><?php esc_html_e( 'Select the Category(s) to show report?', 'wsdesk' ); ?></span>
										<select id="show_category" multiple name="show_category" style="display: inline !important;" class="form-control" aria-describedby="helpBlock">
											<?php
												$cat_field = eh_crm_get_settings( array( 'slug' => 'woo_category' ) );
											if ( $cat_field ) {
												$field_meta = eh_crm_get_settingsmeta( $cat_field[0]['settings_id'], 'field_values' );
												foreach ( $field_meta as $key => $value ) {
													echo '<option value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
												}
											}
											?>
										</select>
										<br>
										<button style="margin-top: 10px" class="btn btn-primary" id="woo_category_report_custom"><?php esc_html_e( 'Show Report', 'wsdesk' ); ?></button>
									</div>
									<div id="7days-woo-category-ticket" style="height: 250px;"></div>
									<script>
										var woo_category_bar_chart = Morris.Bar({
											element: '7days-woo-category-ticket',
											data: <?php echo wp_json_encode( $cat_bar ); ?>,
											xkey: 'y',
											ykeys: ['a'],
											labels: ["<?php esc_html_e( 'Tickets', 'wsdesk' ); ?>", "<?php esc_html_e( 'Category', 'wsdesk' ); ?>"],
											resize:true
										});
									</script>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php
	$html = ob_get_clean();
	return $html;
}
function wsdesk_date_wise_view_section() {
  ob_start();
	$duration  = ( isset( $_GET['duration'] ) ? sanitize_text_field( $_GET['duration'] ) : 'last_7' );
	$from_date = ( isset( $_GET['from_date'] ) ? sanitize_text_field( $_GET['from_date'] ) : gmdate( 'Y-m-d', time() - ( 7 * 86400 ) ) );
	$to_date   = ( isset( $_GET['to_date'] ) ? sanitize_text_field( $_GET['to_date'] ) : gmdate( 'Y-m-d', time() ) );
	if ( strtotime( $to_date ) < strtotime( $from_date ) ) {
		$to_date   = gmdate( 'Y-m-d', time() );
		$from_date = gmdate( 'Y-m-d', time() - ( 7 * 86400 ) );
		echo "<script>alert('From Date cannot be beyond To Date.')</script>";
	}
	switch ( $duration ) {
		case 'last_7':
			$message = 'Last 7 Days';
			break;
		case 'last_30':
			$message = 'Last 30 Days';
			break;
		case 'custom':
			$message = "$from_date to $to_date";
	}
	$donut = eh_crm_generate_donut_values_tags( $from_date, $to_date );
	?>
	<div class="container wrapper" id="reports_page_view">
		<div class="row">
			<div class="col-md-12">
				<div class="panel panel-default reports_panel">
					<div class="panel-heading">
						<h3 class="panel-title"><?php esc_html_e( 'WSDesk - Date Wise Reports', 'wsdesk' ); ?></h3>
					</div>
					<div class="panel-body" id="reports_panel_body" style="text-align: center">
						<form method="GET" action="<?php echo esc_url( admin_url() ) . 'admin.php?page=wsdesk_reports&tab=date_wise'; ?>">
							<input type="hidden" name="page" id="page" value="wsdesk_reports">
							<input type="hidden" name="tab" id="tab" value="date_wise">
							<span class="help-block"><?php esc_html_e( 'Select the Duration:', 'wsdesk' ); ?></span>
							<select id="duration" name="duration" style="display: inline !important;" class="form-control" aria-describedby="helpBlock">
								<option value="last_7" <?php echo ( 'last_7' === $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Last 7 Days', 'wsdesk' ); ?></option>
								<option value="last_30" <?php echo ( 'last_30' === $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Last 30 Days', 'wsdesk' ); ?></option>
								<option value="custom" <?php echo ( 'custom' === $duration ) ? 'selected' : ''; ?>><?php esc_html_e( 'Custom', 'wsdesk' ); ?></option>
							</select>

							<?php
							switch ( $duration ) {
								case 'custom':
									$display = 'inline';
									break;

								default:
									$display = 'none';
									break;
							}
							?>
							<div class = "from_date" style="display: <?php echo esc_html( $display ); ?>;">
								<span class="help-block"><?php esc_html_e( 'Select the From Date:', 'wsdesk' ); ?></span>
								<input style="width: 30%; display: inline !important;" value="<?php echo esc_html( $from_date ); ?>" type="date" name="from_date" id="from_date" class="form-control" aria-describedby="helpBlock">
							</div>
							<div class="to_date" style="display: <?php echo esc_html( $display ); ?>">
								<span class="help-block"><?php esc_html_e( 'Select the To Date:', 'wsdesk' ); ?></span>
								<input style="width: 30%; display: inline !important;" value="<?php echo esc_html( $to_date ); ?>" type="date" name="to_date" id="to_date" class="form-control" aria-describedby="helpBlock">
							</div>
							<br>
							<input type="submit" style="margin-top: 10px" class="btn btn-primary" value="<?php esc_html_e( 'Show Report', 'wsdesk' ); ?>">
						</form>
					</div>
					<div class="row" style="padding-top: 10px;">
						<div class="col-md-12">
							<div class="col-md-3">
							</div>
							<div class="col-md-6">
								<div class="panel panel-default">
									<div class="panel-heading">
										<h3 class="panel-title"><?php esc_html_e( $message . 'Status of Tickets by Tags', 'wsdesk' ); ?></h3>
									</div>
									<div class="panel-body">
										<div class="col-md-12">
											<div class="col-md-6">
												<div id="date_wise" style="height: 250px;"></div>
													<script>
														Morris.Donut({
															element: 'date_wise',
															data: <?php echo wp_json_encode( $donut ); ?>,
															resize:true
														});
													</script>
											</div>
											<div class="col-md-6">
												<div class="panel panel-default">
													<table class="table table-hover">
													<?php
													for ( $i = 0;$i < count( $donut );$i++ ) {
														if ( ! empty( $color ) ) {
															echo "<tr style='background:" . esc_html( $color[ $i ] ) . "'>";
														} else {
															echo '<tr>';
														}
														echo '<td>' . esc_html( $donut[ $i ] )['label'] . '</td>';
														echo '<td>' . esc_html( $donut[ $i ] )['value'] . '</td>';
														echo '</tr>';
													}
													?>
													</table>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="col-md-3">
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php

$html = ob_get_clean();
return $html;
}
