<?php

namespace CarDealerPress\Templates\Inventory;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;

wp_enqueue_script( 'cdp_theme_filters_js', CDP_JS . 'templates/inventory/filter_checkboxes.js', array( 'jquery' ), CDP_VERSION, true );

$ext_functions->load_parameters( $this->parameters );
$search_view        = Option::check_for_option(
	'cdp_theme_options',
	array(
		'theme',
		'inventory_display',
		'list',
		'search_filters',
		'general',
		'search_view',
	)
);
$srp_view           = Option::check_for_option(
	'cdp_theme_options',
	array(
		'theme',
		'inventory_display',
		'list',
		'search_filters',
		'general',
		'srp_view',
	)
);
$vehicles           = array();
$page_title_heading = Option::check_for_option(
	'cdp_theme_options',
	array(
		'theme',
		'inventory_display',
		'list',
		'general',
		'list_title_heading',
	)
);

$display_saved_vehicle = Option::check_for_option(
	'cdp_theme_options',
	array(
		'theme',
		'inventory_display',
		'general',
		'saved_vehicles',
		'display',
	)
);

$ajax = Option::check_for_option(
	'cdp_theme_options',
	array(
		'theme',
		'inventory_display',
		'list',
		'scripts',
		'ajax',
	)
);

$content = '<div id="inventory-wrapper">';
$content .= "<div id='inventory-listing' class='search-" . esc_attr( $search_view ) . " inventory-main-container'>";

$content .= '<div id="inventory-top-container" class="search-' . esc_attr( $search_view ) . ' bar-hedgehog">';
if ( $display_saved_vehicle ) {
	$content .= $ext_functions->extender_saved_vehicle_container();
}
$content       .= '<h1 id="list-page-title">' . esc_html( $page_title_heading ) . '</h1>';  // dynamic, from js.
$content       .= '<div id="pagination-anchor" class="inventory-pagination-container">' . $ext_functions->extender_inventory_pagination( $vehicles ) . '</div>';
$content       .= '<div id="inventory-sort-container">' . $ext_functions->extender_create_sort_dropdown() . '</div>';
$content       .= '</div>'; //end inventory-top-container
$vehicle_count = ( new Requests() )->cdp_db_init( 'searchtotal', $ext_functions::$passed_parameters );

$content .= "<div id='inventory-body-container' class='search-" . esc_attr( $search_view ) . "'>";
$content .= "<div id='filters-wrapper' class='search-" . esc_attr( $search_view ) . " '>";
$content .= "<div id='search-mobile-banner' class='search-item ignore-slide mobile-default-open'>";
$content .= "<span class='filter-label'>Filters</span> (<span class='search-vehicle-count'>$vehicle_count</span>)";
$content .= "<span class='search-item-icon'></span>";
$content .= '</div><!-- end search-mobile-banner -->';
$content .= "<div id='list-filters-container' class='search-" . $search_view . "'>"; // list-filters-container
$content .= $ext_functions->custom_filters_search();
$content .= '</div><!-- list-filters-container -->';
$content .= '</div><!-- end filters wrapper -->';
// Reset Parameters to default URL parameters on init load to keep search results the same.
$ext_functions->load_parameters( $this->parameters );
$content .= '<div id="inventory-vehicles-container" class="vehicles">';
switch ( strtolower( $srp_view ) ) {
	case 'list':
		$content .= $ext_functions->extender_inventory_list_view( $vehicles );
		break;
	case 'grid':
		$content .= $ext_functions->extender_inventory_list_view_grid( $vehicles );
		break;
}
$content .= '</div>'; // end inventory-vehicles-container
$content .= '</div>'; // end inventory-body-container
$content .= '<div id="inventory-bottom-container" class="">';
$content .= '<div class="inventory-pagination-container pagination-anchor">' . $ext_functions->extender_inventory_pagination( $vehicles ) . '</div>';
$content .= '<div class="inventory-disclaimer"><small>' . esc_html( $fn_inventory->get_disclaimer() ) . '</small></div>';
$content .= '</div>';
$content .= '<div id="inventory-search-data">';
foreach ( $ext_functions::$passed_parameters as $key => $value ) {
	if ( 'taxonomy' !== $key ) {
		$content .= '<div data-key="' . esc_attr( $key ) . '" data-value="' . esc_attr( $value ) . '"></div>';
	}
}
$content .= '</div>';
$content .= '<div id="inventory-ajax-icon"><img src="' . esc_url( CDP_IMG . 'templates/inventory/loading.gif' ) . '" alt="loading"></div>';
if ( is_active_sidebar( 'vehicle-listing-page' ) ) {
	$content .= '<div id="list-widget-area" class="">';
	$content .= dynamic_sidebar( 'vehicle-listing-page' );
	$content .= '</div>';
}
if ( $display_saved_vehicle ) {
	$content .= $ext_functions->extender_compare_vehicle_container();
}

if ( $ajax ) {
	$content .= '<div id="ajax-script-wrapper">';
	wp_register_script( 'cdp_injector_js', CDP_JS . 'templates/inventory/injector.js', 'jquery', CDP_VERSION, true ); //injector.js is a placeholder file.
	wp_enqueue_script( 'cdp_injector_js' );
	wp_add_inline_script( 'cdp_injector_js', urldecode( stripslashes( $ajax ) ) );
	$content .= '</div>';
}
$content .= '</div>'; // inventory-listing
$content .= '</div>'; // inventory-wrapper

helpers::output_wp_kses( $content );
