<?php
/**
 * Dashboard template.
 *
 * @package CartNinjaRecovery
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap cartninja-wrap">
	<h1 class="screen-reader-text"><?php esc_html_e( 'Cart Recovery Dashboard', 'cartninja-recovery-for-woocommerce' ); ?></h1>
	<div class="cartninja-page-hero cartninja-page-hero--dashboard">
		<div class="cartninja-page-hero__content">
			<span class="cartninja-page-hero__eyebrow"><?php esc_html_e( 'Performance Overview', 'cartninja-recovery-for-woocommerce' ); ?></span>
			<div class="cartninja-hero-title"><?php esc_html_e( 'Cart Recovery Dashboard', 'cartninja-recovery-for-woocommerce' ); ?></div>
			<p><?php esc_html_e( 'Track abandoned carts, recovered orders, revenue, and channel performance from one view.', 'cartninja-recovery-for-woocommerce' ); ?></p>
		</div>
		<div class="cartninja-page-hero__badge">
			<span class="dashicons dashicons-chart-area" aria-hidden="true"></span>
			<div>
				<strong><?php echo esc_html( number_format_i18n( $stats['recovery_rate'], 2 ) ); ?>%</strong>
				<p><?php esc_html_e( 'Current recovery rate', 'cartninja-recovery-for-woocommerce' ); ?></p>
			</div>
		</div>
	</div>
	<div class="cartninja-card-grid cartninja-card-grid--dashboard">
		<div class="cartninja-card cartninja-stat-card">
			<h3><?php esc_html_e( 'Total Abandoned', 'cartninja-recovery-for-woocommerce' ); ?></h3>
			<strong><?php echo esc_html( number_format_i18n( $stats['total_abandoned'] ) ); ?></strong>
		</div>
		<div class="cartninja-card cartninja-stat-card">
			<h3><?php esc_html_e( 'Total Recovered', 'cartninja-recovery-for-woocommerce' ); ?></h3>
			<strong><?php echo esc_html( number_format_i18n( $stats['total_recovered'] ) ); ?></strong>
		</div>
		<div class="cartninja-card cartninja-stat-card">
			<h3><?php esc_html_e( 'Recovery Rate', 'cartninja-recovery-for-woocommerce' ); ?></h3>
			<strong><?php echo esc_html( number_format_i18n( $stats['recovery_rate'], 2 ) ); ?>%</strong>
		</div>
		<div class="cartninja-card cartninja-stat-card">
			<h3><?php esc_html_e( 'Revenue Recovered', 'cartninja-recovery-for-woocommerce' ); ?></h3>
			<strong><?php echo function_exists( 'wc_price' ) ? wp_kses_post( wc_price( $stats['revenue_recovered'] ) ) : esc_html( number_format_i18n( $stats['revenue_recovered'], 2 ) ); ?></strong>
		</div>
		<div class="cartninja-card cartninja-stat-card">
			<h3><?php esc_html_e( 'Suppressed Carts', 'cartninja-recovery-for-woocommerce' ); ?></h3>
			<strong><?php echo esc_html( number_format_i18n( isset( $stats['total_suppressed'] ) ? $stats['total_suppressed'] : 0 ) ); ?></strong>
		</div>
	</div>

	<div class="cartninja-info-panel">
		<form method="get" class="cartninja-inline-field">
			<input type="hidden" name="page" value="cartninja-dashboard" />
			<select name="period" data-cartninja-period-filter>
				<option value="equals" <?php selected( $period, 'equals' ); ?>><?php esc_html_e( 'Equals', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="not_on" <?php selected( $period, 'not_on' ); ?>><?php esc_html_e( 'Not On', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="after" <?php selected( $period, 'after' ); ?>><?php esc_html_e( 'After', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="before" <?php selected( $period, 'before' ); ?>><?php esc_html_e( 'Before', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="last_7_days" <?php selected( $period, 'last_7_days' ); ?>><?php esc_html_e( 'Last 7 Days', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="last_30_days" <?php selected( $period, 'last_30_days' ); ?>><?php esc_html_e( 'Last 30 Days', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="last_month" <?php selected( $period, 'last_month' ); ?>><?php esc_html_e( 'Last Month', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="this_month" <?php selected( $period, 'this_month' ); ?>><?php esc_html_e( 'This Month', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="last_year" <?php selected( $period, 'last_year' ); ?>><?php esc_html_e( 'Last Year', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="this_year" <?php selected( $period, 'this_year' ); ?>><?php esc_html_e( 'This Year', 'cartninja-recovery-for-woocommerce' ); ?></option>
				<option value="between" <?php selected( $period, 'between' ); ?>><?php esc_html_e( 'Is Between', 'cartninja-recovery-for-woocommerce' ); ?></option>
			</select>
			<input type="date" name="period_date" value="<?php echo esc_attr( $period_date ); ?>" data-cartninja-period-date />
			<input type="date" name="period_date_to" value="<?php echo esc_attr( $period_date_to ); ?>" data-cartninja-period-date-to />
			<?php submit_button( __( 'Apply', 'cartninja-recovery-for-woocommerce' ), 'button', '', false ); ?>
		</form>
		<div class="cartninja-chart-wrap">
			<canvas id="cartninja-revenue-chart"></canvas>
		</div>
	</div>

	<?php if ( cartninja_is_premium_unlocked() ) : ?>
		<div class="cartninja-card-grid">
			<div class="cartninja-card">
				<h3><?php esc_html_e( 'Channel Breakdown', 'cartninja-recovery-for-woocommerce' ); ?></h3>
				<?php foreach ( $channel_breakdown as $row ) : ?>
					<p><strong><?php echo esc_html( strtoupper( $row['channel'] ) ); ?></strong>: <?php echo esc_html( number_format_i18n( $row['sent'] ) ); ?> / <?php echo esc_html( number_format_i18n( $row['rate'], 2 ) ); ?>%</p>
				<?php endforeach; ?>
			</div>
			<div class="cartninja-card">
				<h3><?php esc_html_e( 'Top Abandoned Products', 'cartninja-recovery-for-woocommerce' ); ?></h3>
				<table class="widefat striped">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Product', 'cartninja-recovery-for-woocommerce' ); ?></th>
							<th><?php esc_html_e( 'Count', 'cartninja-recovery-for-woocommerce' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $top_products as $product ) : ?>
							<tr>
								<td><?php echo esc_html( $product['name'] ); ?></td>
								<td><?php echo esc_html( number_format_i18n( $product['count'] ) ); ?></td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		</div>
	<?php endif; ?>
</div>
