<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// New columns for custom post types table in backend
function mobile_api_manage_vehicle_posts_columns( $column_array ) {
	unset( $column_array['author'] );
	unset( $column_array['comments'] );
	unset( $column_array['date'] );

	$column_array['condition'] = __( 'Condition', 'ad-integration-for-mobile-de-api' );
	$column_array['price'] = __( 'Price', 'ad-integration-for-mobile-de-api' );
	$column_array['fuel'] = __( 'Fuel', 'ad-integration-for-mobile-de-api' );
	$column_array['mileage'] = __( 'Mileage', 'ad-integration-for-mobile-de-api' );

	return $column_array;
}
add_filter( 'manage_vehicle_posts_columns', 'mobile_api_manage_vehicle_posts_columns' );

function mobile_api_manage_vehicle_posts_custom_column( $column_name, $post_id ) {
	switch ( $column_name ) {
		case 'condition': {
			echo esc_html( mobile_api_translate_data_value( get_post_meta( $post_id, 'mobile_api_condition', true ) ) );
			break;
		} case 'price': {
			$price = maybe_unserialize( get_post_meta( $post_id, 'mobile_api_price', true ) );
			echo !empty( $price ) && property_exists( $price, 'consumerPriceGross' ) ? esc_html( number_format_i18n( $price->consumerPriceGross ), 0 ).' €' : '–';
			break;
		} case 'fuel': {
			echo esc_html( mobile_api_translate_data_value( get_post_meta( $post_id, 'mobile_api_fuel', true ) ) );
			break;
		} case 'mileage': {
			echo !empty( get_post_meta( $post_id, 'mobile_api_mileage', true ) ) ? esc_html( number_format_i18n( intval( get_post_meta( $post_id, 'mobile_api_mileage', true ) ), 0 ) ).' km' : '–';
			break;
		}
	}

}
add_action( 'manage_vehicle_posts_custom_column', 'mobile_api_manage_vehicle_posts_custom_column', 10, 2 );
