<?php
/**
 * Admin template: Dashboard.
 *
 * Variables provided by Dashboard::render():
 *   @var array      $stats               {total_count, total_amount, avg_amount, monthly[]}
 *   @var array      $recent              Donation[]
 *   @var string     $currency            ISO currency code.
 *   @var int        $goal                Goal amount in major currency unit.
 *   @var int        $progress            Percentage towards goal (0–100).
 *   @var array|null $subscription_stats  {active_count, mrr_cents, total_subscribers}|null
 *
 * @package Donatico
 */

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

// Variables are set by Dashboard::render() before this template is included.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$total_raised = number_format( $stats['total_amount'] / 100, 2 );
$avg_amount   = number_format( $stats['avg_amount'] / 100, 2 );

if ( $subscription_stats !== null ) {
	$mrr_formatted = number_format( $subscription_stats['mrr_cents'] / 100, 2 );
}
?>

<div class="wrap donatico-wrap">
	<h1 class="wp-heading-inline">
		<?php esc_html_e( 'Donatico Dashboard', 'donatico' ); ?>
	</h1>
	<a href="<?php echo esc_url( admin_url( 'admin.php?page=donatico-settings' ) ); ?>" class="page-title-action">
		<?php esc_html_e( 'Settings', 'donatico' ); ?>
	</a>
	<hr class="wp-header-end">

	<?php /* -- Stat cards -- */ ?>
	<div class="donatico-stats-grid">

		<div class="donatico-stat-card">
			<span class="donatico-stat-icon dashicons dashicons-money-alt"></span>
			<div class="donatico-stat-body">
				<span class="donatico-stat-value">
					<?php echo esc_html( $currency . ' ' . $total_raised ); ?>
				</span>
				<span class="donatico-stat-label"><?php esc_html_e( 'Total Raised', 'donatico' ); ?></span>
			</div>
		</div>

		<div class="donatico-stat-card">
			<span class="donatico-stat-icon dashicons dashicons-heart"></span>
			<div class="donatico-stat-body">
				<span class="donatico-stat-value">
					<?php echo esc_html( number_format( $stats['total_count'] ) ); ?>
				</span>
				<span class="donatico-stat-label"><?php esc_html_e( 'Total Donations', 'donatico' ); ?></span>
			</div>
		</div>

		<div class="donatico-stat-card">
			<span class="donatico-stat-icon dashicons dashicons-chart-bar"></span>
			<div class="donatico-stat-body">
				<span class="donatico-stat-value">
					<?php echo esc_html( $currency . ' ' . $avg_amount ); ?>
				</span>
				<span class="donatico-stat-label"><?php esc_html_e( 'Average Donation', 'donatico' ); ?></span>
			</div>
		</div>

		<?php if ( $subscription_stats !== null ) : ?>

		<div class="donatico-stat-card">
			<span class="donatico-stat-icon dashicons dashicons-update"></span>
			<div class="donatico-stat-body">
				<span class="donatico-stat-value">
					<?php echo esc_html( $currency . ' ' . $mrr_formatted ); ?>
				</span>
				<span class="donatico-stat-label"><?php esc_html_e( 'Monthly Recurring Revenue', 'donatico' ); ?></span>
			</div>
		</div>

		<div class="donatico-stat-card">
			<span class="donatico-stat-icon dashicons dashicons-groups"></span>
			<div class="donatico-stat-body">
				<span class="donatico-stat-value">
					<?php echo esc_html( number_format( $subscription_stats['total_subscribers'] ) ); ?>
				</span>
				<span class="donatico-stat-label"><?php esc_html_e( 'Active Subscribers', 'donatico' ); ?></span>
			</div>
		</div>

		<?php endif; ?>

	</div>

	<?php if ( $goal > 0 ) : ?>
	<?php /* -- Progress bar -- */ ?>
	<div class="donatico-card donatico-goal-card">
		<h2 class="donatico-card-title"><?php esc_html_e( 'Goal Progress', 'donatico' ); ?></h2>
		<div class="donatico-progress-track">
			<div class="donatico-progress-bar" style="width:<?php echo esc_attr( $progress ); ?>%"></div>
		</div>
		<p class="donatico-goal-meta">
			<?php
			printf(
				/* translators: 1: currency + amount raised, 2: goal amount, 3: percentage */
				esc_html__( '%1$s raised of %2$s goal (%3$s%%)', 'donatico' ),
				esc_html( $currency . ' ' . $total_raised ),
				esc_html( $currency . ' ' . number_format( $goal, 2 ) ),
				esc_html( (string) $progress )
			);
			?>
		</p>
	</div>
	<?php endif; ?>

	<?php /* -- Recent donations -- */ ?>
	<div class="donatico-card">
		<h2 class="donatico-card-title">
			<?php esc_html_e( 'Recent Donations', 'donatico' ); ?>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=donatico-donations' ) ); ?>" class="donatico-card-link">
				<?php esc_html_e( 'View all →', 'donatico' ); ?>
			</a>
		</h2>

		<?php if ( empty( $recent ) ) : ?>
			<p class="description"><?php esc_html_e( 'No completed donations yet.', 'donatico' ); ?></p>
		<?php else : ?>
		<table class="wp-list-table widefat fixed striped donatico-recent-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Donor', 'donatico' ); ?></th>
					<th><?php esc_html_e( 'Amount', 'donatico' ); ?></th>
					<th><?php esc_html_e( 'Date', 'donatico' ); ?></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach ( $recent as $donation ) : ?>
				<tr>
					<td>
						<?php echo esc_html( $donation->getDisplayName() ); ?>
						<?php if ( ! $donation->isAnonymous() ) : ?>
							<br><small><?php echo esc_html( $donation->getDonorEmail() ); ?></small>
						<?php endif; ?>
					</td>
					<td>
						<strong><?php echo esc_html( $donation->getFormattedAmount() ); ?></strong>
						<small><?php echo esc_html( $donation->getCurrency() ); ?></small>
					</td>
					<td>
						<?php
						echo esc_html(
							wp_date(
								get_option( 'date_format' ),
								strtotime( $donation->getCreatedAt() )
							)
						);
						?>
					</td>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>
		<?php endif; ?>
	</div>

	<?php /* -- Shortcode reference -- */ ?>
	<div class="donatico-card donatico-shortcode-card">
		<h2 class="donatico-card-title"><?php esc_html_e( 'Shortcodes', 'donatico' ); ?></h2>
		<table class="widefat donatico-shortcode-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Shortcode', 'donatico' ); ?></th>
					<th><?php esc_html_e( 'Description', 'donatico' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><code>[donatico_form]</code></td>
					<td><?php esc_html_e( 'Renders the donation form.', 'donatico' ); ?></td>
				</tr>
				<tr>
					<td><code>[donatico_total_raised]</code></td>
					<td><?php esc_html_e( 'Displays total amount raised.', 'donatico' ); ?></td>
				</tr>
				<tr>
					<td><code>[donatico_recent_donors]</code></td>
					<td><?php esc_html_e( 'Lists recent donors.', 'donatico' ); ?></td>
				</tr>
			</tbody>
		</table>
	</div>

</div>
