<?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 class="breadcrumb-wrapper">
            <div class="breadcrumbs">
                <?php
                helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'] ) );
                ?>
            </div>
            <div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args ) ); ?></div>

        </div>
        <div class="inventory-search-text-wrapper">
            <form onsubmit="return list_search_field(event, this.value);" name="list_search_form" id="inventory-search"
                  class="<?php echo esc_attr( $search_input_class ); ?>">
                <label for="search">Inventory Search:</label>
                <input id="inventory-search-box" class="text-search" name="search"
                       value="<?php echo esc_html( $this->parameters['search'] ?? null ); ?>"/>
            </form>
        </div>
        <div id="inventory-list-top-wrapper">
            <div id="inventory-total-found" class="color-one"><?php echo esc_html( $vehicle_total_found ); ?> Cars
                Found
            </div>
            <div id="inventory-sort-wrapper">
                <div id="sort-label">Sort by:</div>
                <div class="sort-value"><a class="<?php echo esc_attr( $sort_year_class ?? '' ); ?>"
                                           href="<?php echo esc_url( esc_url( add_query_arg( array( 'manual_sort' => $sort_year ), $do_not_carry ) ) ); ?>">Year</a>
                </div>
                <div class="sort-value"><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="sort-value"><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>
        </div>

        <div id="inventory-list-sidebar">
            <h3 id="list-sidebar-label-mobile" class="">Refine Your Search</h3>
            <h3 id="list-sidebar-label" class="color-one">Refine Your Search</h3>
            <?php
            $hide_saleclass = Option::check_for_option(
                    'cdp_theme_options',
                    array(
                            'theme',
                            'inventory_display',
                            'list',
                            'search_filters',
                            'general',
                            'hide_saleclass',
                    )
            );
            if ( '' === $hide_saleclass ) {
                echo '<ul id="list-saleclass-filter">';
                echo '<li class="inventory-expanded">';
                echo '<div class="list-sidebar-label"><span>Sale Class</span></div>';
                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 '</li>';
                echo '</ul>';
            }
            ?>
            <?php if ( $this->theme_options['theme']['inventory_display']['list']['search_filters']['geo_search']['display_geo'] ) { ?>
                <ul id="list-geo-filter">
                    <li class="inventory-expanded">
                        <div class="list-sidebar-label"><span>Vehicle Location</span></div>
                        <?php
                        helpers::output_wp_kses( Search_Bar_Filters::geo_dropdown(
                                array(
                                        'dealer_geo' => $dealer_geo,
                                        'geo_params' => $geo_params,
                                )
                        ) );
                        ?>
                    </li>
                </ul>
            <?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 '<ul id="list-vehicleclass-filter">';
                echo '<li class="inventory-expanded">';
                echo '<div class="list-sidebar-label"><span>Vehicle Class</span></div>';
                helpers::output_wp_kses( Search_Bar_Filters::vehicleclass_list(
                        array(
                                'parameters' => $this->parameters,
                                'url_rule'   => $url_rule,
                        )
                ) );
                echo '</li>';
                echo '</ul>';
            }
            ?>
            <?php
            // Make | Model | Trim Fitler.
            if ( 0 !== $trim_count && ! empty( $trim_count ) ) {
                $sidebar_content = '<ul id="list-trim-filter"><li class="inventory-expanded"><div class="list-sidebar-label"><span>Trims</span></div><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></li></ul>';

            } elseif ( 0 !== $model_count && ! empty( $model_count ) ) {
                $sidebar_content = '<ul id="list-model-filter"><li class="inventory-expanded"><div class="list-sidebar-label"><span>Models</span></div><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></li></ul>';
            } elseif ( 0 !== $make_count ) {
                $sidebar_content = '<ul id="list-make-filter"><li class="inventory-expanded"><div class="list-sidebar-label"><span>Makes</span></div><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></li></ul>';
            } else {
                $sidebar_content = '<ul id="list-filter-error"><li><div class="list-sidebar-label"><span>No Makes Found</span></div></li><ul>';
            }
            helpers::output_wp_kses( $sidebar_content );
            ?>
            <ul id="list-price-filter">
                <li class="inventory-expanded">
                    <div class="list-sidebar-label"><span>Price Range</span></div>
                    <?php
                    helpers::output_wp_kses( Search_Bar_Filters::price_range_list(
                            array(
                                    'parameters' => $this->parameters,
                                    'url_rule'   => $url_rule,
                            )
                    ) );
                    ?>
                </li>
            </ul>
        </div>

        <div id="inventory-listing-content">
            <div id="inventory-listing-items">
                <?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 = $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-content-wrapper">
                                <div class="inventory-column-left">
                                    <div class="inventory-photo">
                                        <a class="vehicle-link" href="<?php echo esc_url( $link ); ?>">
                                            <?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="inventory-column-right">
                                    <div class="inventory-main-line"
                                         title="<?php echo esc_attr( $generic_vehicle_title ); ?>">
                                        <a class="vehicle-link" href="<?php echo esc_url( $link ); ?>">
                                            <span class="inventory-saleclass form-value" data-name="saleclass"
                                                  style="display: none;"><?php echo esc_html( $vehicle['saleclass'] ); ?></span>
                                            <span class="inventory-year form-value"
                                                  data-name="year"><?php echo esc_html( $vehicle['year'] ); ?></span>
                                            <span class="inventory-make form-make"
                                                  data-name="make"><?php echo esc_html( $vehicle['make'] ); ?></span>
                                            <span class="inventory-model form-model"
                                                  data-name="model"><?php echo esc_html( $vehicle['model'] ); ?></span>
                                            <span class="inventory-trim form-trim"
                                                  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>
                                        </a>
                                    </div>
                                    <?php
                                    if ( strlen( trim( $vehicle['headline'] ) ) > 0 ) {
                                        echo '<div class="inventory-headline">' . esc_html( $vehicle['headline'] ) . '</div>';
                                    }
                                    $saleclass = strtolower( $this->parameters['saleclass'] );
                                    $price     = $fn_inventory->get_price_display(
                                            array(
                                                    'company' => $this->company,
                                                    'vehicle' => $vehicle,
                                                    'theme'   => 'inventory',
                                            )
                                    );
                                    ?>
                                    <div class="inventory-details-left">
                                        <?php
                                        helpers::output_wp_kses( ( ! empty( $price['msrp_text'] ) && 'new' === strtolower( $vehicle['saleclass'] ) ) ? $price['msrp_text'] : '' );
                                        helpers::output_wp_kses( null !== $vehicle['interior'] ? '<span class="inventory-interior-color">Interior: ' . $vehicle['interior'] . '</span>' : '' );
                                        helpers::output_wp_kses( null !== $vehicle['exterior'] ? '<span class="inventory-exterior-color">Exterior: ' . $vehicle['exterior'] . '</span>' : '' );
                                        helpers::output_wp_kses( null !== $vehicle['transmission'] ? '<span class="inventory-transmission">Transmission: ' . $vehicle['transmission'] . '</span>' : '' );
                                        ?>
                                    </div>
                                    <div class="inventory-details-right">
                                        <span>Stock #: <span class="inventory-stock-number form-value"
                                                             data-name="stock_number"><?php echo esc_html( $vehicle['stock'] ); ?></span></span>
                                        <span class="inventory-odometer">Odometer: <?php echo esc_html( $vehicle['odometer'] ); ?></span>
                                        <span>VIN: <span class="inventory-vin form-value"
                                                         data-name="vin"><?php echo esc_html( $vehicle['vin'] ); ?></span></span>
                                    </div>
                                    <?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 class="inventory-details-bottom">
                                        <div class="bottom-inner-container-one">
                                            <div class="inventory-price-wrapper">
                                                <?php
                                                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="list-detail-button"
                                                 title="More Information: <?php echo esc_attr( $generic_vehicle_title ); ?>">
                                                <a class="vehicle-link"
                                                   href="<?php echo esc_url( $link ); ?>"><?php echo esc_html( empty( $this->theme_options['theme']['inventory_display']['list']['general']['list_info_button'] ) ? 'More Information' : $this->theme_options['theme']['inventory_display']['list']['general']['list_info_button'] ); ?></a>
                                            </div>
                                        </div>
                                        <div class="bottom-inner-container-two">
                                            <div class="inventory-contact-information">
                                                <?php
                                                helpers::output_wp_kses( $fn_inventory->get_dealer_contact_info(
                                                        array(
                                                                'vehicle' => $vehicle,
                                                                'company' => $this->company,
                                                        )
                                                ) );
                                                ?>
                                            </div>
                                            <?php
                                            if ( $gform_class ) {
                                                $form_name = 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'];
                                                helpers::output_wp_kses( "<div class='list-detail-button'><div class='inventory-form-button $gform_class' data-form-id='$gform_id' data-name='$form_name' data-key='$vehicle[vin]'>$form_name</div></div>" );
                                            }

                                            if ( $vehicle['autocheck'] ) {
                                                helpers::output_wp_kses( $fn_inventory->display_autocheck_image( $vehicle['vin'], $vehicle['saleclass'] ) );
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <div class="inventory-form-container"></div>
                                </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' ) );
                            }
                            ?>
                        </div>
                        <?php
                        flush();
                    endforeach;
                }
                ?>
            </div>
        </div>
        <div id="inventory-disclaimer">
            <?php
            helpers::output_wp_kses( $fn_inventory->get_disclaimer() );
            ?>
        </div>
    </div>
    <div class="breadcrumb-wrapper">
        <div class="breadcrumbs"><?php helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'] ) ); ?></div>
        <div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args ) ); ?></div>
    </div>
    <a href="#inventory-top" title="Return to Top" class="inventory-return-to-top">Return to Top</a>
    <?php
    if ( is_active_sidebar( 'vehicle-listing-page' ) ) :
        echo '<div id="list-widget-area">';
        dynamic_sidebar( 'vehicle-listing-page' );
        echo '</div>';
    endif;
    ?>
</div>
