<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; /* Exit if accessed directly*/
}

$lms_current_user = STM_LMS_User::get_current_user( '', true, true );

do_action( 'stm_lms_template_main' );
do_action( 'masterstudy_before_account', $lms_current_user );
wp_enqueue_style( 'masterstudy-account-main' );
wp_enqueue_style( 'masterstudy-account-memberships-pmp' );
?>

<div class="masterstudy-account">
	<?php do_action( 'stm_lms_admin_after_wrapper_start', $lms_current_user ); ?>
	<div class="masterstudy-account-sidebar">
		<div class="masterstudy-account-sidebar__wrapper">
			<?php do_action( 'masterstudy_account_sidebar', $lms_current_user ); ?>
		</div>
	</div>
	<div class="masterstudy-account-container">
		<div class="masterstudy-account-pmp-memberships">
			<?php
			global $wpdb, $pmpro_msg, $pmpro_msgt, $pmpro_levels, $current_user, $levels;

			$pmpro_order = new MemberOrder();
			$pmpro_order->getLastMemberOrder();
			$mylevels     = pmpro_getMembershipLevelsForUser();
			$pmpro_levels = pmpro_getAllLevels( false, true );
			$gateway      = pmpro_getOption( 'gateway' );
			$invoices     = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT *, UNIX_TIMESTAMP(CONVERT_TZ(timestamp, '+00:00', @@global.time_zone)) as timestamp FROM $wpdb->pmpro_membership_orders WHERE user_id = %d AND status NOT IN('review', 'token', 'error') ORDER BY timestamp DESC LIMIT 4",
					$current_user->ID
				)
			);
			?>
			<span class="masterstudy-account-pmp-memberships__section-title"><?php esc_html_e( 'Active Memberships', 'masterstudy-lms-learning-management-system' ); ?></span>
			<div class="masterstudy-account-pmp-memberships__memberships <?php echo esc_attr( pmpro_get_element_class( 'pmpro_box', 'pmpro_account-membership' ) ); ?>">
				<table class="<?php echo esc_attr( pmpro_get_element_class( 'pmpro_table' ) ); ?>" width="100%"
						cellpadding="0" cellspacing="0" border="0">
					<thead>
					<tr>
						<th><?php esc_html_e( 'Level', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th><?php esc_html_e( 'Billing', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th><?php esc_html_e( 'Expiration', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th></th>
					</tr>
					</thead>
					<tbody>
					<?php if ( empty( $mylevels ) ) { ?>
						<tr>
							<td colspan="3">
								<?php
								/* Check to see if the user has a cancelled order*/
								$pmpro_order = new MemberOrder();
								$pmpro_order->getLastMemberOrder( $current_user->ID, array( 'cancelled', 'expired', 'admin_cancelled' ) );

								if ( isset( $pmpro_order->membership_id ) && ! empty( $pmpro_order->membership_id ) && empty( $level->id ) ) {
									$level = pmpro_getLevel( $pmpro_order->membership_id );
								}

								// If no level check for a default level.
								if ( empty( $level ) || ! $level->allow_signups ) {
									$default_level_id = apply_filters( 'pmpro_default_level', 0 );
								}

								// Show the correct checkout link.
								if ( ! empty( $level ) && ! empty( $level->allow_signups ) ) {
									printf(
									/* translators: %s URL */
										wp_kses_post( __( "Your membership is not active. <a href='%s'>Renew now.</a>", 'masterstudy-lms-learning-management-system' ) ),
										esc_url( pmpro_url( 'checkout', '?level=' . $level->id ) )
									);
								} elseif ( ! empty( $default_level_id ) ) {
									printf(
									/* translators: %s URL */
										wp_kses_post( __( "You do not have an active membership. <a href='%s'>Register here.</a>", 'masterstudy-lms-learning-management-system' ) ),
										esc_url( pmpro_url( 'checkout', '?level=' . $default_level_id ) )
									);
								} else {
									printf(
									/* translators: %s URL */
										wp_kses_post( __( "You do not have an active membership. <a href='%s'>Choose a membership level.</a>", 'masterstudy-lms-learning-management-system' ) ),
										esc_url( pmpro_url( 'levels' ) )
									);
								}
								?>
							</td>
						</tr>
					<?php } else { ?>
						<?php
						foreach ( $mylevels as $level ) {
							?>
							<tr>
								<td class="masterstudy-account-pmp-memberships__memberships-level-title <?php echo esc_attr( pmpro_get_element_class( 'pmpro_account-membership-levelname' ) ); ?>">
									<?php echo esc_html( $level->name ); ?>
								</td>
								<td class="masterstudy-account-pmp-memberships__memberships-level-price <?php echo esc_attr( pmpro_get_element_class( 'pmpro_account-membership-levelfee' ) ); ?>">
									<p><?php echo wp_kses_post( pmpro_getLevelCost( $level, true, true ) ); ?></p>
								</td>
								<td class="masterstudy-account-pmp-memberships__memberships-level-expiration <?php echo esc_attr( pmpro_get_element_class( 'pmpro_account-membership-expiration' ) ); ?>" data-td="<?php echo esc_html__( 'Expiration', 'masterstudy-lms-learning-management-system' ); ?>: ">
									<?php
									$expiration_text = '<p>';
									if ( $level->enddate ) {
										$expiration_text .= date_i18n( get_option( 'date_format' ), $level->enddate );
										/**
										 * Filter to include the expiration time with expiration date
										 *
										 * @param bool $pmpro_show_time_on_expiration_date Show the expiration time with expiration date (default: false).
										 *
										 * @return bool $pmpro_show_time_on_expiration_date Whether to show the expiration time with expiration date.
										 */
										if ( apply_filters( 'pmpro_show_time_on_expiration_date', false ) ) {
											$expiration_text .= ' ' . date_i18n( get_option( 'time_format', __( 'g:i a' ) ), $level->enddate );
										}
									} else {
										$expiration_text .= esc_html_x( '&#8212;', 'A dash is shown when there is no expiration date.', 'masterstudy-lms-learning-management-system' );
									}
									$expiration_text .= '</p>';
									echo wp_kses_post( apply_filters( 'pmpro_account_membership_expiration_text', $expiration_text, $level ) );
									?>
								</td>
								<td class="masterstudy-account-pmp-memberships__memberships-actions">
									<div
										class="<?php echo esc_attr( pmpro_get_element_class( 'pmpro_actionlinks' ) ); ?>">
										<?php
										do_action( 'pmpro_member_action_links_before' );

										// Build the links to return.
										$pmpro_member_action_links = array();

										if ( array_key_exists( $level->id, $pmpro_levels ) && pmpro_isLevelExpiringSoon( $level ) ) {
											STM_LMS_Templates::show_lms_template(
												'components/button',
												array(
													'id'   => 'pmpro_actionlink-renew',
													'class' => 'masterstudy-account-pmp-memberships__renew-btn',
													'title' => esc_html__( 'Renew', 'masterstudy-lms-learning-management-system' ),
													'link' => esc_url( add_query_arg( 'level', $level->id, pmpro_url( 'checkout', '', 'https' ) ) ),
													'style' => 'secondary',
													'size' => 'sm',
												)
											);
										}

										if ( ( isset( $pmpro_order->status ) && 'success' === $pmpro_order->status ) && ( 'stripe' === $gateway ) && pmpro_isLevelRecurring( $level ) ) {
											STM_LMS_Templates::show_lms_template(
												'components/button',
												array(
													'id'   => 'pmpro_actionlink-change',
													'class' => 'masterstudy-account-pmp-memberships__edit-btn',
													'title' => esc_html__( 'Edit', 'masterstudy-lms-learning-management-system' ),
													'link' => esc_url( pmpro_url( 'billing', '', 'https' ) ),
													'style' => 'secondary',
													'size' => 'sm',
												)
											);
										}
											/* To do: Only show CHANGE link if this level is in a group that has upgrade/downgrade rules*/
										if ( count( $pmpro_levels ) > 1 && ! defined( 'PMPRO_DEFAULT_LEVEL' ) ) {
											STM_LMS_Templates::show_lms_template(
												'components/button',
												array(
													'id'   => 'pmpro_actionlink-change',
													'class' => 'masterstudy-account-pmp-memberships__change-btn',
													'title' => esc_html__( 'Change', 'masterstudy-lms-learning-management-system' ),
													'link' => esc_url( pmpro_url( 'levels' ) ),
													'style' => 'secondary',
													'size' => 'sm',
												)
											);
										}

										if ( array_key_exists( $level->id, $pmpro_levels ) ) {
											STM_LMS_Templates::show_lms_template(
												'components/button',
												array(
													'id'   => 'pmpro_actionlink-billing',
													'class' => 'masterstudy-account-pmp-memberships__cancel-btn',
													'title' => '',
													'link' => esc_url( add_query_arg( 'levelstocancel', $level->id, pmpro_url( 'cancel' ) ) ),
													'style' => 'secondary',
													'size' => 'sm',
												)
											);
										}
										do_action( 'pmpro_member_action_links_after' );
										?>
									</div> <!-- end pmpro_actionlinks -->
								</td>
							</tr>
						<?php } ?>
					<?php } ?>
					</tbody>
				</table>
			</div> <!-- end pmpro_account-membership -->
			<span class="masterstudy-account-pmp-memberships__section-title"><?php esc_html_e( 'Past Invoices', 'masterstudy-lms-learning-management-system' ); ?></span>
			<div
				class="masterstudy-account-pmp-memberships__past-invoices <?php echo esc_attr( pmpro_get_element_class( 'pmpro_box', 'pmpro_account-invoices' ) ); ?>">
				<table class="<?php echo esc_attr( pmpro_get_element_class( 'pmpro_table' ) ); ?>" width="100%"
						cellpadding="0" cellspacing="0" border="0">
					<thead>
					<tr>
						<th><?php esc_html_e( 'Date', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th><?php esc_html_e( 'Level', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th><?php esc_html_e( 'Amount', 'masterstudy-lms-learning-management-system' ); ?></th>
						<th><?php esc_html_e( 'Status', 'masterstudy-lms-learning-management-system' ); ?></th>
					</tr>
					</thead>
					<tbody>
					<?php
					foreach ( $invoices as $invoice ) {
						/* get a member order object*/
						$invoice_id = $invoice->id;
						$invoice    = new MemberOrder();

						$invoice->getMemberOrderByID( $invoice_id );
						$invoice->getMembershipLevel();

						// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
						if ( in_array( $invoice->status, array( '', 'success', 'cancelled' ) ) ) {
							$display_status = __( 'Paid', 'masterstudy-lms-learning-management-system' );
						} elseif ( 'pending' === $invoice->status ) {
							// Some Add Ons set status to pending.
							$display_status = __( 'Pending', 'masterstudy-lms-learning-management-system' );
						} elseif ( 'refunded' === $invoice->status ) {
							$display_status = __( 'Refunded', 'masterstudy-lms-learning-management-system' );
						}
						?>
						<tr id="pmpro_account-invoice-<?php echo esc_attr( $invoice->code ); ?>">
							<td class="masterstudy-account-pmp-memberships__past-invoices-date">
								<a href="<?php echo esc_url( pmpro_url( 'invoice', '?invoice=' . $invoice->code ) ); ?>"><?php echo esc_html( date_i18n( get_option( 'date_format' ), $invoice->getTimestamp() ) ); ?></a>
							</td>
							<td class="masterstudy-account-pmp-memberships__past-invoices-name">
								<?php
								if ( ! empty( $invoice->membership_level ) ) {
									echo esc_html( $invoice->membership_level->name );
								} else {
									echo esc_html__( 'N/A', 'masterstudy-lms-learning-management-system' );
								}
								?>
							</td>
							<td class="masterstudy-account-pmp-memberships__past-invoices-price"><?php echo wp_kses_post( pmpro_escape_price( pmpro_formatPrice( $invoice->total ) ) ); ?></td>
							<td class="masterstudy-account-pmp-memberships__past-invoices-status-row">
								<div class="masterstudy-account-pmp-memberships__past-invoices-status masterstudy-account-pmp-memberships__past-invoices-status_<?php echo esc_attr( $invoice->status ); ?>">
									<?php echo ( isset( $display_status ) ) ? esc_html( $display_status ) : ''; ?>
								</div>
							</td>
						</tr>
						<?php
					}
					?>
					</tbody>
				</table>
				<?php if ( count( $invoices ) === 4 ) { ?>
					<div class="view_all_invoices"><a id="pmpro_actionlink-invoices"
														href="<?php echo esc_url( pmpro_url( 'invoice' ) ); ?>"><?php esc_html_e( 'View All Invoices', 'masterstudy-lms-learning-management-system' ); ?></a>
					</div>
				<?php } ?>
			</div> <!-- end pmpro_account-invoices -->
		</div> <!-- end pmpro_account -->
	</div>
</div>
<?php do_action( 'masterstudy_after_account', $lms_current_user ); ?>
