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

$base_fields = array();

if ($vehicle) {
	$base_fields = array(
		array(
			'label' => __('Make', 'eurostocks'),
			'value' => isset($vehicle['make']) ? $vehicle['make'] : '-',
		),
		array(
			'label' => __('Model', 'eurostocks'),
			'value' => isset($vehicle['modelDescription']) ? $vehicle['modelDescription'] : (isset($vehicle['model']) ? $vehicle['model'] : '-'),
		),
		array(
			'label' => __('Mileage', 'eurostocks'),
			'value' => (isset($vehicle['mileage']) && '' !== $vehicle['mileage'])
				? number_format((float) $vehicle['mileage'], 0, ',', '.') . ' km'
				: '-',
		),
		array(
			'label' => __('Power', 'eurostocks'),
			'value' => (isset($vehicle['power']) && '' !== $vehicle['power'])
				? sprintf(
					'%s kW (%s %s)',
					number_format((float) $vehicle['power'], 0, ',', '.'),
					number_format((float) ($vehicle['power'] * 1.362), 0, ',', '.'),
					__('HP', 'eurostocks')
				)
				: '-',
		),
		array(
			'label' => __('Registration date', 'eurostocks'),
			'value' => isset($vehicle['firstRegistrationDate']) && '' !== $vehicle['firstRegistrationDate'] ? $vehicle['firstRegistrationDate'] : '-',
		),
		array(
			'label' => __('Fuel', 'eurostocks'),
			'value' => isset($vehicle['fuelType']) && '' !== $vehicle['fuelType'] ? $vehicle['fuelType'] : '-',
		),
		array(
			'label' => __('Color', 'eurostocks'),
			'value' => isset($vehicle['exteriorColor']) && '' !== $vehicle['exteriorColor'] ? $vehicle['exteriorColor'] : '-',
		),
		array(
			'label' => __('Transmission', 'eurostocks'),
			'value' => isset($vehicle['gearBox']) && '' !== $vehicle['gearBox'] ? $vehicle['gearBox'] : '-',
		),
		array(
			'label' => __('Plug-in hybrid', 'eurostocks'),
			'value' => $this->resolve_plugin_hybrid_value($vehicle),
		),
	);
}
?>

<div class="wrap eurostocks-wrap">
	<h1><?php esc_html_e('Price breakdown', 'eurostocks'); ?></h1>
	<p><?php esc_html_e('Enter a mobile.de ID or advertisement URL to view the vehicle price breakdown.', 'eurostocks'); ?></p>

	<form method="post" class="euro-postbox eurostocks-price-structure-form">
		<?php wp_nonce_field('eurostocks_price_lookup', 'eurostocks_price_lookup_nonce'); ?>
		<label for="mobile_id" style="display: block; margin-bottom: 8px;">
			<?php esc_html_e('mobile.de ID or URL', 'eurostocks'); ?>
		</label>
		<input type="text" id="mobile_id" name="mobile_id" value="<?php echo esc_attr($lookup_value); ?>" class="regular-text" style="max-width: 420px;" />
		<button type="submit" class="button button-primary" style="margin-left: 8px;"><?php esc_html_e('Get price breakdown', 'eurostocks'); ?></button>
		<?php if ('' !== $vehicle_id) : ?>
			<p class="description" style="margin-top: 6px;">
				<?php printf(esc_html__('Resolved ID: %s', 'eurostocks'), esc_html($vehicle_id)); ?>
			</p>
		<?php endif; ?>
	</form>

	<?php if ('' !== $error_message) : ?>
		<div class="notice notice-error" style="margin-top: 10px;">
			<p><?php echo esc_html($error_message); ?></p>
		</div>
	<?php endif; ?>

	<?php if ($vehicle) : ?>
		<div class="eurostocks-admin-header" style="margin-top: 20px;">
			<h2><?php esc_html_e('Vehicle basics', 'eurostocks'); ?></h2>
		</div>
		<div class="postbox" style="padding: 16px;">
			<div style="display: flex; gap: 20px; align-items: flex-start; flex-wrap: wrap;">
				<?php if ('' !== $vehicle_image) : ?>
					<div style="max-width: 640px; min-width: 400px;">
						<img src="<?php echo esc_url($vehicle_image); ?>" alt="" style="width: 100%; height: auto;" />
					</div>
				<?php endif; ?>
				<div style="flex: 1 1 320px;">
					<table class="widefat striped" style="margin-top: 0; border:0;">
						<tbody>
						<?php foreach ($base_fields as $field) : ?>
							<tr>
								<th style="width: 180px;"><?php echo esc_html($field['label']); ?></th>
								<td><?php echo esc_html($field['value']); ?></td>
							</tr>
						<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>

		<div class="eurostocks-admin-header">
			<h2><?php esc_html_e('Price breakdown', 'eurostocks'); ?></h2>
		</div>
		<div class="postbox" style="padding: 15px 0 15px 0;">
			<table class="widefat fixed striped" style="margin-top: 0;border:0;">
				<tbody>
					<?php foreach ($price_fields as $field) : ?>
						<tr>
							<td>
								<strong><?php echo esc_html($field['label']); ?></strong>
								<?php if (!empty($field['description'])) : ?>
									<p class="description" style="margin: 4px 0 0;"><?php echo esc_html($field['description']); ?></p>
								<?php endif; ?>
							</td>
							<td style="text-align: right;">
								<?php echo esc_html($field['value']); ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>
