<?php
/**
 * Edit a subscription in the admin.
 *
 * @since 3.7
 */

// Edit a subscription.
$user_id = empty( $_REQUEST['user_id'] ) ? $subscription->get_user_id() : sanitize_text_field( $_REQUEST['user_id'] );
$membership_level_id = empty( $_REQUEST['membership_level_id'] ) ? $subscription->get_membership_level_id() : sanitize_text_field( $_REQUEST['membership_level_id'] );
?>

<h1 class="wp-heading-inline"><?php printf( esc_html__( 'Edit Subscription # %s', 'paid-memberships-pro' ), esc_html( $subscription->get_id() ) ); ?></h1>

<form action="" method="post">
	<div class="pmpro_section" data-visibility="shown" data-activated="true">
		<div class="pmpro_section_toggle">
			<button class="pmpro_section-toggle-button" type="button" aria-expanded="true">
				<span class="dashicons dashicons-arrow-up-alt2"></span>
				<?php esc_html_e( 'Member Information', 'paid-memberships-pro' ); ?>
			</button>
		</div>
		<div class="pmpro_section_inside">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row" valign="top"><label for="user_id"><?php esc_html_e( 'User ID', 'paid-memberships-pro' ); ?></label></th>
						<td>
							<input id="user_id" name="user_id" type="text" size="10" value="<?php echo esc_attr( $user_id ); ?>" />
						</td>
					</tr>
					<tr>
						<th scope="row" valign="top"><label for="membership_level_id"><?php esc_html_e( 'Membership Level', 'paid-memberships-pro' ); ?></label></th>
						<td>
							<?php
							// Get all membership levels.
							$levels = pmpro_getAllLevels( true, true );
							?>
							<select id="membership_level_id" name="membership_level_id">
								<?php
								// If the current membership level is not in the list, add it as "ID {membership_level_id} [deleted]".
								if ( ! empty( $membership_level_id ) && ! in_array( $membership_level_id, wp_list_pluck( $levels, 'id' ) ) ) {
									?>
									<option value="<?php echo esc_attr( $membership_level_id ); ?>" selected><?php echo esc_html( sprintf( __( 'ID %d [deleted]', 'paid-memberships-pro' ), $membership_level_id ) ); ?></option>
									<?php
								}

								// Add the rest of the levels.
								foreach ( $levels as $level ) {
									?>
									<option value="<?php echo esc_attr( $level->id ); ?>" <?php selected( $level->id, $membership_level_id ); ?>><?php echo esc_html( $level->name ); ?></option>
									<?php
								}
								?>
							</select>
						</td>
					</tr>
				</tbody>
			</table>
		</div> <!-- end pmpro_section_inside -->
	</div> <!-- end pmpro_section -->

	<div class="pmpro_section" data-visibility="shown" data-activated="true">
		<div class="pmpro_section_toggle">
			<button class="pmpro_section-toggle-button" type="button" aria-expanded="true">
				<span class="dashicons dashicons-arrow-up-alt2"></span>
				<?php esc_html_e( 'Payment Gateway Information', 'paid-memberships-pro' ); ?>
			</button>
		</div>
		<div class="pmpro_section_inside">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row" valign="top"><?php esc_html_e( 'ID', 'paid-memberships-pro' ); ?></th>
						<td><?php echo esc_html( $subscription->get_id() ); ?></td>
					</tr>
					<tr>
						<th scope="row" valign="top"><?php esc_html_e( 'Subscription Transaction ID', 'paid-memberships-pro' ); ?></th>
						<td>
							<code><?php echo esc_html( $subscription->get_subscription_transaction_id() ); ?></code>
							<p class="description"><?php esc_html_e( 'Generated by the gateway. Useful to cross reference subscriptions.', 'paid-memberships-pro' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row" valign="top"><?php esc_html_e( 'Gateway', 'paid-memberships-pro' ); ?></th>
						<td><?php echo esc_html( pmpro_get_gateway_nicename( $subscription->get_gateway() ) ); ?></td>
					</tr>
					<tr>
						<th scope="row" valign="top"><?php esc_html_e( 'Gateway Environment', 'paid-memberships-pro' ); ?></th>
						<td><?php echo esc_html( $subscription->get_gateway_environment() ); ?></td>
					</tr>
				</tbody>
			</table>
		</div> <!-- end pmpro_section_inside -->
	</div> <!-- end pmpro_section -->

	<p class="submit">
		<?php wp_nonce_field( 'edit', 'pmpro_subscriptions_nonce' ); ?>
		<input type="hidden" name="id" value="<?php echo esc_attr( $subscription->get_id() ); ?>" />
		<input type="hidden" name="action" value="edit" />
		<input type="submit" name="save" class="button button-primary" value="<?php esc_attr_e( 'Save Subscription', 'paid-memberships-pro' ); ?>" />
		<input type="button" name="cancel" class="button button-secondary" value="<?php esc_attr_e( 'Cancel', 'paid-memberships-pro' ); ?>" onclick="location.href='<?php echo esc_url( add_query_arg( array( 'page' => 'pmpro-subscriptions', 'id' => $subscription->get_id() ), admin_url( 'admin.php' ) ) ); ?>';" />
	</p>
</form>
