<?php

namespace CarDealerPress\Templates\Inventory;

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

?>

<div id="inventory-wrapper">
	<div id="inventory-listing">
		<div class="breadcrumbs">
		<?php

		use CarDealerPress\includes\helpers\helpers;
		use CarDealerPress\Includes\Helpers\Option;
        use CarDealerPress\templates\partials\inventory\List_Search_Filters;
        use CarDealerPress\templates\partials\inventory\Search_Bar_Filters;

			helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'] ));
		?>
		</div>
		<hr class="full-divider">

		<div id="inventory-quick-links">
			<?php
			$back_links    = '';
			$search_params = '';
			$link_params   = array( 'saleclass' => $this->parameters['saleclass'] );
			if ( isset( $this->parameters['make'] ) && 'all' !== $this->parameters['make'] ) {
				$link                = $fn_inventory->generate_inventory_link(array(
					'params' => $this->parameters,
					'remove' =>	array(
						'make',
						'model',
						'trim',
					))
				);
				$back_links          = '<div class="quick-back-link"><a href="' . $link . '">View all Makes</a></div>';
				$link_params['make'] = $this->parameters['make'];
				$search_params       = '<input id="search-make" type="hidden" readonly value="' . $this->parameters['make'] . '" />';
				if ( isset( $this->parameters['model'] ) && 'all' !== $this->parameters['model'] ) {
					$link                 = $fn_inventory->generate_inventory_link(array(
						'params' => $this->parameters,
						'remove' => array(
							'model',
							'trim',
						))
					);
					$back_links          .= '<div class="quick-back-link"><a href="' . $link . '">View all Models</a></div>';
					$link_params['model'] = $this->parameters['model'];
					$search_params       .= '<input id="search-model" type="hidden" readonly value="' . $this->parameters['model'] . '" />';
					if ( isset( $this->parameters['trim'] ) && 'all' !== $this->parameters['trim'] ) {
						$link           = $fn_inventory->generate_inventory_link( array( 'params' => $this->parameters, 'remove' => array( 'trim' ) ) );
						$back_links    .= '<div class="quick-back-link"><a href="' . $link . '">View all Trims</a></div>';
						$search_params .= '<input id="search-trim" type="hidden" readonly value="' . $this->parameters['trim'] . '" />';
					} else {
						echo '<div id="trim-wrapper" class="quick-link-items">';
						foreach ( $trims as $trim ) {
							$trim_safe = str_replace( '/', '%2F', $trim );
							$link      = $fn_inventory->generate_inventory_link( array('params' => $this->parameters, 'include' => array( 'trim' => $trim_safe ) ) );
							helpers::output_wp_kses( '<div class="quick-link-item"><a href="' . $link . '">' . $trim . '</a></div>');
						}
						echo '</div>';
					}
				} else {
					echo '<div id="model-wrapper" class="quick-link-items">';
					foreach ( $models as $model ) {
						$model_safe = str_replace( '/', '%2F', $model );
						$link       = $fn_inventory->generate_inventory_link( array('params' => $this->parameters, 'include' => array( 'model' => $model_safe ) ) );
						helpers::output_wp_kses( '<div class="quick-link-item"><a href="' . $link . '">' . $model . '</a></div>');
					}
					echo '</div>';
				}
			} else {
				echo '<div id="make-wrapper" class="quick-link-items">';
				foreach ( $makes as $make ) {
					$make_safe = str_replace( '/', '%2F', $make );
					$link      = $fn_inventory->generate_inventory_link( array('params' => $this->parameters, 'include' => array( 'make' => $make_safe ) ) );
					helpers::output_wp_kses( '<div class="quick-link-item"><a href="' . $link . '">' . $make . '</a></div>');
				}
				echo '</div>';
			}
			helpers::output_wp_kses( ( $back_links ) ? '<div id="quick-back-links">' . $back_links . '</div>' : '');
			helpers::output_wp_kses( ( $search_params ) ? '<div id="hidden-search-params">' . $search_params . '</div>' : '');
			?>
		</div>
		<hr class="full-divider">

		<div id="inventory-search-wrapper">
			<div class="inventory-search-text-wrapper">
				<div id="inventory-search-text" class="<?php echo esc_attr( $search_input_class); ?>">
					<input id="inventory-search-box" class="text-search" name="search"
						   value="<?php echo esc_html( $this->parameters['search'] ?? null); ?>"/>
					<div id="inventory-search-submit">Search</div>
				</div>
			</div>
			<form action="#" method="POST" id="inventory-search"> <!-- Vehicle Search -->
				<input type="hidden" id="hidden-rewrite" value="
				<?php
				if ( isset( $rules['^(inventory)'] ) ) {
					echo esc_attr( 'true');
				}
				?>
				" name="h_taxonomy"/>
				<input type="hidden" id="hidden-saleclass"
					   value="<?php echo esc_attr( ucwords( strtolower( $this->parameters['saleclass'] ) )); ?>"
					   name="h_saleclass"/>
				<div id="inventory-search-advance" style="display: none;">
					<div id="price-range-wrapper" class="search-wrapper">
						<div id="price-range-text" class="search-text">
							<?php $range_flag = ( isset( $this->parameters['price_from'] ) && isset( $this->parameters['price_to'] ) ) ? 'true' : 'false'; ?>
                            <input type="hidden" value="<?php echo esc_attr( $range_flag); ?>" id="price-range-flag"/>
                            <?php helpers::output_wp_kses(  List_Search_Filters::range_sliders(array('name' => 'price')))?>

						</div>
					</div>
					<div id="year-range-wrapper" class="search-wrapper">
						<div id="year-range-text" class="search-text">
							<?php $range_flag = ( isset( $this->parameters['year_from'] ) && isset( $this->parameters['year_to'] ) ) ? 'true' : 'false'; ?>
                            <input type="hidden" value="<?php echo esc_attr( $range_flag); ?>" id="year-range-flag"/>
                            <?php helpers::output_wp_kses(  List_Search_Filters::range_sliders(array('name' => 'year')))?>
						</div>
					</div>
					<div id="odometer-range-wrapper" class="search-wrapper">
						<div id="odometer-range-text" class="search-text">
							<?php $range_flag = ( isset( $this->parameters['mileage_from'] ) && isset( $this->parameters['mileage_to'] ) ) ? 'true' : 'false'; ?>
                            <input type="hidden" value="<?php echo esc_attr( $range_flag); ?>" id="odometer-range-flag"/>
                            <?php helpers::output_wp_kses(  List_Search_Filters::range_sliders(array('name' => 'odometer')))?>
						</div>
					</div>
					<hr class="inventory-hr">
					<?php
					$hide_vehicleclass = Option::check_for_option(
						'cdp_theme_options',
						array(
							'theme',
							'inventory_display',
							'list',
							'search_filters',
							'general',
							'hide_vehicleclass',
						)
					);
					if ( '' === $hide_vehicleclass ) {
						echo '<div class="inventory-advance-param">';
						helpers::output_wp_kses( Search_Bar_Filters::vehicleclass_dropdown());
						echo '</div>';
					}
					?>
					<?php
					$hide_saleclass = Option::check_for_option(
						'cdp_theme_options',
						array(
							'theme',
							'inventory_display',
							'list',
							'search_filters',
							'general',
							'hide_saleclass',
						)
					);
					if ( '' === $hide_saleclass ) {
						echo '<div class="inventory-advance-param">';
						echo '<label class="inventory-label">Sale Class:</label><br/>';
						helpers::output_wp_kses( Search_Bar_Filters::sale_class_dropdown(
							array(
								'parameters' => $this->parameters,
								'all_link'   => $all_link,
								'new_link'   => $new_link,
								'used_link'  => $used_link,
								'cert_link'  => $cert_link,
								'heading'    => 'Sale Class',
							)
						));
						echo '</div>';
					}
					?>
					<?php if ( $this->theme_options['theme']['inventory_display']['list']['search_filters']['geo_search']['display_geo'] ) { ?>
						<div class="inventory-advance-param">
							<label class="inventory-label">Vehicle Location: </label>
							<?php
							helpers::output_wp_kses( Search_Bar_Filters::geo_dropdown(
								array(
									'dealer_geo' => $dealer_geo,
									'geo_params' => $geo_params,
								)
							));
							?>
						</div>
					<?php } ?>
					<div class="inventory-advance-param">
						<div class="reset-search"><a
									href="<?php echo esc_url( ! empty( $this->parameters['saleclass'] ) ? '/inventory/' . $this->parameters['saleclass'] . '/' : '/inventory/'); ?>">Reset
								Search</a></div>
					</div>
				</div>
				<input id="search-form-submit" style="display: none;" type="submit" value="go"/>
			</form>
			<div id="inventory-advance-show" data-name="hidden">
				Advanced Search
			</div>
		</div>

		<hr class="full-divider">
		<div id="inventory-total-found">Found <span><?php echo esc_html( $vehicle_total_found); ?></span>
			Results <?php helpers::output_wp_kses( ! empty( $geo_output['search'] ) ? 'in ' . $geo_output['search'] : ''); ?></div>
		<div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args )); ?></div>

		<div id="inventory-inventory-wrapper">
			<?php
			if ( empty( $vehicles ) ) {
				helpers::output_wp_kses( $ext_functions->extender_vehicle_list_page_no_results());
			} else {
				foreach ( $vehicles as $vehicle ) {
					$generic_vehicle_title = $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'];
					$link_params           = array(
						'year'  => $vehicle['year'],
						'make'  => $vehicle['make'],
						'model' => $vehicle['model'],
						'state' => $state,
						'city'  => $city,
						'vin'   => $vehicle['vin'],
						'vms_id'    => $vehicle['vms_id'],
					);
					// $link = get_inventory_link( $rules['^(inventory)'], $link_params, 1);
					$link = $fn_inventory->generate_inventory_link( array('params' => $link_params, 'type' => 1 ) );
					?>
					<div id="<?php echo esc_attr( $vehicle['vin']); ?>"
						 class="inventory-vehicle saleclass-<?php echo esc_attr( strtolower( $vehicle['saleclass'] )); ?>">

						<div class="inventory-vehicle-top">
							<?php ( $vehicle['headline'] ? '<div class="inventory-list-headline">' . $vehicle['headline'] . '</div>' : '' ); ?>
							<div class="vehicle-title">
								<a href="<?php echo esc_url( $link); ?>" title="<?php echo esc_attr( $generic_vehicle_title); ?>"
								   class="title-details">
									<span class="title-year inventory-year form-value"
										  data-name="year"><?php echo esc_html( $vehicle['year']); ?></span>
									<span class="title-make inventory-make form-value"
										  data-name="make"><?php echo esc_html( $vehicle['make']); ?></span>
									<span class="title-model inventory-model form-value"
										  data-name="model"><?php echo esc_html( $vehicle['model']); ?></span>
									<span class="title-trim inventory-trim form-value"
										  data-name=trim><?php echo esc_html( $vehicle['trim']); ?></span>
									<span class="inventory-saleclass form-value" data-name="saleclass"
										  style="display: none;"><?php echo esc_html( $vehicle['saleclass']); ?></span>
								</a>
							</div>
						</div>
						<div class="inventory-vehicle-middle">
							<div class="middle-left">
								<?php
								$price           = $fn_inventory->get_price_display(
									array(
										'company' => $this->company,
										'vehicle' => $vehicle,
										'theme'   => 'inventory',
									)
								);
								$loan_parameters = array(
									'make'      => $vehicle['make'],
									'model'     => $vehicle['model'],
									'trim'      => $vehicle['trim'],
									'year'      => $vehicle['year'],
									'saleclass' => $vehicle['saleclass'],
								);
								?>
								<div class="inventory-price-wrap">
									<?php
									helpers::output_wp_kses(( ! empty( $price['msrp_text'] ) && 'new' === strtolower( $vehicle['saleclass'] ) ? $price['msrp_text'] : '' ));
									helpers::output_wp_kses( $price['primary_text'] . $price['compare_price_text'] . $price['expire_text'] . $price['hidden_prices']);
									helpers::output_wp_kses( $fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $price['primary_price'], $loan_parameters ));
									?>
								</div>
								<?php
								if ( $gform_class ) {
									helpers::output_wp_kses( "<div class='inventory-more-info'><div class='inventory-more-info-button $gform_class' data-form-id='$gform_id' data-name='Confirm Availability' data-key='$vehicle[vin]'>" . ( empty( $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['name'] ) ? 'Get E-Price' : $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['name'] )."</div></div>");
								}
								?>
							</div>
							<div class="middle-center">
								<div class="photo">
									<a href="<?php echo esc_url( $link); ?>" title="<?php echo esc_attr( $generic_vehicle_title); ?>">
										<?php
										$list_image = $fn_inventory->no_photo_replace( $vehicle['thumbnail'] );
										// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
                                        $sold_img_overlay = $vehicle['sold_on'] ? '<img class="marked-sold-overlay" src="' . CDP_IMG . 'templates/inventory/sold_overlay.png" />' : '';
                                        echo wp_kses( $sold_img_overlay, 'post' );
										// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
										echo wp_kses ( '<img class="list-image" src="' . $list_image . '" alt="' . $generic_vehicle_title . '" title="' . $generic_vehicle_title . '" />', 'post');
										?>
									</a>
								</div>
							</div>
							<div class="middle-right">
								<div class="vehicle-information">
									<div>Stock #: <span class="vehicle-stock inventory-stock-number form-value" data-name="stock_number"><?php echo esc_html( $vehicle['stock']); ?></span></div>
									<div>VIN: <span class="vehicle-vin inventory-vin form-value" data-name="vin"><?php echo esc_html( $vehicle['vin']); ?></span></div>
									<?php
									helpers::output_wp_kses( ! empty( $vehicle['exterior'] ) ? '<div><span class="exterior-color">Exterior: ' . $vehicle['exterior'] . '</span></div>' : '' );
									helpers::output_wp_kses( ! empty( $vehicle['interior'] ) ? '<div><span class="interior-color">Interior: ' . $vehicle['interior'] . '</span></div>' : '' );
									?>
								</div>
								<div class="inventory-more-info">
									<div class="inventory-more-info-button">
										<a href="<?php echo esc_url( $link); ?>">
											<?php helpers::output_wp_kses(( empty( $this->theme_settings['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ) ? 'More Info' : $this->theme_settings['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] )); ?>
										</a>
									</div>
								</div>
							</div>
						</div>
						<div class="inventory-vehicle-bottom">
							<?php
							helpers::output_wp_kses( $fn_inventory->get_dealer_contact_info(
								array(
									'vehicle' => $vehicle,
									'company' => $this->company,
								)
							));
							if ( $this->theme_options['theme']['inventory_display']['tags']['display_tags'] ) {
								if ( ! empty( $vehicle['tags'] ) ) {
									echo '<div class="inventory-listing-tags">';
									$tag_icons = $fn_inventory->build_tag_icons( $vehicle['tags'], $vehicle['vin'] );
									helpers::output_wp_kses( $tag_icons);
									echo '</div>';
								}
							}

							if ( $vehicle['autocheck'] ) {
								helpers::output_wp_kses( $fn_inventory->display_autocheck_image( $vehicle['vin'], $vehicle['saleclass'] ));
							}
							?>
						</div>
						<div class="inventory-form-container"></div>
						<?php
						if ( ! empty( $vehicle['carfax'] ) && 'used' === strtolower( $vehicle['saleclass'] ) ) {
							helpers::output_wp_kses( $fn_inventory->cdp_display_carfax( $vehicle['vin'], $vehicle['carfax'], $vehicle['carfax_image_url'], 'list' ));
						}
						?>
					</div>
					<?php
				}
			}
			?>
			<div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args )); ?></div>
			<?php
			if ( function_exists( 'gravity_form' ) && $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['gform_id'] ) {
				echo '<div id="hidden-form-wrapper" ><div id="list-gform-wrapper" >';
				helpers::output_wp_kses( gravity_form( $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['gform_id'], true, false, false, '', true ));
				echo '</div></div>';
			}
			?>
			<div id="inventory-disclaimer">
				<?php
				helpers::output_wp_kses( $fn_inventory->get_disclaimer());
				?>
			</div>
		</div>
		<?php
		if ( is_active_sidebar( 'vehicle-listing-page' ) ) :
			echo '<div id="list-widget-area">';
			dynamic_sidebar( 'vehicle-listing-page' );
			echo '</div>';
		endif;
		?>
	</div>
</div>
