<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Website Activities.
 *
 * Variables such as $action, $post_id, $displayed_dates may be set by Commenting_Block_Activities before this partial is included.
 */
$multicollab_activity_limit                  = 10;
$multicollab_new_pointer                     = $new_pointer ?? $multicollab_activity_limit;
$multicollab_activity_view                   = $activity_view ?? 'list-view';
$multicollab_its_loadmore                    = isset( $action );
$multicollab_date_continue_in_loadmore       = $date_continue_in_loadmore ?? false;
$multicollab_post_continue_in_loadmore       = $post_continue_in_loadmore ?? false;
$multicollab_outer_main_timestamp            = $main_timestamp ?? null;
$multicollab_detail_post_id                  = isset( $post_id ) ? (int) $post_id : 0;
$multicollab_displayed_dates_input           = $displayed_dates ?? array();


// If no data found, an HTML (message) will be returned instead of an array.
if ( ! is_array( $this->cf_activities ) ) { //phpcs:ignore

	require_once COMMENTING_BLOCK_DIR . 'admin/partials/commenting-block-dashboard-no-results.php';
	// echo $this->cf_activities; //phpcs:ignore

	return;
}

?>
<!-- <div class="board-items-rows<?php echo $multicollab_date_continue_in_loadmore ? ' date_continued' : ''; ?><?php echo $multicollab_post_continue_in_loadmore ? ' post_continued' : ''; ?>"> -->
<?php
$multicollab_date_format = get_option( 'date_format' );
$multicollab_time_format = get_option( 'time_format' );

$multicollab_user_data    = array(); //phpcs:ignore
$multicollab_date_displayed = $multicollab_displayed_dates_input; //phpcs:ignore
?>
<?php
if ( 'cf-detail-view' === $multicollab_activity_view ) {
	?>
	<div class="board-detail-head">
		<a href="javascript::void(0)" id="activity-go-back"><?php esc_html_e( 'Go Back', 'commenting-feature' ); ?></a>
	   
		<h2 id="main-detailed-view">
            <a href="<?php echo esc_url( get_edit_post_link( $multicollab_detail_post_id ) ); //phpcs:ignore ?>">
				<?php echo esc_html( get_the_title( $multicollab_detail_post_id ) ); ?>
			</a>
		</h2>
	</div>
	<?php
}

$multicollab_post_ids                  = array();
$multicollab_new_date_displayed        = true;
$multicollab_board_position            = $board_position ?? 'cf-dashboard-panel__row-rhs';  //phpcs:ignore
$multicollab_side_time_format          = 'g:i a F j, Y';
$multicollab_count_iterations          = 0;
// Plain-text length at/above this uses show-all/show-less + cf-show-more (matches activityCentre.js collapseLimit).
$multicollab_commented_on_collapse_at = 50;
$multicollab_previous_post_id          = '';

if ((is_array($this->cf_activities) && !empty($this->cf_activities)) || (is_object($this->cf_activities) && !empty((array)$this->cf_activities))) {
	foreach ( $this->cf_activities as $multicollab_meta_single ) { //phpcs:ignore

		if ( isset( $multicollab_meta_single->post_id ) ) {

			$multicollab_main_timestamp          = $multicollab_meta_single->meta_value;
			$multicollab_activity_post_id                 = $multicollab_meta_single->post_id; //phpcs:ignore
			$multicollab_post_title              = get_the_title( $multicollab_activity_post_id );
			$multicollab_meta_key                = $multicollab_meta_single->meta_key;

			$multicollab_board_type = '';
			// pooja
			$multicollab_show_reply = 0;
			if ( strpos( $multicollab_meta_key, 'el' ) !== false ) {
				$multicollab_title_displayed_already = in_array( $multicollab_activity_post_id, $multicollab_post_ids, true );
				$multicollab_post_ids[]              = $multicollab_activity_post_id;

				// Skip first iteration to avoid variable value in first iteration, Then update continuation variables.

				if ( 0 !== $multicollab_count_iterations ) {
					$multicollab_post_continue_in_loadmore = $multicollab_title_displayed_already ? true : false;
				}

				// If its for comments.
				$multicollab_board_type       = 'comments';
				$multicollab_meta_key         = str_replace( 'th', '', $multicollab_meta_key );
				$multicollab_thread_edit_link = get_edit_post_link( $multicollab_activity_post_id ) . '&current_url=' . str_replace( '_', '', $multicollab_meta_key );
				$multicollab_meta             = get_post_meta( $multicollab_meta_single->post_id, $multicollab_meta_key, true );
			} else {
				// Non-comment th_* rows (legacy suggestion indexes) are ignored in this build.
				++$multicollab_count_iterations;
				$multicollab_previous_post_id = $multicollab_activity_post_id;
				continue;
			}

			$multicollab_date_displaying   = date_i18n( 'l, F j', $multicollab_main_timestamp );
			$multicollab_now               = time();
			$multicollab_beginning_of_week = strtotime( 'last Monday', $multicollab_now ); // Gives you the time at the BEGINNING of the week
			$multicollab_yesterday         = strtotime( '-1 day', $multicollab_now );

			// Display only the day, if its from current week.
			if ( $multicollab_main_timestamp > $multicollab_beginning_of_week ) {
				if ( gmdate( 'Y-m-d', $multicollab_main_timestamp ) === gmdate( 'Y-m-d', $multicollab_now ) ) {
					$multicollab_date_displaying = __( 'Today', 'commenting-feature' );
				} elseif ( gmdate( 'Y-m-d', $multicollab_main_timestamp ) === gmdate( 'Y-m-d', $multicollab_yesterday ) ) {
					$multicollab_date_displaying = __( 'Yesterday', 'commenting-feature' );
				}
			}

			// Skip first iteration to avoid variable value in first iteration, Then update continuation variables.
			if ( 0 !== count( $multicollab_date_displayed ) ) {
				$multicollab_date_continue_in_loadmore = in_array( $multicollab_date_displaying, $multicollab_date_displayed, true ) ? true : false;
			}

			// Check if date should be displayed.
			if ( ! in_array( $multicollab_date_displaying, $multicollab_date_displayed, true ) && ! $multicollab_date_continue_in_loadmore ) {
				$multicollab_date_displayed[] = $multicollab_date_displaying;

				echo "<h4 class='cf-dashboard-panel__board-day'>" . esc_html( $multicollab_date_displaying ) . '</h4>';
				$multicollab_new_date_displayed = true;

				// Force the post title to be displayed.
				$multicollab_title_displayed_already = false;
			} else {
				$multicollab_new_date_displayed = false;
			}

			// Update board position.
			if (
			( $multicollab_its_loadmore && ( ! $multicollab_date_continue_in_loadmore || ! $multicollab_post_continue_in_loadmore ) )
			|| ( ! $multicollab_its_loadmore && ( ! $multicollab_title_displayed_already || $multicollab_new_date_displayed ) )
			|| ( $multicollab_previous_post_id !== $multicollab_activity_post_id )
			) {
				$multicollab_board_position = 'cf-dashboard-panel__row-lhs' === $multicollab_board_position ? 'cf-dashboard-panel__row-rhs' : 'cf-dashboard-panel__row-lhs';
			}

			// fix to separate comment by post id
			if ( $multicollab_previous_post_id !== $multicollab_activity_post_id ) {
				$multicollab_title_displayed_already   = false;
				$multicollab_post_continue_in_loadmore = false;
			}

			$multicollab_all_comments = isset( $multicollab_meta['comments'] ) ? $multicollab_meta['comments'] : '';
			if ( is_array( $multicollab_all_comments ) ) {
				// Add mark as resolved item.
				if ( isset( $multicollab_meta['resolved_by'] ) ) {
					$multicollab_all_comments['resolved']['resolved_by']        = $multicollab_meta['resolved_by'];
					$multicollab_all_comments['resolved']['resolved_timestamp'] = $multicollab_meta['resolved_timestamp'];
					$multicollab_all_comments['resolved']['resolved_text']      = isset( $multicollab_meta['resolved_text'] ) ? $multicollab_meta['resolved_text'] : __( 'Marked as Resolved', 'commenting-feature' );
				}


				if ( isset( $multicollab_meta['assigned_to'] ) ) {
					$multicollab_user_data_assign   = get_user_by( 'ID', $multicollab_meta['assigned_to'] );
					$multicollab_login_user  = wp_get_current_user();
					$multicollab_display_name = ( $multicollab_login_user->data->ID === $multicollab_user_data_assign->ID ) ? __( 'You', 'commenting-feature' ) : $multicollab_user_data_assign->display_name; // phpcs:ignore

					$multicollab_assigned_to = array(
						'ID'           => $multicollab_user_data_assign->ID,
						'display_name' => $multicollab_display_name,
						'user_email'   => $multicollab_user_data_assign->user_email,
						'avatar'       => get_avatar_url( $multicollab_user_data_assign->ID, array( 'size' => 32 ) ),
					);
				}

				?>
			<div class="cf-dashboard-panel__board-row 
				<?php
				echo esc_attr( $multicollab_board_position );
				echo $multicollab_date_continue_in_loadmore ? ' date_continued' : '';
				?>
				<?php echo $multicollab_post_continue_in_loadmore ? 'post_continued' : ''; ?>">
				<div class="cf-dashboard-panel__row-inner">
					<div class="cf-dashboard-panel__row-wrap">
						<?php if ( ! $multicollab_title_displayed_already ) { ?>
							<h4 class="cf-dashboard-panel__row-title"><a href="javascript:void(0)" class="show_activity_details" data-id="<?php echo esc_attr( (string) $multicollab_activity_post_id ); ?>"><?php echo esc_html( $multicollab_post_title ); ?></a></h4>
						<?php } ?>
						<div class="user-data-row">
							<?php
							// Display thread comments.
							$multicollab_comments_counts = 0;
							$multicollab_side_time       = '';
							$multicollab_side_time_stamp = '';

							$multicollab_index                 = 0;
							$multicollab_showallcommentbtnflag = 0;
							foreach ( $multicollab_all_comments as $multicollab_comment_loop_ts => $multicollab_comment ) {  //phpcs:ignore
								++$multicollab_comments_counts;

								if ( 1 === $multicollab_comments_counts ) {
									$multicollab_side_time_stamp = $multicollab_comment_loop_ts;
								}

								$multicollab_timestamp = 'comments' === $multicollab_board_type || 'resolved' === $multicollab_comment_loop_ts ? $multicollab_comment_loop_ts : $multicollab_comment['timestamp'];

								$multicollab_box_class = 'user-data-box';
								if ( 'resolved' === $multicollab_timestamp ) {
									$multicollab_box_class          .= ' cf-mark-as-resolved';
									$multicollab_timestamp           = $multicollab_comment['resolved_timestamp'];
									$multicollab_comment['userData'] = $multicollab_comment['resolved_by'];  //phpcs:ignore
								} elseif ( 1 !== $multicollab_comments_counts ) {
									$multicollab_box_class .= ' user-reply';
								}

								$multicollab_user_id = isset( $multicollab_comment['userData'] ) ? $multicollab_comment['userData'] : $multicollab_comment['uid'];

								if ( ! array_key_exists( $multicollab_user_id, $multicollab_user_data ) ) {
									$multicollab_user_info = get_userdata( $multicollab_user_id );

									$multicollab_user_data[ $multicollab_user_id ]['username']   = $multicollab_username = $multicollab_user_info->display_name;
									$multicollab_user_data[ $multicollab_user_id ]['profileURL'] = $multicollab_profile_url = get_avatar_url( $multicollab_user_info->user_email );
									$multicollab_user_data[ $multicollab_user_id ]['userrole']   = $multicollab_userrole = isset( $multicollab_user_info->roles ) ? implode( ', ', $multicollab_user_info->roles ) : '';

								} else {
									$multicollab_username    = $multicollab_user_data[ $multicollab_user_id ]['username'];
									$multicollab_profile_url = $multicollab_user_data[ $multicollab_user_id ]['profileURL'];
									$multicollab_userrole    = $multicollab_user_data[ $multicollab_user_id ]['userrole'];

								}

								if ( isset( $multicollab_comment['thread'] ) && 'comments' === $multicollab_board_type ) {
									$multicollab_thread = ( isset( $multicollab_comment['status'] ) && 'deleted' === $multicollab_comment['status'] ) ? '<del>' . $multicollab_comment['thread'] . '</del>' : $multicollab_comment['thread'];
								} elseif ( isset( $multicollab_comment['action'] ) && 'reply' === $multicollab_comment['action'] ) {
									$multicollab_thread = 'deleted' === $multicollab_comment['status'] ? '<del>' . $multicollab_comment['text'] . '</del>' : $multicollab_comment['text'];
								} else {
									$multicollab_thread = '';
								}

								$multicollab_date_time = date_i18n( $multicollab_time_format . ' ' . $multicollab_date_format, $multicollab_timestamp );
								?>
								<div class="<?php echo esc_attr( $multicollab_box_class ); ?>" style="<?php echo ( $multicollab_index > 6 && ! isset( $multicollab_comment['resolved_by'] ) ) ? 'display:none' : ''; ?>">
									<div class="user-data">
										<div class="user-data-header">
											<div class="user-avatar">
												<img src="<?php echo esc_url( $multicollab_profile_url ); ?>" alt="<?php echo esc_attr( $multicollab_username ); ?>">
											</div>
											<div class="user-display-name">
												<span class="user-name"><?php echo esc_html( $multicollab_username ); ?><span class="tooltip"><?php echo esc_html( $multicollab_userrole ); ?></span></span>
												<time class="user-commented-date"><?php echo esc_html( $multicollab_date_time ); ?></time>
											</div>
										</div>
										<?php if ( isset( $multicollab_comment['resolved_timestamp'] ) ) { ?>
											<div class="user-comment <?php echo 'comments' === $multicollab_board_type ? 'mark-resolved-icon' : ''; ?>">
												<strong><?php echo esc_html( $multicollab_comment['resolved_text'] ); ?></strong>
											</div>
										<?php } else { ?>
											<div class="user-data-wrapper">
												<?php
												if ( 1 === $multicollab_comments_counts && ( 'comments' === $multicollab_board_type || 'collaborators' === $multicollab_board_type ) ) {
													$multicollab_row_class = '';
													if ( 'collaborators' === $multicollab_board_type ) {
														$multicollab_row_class = 'collaborator';
													}
													?>
													<?php
													$multicollab_comment_on_plain_len    = isset( $multicollab_meta['commentedOnText'] ) ? strlen( wp_strip_all_tags( $multicollab_meta['commentedOnText'] ) ) : 0;
													$multicollab_comment_on_needs_toggle = ( '' !== $multicollab_meta['commentedOnText'] && $multicollab_comment_on_plain_len >= $multicollab_commented_on_collapse_at );
													?>
													<div class="user-commented-on <?php echo esc_attr( $multicollab_row_class ); ?>">
														<blockquote class="user-commented-icon<?php echo ( isset( $multicollab_meta['blockType'] ) ) ? esc_attr( '-' . $multicollab_meta['blockType'] ) : ''; ?>">
															<?php if ( $multicollab_comment_on_needs_toggle ) { ?>
															<a class="user-commented-on show-all js-hide" href="javascript:void(0)">
																<?php echo wp_kses( $multicollab_meta['commentedOnText'], wp_kses_allowed_html( 'post' ) ); ?>
															</a>
															<a class="user-commented-on show-less" href="javascript:void(0)">
																<?php echo esc_html( wp_html_excerpt( wp_strip_all_tags( $multicollab_meta['commentedOnText'] ), $multicollab_commented_on_collapse_at, '…' ) ); ?>
															</a>
															<a href="javascript:void(0)" class="cf-show-more">
																<?php echo esc_html__( 'Show all', 'commenting-feature' ); ?>
															</a>
															<?php } elseif ( '' !== $multicollab_meta['commentedOnText'] ) { ?>
															<a class="user-commented-on" href="javascript:void(0)">
																<?php echo wp_kses( $multicollab_meta['commentedOnText'], wp_kses_allowed_html( 'post' ) ); ?>
															</a>
															<?php } ?>
														</blockquote>
													</div>
													<?php
												} elseif ( 'suggestions' === $multicollab_board_type && isset( $multicollab_comment['action'] ) && 'reply' !== $multicollab_comment['action'] ) {
														$multicollab_commenting_function = new Commenting_block_Functions();
														$multicollab_comment['text']    = $multicollab_commenting_function->translate_strings_format( $multicollab_comment['text'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
													?>
													<?php
													$multicollab_sugg_plain_len    = isset( $multicollab_comment['text'] ) ? strlen( wp_strip_all_tags( $multicollab_comment['text'] ) ) : 0;
													$multicollab_sugg_needs_toggle = ( '' !== $multicollab_comment['text'] && $multicollab_sugg_plain_len >= $multicollab_commented_on_collapse_at );
													?>
													<div class="user-commented-on <?php echo ( isset( $multicollab_comment['action'] ) && 'delete' === strtolower( $multicollab_comment['action'] ) ) ? 'delete' : 'add'; ?>">
														<blockquote class="user-commented-icon">
															<?php
															echo '<strong>' . esc_html( $this->translate_suggestion_action_for_display( $multicollab_comment['action'] ) ) . '</strong> : ';
															?>
															<?php if ( $multicollab_sugg_needs_toggle ) { ?>
															<a class="user-commented-on show-all js-hide" href="javascript:void(0)">
																<?php echo esc_html( wp_strip_all_tags( $multicollab_comment['text'] ) ); ?>
															</a>
															<a class="user-commented-on show-less" href="javascript:void(0)">
																<?php echo esc_html( wp_html_excerpt( wp_strip_all_tags( $multicollab_comment['text'] ), $multicollab_commented_on_collapse_at, '…' ) ); ?>
															</a>
															<a href="javascript:void(0)" class="cf-show-more">
																<?php echo esc_html__( 'Show all', 'commenting-feature' ); ?>
															</a>
															<?php } elseif ( '' !== $multicollab_comment['text'] ) { ?>
															<a class="user-commented-on" href="javascript:void(0)">
																<?php echo esc_html( wp_strip_all_tags( $multicollab_comment['text'] ) ); ?>
															</a>
															<?php } ?>
														</blockquote>
													</div>
												<?php } ?>
												<div class="user-comment">
													<span><?php echo wp_kses( $multicollab_thread, wp_kses_allowed_html( 'post' ) ); ?></span>
												</div>
												<?php if ( 1 === $multicollab_comments_counts && 'comments' === $multicollab_board_type && isset( $multicollab_meta['assigned_to'] ) ) { ?>
												<div class="user-assigned-to"><span class="icon"></span><span class="assign-avatar-data"><?php esc_html_e( 'Assigned to', 'commenting-feature' ); ?><a href="mailto:<?php echo esc_attr( $multicollab_assigned_to['user_email'] ); ?>" title="<?php echo esc_attr( $multicollab_assigned_to['display_name'] ); ?>"> <?php echo esc_html( $multicollab_assigned_to['display_name'] ); ?></a></span></div>
												<?php } ?>
												<?php
												$multicollab_cf_comment_status = isset( $multicollab_comment['status'] ) ? $multicollab_comment['status'] : '';
												if ( isset( $multicollab_comment['editedTime'] ) && 'deleted' !== $multicollab_cf_comment_status ) {
													if ( isset( $multicollab_comment['editedTimestamp'] ) ) {
														$multicollab_timestamp = $multicollab_comment['editedTimestamp'];
													} elseif ( 10 !== strlen( $multicollab_comment['editedTime'] ) ) {
														// Update $multicollab_timestamp with edited time, to update side time value.
														$multicollab_d         = DateTime::createFromFormat(
															$multicollab_side_time_format,
															$multicollab_comment['editedTime'],
															wp_timezone()
														);
														$multicollab_timestamp = $multicollab_d ? $multicollab_d->getTimestamp() : $multicollab_timestamp;
													} else {
														$multicollab_timestamp = $multicollab_comment['editedTime'];
													}
													$multicollab_edited_time = date_i18n( $multicollab_side_time_format, $multicollab_timestamp );
													?>
													<time class="user-commented-date user-inner-box-time">(<?php esc_html_e( 'edited', 'commenting-feature' ); ?> <?php echo esc_html( $multicollab_edited_time ); ?>)</time>
												<?php } ?>
											</div>
											<?php
										}
										?>
									</div>
								</div> 
								<?php

								if ( $multicollab_timestamp > $multicollab_side_time_stamp ) {
									$multicollab_side_time_stamp = $multicollab_timestamp;
								}

								if ( ! $multicollab_showallcommentbtnflag && $multicollab_index > 6 && count( $multicollab_all_comments ) > 7 ) {
									$multicollab_showallcommentbtnflag = true;
									$multicollab_reply_count                 = isset( $multicollab_all_comments['resolved'] ) ? count( $multicollab_all_comments ) - 2 : count( $multicollab_all_comments ) - 1;
									?>
									<div class="show-all-comments"><?php echo esc_html__( 'Show all', 'commenting-feature' ); ?> <?php echo esc_html( $multicollab_reply_count ); ?> <?php echo esc_html__( 'replies', 'commenting-feature' ); ?></div>
									<?php
								}

								++$multicollab_index;
							}

							$multicollab_side_time = date_i18n( $multicollab_time_format, $multicollab_side_time_stamp );


							if( 'comments' == $multicollab_board_type){ //phpcs:ignore

								$multicollab_show_reply = 1;
							}


							if ( ! isset($multicollab_meta['resolved_by']) && 1 == $multicollab_show_reply ) { //phpcs:ignore
								?>
								<div class="user-action">
									<a href="<?php echo esc_url( $multicollab_thread_edit_link ); ?>" class="user-cmnt-reply"><?php esc_html_e( 'Reply', 'commenting-feature' ); ?> <span class="tooltip"> <?php esc_html_e( 'Reply to this comment', 'commenting-feature' ); ?> </span></a>
								</div>
							<?php } ?>
							<span class="side-time"><?php echo esc_html( $multicollab_side_time ); ?></span>
						</div>
					</div>
				</div>
			</div>
				<?php
			}
			++$multicollab_count_iterations;
			$multicollab_previous_post_id = $multicollab_activity_post_id;
		}
	}
}

?>
<?php
if ( 'list-view' === $multicollab_activity_view && ! empty( $multicollab_outer_main_timestamp ) && count( $this->cf_activities ) === $multicollab_activity_limit ) {
	$multicollab_latest_displaying_date = gmdate( 'l, F j', $multicollab_outer_main_timestamp );
	$multicollab_term_id                = '';
	if( isset($_GET['cat']) ){  //phpcs:ignore  
		$multicollab_cat_id  = term_exists( intval($_GET['cat']), 'category' ) ; //phpcs:ignore  
		$multicollab_term_id = ( $multicollab_cat_id !== 0 ) ? $multicollab_cat_id['term_id'] : null;
	}

	$multicollab_cpt  = isset($_GET['cpt']);//phpcs:ignore
	?>
	 
	<a href="javascript:void(0)" class="load-more-activity" data-pointer="<?php echo esc_attr( $multicollab_new_pointer ); ?>" data-date="<?php echo esc_attr( $multicollab_latest_displaying_date ); ?>" data-post="<?php echo esc_attr( $multicollab_activity_post_id ); ?>"  data-category ="<?php echo esc_attr( $multicollab_term_id ); ?>" data-cpt= "<?php echo esc_attr( $multicollab_cpt ); ?>" data-board-position="<?php echo esc_attr( $multicollab_board_position ); ?>" style="opacity: 1">Load More</a>
<?php } ?>
