<?php
/**
 * Plan categories 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.

$repo       = new LUCRSURE_Plan_Category_Repository();
$categories = $repo->all( 'name', 'ASC' );
$page_map   = LUCRSURE_Plan_Pages::get_map();
$hub_url    = LUCRSURE_Plan_Pages::get_hub_url();

global $wpdb;
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Plan Categories', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Organize subscription plans into categories.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'plan-categories', array( 'action' => 'new' ) ) ); ?>">+ <?php esc_html_e( 'Add Category', 'lucrativeit-subscription-recurring-billing' ); ?></a>
	<?php if ( ! empty( $categories ) ) : ?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-inline-form">
			<?php wp_nonce_field( 'lucrsure_sync_plan_pages' ); ?>
			<input type="hidden" name="action" value="lucrsure_sync_plan_pages" />
			<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Create public pages', 'lucrativeit-subscription-recurring-billing' ); ?></button>
		</form>
	<?php endif; ?>
</div>

<?php if ( $hub_url ) : ?>
	<div class="lucrsure-card lucrsure-mb-20">
		<p class="lucrsure-muted lucrsure-m-0">
			<?php esc_html_e( 'Public plans hub:', 'lucrativeit-subscription-recurring-billing' ); ?>
			<a href="<?php echo esc_url( $hub_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $hub_url ); ?></a>
		</p>
	</div>
<?php endif; ?>

<?php if ( empty( $categories ) ) : ?>
	<div class="lucrsure-card lucrsure-empty-state">
		<p><?php esc_html_e( 'No plan categories yet. Create your first category to group plans.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
<?php else : ?>
	<div class="lucrsure-cards-grid">
		<?php
		foreach ( $categories as $category ) :
			$plan_count = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter
				$wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE category_id = %d', lucrsure_table( 'plans' ), (int) $category['id'] )
			);
			?>
			<div class="lucrsure-card lucrsure-branch-card">
				<div class="lucrsure-branch-top">
					<span class="lucrsure-branch-icon">&#128193;</span>
					<div class="lucrsure-branch-title">
						<strong><?php echo esc_html( $category['name'] ); ?></strong>
						<span><?php echo esc_html( $category['slug'] ); ?></span>
					</div>
					<?php if ( ! $category['is_active'] ) : ?>
						<span class="lucrsure-pill"><?php esc_html_e( 'Inactive', 'lucrativeit-subscription-recurring-billing' ); ?></span>
					<?php endif; ?>
				</div>
				<div class="lucrsure-branch-body">
					<p class="lucrsure-muted"><?php echo esc_html( $category['description'] ? $category['description'] : __( 'Plan category', 'lucrativeit-subscription-recurring-billing' ) ); ?></p>
					<?php
					$page_url = LUCRSURE_Plan_Pages::get_url( $category['slug'] );
					if ( $page_url ) :
						?>
						<p class="lucrsure-muted"><a href="<?php echo esc_url( $page_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'View public page', 'lucrativeit-subscription-recurring-billing' ); ?></a></p>
					<?php endif; ?>
					<div class="lucrsure-category-shortcodes">
						<?php if ( ! $page_url ) : ?>
							<code>[lucrsure_locations_plans category="<?php echo esc_attr( $category['slug'] ); ?>"]</code>
							<code>[lucrsure_plans category="<?php echo esc_attr( $category['slug'] ); ?>"]</code>
						<?php endif; ?>
					</div>
				</div>
				<div class="lucrsure-branch-foot">
					<span class="lucrsure-muted"><?php echo esc_html( sprintf( /* translators: %d: count */ _n( '%d plan', '%d plans', $plan_count, 'lucrativeit-subscription-recurring-billing' ), $plan_count ) ); ?></span>
					<div class="lucrsure-actions">
						<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'plan-categories', array( 'action' => 'edit', 'id' => $category['id'] ) ) ); ?>"><?php esc_html_e( 'Edit', 'lucrativeit-subscription-recurring-billing' ); ?></a>
						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="lucrsure-inline-form lucrsure-confirm-submit" data-lucrsure-confirm="<?php echo esc_attr__( 'Delete this category?', 'lucrativeit-subscription-recurring-billing' ); ?>">
							<?php wp_nonce_field( 'lucrsure_delete_record' ); ?>
							<input type="hidden" name="action" value="lucrsure_delete_record" />
							<input type="hidden" name="type" value="plan_category" />
							<input type="hidden" name="id" value="<?php echo esc_attr( $category['id'] ); ?>" />
							<button type="submit" class="lucrsure-btn lucrsure-btn-light"><?php esc_html_e( 'Delete', 'lucrativeit-subscription-recurring-billing' ); ?></button>
						</form>
					</div>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
<?php endif; ?>
