<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Gigneo\Notifications;

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.
$ref        = ! empty( $_GET['ref'] ) ? sanitize_text_field( wp_unslash( $_GET['ref'] ) ) : 'all';
$p_per_page = ! empty( $_GET['per_page'] ) ? absint( wp_unslash( $_GET['per_page'] ) ) : 10;
$p_paged    = ! empty( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 1;
// phpcs:enable WordPress.Security.NonceVerification.Recommended -- Read-only parameters no nonce required.

$ref        = $args['ref'] ?? $ref;
$p_per_page = $args['per_page'] ?? $p_per_page;
$p_paged    = $args['paged'] ?? $p_paged;

$ref = ! empty( $ref ) && in_array( $ref, array( 'all', 'read', 'unread' ), true ) ? $ref : 'all';

$profile_id   = gigneo_active_profile_id();
$profile_type = gigneo_profile_type( $profile_id );
$date_format  = get_option( 'date_format' );

$notification_data = Notifications::get_notifications( $p_per_page, $p_paged, $ref );
$total_page        = $notification_data['total_pages'] ?? 1;
$notifications     = $notification_data['notifications'] ?? array();
?>
<div id="gn-notifications-wrapper" class="gn-notification-wrapper">
	<div class="gn-listing-actions-wrapper">
		<ul id="gn-order-filters" class="gn-filters">
			<li class="gn-filter<?php echo esc_attr( empty( $ref ) || 'all' === $ref ? ' gn-active' : '' ); ?>">
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'notification', 'all' ) ); ?>" class="gn-title"><?php echo esc_html__( 'All', 'gigneo' ); ?></a>
			</li>
			<li class="gn-filter<?php echo esc_attr( 'read' === $ref ? ' gn-active' : '' ); ?>">
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'notification', 'read' ) ); ?>" class="gn-title"><?php echo esc_html__( 'Read', 'gigneo' ); ?></a>
			</li>
			<li class="gn-filter<?php echo esc_attr( 'unread' === $ref ? ' gn-active' : '' ); ?>">
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'notification', 'unread' ) ); ?>" class="gn-title"><?php echo esc_html__( 'Unread', 'gigneo' ); ?></a>
			</li>
		</ul>
		<div class="gn-bulk-action-bar">
			<div class="gn-action-bar-actions">
				<div class="gn-select-all gn-checkbox">
					<input type="checkbox" id="gn-select-all-notifications"/>
					<label class="gn-btn gn-btn-outline-grey gn-btn-md" for="gn-select-all-notifications">
						<?php esc_html_e( 'Select all', 'gigneo' ); ?>
					</label>
				</div>
				<button id="gn-marked-read-notifications" disabled type="button" class="gn-btn gn-btn-outline-grey gn-btn-md">
					<i class="hi hi-envelope-open" aria-hidden="true"></i>
					<?php esc_html_e( 'Mark as read', 'gigneo' ); ?>
				</button>
				<button id="gn-delete-notifications" disabled type="button" class="gn-btn gn-btn-outline-danger gn-btn-md">
					<i class="hi hi-trash" aria-hidden="true"></i>
					<?php esc_html_e( 'Delete', 'gigneo' ); ?>
				</button>
			</div>
			<button id="gn-action-bar-toggler" data-gn-select-text="<?php echo esc_html__( 'Select', 'gigneo' ); ?>" data-gn-cancel-text="<?php echo esc_html__( 'Cancel', 'gigneo' ); ?>" class="gn-btn gn-btn-outline-grey gn-btn-md">
				<i class="hi hi-list-bullet" aria-hidden="true"></i>
			</button>
		</div>
	</div>
	<?php if ( ! empty( $notifications ) ) { ?>
	<div class="gn-notification-list">
		<div id="gn-notification-form" class="gn-notification-form">
			<?php
			$current_group = '';

			foreach ( $notifications as $key => $notification ) {
				// Notification details
				$notification_profile_id = $notification['profile_id'] ?? '';
				$notification_post_id    = $notification['post_id'] ?? '';
				$notification_title      = $notification['title'] ?? '';
				$notification_content    = $notification['content'] ?? '';
				$notification_category   = $notification['category'] ?? '';
				$notification_type       = $notification['type'] ?? '';
				$notification_status     = $notification['status'] ?? '';
				$notification_meta       = $notification['meta'] ?? '';
				$notification_created_at = $notification['created_at_gmt'] ?? '';
				?>
				<div id="gn-notification-<?php echo esc_attr( $notification['id'] ); ?>" class="gn-notification-item gn-type-<?php echo esc_attr( $notification_type ); ?> gn-<?php echo esc_attr( 'read' === $notification_status ? 'read' : 'unread' ); ?>">
					<div class="gn-checkbox">
						<input id="gn-notification-checkbox-<?php echo esc_attr( $notification['id'] ); ?>" type="checkbox" name="gn-selected-notifications[]" value="<?php echo esc_attr( $notification['id'] ); ?>">
						<label for="gn-notification-checkbox-<?php echo esc_attr( $notification['id'] ); ?>"></label>
					</div>
					<?php
					//Avatar
					$participant_profile_thumbnail_id = Notifications::avatar( $notification_category, $notification_type, $notification_profile_id, $notification_post_id );
					if ( false !== $participant_profile_thumbnail_id ) {
						?>
						<figure class="gn-avatar">
							<?php gigneo_attachment_image_src( $participant_profile_thumbnail_id, 'thumbnail', 'profile' ); ?>
						</figure>
						<?php
					} else {
						$icon_data  = Notifications::icon( $notification_type );
						$icon       = $icon_data['icon'];
						$icon_class = $icon_data['status'];
						?>
						<span class="gn-icon gn-status-<?php echo esc_attr( $icon_class ); ?>">
							<i class="<?php echo esc_html( $icon ); ?>" aria-hidden="true"></i>
						</span>
					<?php } ?>
					<div class="gn-info">
						<div class="gn-head">
							<h6 class="gn-title"><?php echo esc_html( $notification_title ); ?></h6>
							<p class="gn-description"><?php echo esc_html( $notification_content ); ?></p>
						</div>
						<div class="gn-body">
							<div class="gn-meta">
								<span class="gn-actions">
									<?php
									$notification_button = Notifications::link( $notification_category, $notification_type, $notification_profile_id, $notification_post_id );
									if ( ! empty( $notification_button['link'] ) ) {
										?>
										<a href="<?php echo esc_url( $notification_button['link'] ); ?>" target="<?php echo esc_attr( $notification_button['target'] ?? '_self' ); ?>" class="gn-btn gn-btn gn-btn-outline-info">
										<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
										</a>
										<?php
									}
									?>
									<button type="button" class="gn-btn gn-btn-outline-grey gn-notification-mark-read"
											data-gn-status="<?php echo esc_attr( 'read' === $notification_status ? 'unread' : 'read' ); ?>"
											data-gn-id="<?php echo esc_attr( $notification['id'] ); ?>">
										<i class="hi hi-envelope" aria-hidden="true"></i>
									</button>
									<button type="button" class="gn-btn gn-btn-outline-danger gn-delete-notification"
											data-gn-id="<?php echo esc_attr( $notification['id'] ); ?>">
										<i class="hi hi-trash" aria-hidden="true"></i>
									</button>
								</span>
							</div>
						</div>
						<div class="gn-footer">
							<?php
							$notification_date = Notifications::date( $notification_created_at, $notification_profile_id );
							?>
							<span class="gn-date"><?php echo esc_html( $notification_date ); ?></span>
						</div>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
		<?php if ( $p_paged < $total_page ) { ?>
		<div class="gn-load-more-wrapper">
			<button id="gn-load-more-notifications" data-gn-current-page="<?php echo esc_attr( $p_paged ); ?>" data-gn-per-page="<?php echo esc_attr( $p_per_page ); ?>" data-gn-total-pages="<?php echo esc_attr( $total_page ); ?>" data-gn-ref="<?php echo esc_attr( $ref ); ?>" type="button" class="gn-link gn-link-primary gn-load-more-button">
				<?php echo esc_html__( 'Load more notifications', 'gigneo' ); ?>
			</button>
		</div>
			<?php
		}
	} else {
		$not_found_args = array(
			'title' => esc_html__( 'No notification found', 'gigneo' ),
		);
		gigneo_not_found( 'notification', $not_found_args );
	}
	?>
</div>
