<?php
/**
 * Subscriptions list view.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

if ( ! current_user_can( lucrsure_admin_capability() ) ) {
	wp_die( esc_html__( 'You are not allowed to access this page.', 'lucrativeit-subscription-recurring-billing' ) );
}

$repo   = new LUCRSURE_Subscription_Repository();
$search = '';
$status = '';

if ( isset( $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_filter_subscriptions' )
) {
	$search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
	$status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
}

$subscriptions = $repo->query(
	array(
		'search' => $search,
		'status' => $status,
	)
);
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Subscriptions', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Track active, trialing, and canceled subscription lifecycles.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'subscriptions', array( 'action' => 'new' ) ) ); ?>">+ <?php esc_html_e( 'New Subscription', 'lucrativeit-subscription-recurring-billing' ); ?></a>
</div>

<form class="lucrsure-card lucrsure-toolbar" method="get">
	<input type="hidden" name="page" value="lucrsure-subscriptions" />
	<?php wp_nonce_field( 'lucrsure_filter_subscriptions', '_wpnonce', false ); ?>
	<div class="lucrsure-search lucrsure-search-wide">
		<span class="lucrsure-search-icon" aria-hidden="true">&#128269;</span>
		<input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search by customer...', 'lucrativeit-subscription-recurring-billing' ); ?>" />
	</div>
	<select name="status" class="lucrsure-auto-submit">
		<option value=""><?php esc_html_e( 'Status: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<?php foreach ( lucrsure_subscription_statuses() as $key => $label ) : ?>
			<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $status, $key ); ?>><?php echo esc_html( $label ); ?></option>
		<?php endforeach; ?>
	</select>
	<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Filter', 'lucrativeit-subscription-recurring-billing' ); ?></button>
</form>

<div class="lucrsure-card lucrsure-table-card">
	<table class="lucrsure-table">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Customer', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Plan', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'MRR', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Status', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th><?php esc_html_e( 'Started', 'lucrativeit-subscription-recurring-billing' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $subscriptions ) ) : ?>
				<tr><td colspan="6" class="lucrsure-empty"><?php esc_html_e( 'No subscriptions found.', 'lucrativeit-subscription-recurring-billing' ); ?></td></tr>
			<?php else : ?>
				<?php foreach ( $subscriptions as $sub ) : ?>
					<tr>
						<td><strong><?php echo esc_html( $sub['customer_name'] ); ?></strong></td>
						<td><span class="lucrsure-pill lucrsure-pill-blue"><?php echo esc_html( $sub['plan_name'] ); ?></span></td>
						<td><?php echo esc_html( lucrsure_format_money( $sub['mrr'], $sub['plan_currency'] ) ); ?></td>
						<td><span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $sub['status'] ); ?>"><?php echo esc_html( $sub['status'] ); ?></span></td>
						<td><?php echo esc_html( $sub['start_date'] ? date_i18n( 'M j, Y', strtotime( $sub['start_date'] ) ) : '—' ); ?></td>
						<td class="lucrsure-right">
							<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'subscriptions', array( 'action' => 'edit', 'id' => $sub['id'] ) ) ); ?>"><?php esc_html_e( 'Manage', 'lucrativeit-subscription-recurring-billing' ); ?></a>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</div>
