<?php
if (!defined('ABSPATH')) {
	exit;
}

$period_options = array(
	'today'          => __('Today', 'eurostocks'),
	'yesterday'      => __('Yesterday', 'eurostocks'),
	'current_month'  => __('Current month', 'eurostocks'),
	'previous_month' => __('Previous month', 'eurostocks'),
	'custom'         => __('Custom date range', 'eurostocks'),
);

$default_from = isset($default_range['from']) ? $default_range['from'] : '';
$default_to = isset($default_range['to']) ? $default_range['to'] : '';
?>

<div class="wrap eurostocks-wrap eurostocks-api-statistics" data-has-credentials="<?php echo $has_credentials ? '1' : '0'; ?>" data-default-period="<?php echo esc_attr($default_period); ?>" data-default-from="<?php echo esc_attr($default_from); ?>" data-default-to="<?php echo esc_attr($default_to); ?>">
	<h1><?php esc_html_e('API statistics', 'eurostocks'); ?></h1>

	<div class="notice notice-info">
		<p><?php esc_html_e('Statistics for the current day are updated once per hour.', 'eurostocks'); ?></p>
	</div>

	<?php if (!$has_credentials) : ?>
		<div class="notice notice-warning">
			<p><?php esc_html_e('Enter your Eurostocks API details on the settings page to fetch statistics.', 'eurostocks'); ?></p>
		</div>
	<?php endif; ?>

	<form id="eurostocks-api-stats-form" class="eurostocks-stats-form" action="#" method="post">
		<fieldset class="eurostocks-stats-period">
			<legend><?php esc_html_e('Select period', 'eurostocks'); ?></legend>
			<?php foreach ($period_options as $value => $label) : ?>
				<label class="eurostocks-stats-option">
					<input type="radio" name="eurostocks_api_period" value="<?php echo esc_attr($value); ?>" <?php checked($default_period, $value); ?> />
					<span><?php echo esc_html($label); ?></span>
				</label>
			<?php endforeach; ?>
		</fieldset>

		<div class="eurostocks-custom-range <?php echo ('custom' === $default_period) ? '' : 'eurostocks-hidden'; ?>">
			<div class="eurostocks-date-field">
				<label for="eurostocks-from-date"><?php esc_html_e('From', 'eurostocks'); ?></label>
				<input type="date" id="eurostocks-from-date" name="fromDate" value="<?php echo esc_attr($default_from); ?>" placeholder="yyyy-mm-dd" <?php echo ('custom' === $default_period) ? '' : 'disabled'; ?> />
			</div>
			<div class="eurostocks-date-field">
				<label for="eurostocks-to-date"><?php esc_html_e('To', 'eurostocks'); ?></label>
				<input type="date" id="eurostocks-to-date" name="toDate" value="<?php echo esc_attr($default_to); ?>" placeholder="yyyy-mm-dd" <?php echo ('custom' === $default_period) ? '' : 'disabled'; ?> />
			</div>
		</div>

		<div class="eurostocks-stats-actions">
			<button type="button" class="button button-primary" id="eurostocks-fetch-stats"><?php esc_html_e('Fetch statistics', 'eurostocks'); ?></button>
			<button type="button" class="button" id="eurostocks-reset-stats"><?php esc_html_e('Reset', 'eurostocks'); ?></button>
			<span id="eurostocks-active-range" class="description">
				<?php if ($default_from && $default_to) : ?>
					<?php printf(esc_html__('Current period: %1$s to %2$s', 'eurostocks'), esc_html($default_from), esc_html($default_to)); ?>
				<?php endif; ?>
			</span>
		</div>
	</form>

	<div class="eurostocks-stats-notices"></div>
	<div class="notice notice-info eurostocks-stats-loading eurostocks-hidden">
		<p><?php esc_html_e('Fetching statistics...', 'eurostocks'); ?></p>
	</div>

	<div class="eurostocks-stats-cards">
		<?php
		$cards = array(
			array(
				'key'   => 'TotalRequests',
				'label' => __('Total requests', 'eurostocks'),
			),
			array(
				'key'   => 'TotalListRequests',
				'label' => __('Total list requests', 'eurostocks'),
			),
			array(
				'key'   => 'TotalDetailRequests',
				'label' => __('Total detail requests', 'eurostocks'),
			),
			array(
				'key'   => 'TotalSearchRequests',
				'label' => __('Mobile.de Search API requests', 'eurostocks'),
			),
		);

		foreach ($cards as $card) :
			$value = isset($statistics_totals[$card['key']]) ? $statistics_totals[$card['key']] : null;
		?>
			<div class="eurostocks-stat-card">
				<p class="eurostocks-stat-label"><?php echo esc_html($card['label']); ?></p>
				<p class="eurostocks-stat-card-value" data-field="<?php echo esc_attr($card['key']); ?>">
					<?php echo (null !== $value) ? esc_html(number_format((int) $value, 0, ',', '.')) : '—'; ?>
				</p>
			</div>
		<?php endforeach; ?>
	</div>
</div>
