<?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\Search_Bar_Filters;

            helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'] ) );
            ?>
        </div>
        <div id="inventory-quick-links">
            <div id="inventory-quick-selects">
                <div class="quick-link-wrap">
                    <label class="inventory-label">Makes:</label>
                    <select onchange="document.location = this.value;" class="inventory-select inventory-select-makes">
                        <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 class="quick-link-wrap">
                    <label class="inventory-label">Models:</label>
                    <select onchange="document.location = this.value;"
                            class="inventory-select inventory-select-models"
                            <?php
                            if ( ! isset( $model_count ) || 0 === $model_count ) {
                                echo esc_attr( 'readonly' );
                            }
                            ?>
                    >
                        <option value="
						<?php
                        echo esc_url( $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 class="quick-link-wrap">
                    <label class="inventory-label">Trims:</label>
                    <select onchange="document.location = this.value;"
                            class="inventory-select inventory-select-trims"
                            <?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_html( $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>
            </div>
            <div id="inventory-search-text" class="<?php echo esc_attr( $search_input_class ); ?>">
                <input id="inventory-search-box" class="text-search list-search-value" name="search"
                       value="<?php echo esc_html( $this->parameters['search'] ?? null ); ?>"/>
                <button onclick="return get_list_input_values(event);" id="inventory-search-submit">GO
                </button>
            </div>
        </div>
        <div id="inventory-search-wrapper"> <!-- Search Wrapper -->
            <div id="inventory-search-advance" style="display: none;">
                <div class="inventory-advance-param">
                    <!--                    -->
                    <?php
                    // helpers::output_wp_kses( Search_Bar_Filters::range_dropdown(array('name'=> 'price','parameters'=> $this->parameters));
                    ?>
                    <label class="inventory-label" for="price-range">Price Range
                        -</label><span>(From: 1000 To: 10000)</span><br>
                    <label class="inventory-label" for="price_from">From:</label>
                    <input id="inventory-price-from" class="list-search-value" name="price_from"
                           value="<?php echo esc_html( $this->parameters['price_from'] ?? null ); ?>"/>
                    <label class="inventory-label" for="price_to">To:</label>
                    <input id="inventory-price-to" class="list-search-value" name="price_to"
                           value="<?php echo esc_html( $this->parameters['price_to'] ?? null ); ?>"/>
                </div>
                <div class="inventory-advance-param">
                    <label class="inventory-label" for="year-range">Year -</label><span>(From: 2010 To: 2013)</span><br>
                    <label class="inventory-label" for="year_from">From:</label>
                    <input id="inventory-year-from" class="list-search-value" name="year_from"
                           value="<?php echo esc_html( $this->parameters['year_from'] ?? null ); ?>"/>
                    <label class="inventory-label" for="year_to">To:</label>
                    <input id="inventory-year-to" class="list-search-value" name="year_to"
                           value="<?php echo esc_html( $this->parameters['year_to'] ?? null ); ?>"/>
                </div>
                <div class="inventory-advance-param">
                    <label class="inventory-label" for="mileage-range">Odometer
                        -</label><span>(From: 20000 To: 50000)</span><br>
                    <label class="inventory-label" for="mileage_from">From:</label>
                    <input id="inventory-mileage-from" class="list-search-value" name="mileage_from"
                           value="<?php echo esc_html( $this->parameters['mileage_from'] ?? null ); ?>"/>
                    <label class="inventory-label" for="mileage_to">To:</label>
                    <input id="inventory-mileage-to" class="list-search-value" name="mileage_to"
                           value="<?php echo esc_html( $this->parameters['mileage_to'] ?? null ); ?>"/>
                </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( array( 'params' => $this->parameters ) ) );
                    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>';
                    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>
            <div id="inventory-advance-show" data-name="hidden">
                Advanced Search
            </div>
        </div>
        <div id="inventory-total-found">Found <?php echo esc_html( $vehicle_total_found ); ?> Exact
            Matches <?php helpers::output_wp_kses( ! empty( $geo_output['search'] ) ? '<br/>in ' . $geo_output['search'] : '' ); ?></div>

        <div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args ) ); ?></div>

        <div id="inventory-sorting">Sort options:
            <a class="<?php echo esc_html( $sort_year_class ?? '' ); ?>"
               href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_year ), $do_not_carry ) ); ?>">Year</a>
            /
            <a class="<?php echo esc_html( $sort_price_class ?? '' ); ?>"
               href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_price ), $do_not_carry ) ); ?>">Price</a>
            /
            <a class="<?php echo esc_html( $sort_mileage_class ?? '' ); ?>"
               href="<?php echo esc_url( add_query_arg( array( 'manual_sort' => $sort_mileage ), $do_not_carry ) ); ?>">Mileage</a>
        </div>

        <div id="inventory-content-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'],
                    );
                    $inventory_url         = $fn_inventory->generate_inventory_link( array(
                            'params' => $link_params,
                            'type'   => 1
                    ) );
                    ?>

                    <div class="inventory-vehicle saleclass-<?php echo esc_attr( strtolower( $vehicle['saleclass'] ) ); ?>"
                         id="<?php echo esc_attr( $vehicle['vin'] ); ?>">
                        <div class="inventory-vehicle-top"><?php helpers::output_wp_kses( $vehicle['headline'] ? '<div class="inventory-list-headline">' . $vehicle['headline'] . '</div>' : '' ); ?></div>
                        <div class="inventory-vehicle-left">
                            <div class="photo">
                                <a href="<?php echo esc_url( $inventory_url ); ?>"
                                   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="inventory-vehicle-right">

                            <div class="inventory-vehicle-inner-wrap">
                                <div class="inventory-vehicle-inner-left">
                                    <div class="inventory-vehicle-title">
                                        <a href="<?php echo esc_url( $inventory_url ); ?>"
                                           title="<?php echo esc_attr( $generic_vehicle_title ); ?>"
                                           class="title-details">
                                            <div>
												<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>
                                            </div>
                                            <span class="title-trim inventory-trim form-value"
                                                  data-name="trim"><?php echo esc_html( $vehicle['trim'] ); ?>&nbsp;</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-vehicle-identifier">Stock #: <span
                                                class="vehicle-stock inventory-stock-number form-value"
                                                data-name="stock_number"><?php echo esc_html( $vehicle['stock'] ); ?></span><br/>
                                        VIN:
                                        <span class="vehicle-vin inventory-vin form-value"
                                              data-name="vin"><?php echo esc_html( $vehicle['vin'] ); ?></span></div>
                                    <div class="inventory-vehicle-extras">
                                        <?php
                                        helpers::output_wp_kses( $vehicle['exterior'] ? '<span class="exterior-color"> Exterior: ' . $vehicle['exterior'] . '</span>' : '' );
                                        helpers::output_wp_kses( $vehicle['interior'] ? '<span class="interior-color"> Interior: ' . $vehicle['interior'] . '</span>' : '' );
                                        helpers::output_wp_kses( $vehicle['odometer'] && 'used' === strtolower( $vehicle['saleclass'] ) ? '<br><span class="odometer-color"> Odometer: ' . $vehicle['odometer'] . '</span>' : '' );
                                        ?>
                                    </div>
                                </div>
                                <div class="inventory-vehicle-inner-right">
                                    <?php
                                    $price = $fn_inventory->get_price_display(
                                            array(
                                                    'company' => $this->company,
                                                    'vehicle' => $vehicle,
                                                    'theme'   => 'inventory',
                                            )
                                    );
                                    ?>
                                    <div class="inventory-price-wrap">
                                        <?php
                                        helpers::output_wp_kses( ( ! empty( $price['msrp_text'] ) && 'new' === strtolower( $vehicle['saleclass'] ) ? $price['msrp_text'] : '' ) . $price['primary_text'] . $price['compare_price_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-more-info">
                                        <div class="inventory-more-info-button"><a
                                                    href="<?php echo esc_url( $inventory_url ); ?>">
                                                <?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>
                        <div class="inventory-vehicle-bottom">
                            <div class="bottom-left">
                                <?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="bottom-right">
                                <?php
                                helpers::output_wp_kses( $fn_inventory->get_dealer_contact_info(
                                        array(
                                                'vehicle' => $vehicle,
                                                'company' => $this->company,
                                        )
                                ) );

                                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-more-info list-form-button'><div class='inventory-more-info-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>
                        <?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 class="inventory-form-container"></div>
                    </div>

                    <?php
                }
            }
            ?>
            <div id="inventory-disclaimer">
                <?php
                helpers::output_wp_kses( $fn_inventory->get_disclaimer() );
                ?>
            </div>
        </div>
        <div class="inventory-pager"><?php helpers::output_wp_kses( paginate_links( $args ) ); ?></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>
