<?php
	$data = apply_filters( 'stm_single_car_data', stm_get_single_car_listings() );
	$post_id = get_the_ID();

	$show_vin = stm_me_get_wpcfto_mod('show_vin', false);
	$vin_num = get_post_meta(get_the_id(),'vin_number', true);

	$show_stock = stm_me_get_wpcfto_mod('show_stock', false);
	$stock_number = get_post_meta(get_the_id(),'stock_number',true);

	$show_registered = stm_me_get_wpcfto_mod('show_registered', false);
	$registration_date = get_post_meta(get_the_id(),'registration_date',true);

	$show_history = stm_me_get_wpcfto_mod('show_history', false);

	$history = get_post_meta(get_the_id(),'history',true);
	$history_link = '';
	$history_link = get_post_meta(get_the_id(),'history_link',true);
	
	//Registration
	if ( ! empty( $registration_date ) and $show_registered ) {
		$data[] = array(
			'single_name' => esc_html__( 'Registered', 'motors' ),
			'value'       => $registration_date,
			'font'        => 'stm-icon-key',
			'standart'    => false,
		);
	}

	if(empty($registration_date) and $show_registered) {
		$data[] = array(
			'single_name' => esc_html__( 'Registered', 'motors' ),
			'value'       => esc_html__('N/A', 'motors'),
			'font'        => 'stm-icon-key',
			'standart'    => false,
		);
	}

	//History
	if ( ! empty( $history ) and $show_history ) {
		$data[] = array(
			'single_name' => esc_html__( 'History', 'motors' ),
			'value'       => $history,
			'link'        => $history_link,
			'font'        => 'stm-icon-time',
			'standart'    => false,
		);
	}

	if ( empty( $history ) and $show_history ) {
		$data[] = array(
			'single_name' => esc_html__( 'History', 'motors' ),
			'value'       => esc_html__('N/A', 'motors'),
			'font'        => 'stm-icon-time',
			'standart'    => false,
		);
	}

	//Stock
	if ( ! empty( $stock_number ) and $show_stock ) {
		$data[] = array(
			'single_name' => esc_html__( 'Stock id', 'motors' ),
			'value'       => $stock_number,
			'font'        => 'stm-service-icon-hashtag',
			'standart'    => false,
		);
	}

	if ( empty( $stock_number ) and $show_stock ) {
		$data[] = array(
			'single_name' => esc_html__( 'Stock id', 'motors' ),
			'value'       => esc_html__('N/A', 'motors'),
			'font'        => 'stm-service-icon-hashtag',
			'standart'    => false,
		);
	}



	//VIN
	if ( ! empty( $vin_num ) and $show_vin ) {
		$data[] = array(
			'single_name' => esc_html__( 'VIN:', 'motors' ),
			'value'       => $vin_num,
			'font'        => 'stm-service-icon-vin_check',
			'standart'    => false,
			'vin'         => true
		);
	}

	if ( empty( $vin_num ) and $show_vin ) {
		$data[] = array(
			'single_name' => esc_html__( 'VIN:', 'motors' ),
			'value'       => $vin_num,
			'font'        => 'stm-service-icon-vin_check',
			'standart'    => false,
			'vin'         => true
		);
	}
?>

<?php if(!empty($data)): ?>
	<div class="stm-single-car-listing-data">
		<table class="stm-table-main">
			<tr>
				<?php foreach($data as $data_key => $data_single): ?>

					<?php if($data_key%3 == 0 and $data_key != 0): ?>
						</tr>
						<tr/>
					<?php endif; ?>

					<td>
						<table class="inner-table">
							<?php
								if(!empty($data_single['slug'])) {
									$value = get_post_meta( get_the_ID(), $data_single['slug'], true );
									if($value !== '') {
										if ( ! empty( $data_single['numeric'] ) and $data_single['numeric'] ) {
											if( !empty( $data_single['use_delimiter'] ) ) $value = number_format(abs($value), 0, '', ' ');
											
											if ( ! empty( $data_single['number_field_affix'] ) ) {
												$value .= ' ' . $data_single['number_field_affix'];
											}
										} else {
											$term_slugs = explode( ',', $value );
											$values     = array();

											foreach ( $term_slugs as $term_slug ) {
												$term     = get_term_by( 'slug', $term_slug, $data_single['slug'] );
												if(!empty($term->name)) {
													$values[] = $term->name;
												}
											}

											$value = implode( ', ', $values );
										}
									} else {
										$value = esc_html__('N/A', 'motors');
									}
								} else {
									$value = $data_single['value'];
								}
							?>
							<tr>
								<?php if(!empty($data_single['vin'])): ?>
									<td class="label-td">
										<?php if(!empty($data_single['font'])): ?>
											<i class="<?php echo esc_attr($data_single['font']) ?>"></i>
										<?php endif; ?>
										<?php echo esc_html($data_single['single_name']); ?> <?php echo esc_html($value); ?>
									</td>
									</td>
								<?php else: ?>
									<td class="label-td">
										<?php if(!empty($data_single['font'])): ?>
											<i class="<?php echo esc_attr($data_single['font']) ?>"></i>
										<?php endif; ?>
										<?php echo stm_dynamic_string_translation('Listing Category ' . $data_single['single_name'], $data_single['single_name']); ?>
									</td>
									<td class="heading-font">
										<?php if(!empty($data_single['link'])): ?>
											<a href="<?php echo esc_url($data_single['link']); ?>" target="_blank">
										<?php endif; ?>

											<?php echo stm_dynamic_string_translation('Listing Term ' . $value, $value); ?>

										<?php if(!empty($data_single['link'])): ?>
											</a>
										<?php endif; ?>
									</td>
								<?php endif; ?>
							</tr>
						</table>
					</td>
					<td class="divider-td"></td>

				<?php endforeach; ?>
			</tr>
		</table>
	</div>
<?php endif; ?>