<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wbcomdesigns.com/
 * @since      1.0.0
 *
 * @package    Bp_Checkins
 * @subpackage Bp_Checkins/admin
 * */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $bpchk_globals;
$bpchk_apikey = $bpchk_globals->apikey;

$bpchk_fav_places = get_user_meta( bp_displayed_user_id(), 'bpchk_fav_places', true );

if ( ! empty( $bpchk_fav_places ) && is_array( $bpchk_fav_places ) ) {
	$bpchk_fav_places = array_reverse( $bpchk_fav_places );
} else {
	$bpchk_fav_places = array();
}

// Define how many check-ins to display per page
$bpchk_checkins_per_page = apply_filters( 'bpchk_activity_listing_per_page_user_profile', 10 );

// Get the total number of check-ins
$bpchk_total_checkins = ( ! empty( $bpchk_fav_places ) && is_array( $bpchk_fav_places ) ) ? count( $bpchk_fav_places ) : 0;

// Calculate the total number of pages
$bpchk_total_pages = ! is_null( $bpchk_total_checkins ) ? ceil( $bpchk_total_checkins / $bpchk_checkins_per_page ) : 0;

// Get the current page from the URL, default to 1 if not set
$bpchk_current_page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1; //phpcs:ignore

// Calculate the offset for the query
$bpchk_offset = ($bpchk_current_page - 1) * $bpchk_checkins_per_page;

// Slice the check-ins array to get the current page's check-ins
$bpchk_current_checkins = array_slice( $bpchk_fav_places, $bpchk_offset, $bpchk_checkins_per_page );

if ( ! empty( $bpchk_current_checkins ) && is_array( $bpchk_current_checkins ) ) {	
	?>
	<div class="bpchk-activity-checkins-section">
		<!-- Map section to display user's check-ins -->
		<div id="bpchk-checkins-map" style="height: 400px; width = 100%" ></div>
		
		<div class="bpchk-activity-checkins-list">
			<h4><?php esc_html_e( 'My Check-ins Listing', 'bp-check-in' ); ?></h4>
			<form method="post">
				<table class="">
					<thead>
						<tr>
							<?php if ( bp_loggedin_user_id() == bp_displayed_user_id() ) { ?>
								<th class="contact-me-sender-id"><input type="checkbox" id="bpchk-select-all-contact"/></th> <!-- Select all checkbox for bulk actions -->
							<?php } ?>
							<th class="contact-me-subject"><?php esc_html_e( 'Location', 'bp-check-in' ); ?></th> <!-- Column for check-in location -->
							<th class="contact-me-message"><?php esc_html_e( 'Date', 'bp-check-in' ); ?></th> <!-- Column for check-in date -->
							<th class="contact-me-btn"><?php esc_html_e( 'Action', 'bp-check-in' ); ?></th> <!-- Column for action buttons -->
						</tr>
					</thead>
					<tbody>							
					<?php foreach ( $bpchk_current_checkins as $bpchk_key => $bpchk_fav_place ) : ?>
						<div class="bpchk-checkins-location" 
							data-latitude="<?php echo esc_attr( $bpchk_fav_place['latitude'] ); ?>" 
							data-longitude="<?php echo esc_attr( $bpchk_fav_place['longitude'] ); ?>" 
							data-address="<?php echo esc_attr( $bpchk_fav_place['formatted_address'] ); ?>" 
							data-act-id="<?php echo esc_attr( $bpchk_fav_place['activity_id'] ); ?>">		

							<tr data-latitude="<?php echo esc_attr( $bpchk_fav_place['latitude'] ); ?>" 
								data-longitude="<?php echo esc_attr( $bpchk_fav_place['longitude'] ); ?>" 
								data-address="<?php echo esc_attr( $bpchk_fav_place['formatted_address'] ); ?>" 
								data-act-id="<?php echo esc_attr( $bpchk_fav_place['activity_id'] ); ?>">
								<?php if ( bp_loggedin_user_id() == bp_displayed_user_id() ) { ?>
									<td class="contact-me-sender-id"><input type="checkbox" name="bpchk_activities[]" class="bpchk-all-check" value="<?php echo esc_attr( $bpchk_fav_place['activity_id'] ); ?>"/></td> <!-- Checkbox for selecting individual check-ins for bulk actions -->
								<?php } ?> 
								<td class="bpchk-checkins-address"><?php echo esc_html( $bpchk_fav_place['formatted_address'] ); ?></td> <!-- Display the check-in location address -->
								<?php
								// Fetch activity data for the specific check-in.
								$bpchk_checkins_activity_args = array(
									'activity_ids' => $bpchk_fav_place['activity_id'],
								);
								$bpchk_activity_data = bp_activity_get_specific( $bpchk_checkins_activity_args );						
								?>
								<td><span class="bpchk-checkins-visit-date"><?php echo ( ! empty( $bpchk_activity_data['activities'] ) ) ? esc_html( bp_core_time_since( $bpchk_activity_data['activities'][0]->date_recorded ) ) : esc_html__( 'Date/Time record not found.', 'bp-check-in' ); ?></span></td> <!-- Display the date of the check-in -->
								<td>
									<?php if ( ! empty( $bpchk_fav_place['activity_id'] ) ) : ?>
										<div class="bpchk-checkins-location-view">
											<!-- Link to view the check-in activity -->
											<a href="<?php echo esc_url( bp_activity_get_permalink( $bpchk_fav_place['activity_id'] ) ); ?>" class="bp-tooltip" data-bp-tooltip-pos="up" data-bp-tooltip="<?php esc_attr_e( 'View', 'bp-check-in' ); ?>">
												<span class="dashicons dashicons-visibility" aria-hidden="true"></span>
											</a>
											<?php if ( bp_displayed_user_id() === bp_loggedin_user_id() ) : ?>
												<!-- Option to delete the check-in -->
												<div class="bpchk-trash-checkin-wrap bp-tooltip" data-bp-tooltip-pos="up" data-bp-tooltip="<?php esc_attr_e( 'Trash', 'bp-check-in' ); ?>">
													<span class="dashicons dashicons-dismiss bpchk-trash-checkin" aria-hidden="true" attr-key="<?php echo esc_attr( $bpchk_fav_place['activity_id'] ); ?>"></span>
												</div>
											<?php endif; ?>	
										</div>
									<?php endif; ?>
								</td>
							</tr>
						</div>
					<?php endforeach; ?>
					</tbody>
				</table>
				<div class="bpchk-list-navigation-wrapper">
				<?php if ( bp_loggedin_user_id() == bp_displayed_user_id() ) { ?>
					<!-- Bulk actions for logged-in users -->
					<div class="bpchk-contact-options-nav">
						<div class="select-wrap">
							<label class="bp-screen-reader-text" for="bpchk-select">
								<?php esc_html_e( 'Select Bulk Action', 'bp-check-in' ); ?>
							</label>
							<select name="bpchk_activity_bulk_action" id="bpchk-select">
								<option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bp-check-in' ); ?></option>
								<option value="delete"><?php esc_html_e( 'Delete', 'bp-check-in' ); ?></option>
							</select>
						</div>
						<input type="submit" id="bpchk-bulk-manage" class="button action" value="<?php esc_attr_e( 'Apply', 'bp-check-in' ); ?>">
					</div>
				<?php } ?>
				<?php wp_nonce_field( 'bpchk_activity_bulk_nonce', 'bpchk_activity_bulk_nonce' ); ?> <!-- Nonce field for security -->
				<?php if ( $bpchk_total_pages > 1 ) : ?>
					<div class="bpchk-list-pagination">
						<?php
						// Show "Prev" button if not on the first page
						if ( $bpchk_current_page > 1 ) {
							$bpchk_prev_page = $bpchk_current_page - 1;
							echo '<a href="' . esc_url( add_query_arg( 'page', $bpchk_prev_page ) ) . '" class="prev">' . esc_html__( 'Prev', 'bp-check-in' ) . '</a>';
						}

						// Show pagination only when there are more than 1 page
						if ( $bpchk_total_pages > 1 ) {

							if ( $bpchk_total_pages <= 6 ) {									
									for ( $bpchk_i = 1; $bpchk_i <= $bpchk_total_pages; $bpchk_i++ ) {
										echo '<a href="' . esc_url( add_query_arg( 'page', $bpchk_i ) ) . '" class="' . ( $bpchk_i == $bpchk_current_page ? 'current' : '' ) . '">' . esc_html( $bpchk_i ) . '</a>';
									}
								} else {
									// Modified to show ellipses when there are more than 5 pages
									echo '<a href="' . esc_url( add_query_arg( 'page', 1 ) ) . '" class="' . ( $bpchk_current_page == 1 ? 'current' : '' ) . '">1</a>';
									echo '<a href="' . esc_url( add_query_arg( 'page', 2 ) ) . '" class="' . ( $bpchk_current_page == 2 ? 'current' : '' ) . '">2</a>';

									if ( $bpchk_current_page > 4 ) {
										echo '<span>...</span>';
									}

									for ( $bpchk_i = max( 3, $bpchk_current_page - 1 ); $bpchk_i <= min( $bpchk_current_page + 1, $bpchk_total_pages - 1 ); $bpchk_i++ ) {
										echo '<a href="' . esc_url( add_query_arg( 'page', $bpchk_i ) ) . '" class="' . ( $bpchk_i == $bpchk_current_page ? 'current' : '' ) . '">' . esc_html( $bpchk_i ) . '</a>';
									}

									if ( $bpchk_current_page < $bpchk_total_pages - 3 ) {
										echo '<span>...</span>';
									}

									echo '<a href="' . esc_url( add_query_arg( 'page', $bpchk_total_pages ) ) . '" class="' . ( $bpchk_current_page == $bpchk_total_pages ? 'current' : '' ) . '">' . esc_html( $bpchk_total_pages ) . '</a>';
								}
						}

						// Show "Next" button if not on the last page
						if ( $bpchk_current_page < $bpchk_total_pages ) {
							$bpchk_next_page = $bpchk_current_page + 1;
							echo '<a href="' . esc_url( add_query_arg( 'page', $bpchk_next_page ) ) . '" class="next">' . esc_html__( 'Next', 'bp-check-in' ) . '</a>';
						}
						?>
					</div>
				<?php endif; ?>
			</div>
			</form>
		</div>
	</div>
	<?php
} else {
	?>
	<aside class="bp-feedback bp-messages info">
		<span class="bp-icon" aria-hidden="true"></span>
		<p><?php esc_html_e( 'No check-ins yet. Start exploring and sharing your locations!', 'bp-check-in' ); ?></p>
	</aside>
	<?php
}
