<?php

namespace CarDealerPress\Templates\Inventory;

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

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;

?>

<div id="inventory-wrapper">
    <div id="inventory-listing">
        <div id="inventory-top"> <!-- Listing Top -->
            <div class="inventory-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'], $this->parameters['saleclass'] ) );
                    ?>
                </div>
                <!-- Breadcrumbs -->
                <div class="inventory-pager"> <!-- Pager -->
                    <?php helpers::output_wp_kses( paginate_links( $args ) ); ?>
                </div>
            </div>
            <div id="mobile-show-search">Display Search Options</div>
            <div id="inventory-search-wrapper"> <!-- Search Filter Wrapper -->
                <div id="search-top-wrapper">
                    <?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 id="saleclass-wrapper" class="inventory-option-wrapper search-option-top">';
                        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
                    $hide_vehicleclass = Option::check_for_option(
                            'cdp_theme_options',
                            array(
                                    'theme',
                                    'inventory_display',
                                    'list',
                                    'search_filters',
                                    'general',
                                    'hide_vehicleclass',
                            )
                    );
                    if ( '' === $hide_vehicleclass ) {
                        echo '<div id="vehicleclass-wrapper" class="inventory-option-wrapper search-option-top">';
                        helpers::output_wp_kses( Search_Bar_Filters::vehicleclass_dropdown( array( 'params' => $this->parameters ) ) );
                        echo '</div>';
                    }
                    ?>
                    <div id="price-range-wrapper" class="inventory-option-wrapper search-option-top">
                        <label class="inventory-label" for="price-range">Price Range</label><br/>
                        <?php helpers::output_wp_kses( Search_Bar_Filters::price_range_dropdown( array( 'parameters' => $this->parameters ) ) ); ?>
                    </div>
                    <div id="inventory-search-text"
                         class="search-option-top <?php echo esc_attr( $search_input_class ); ?>">
                        <label for="search">Inventory Search</label><br/>
                        <input id="inventory-search-box"
                               class="text-search <?php echo esc_attr( isset( $this->parameters['search'] ) ? '' : 'invalid ' ); ?>list-search-value"
                               name="search"
                               value="<?php echo esc_attr( $this->parameters['search'] ?? 'Text Search' ); ?>"/>
                    </div>
                    <button onclick="return get_list_input_values(event);" id="inventory-search-submit">
                        Search
                    </button>
                </div>
                <div id="search-mid-wrapper">
                    <div id="inventory-makes" class="inventory-option-wrapper search-option-mid"> <!-- Makes -->
                        <label class="inventory-label">Makes</label><br/>
                        <select class="inventory-select" onchange="document.location = this.value;">
                            <option value="
							<?php
                            helpers::output_wp_kses( $fn_inventory->generate_inventory_link( array(
                                            'params' => $this->parameters,
                                            'remove' => array(
                                                    'make',
                                                    'model',
                                                    'trim',
                                            )
                                    )
                            ) );
                            ?>
							">
                                All Makes
                            </option>
                            <?php
                            $shown_makes = array();
                            foreach ( $makes as $make ) {
                                $make_safe = str_replace( '/', '%2F', $make );
                                $make_safe = ucwords( strtolower( $make_safe ) );
                                if ( ! in_array( $make_safe, $shown_makes, true ) ) {
                                    $shown_makes[] = $make_safe;
                                    $link          = $fn_inventory->generate_inventory_link( array(
                                                    'params'  => $this->parameters,
                                                    'include' => array( 'make' => $make_safe ),
                                                    'remove'  => array(
                                                            'model',
                                                            'trim',
                                                    )
                                            )
                                    );
                                    $selected      = ( isset( $this->parameters['make'] ) ? ( rawurlencode( strtolower( $make_safe ) ) === strtolower( $this->parameters['make'] ) ? 'selected' : '' ) : '' );
                                    helpers::output_wp_kses( '<option value="' . $link . '" ' . $selected . ' >' . $make . '</option>' );
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div id="inventory-models" class="inventory-option-wrapper search-option-mid"> <!-- Models -->
                        <label class="inventory-label">Models</label><br/>
                        <select class="inventory-select"
                                onchange="document.location = this.value;"
                                <?php
                                if ( ! isset( $model_count ) || 0 === $model_count ) {
                                    echo esc_html( 'readonly' );
                                }
                                ?>
                        >
                            <option value="
							<?php
                            helpers::output_wp_kses( $fn_inventory->generate_inventory_link( array(
                                            'params' => $this->parameters,
                                            'remove' => array(
                                                    'model',
                                                    'trim',
                                            )
                                    )
                            ) );
                            ?>
							"><?php echo esc_html( $model_text ); ?></option>
                            <?php
                            if ( $model_count > 0 ) {
                                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 ),
                                            'remove'  => array( 'trim' )
                                    ) );
                                    $selected   = ( isset( $this->parameters['model'] ) ? ( rawurlencode( strtolower( $model_safe ) ) === strtolower( $this->parameters['model'] ) ? 'selected' : '' ) : '' );
                                    helpers::output_wp_kses( '<option value="' . $link . '" ' . $selected . ' >' . $model . '</option>' );
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div id="inventory-trims" class="inventory-option-wrapper search-option-mid"> <!-- Trims -->
                        <label class="inventory-label">Trims</label><br/>
                        <select class="inventory-select"
                                onchange="document.location = this.value;"
                                <?php
                                if ( ! isset( $trim_count ) || 0 === $trim_count ) {
                                    echo esc_attr( 'readonly' );
                                }
                                ?>
                        >
                            <option value="<?php echo esc_url( remove_query_arg( array( 'trim' ) ) ); ?>"><?php echo esc_attr( $trim_text ); ?></option>
                            <?php
                            if ( isset( $trim_count ) && 0 !== $trim_count ) {
                                if ( 1 === $trim_count ) {
                                    $this->parameters['trim'] = $trims[0];
                                }
                                foreach ( $trims as $trim ) {
                                    $trim_safe = str_replace( '/', '%2F', $trim );
                                    $selected  = ( isset( $this->parameters['trim'] ) ? ( rawurlencode( strtolower( $trim_safe ) ) === strtolower( $this->parameters['trim'] ) ? 'selected' : '' ) : '' );
                                    helpers::output_wp_kses( '<option value="' . esc_url( add_query_arg( array( 'trim' => urlencode( $trim_safe ) ) ) ) . '" ' . $selected . '> ' . $trim . '</option>' );
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <?php if ( $this->theme_options['theme']['inventory_display']['list']['search_filters']['geo_search']['display_geo'] ) { ?>
                        <div id="list-geo-filter" class="inventory-option-wrapper search-option-mid">
                            <div class="inventory-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,
                                    )
                            ) );
                            ?>
                        </div>
                    <?php } ?>
                </div>
            </div>

            <div id="inventory-search-info"> <!-- Search Info -->
                <div id="inventory-total-found">Vehicles Found:
                    <?php
                    echo esc_html( $vehicle_total_found );
                    helpers::output_wp_kses( ! empty( $geo_output['search'] ) ? ' in ' . $geo_output['search'] : '' );
                    helpers::output_wp_kses( ! empty( $geo_output['back_link'] ) ? ' | ' . $geo_output['back_link'] . 'Geo Search' : '' );
                    ?>
                </div>
                <div id="inventory-sort-wrapper">
                    <div id="sort-label">Sort by:</div>
                    <div class="sort-value"><a
                                class="<?php echo esc_attr( isset( $sort_year_class ) ? $sort_year_class : '' ); ?>"
                                href="<?php echo 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( isset( $sort_price_class ) ? $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( isset( $sort_mileage_class ) ? $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>
    </div>


    <div id="inventory-content"> <!-- Inventory -->
        <div id="inventory-listing-items"> <!-- Inventory Listing -->
            <?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 ) );
                    $price = $fn_inventory->get_price_display(
                            array(
                                    'company' => $this->company,
                                    'vehicle' => $vehicle,
                                    'theme'   => 'inventory',
                            )
                    );
                    ?>
                    <div class="inventory-item" id="<?php echo esc_attr( $vehicle['vin'] ); ?>">
                        <!-- Inventory Listing -->
                        <div class="inventory-column-left"> <!-- inventory column left -->
                            <div class="inventory-photo"> <!-- inventory 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>
                            <!-- inventory icons -->
                            <?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="inventory-column-right"> <!-- inventory column right -->
                            <div class="inventory-headline">
                                <div class="inventory-headline-details">
									<span class="inventory-saleclass form-value"
                                          data-name="saleclass"><?php echo esc_html( $vehicle['saleclass'] ); ?></span>
                                    <a href="<?php echo esc_url( $link ); ?>"
                                       title="<?php echo esc_attr( $generic_vehicle_title ); ?>">
										<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>
                                    </a>
                                </div>
                                <?php
                                if ( $vehicle['retailprice'] > 0 && 'new' === strtolower( $vehicle['saleclass'] ) ) {
                                    helpers::output_wp_kses( '<div class="inventory-headline-msrp" data-alt="' . $vehicle['retailprice'] . '">MSRP: ' . '$' . number_format( $vehicle['retailprice'], 0, '.', ',' ) . '</div>' );
                                }
                                if ( strlen( trim( $vehicle['headline'] ) ) > 0 ) {
                                    helpers::output_wp_kses( '<div class="inventory-headline-text">' . $vehicle['headline'] . '</div>' );
                                }
                                ?>
                            </div>
                            <div class="inventory-listing-info">
                                <div class="inventory-details-left">
									<span data-alt="<?php echo esc_attr( $vehicle['stock'] ); ?>">Stock #: <span
                                                class="inventory-stock-number form-value"
                                                data-name="stock_number"><?php echo esc_html( $vehicle['stock'] ); ?></span></span>
                                    <span data-alt="<?php echo esc_attr( $vehicle['vin'] ); ?>">VIN: <span
                                                class="inventory-vin form-value"
                                                data-name="vin"><?php echo esc_html( $vehicle['vin'] ); ?></span></span>
                                    <?php
                                    if ( $vehicle['odometer'] > 100 ) {
                                        helpers::output_wp_kses( '<span class="inventory-odometer">Odometer: ' . $vehicle['odometer'] . '</span>' );
                                    }
                                    if ( 'true' === $vehicle['certified'] ) {
                                        helpers::output_wp_kses( '<span class="inventory-certified">Certified Pre-Owned</span>' );
                                    }
                                    ?>
                                </div>
                                <div class="inventory-details-right">
                                    <?php
                                    helpers::output_wp_kses( null !== $vehicle['body'] ? '<span class="inventory-body-style">Body Style: ' . $vehicle['body'] . '</span>' : null );
                                    helpers::output_wp_kses( null !== $vehicle['interior'] ? '<span class="inventory-interior-color">Int. Color: ' . $vehicle['interior'] . '</span>' : null );
                                    helpers::output_wp_kses( null !== $vehicle['exterior'] ? '<span class="inventory-exterior-color">Ext. Color: ' . $vehicle['exterior'] . '</span>' : null );
                                    helpers::output_wp_kses( null !== $vehicle['transmission'] ? '<span class="inventory-transmission">Trans: ' . $vehicle['transmission'] . '</span>' : null );
                                    ?>
                                </div>
                                <div class="inventory-price">
                                    <?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>
                            <div class="inventory-more-info">
                                <div class="inventory-contact-information">
                                    <?php
                                    helpers::output_wp_kses( $fn_inventory->get_dealer_contact_info(
                                            array(
                                                    'vehicle' => $vehicle,
                                                    'company' => $this->company,
                                            )
                                    ) );
                                    ?>
                                </div>
                                <div class="inventory-detail-button">
                                    <a class="detail-link-button" href="<?php echo esc_url( $link ); ?>"
                                       title="More Information: <?php echo esc_html( $generic_vehicle_title ); ?>"><?php echo esc_html( ( empty( $this->theme_settings['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ) ? 'More Information' : $this->theme_settings['theme']['inventory_display']['list']['general']['buttons']['vdp_link']['name'] ) ); ?></a>
                                </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='inventory-detail-button'><div class='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 class="inventory-form-container"></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">
        <p>
            <?php
            helpers::output_wp_kses( $fn_inventory->get_disclaimer() );
            ?>
        </p>
    </div>
    <div id="inventory-bottom">
        <div class="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">
            <?php helpers::output_wp_kses( paginate_links( $args ) ); ?>
        </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>
