<?php

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\Fn_Inventory;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\templates\partials\inventory\Prices;
use CarDealerPress\templates\partials\inventory\Vehicle_Attributes;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$this->vms->tracer = 'Obtaining requested sc inventory.';
$inventory         = ( new Requests() )->cdp_db_init( 'inventory', $this->sc_atts );

$state = $this->company['state'];
$city  = $this->company['city'];

$site_url = site_url();


$sc_content = '<div id="sc-inventory-wrapper" >';

if ( empty( $inventory ) ) {
	$sc_content .= '<div class="sc-not-found"><h2><strong>No vehicles available for this search.</strong></h2></div>';
} else {
	foreach ( $inventory as $vehicle ) {
		$target      = '';
		$link_params = array(
			'year'   => $vehicle['year'],
			'make'   => $vehicle['make'],
			'model'  => $vehicle['model'],
			'state'  => $state,
			'city'   => $city,
			'vin'    => $vehicle['vin'],
			'vms_id' => $vehicle['vms_id'],
		);
		$vdp_link    = $this->fn_inventory->generate_inventory_link( array( 'params' => $link_params, 'type' => 1 ) );
		if ( ! empty( $vehicle['vehicle_url'] ) && $this->use_vehicle_url ) {
			$vdp_link = $vehicle['vehicle_url'];
			$target   = 'target="_blank"';
		}
		$sc_content .= '<div class="sc-item-wrapper">';
		$sc_content .= '<div class="sc-item-inner">';
		$sc_content .= '<a href="' . $vdp_link . '" ' . $target . '>';
		// title.
		$sc_content .= '<div class="sc-title-wrapper">';
		$sc_content .= '<span class="sc-year">' . $vehicle['year'] . '</span>';
		$sc_content .= '<span class="sc-make">' . $vehicle['make'] . '</span>';
		$sc_content .= '<span class="sc-model">' . $vehicle['model'] . '</span>';
		$sc_content .= '</div>';
		// image.
		$list_image = $this->fn_inventory->no_photo_replace( $vehicle['thumbnail'] );
		$sc_content .= '<div class="sc-img-wrapper">';
		$alt        = $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'];
		// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
		$sc_content .= '<img src="' . str_replace( '_small', '_' . $this->sc_image_size, $list_image ) . '" alt="' . $alt . '"/>';
		$sc_content .= '</div>';
		// price.
		$price = $this->fn_inventory->get_price_display(
			array(
				'company' => $this->company,
				'vehicle' => $vehicle,
				'theme'   => 'short-code',
			)
		);

		$sc_content .= '<div class="sc-price-wrapper">';
		//$sc_content .= '<div class="sc-price-value price-display-container">' . ( new Prices($vehicle) )->price_box() . '</div>';
		if ( $price['primary_price'] > 0 ) {
			$sc_content .= '<div class="sc-price-value"><span class="sc-price-symbol">$</span>' . number_format( $price['primary_price'], 0, '.', ',' ) . '</div>';
		} else {
			$sc_content .= '<div class="sc-price-value-text">' . $price['primary_text'] . '</div>';
		}
		$sc_content .= '</div>';
		$sc_content .= '</a>';

		if ( $this->sc_atts['show_attributes'] ) {
			$sc_content .= '<div class="sc-attributes-wrapper">';
			$sc_content .= '<div class="vehicle-information-attributes">';
			$sc_content .= Vehicle_Attributes::call(
				array(
					'vehicle' => $vehicle,
					'layout'  => 'list',
					'page'    => 'inventory_list',
					'option'  => 'cdp_shortcode_options',
				)
			);
			$sc_content .= '</div>';
			$sc_content .= '</div>';
		}
		if ( $this->sc_atts['button'] ) {
			$sc_content .= '<div class="sc-buttons-wrapper">';
			$sc_content .= '<div class="vdp-button"><a href="' . $vdp_link . '">' . $this->sc_atts['button'] . '</a></div>';
			$sc_content .= '</div>';
		}
		$sc_content .= '</div>'; // end inner
		$sc_content .= '</div>';

	}
}

$sc_content       .= '</div>';
$this->sc_content = $sc_content;
