<?php

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\sc_views;
use CarDealerPress\Includes\Helpers\Option;

$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'];
$c_code = $this->company['country_code'];

$sc_content = ' <div style="display: none; width: 100%; text-align: center;" id="cdp-ajax-status">0 | 0</div>';
$sc_content .= '<div id="sc-detail-container" >';

if ( $this->sc_search ) {
	$sc_content .= '<div id="sc-detail-search-container">';
	// Mobile.
	// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
	$sc_content .= '<div id="sc-detail-mobile-wrapper"><div id="mobile-image-wrapper"><img id="mobile-action-button" src="' . CDP_IMG . 'templates/inventory/search_icon.png" alt="search-image"/></div></div>';
	// SaleClass.
	$sc_content .= $this->fn_inventory->cdp_create_saleclass_dd_sc( $this->sc_flag_saleclass, $this->sc_default_saleclass );
	// Makes.
	$sc_content .= '<div class="sc-search-item makes">';
	$sc_content .= '<label class="sc-search-label">Makes</label>';
	$sc_content .= '<select id="sc-search-make" class="inventory-select" onchange="cdp_vehicle_caller(this.value, \'make\');">';
	$makes      = $this->fn_inventory->cdp_generate_make_options(
		$this->vms,
		$this->sc_default_saleclass,
		$this->theme_options['theme']['overrides']['makes_new']['included'],
		false,
		$this->sc_dealer_id
	);

	$sc_content .= $makes['display'] ?? '';
	$sc_content .= '</select></div>';
	// Models.

	$models     = $this->fn_inventory->cdp_generate_model_options( $this->vms, $this->sc_default_saleclass, $makes['val'], false );
	$disabled   = isset( $models['disabled'] ) ? 'disabled' : '';
	$sc_content .= '<div class="sc-search-item models">';
	$sc_content .= '<label class="sc-search-label">Models</label>';
	$sc_content .= '<select id="sc-search-model" class="inventory-select" ' . $disabled . ' onchange="cdp_vehicle_caller(this.value, \'model\');">';
	$sc_content .= $models['display'] ?? '';
	$sc_content .= '</select></div>';
	// Trims.
	$trims      = $this->fn_inventory->cdp_generate_trim_options( $this->vms, $this->sc_default_saleclass, $makes['val'], $models['val'], false );
	$disabled   = isset( $trims['disabled'] ) ? 'disabled' : '';
	$sc_content .= '<div class="sc-search-item trims">';
	$sc_content .= '<label class="sc-search-label">Trims</label>';
	$sc_content .= '<select id="sc-search-trim" class="inventory-select" ' . $disabled . ' onchange="cdp_vehicle_caller(this.value, \'trim\');">';
	$sc_content .= isset( $trims['display'] ) ? $trims['display'] : '';
	$sc_content .= '</select></div>';
	$sc_content .= '</div>';
}
// TODO: future this should be called before wp_head
// https://docs.gravityforms.com/gravity_form_enqueue_scripts/.
if ( function_exists( 'gravity_form_enqueue_scripts' ) ) {
	if ( $this->sc_form ) {
		$sc_content .= '<div style="display: none;" data-form="' . $this->sc_form . '" id="sc-detail-form-id"></div>';
		//gravity_form_enqueue_scripts( $this->sc_form, true );
		add_action( 'get_header', array( 'gf_enqueue_required_files', $this->sc_form, true ) );

	}
}

//add_action( 'gform_enqueue_scripts', 'enqueue_custom_script', 10, 2 );
function enqueue_custom_script( $form ) {
	gravity_form_enqueue_scripts( $form, true );
}

function gf_enqueue_required_files( $form ) {
	gravity_form_enqueue_scripts( $form, true );
}

if ( empty( $inventory ) ) {
	$sc_content .= '<div class="sc-not-found"><h2><strong>Search criteria did not return any results.</strong></h2></div>';
} else {
	foreach ( $inventory as $vehicle ) {
		$fuel_economy            = array(
			'city'    => $vehicle['city'],
			'highway' => $vehicle['highway'],
		);
		$vehicle['fuel_economy'] = $fuel_economy;
		$sc_content              .= $this->sc_views->get_sc_detail_vehicles( $vehicle, $this->company, $this->theme_options,
			$this->theme_options['theme']['overrides']['custom_contact'], $this->sc_form );
	}
}
$sc_content .= '</div>';

// AJAX Helpers.
$sc_content .= '<div style="display: none;" id="cdp-ajax-loader">';
$sc_content .= $this->sc_form ? '<div data-key="form">' . $this->sc_form . '</div>' : '';
foreach ( $this->sc_atts as $key => $att ) {
	$sc_content .= '<div data-key="' . $key . '">' . $att . '</div>';
}
$sc_content .= '</div>';

$this->sc_content = $sc_content;
