<?php
namespace CarDealerPress\Templates\Inventory;

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

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

?>
<div id="inventory-wrapper">
    <div id="inventory-listing">
        <div id="inventory-top"> <!-- inventory Top -->
            <div class="inventory-breadcrumbs">
                <?php
                helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'], $this->parameters['saleclass'] ) );
                ?>
            </div>
            <div class="inventory-pager">
                <span>Page:</span><?php helpers::output_wp_kses( paginate_links( $args ) ); ?>
            </div>
        </div>

        <div id="inventory-content">  <!-- inventory Content -->
            <div id="inventory-content-top"> <!-- inventory Content Top -->
                <div id="inventory-total-found"><span><?php echo esc_html( $vehicle_total_found ); ?></span> Vehicles
                    Found
                </div>
                <div id="inventory-sorting-columns">
                    <div>Sort by:</div>
                    <div class="inventory-sorting-divider"><a class="<?php echo esc_attr( $sort_year_class ?? '' ); ?>"
                                                              href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_year ), $do_not_carry ) ); ?>">Year</a>
                    </div>
                    <div class="inventory-sorting-divider"><a class="<?php echo esc_attr( $sort_price_class ?? '' ); ?>"
                                                              href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_price ), $do_not_carry ) ); ?>">Price</a>
                    </div>
                    <div class="inventory-sorting-divider"><a
                                class="<?php echo esc_attr( $sort_mileage_class ?? '' ); ?>"
                                href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_mileage ), $do_not_carry ) ); ?>">Mileage</a>
                    </div>
                    <div class="inventory-sorting-divider"><a class="<?php echo esc_attr( $sort_make_class ?? '' ); ?>"
                                                              href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_make ), $do_not_carry ) ); ?>">Make</a>
                    </div>
                </div>
            </div>
            <div id="inventory-content-center"> <!-- inventory Content Center -->
                <div id="inventory-mobile-search-wrap" class="inactive">
                    <div id="inventory-mobile-search-img"></div>
                    <div id="inventory-mobile-search-text">Search</div>
                </div>
                <div id="inventory-content-left"> <!-- inventory Content Left -->
                    <div id="inventory-content-left-wrapper">
                        <div class="inventory-sidebar-content sidebar-text-search">
                            <div class="inventory-search">
                                <form action="" method="GET" 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_attr( $this->parameters['search'] ?? null ); ?>"/>
                                    <button id="inventory-search-submit">GO</button>
                                </form>
                            </div>
                        </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-sidebar sidebar-new-used" >';
                            echo '<h3 > Search new and Used:</h3 >';
                            echo '<div class="inventory-sidebar-content content-new-used" >';
                            echo '<h4 class="" data-name = "condition" > Condition</h4 >';
                            helpers::output_wp_kses( Search_Bar_Filters::sale_class_list(
                                    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 >';
                            echo '</div >';
                        }
                        ?>
                        <div class="inventory-sidebar sidebar-refine-search">
                            <h3>Refine Your Search By:</h3>
                            <?php if ( $this->theme_options['theme']['inventory_display']['list']['search_filters']['geo_search']['display_geo'] ) { ?>
                                <div id="list-geo-filter" class="inventory-sidebar-content content-geo">
                                    <h4>Vehicle Location</h4>
                                    <?php
                                    helpers::output_wp_kses( Search_Bar_Filters::geo_dropdown(
                                            array(
                                                    'dealer_geo' => $dealer_geo,
                                                    'geo_params' => $geo_params,
                                            )
                                    ) );
                                    ?>
                                </div>
                            <?php } ?>
                            <?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-sidebar-content content-bodystyle">';
                                echo '<h4 class="" data-name="styles">Vehicle Class</h4>';
                                helpers::output_wp_kses( Search_Bar_Filters::vehicleclass_list(
                                        array(
                                                'parameters' => $this->parameters,
                                                'url_rule'   => $url_rule,
                                        )
                                ) );
                                echo '</div>';
                            }
                            ?>
                            <div class="inventory-sidebar-content content-make-model-trim">
                                <?php
                                if ( 0 !== $trim_count ) {
                                    $sidebar_content = '<h4 class="" data-name="vehicles">Trims</h4>';
                                    $sidebar_content .= '<ul>';
                                    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 )
                                        ) );
                                        $sidebar_content .= '<li><a href="' . $link . '">' . $trim . '</a></li>';
                                    }
                                    $back_link       = $fn_inventory->generate_inventory_link( array(
                                                    'params' => $this->parameters,
                                                    'remove' => array(
                                                            'model',
                                                            'trim',
                                                    )
                                            )
                                    );
                                    $sidebar_content .= '<li><span class="no-style"><a href="' . $back_link . '" class="inventory-filter-prev" title="View ' . $this->parameters['make'] . ' Models">&#60; View ' . $this->parameters['make'] . '</a></span></li>';
                                    $sidebar_content .= '</ul>';

                                } elseif ( 0 !== $model_count ) {
                                    $sidebar_content = '<h4 class="" data-name="vehicles">Models</h4>';
                                    $sidebar_content .= '<ul>';
                                    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 )
                                        ) );
                                        $sidebar_content .= '<li><a href="' . $link . '">' . $model . '</a></li>';
                                    }
                                    $back_link       = $fn_inventory->generate_inventory_link( array(
                                                    'params' => $this->parameters,
                                                    'remove' => array(
                                                            'make',
                                                            'model',
                                                    )
                                            )
                                    );
                                    $sidebar_content .= '<li><span class="no-style"><a href="' . $back_link . '" class="inventory-filter-prev" title="View ' . $this->parameters['saleclass'] . ' Vehicles">&#60; All ' . $this->parameters['saleclass'] . ' Vehicles</a></span></li>';
                                    $sidebar_content .= '</ul>';
                                } elseif ( 0 !== $make_count ) {
                                    $sidebar_content = '<h4 class="" data-name="vehicles">Makes</h4>';
                                    $sidebar_content .= '<ul>';
                                    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 )
                                        ) );
                                        $sidebar_content .= '<li><a href="' . $link . '">' . $make . '</a></li>';
                                    }
                                    $sidebar_content .= '</ul>';
                                } else {
                                    $sidebar_content = '<h3>No Makes Found</h3>';
                                }
                                helpers::output_wp_kses( $sidebar_content );
                                ?>
                            </div>
                            <div class="inventory-sidebar-content content-price-range">
                                <h4 class="" data-name="price">Price Range</h4>
                                <?php
                                helpers::output_wp_kses( Search_Bar_Filters::price_range_list(
                                        array(
                                                'parameters' => $this->parameters,
                                                'url_rule'   => $url_rule,
                                        )
                                ) );
                                ?>
                            </div>
                        </div>
                    </div>
                </div>

                <div id="inventory-content-right"> <!-- inventory Content Right -->
                    <div id="inventory-vehicle-listings">
                        <?php
                        if ( empty( $vehicles ) ) {
                            helpers::output_wp_kses( $ext_functions->extender_vehicle_list_page_no_results() );
                        } else {
                            foreach ( $vehicles as $vehicle ) :
                                $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 = $fn_inventory->generate_inventory_link( array(
                                        'params' => $link_params,
                                        'type'   => 1
                                ) );
                                $generic_vehicle_title = $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'];

                                ?>
                                <div class="inventory-vehicle" id="<?php echo esc_attr( $vehicle['vin'] ); ?>">
                                    <div class="inventory-listing-top"> <!-- inventory Listing Top -->
                                        <div class="inventory-listing-vehicle-headline"><?php echo esc_html( $vehicle['headline'] ); ?></div>
                                        <div class="inventory-column-left">
                                            <div class="inventory-main-line">
                                                <a href="<?php echo esc_url( $link ); ?>"
                                                   title="<?php echo esc_attr( $generic_vehicle_title ); ?>"
                                                   class="details">
													<span class="inventory-year form-value"
                                                          data-name="year"><?php echo esc_html( $vehicle['year'] ); ?></span>
                                                    <span class="inventory-make form-value"
                                                          data-name="make"><?php echo esc_html( $vehicle['make'] ); ?></span>
                                                    <span class="inventory-model form-value"
                                                          data-name="model"><?php echo esc_html( $vehicle['model'] ); ?></span>
                                                    <span class="inventory-trim form-value"
                                                          data-name="trim"><?php echo esc_html( $vehicle['trim'] ); ?></span>
                                                    <span class="inventory-drive-train"><?php echo esc_html( $vehicle['drivetrain'] ); ?></span>
                                                    <span class="inventory-body-style"><?php echo esc_html( $vehicle['body'] ); ?></span>
                                                    <span class="inventory-saleclass form-value" data-name="saleclass"
                                                          style="display: none;"><?php echo esc_html( $vehicle['saleclass'] ); ?></span>
                                                </a>
                                            </div>
                                            <div class="inventory-photo">
                                                <a href="<?php echo esc_url( $link ); ?>"
                                                   title="<?php echo esc_html( $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 class="inventory-listing-info">
                                                <?php
                                                if ( $vehicle['retailprice'] > 0 && 'new' === strtolower( $vehicle['saleclass'] ) ) {
                                                    helpers::output_wp_kses( '<div class="inventory-msrp" data-alt="' . $vehicle['retailprice'] . '"><span>MSRP:</span> ' . '$' . number_format( $vehicle['retailprice'], 0, '.', ',' ) . '</div>' );
                                                }
                                                if ( $vehicle['odometer'] > 100 ) {
                                                    helpers::output_wp_kses( '<div class="inventory-odometer"><span>Odometer:</span> ' . $vehicle['odometer'] . '</div>' );
                                                }
                                                helpers::output_wp_kses( null !== $vehicle['exterior'] ? '<div class="inventory-exterior-color"><span>Exterior:</span> ' . $vehicle['exterior'] . '</div>' : null );
                                                helpers::output_wp_kses( null !== $vehicle['interior'] ? '<div class="inventory-interior-color"><span>Interior:</span> ' . $vehicle['interior'] . '</div>' : null );
                                                helpers::output_wp_kses( null !== $vehicle['transmission'] ? '<div class="inventory-transmission"><span>Transmission:</span> ' . $vehicle['transmission'] . '</div>' : null );
                                                ?>
                                            </div>
                                        </div>
                                        <div class="inventory-column-right">
                                            <div class="inventory-price">
                                                <?php
                                                $price = $fn_inventory->get_price_display(
                                                        array(
                                                                'company' => $this->company,
                                                                'vehicle' => $vehicle,
                                                                'theme'   => 'inventory',
                                                        )
                                                );
                                                helpers::output_wp_kses( $price['compare_price_text'] . $price['primary_text'] . $price['expire_text'] . $price['hidden_prices'] );

                                                $loan_parameters = array(
                                                        'make'      => $vehicle['make'],
                                                        'model'     => $vehicle['model'],
                                                        'trim'      => $vehicle['trim'],
                                                        'year'      => $vehicle['year'],
                                                        'saleclass' => $vehicle['saleclass'],
                                                );
                                                helpers::output_wp_kses( $fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $price['primary_price'], $loan_parameters ) );
                                                ?>
                                            </div>
                                            <div class="inventory-detail-button inventory-show-form"
                                                 data-name="<?php echo esc_attr( empty( $this->theme_options['theme']['inventory_display']['list']['general']['list_info_button'] ) ? 'Get Your ePrice' : $this->theme_options['theme']['inventory_display']['list']['general']['list_info_button'] ); ?>">
                                                <a href="<?php echo esc_url( $link ); ?>"><?php echo esc_html( empty( $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ) ? 'GET YOUR ePrice' : $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ); ?></a>
                                            </div>

                                            <?php
                                            if ( $vehicle['autocheck'] ) {
                                                helpers::output_wp_kses( $fn_inventory->display_autocheck_image( $vehicle['vin'], $vehicle['saleclass'] ) );
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <div class="inventory-listing-bottom"> <!-- inventory Listing Bottom -->
                                        <div class="listing-bottom-container-one">
                                            <div class="inventory-listing-buttons">
                                                <?php
                                                if ( $gform_class ) {
                                                    $form_name = empty( $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['name'] ) ? 'Confirm Availability' : $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['form']['name'];
                                                    helpers::output_wp_kses( "<div class='inventory-listing-button inventory-confirm-button $gform_class' data-form-id='$gform_id' data-name='$form_name' data-key='$vehicle[vin]'>$form_name</div>" );
                                                }
                                                ?>
                                                <div class="inventory-listing-button inventory-details-button"><a
                                                            href="<?php echo esc_url( $link ); ?>">View More Details</a>
                                                </div>
                                            </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' ) );
                                            }
                                            ?>
                                            <?php
                                            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>';
                                                }
                                            }
                                            ?>
                                        </div>
                                        <div class="listing-bottom-container-two">
                                            <div class="inventory-vin-wrapper">Vin #: <span
                                                        class="inventory-vin form-value"
                                                        data-name="vin"><?php echo esc_html( $vehicle['vin'] ); ?></span>
                                            </div>
                                            <div class="inventory-stock-wrapper">Stock #: <span
                                                        class="inventory-stock-number form-value"
                                                        data-name="stock_number"><?php echo esc_html( $vehicle['stock'] ); ?></span>
                                            </div>
                                        </div>
                                        <div class="listing-bottom-container-three">
                                            <div class="inventory-listing-dealer-info">
                                                <?php
                                                helpers::output_wp_kses( $fn_inventory->get_dealer_contact_info(
                                                        array(
                                                                'vehicle' => $vehicle,
                                                                'company' => $this->company,
                                                        )
                                                ) );
                                                ?>
                                            </div>
                                        </div>

                                        <div class="inventory-mobile-bottom">
                                            <?php helpers::output_wp_kses( $price['primary_text'] ); ?>
                                            <div class="inventory-detail-button">
                                                <a href="<?php echo esc_url( $link ); ?>"><?php echo esc_html( ( $this->theme_options['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ?? 'GET YOUR ePrice' ) ); ?></a>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="inventory-form-container"></div>
                                </div>
                                <?php
                                flush();
                            endforeach;
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div id="inventory-content-bottom"> <!-- inventory Content Bottom -->
                <div class="inventory-content-bottom-wrapper">
                    <div class="inventory-pager">
                        <span>Page:</span>
                        <?php helpers::output_wp_kses( paginate_links( $args ) ); ?>
                    </div>
                </div>
            </div>
        </div>

        <div id="inventory-bottom">  <!-- inventory Bottom -->
            <div class="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>
