<?php

namespace CarDealerPress\Templates\Inventory;

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

use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;

sort( $vehicle['dealer_options'] );

?>

<div id="inventory-wrapper">
    <?php
    $banners = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'banners' ) );
    if ( ! empty( $banners ) ) {
        $content        = '<div id="cdp-banner-top-container">';
        $banner_results = $fn_inventory->cdp_get_banners( 'detail', 'top', $banners, $this->parameters );
        foreach ( $banner_results as $banner ) {
            $content .= $banner;
        }
        $content .= '</div>';
        echo wp_kses( $content, 'post' );
    }
    ?>
    <div id="inventory-detail">
        <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'], $saleclass ) ); ?>
            </div>
            <?php
            helpers::output_wp_kses( $ext_functions->detail_page_sold_vehicle( $vehicle ) );
            ?>
            <div id="inventory-top-info">
                <div id="inventory-headline-wrap">
                    <div id="inventory-main-headline">
                        <h2>
							<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-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-transmission"><?php echo esc_html( $vehicle['transmission'] ); ?></span>
                            <span class="inventory-body-style"><?php echo esc_html( $vehicle['body'] ); ?></span>
                        </h2>
                    </div>
                    <div id="inventory-sub-headline">
                        <h3>
                            <span class="inventory-make-subheadline"><?php echo esc_html( $vehicle['make'] ); ?></span>
                            <span class="inventory-model-subheadline"><?php echo esc_html( $vehicle['model'] ); ?></span>
                            <span class="inventory-trim-subheadline"><?php echo esc_html( $vehicle['trim'] ); ?></span>
                            <span class="inventory-city-subheadline"><?php echo esc_html( $city ); ?></span>,
                            <span class="inventory-state-subheadline"><?php echo esc_html( $state ); ?></span>

                        </h3>
                        <h3>
                            <span class="inventory-sh-vehicle-location"><?php echo esc_html( $vehicle['location'] ); ?></span>
                        </h3>
                    </div>
                </div>
                <div id="inventory-top-price">
                    <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'] );

                        helpers::output_wp_kses( $fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $vehicle['primary_price'], $loan_parameters ) );
                        ?>

                    </div>
                    <?php
                    if ( $this->theme_options['theme']['inventory_display']['tags']['display_tags'] ) {
                        if ( ! empty( $vehicle['tags'] ) ) {
                            echo '<div class="inventory-detail-tags">';
                            $tag_icons = $fn_inventory->build_tag_icons( $vehicle['tags'], $vehicle['vin'] );
                            helpers::output_wp_kses( $tag_icons );
                            echo '</div>';
                        }
                    }
                    ?>
                </div>
                <?php
                if ( $vehicle['headline'] ) {
                    helpers::output_wp_kses( '<div id="inventory-custom-headline">' . $vehicle['headline'] . '</div>' );
                }
                ?>
            </div>
        </div>
        <div id="inventory-content"> <!-- inventory Content -->
            <div id="inventory-content-top"> <!-- inventory Content Top -->
                <div id="inventory-content-headline">
                    <span class="inventory-content-year"><?php echo esc_html( $vehicle['year'] ); ?></span>
                    <span class="inventory-content-make"><?php echo esc_html( $vehicle['make'] ); ?></span>
                    <span class="inventory-content-model"><?php echo esc_html( $vehicle['model'] ); ?></span>
                    <span class="inventory-text">Photos:</span>
                    <a id="friendly-print"
                       onclick="window.open('?print_page','popup','width=800,height=900,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=yes,status=no,left=0,top=0'); return false">Print</a>
                </div>
            </div>
            <div id="inventory-content-center"> <!-- inventory Content Center -->
                <div id="inventory-content-detail-left"> <!-- inventory Content Detail Left -->
                    <div id="inventory-image-wrapper"> <!-- inventory Image Wrapper -->
                        <?php
                        $image_tab = $this->theme_options['theme']['inventory_display']['detail']['general']['default_image_tab'] ?? '';
                        helpers::output_wp_kses( $fn_inventory->get_photo_detail_display( $vehicle, $image_tab, '' ) );
                        ?>
                    </div>
                    <div id="inventory-vehicle-information"> <!-- inventory Vehicle Information -->
                        <div class="inventory-vehicle-info-divider">
                            <h4 class="inventory-divider-headline">
                                <span class="inventory-info-year"><?php echo esc_html( $vehicle['year'] ); ?></span>
                                <span class="inventory-info-make"><?php echo esc_html( $vehicle['make'] ); ?></span>
                                <span class="inventory-info-model"><?php echo esc_html( $vehicle['model'] ); ?></span>
                                <span class="inventory-divider-text">Vehicle Details:</span>
                            </h4>
                            <div class="inventory-divider-content">
                                <div id="inventory-stock-vin-wrapper">
                                    <span class="inventory-text-bold">Stock #:</span><span id=""
                                                                                           class="inventory-stock-number inventory-text-space form-value"
                                                                                           data-name="stock_number"><?php echo esc_html( $vehicle['stock'] ); ?></span>
                                    <span class="inventory-text-bold">Vin #:</span><span id=""
                                                                                         class="inventory-vin inventory-text-space form-value"
                                                                                         data-name="vin"><?php echo esc_html( $vehicle['vin'] ); ?></span>
                                </div>
                                <div id="inventory-vehicle-details-wrapper">
                                    <div id="inventory-vehicle-detail-left">
                                        <?php
                                        $vehicle_info = '';

                                        if ( ! empty( $vehicle['saleclass'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-saleclass">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Condition:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-saleclass-value">' . $vehicle['saleclass'] . '</div>';
                                            $vehicle_info .= '</div>';

                                        }

                                        if ( 'true' === $vehicle['certified'] ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-certified">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Certified:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-certified-value">Yes</div>';
                                            $vehicle_info .= '</div>';

                                        }

                                        if ( ! empty( $vehicle['odometer'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-odometer">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Odometer:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-odometer-value">' . $vehicle['odometer'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['engine'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-engine">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Engine:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-engine-value">' . $vehicle['engine'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['fuel_type'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Fuel Type:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-fuel-type-value">' . $vehicle['fuel_type'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['transmission'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-transmission">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Transmission:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-transmission-value">' . $vehicle['transmission'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['drivetrain'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-drivetrain">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Drivetrain:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-drivetrain-value">' . $vehicle['drivetrain'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['exterior'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-exterior">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Exterior Color:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-exterior-value">' . $vehicle['exterior'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['interior'] ) ) {
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-wrap vehicle-interior">';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-left">Interior color:</div>';
                                            $vehicle_info .= '<div class="inventory-vehicle-overview-right vehicle-interior-value">' . $vehicle['interior'] . '</div>';
                                            $vehicle_info .= '</div>';
                                        }

                                        if ( ! empty( $vehicle['carfax'] ) && 'used' === strtolower( $vehicle['saleclass'] ) ) {
                                            $vehicle_info .= $fn_inventory->cdp_display_carfax( $vehicle['vin'], $vehicle['carfax'], $vehicle['carfax_image_url'] );
                                        }
                                        helpers::output_wp_kses( $vehicle_info );

                                        if ( $vehicle['autocheck'] ) {
                                            helpers::output_wp_kses( $fn_inventory->display_autocheck_image( $vehicle['vin'], $vehicle['saleclass'], 'detail' ) );
                                        }

                                        ?>
                                    </div>
                                    <div id="inventory-vehicle-detail-right">
                                        <?php
                                        $fuel_info = $fn_inventory->get_fuel_economy_display( $fuel_economy, $country_code, 0 );
                                        $fuel_text = '<div id="inventory-fuel-headline">Fuel Economy:</div>';
                                        $fuel_text .= '<div id="inventory-fuel-economy">';
                                        $fuel_text .= $fuel_info;
                                        $fuel_text .= '</div>';
                                        if ( ! empty( $fuel_info ) ) {
                                            helpers::output_wp_kses( $fuel_text );
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="inventory-vehicle-info-divider" id="inventory-content-description-wrapper">
                            <?php
                            if ( $vehicle['description'] ) {
                                echo '<h4 class="inventory-divider-headline"><span class="inventory-divider-text">Vehicle Comments:</span></h4>';
                                echo '<div class="inventory-divider-content"><div id="inventory-content-description"><p>' . wp_kses( $vehicle['description'], 'post' ) . '</p></div></div>';
                            }
                            ?>
                        </div>

                        <div class="inventory-vehicle-info-divider" id="inventory-content-features-wrapper">
                            <?php
                            if ( ! empty( $vehicle['dealer_options'] ) ) {
                                echo '<h4 class="inventory-divider-headline"><span class="inventory-divider-text collapse-toggle" data-name="dealer_options">Vehicle Specifications and Features:</span></h4>';
                                echo '<div class="inventory-divider-content collapse-divider dealer_options">';
                                $inventory_value = '<div id="inventory-content-features"><ul>';
                                sort( $vehicle['dealer_options'] );
                                foreach ( $vehicle['dealer_options'] as $option ) {
                                    $inventory_value .= '<li>' . $option . '</li>';
                                }
                                $inventory_value .= '</ul></div></div>';
                                helpers::output_wp_kses( $inventory_value );
                            }
                            ?>
                        </div>

                        <?php
                        if ( isset( $vehicle['standard_equipment'] ) && ! $fn_inventory->is_Empty_check( $vehicle['standard_equipment'] ) && $this->theme_options['theme']['inventory_display']['detail']['general']['show_standard_eq'] ) {
                            ?>
                            <div class="inventory-vehicle-info-divider" id="inventory-content-equipment-wrapper">
                                <h4 class="inventory-divider-headline"><span
                                            class="inventory-divider-text collapse-toggle"
                                            data-name="standard_equipment">Vehicle Standard Equipment:</span>
                                </h4>
                                <div class="inventory-divider-content collapse-divider standard_equipment"> <?php helpers::output_wp_kses( $fn_inventory->display_equipment( $vehicle['standard_equipment'] ) ); ?></div>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>

                <div id="inventory-content-detail-right"> <!-- inventory Content Detail Right -->
                    <div id="inventory-contact-information">
                        <?php
                        $contact_info_value = '<div id="inventory-contact-name">' . $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'name' ) . '</div>';
                        $contact_info_value .= '<div id="inventory-contact-vehicle-location">' . $vehicle['location'] . '</div>';
                        $phone              = $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' );
                        $contact_info_value .= '<div id="inventory-contact-phone"><a href="tel:' . $phone . '">' . $phone . '</a></div>';
                        $contact_info_value .= '<div id="inventory-contact-message">' . $contact_info['greeting'] . '</div>';
                        helpers::output_wp_kses( $contact_info_value );
                        ?>

                    </div>
                    <div class="inventory-content-sidebar-wrapper">
                        <?php
                        $forms = $this->theme_options['theme']['inventory_display']['detail']['forms'];
                        if ( function_exists( 'gravity_form' ) && ! empty( $forms ) ) {
                            $form_id = $forms[0]['id'];
                            $form    = '<div id="content-inner-form">';
                            $form    .= '<div class="form-wrapper"><div id="info-form-id-' . $form_id . '" class="inventory-form form-display-wrap form-' . $form_id . '" data-name="form-id-' . $form_id . '">';
                            $form    .= do_shortcode( '[gravityform id=' . $form_id . ' title=true description=false]' );
                            $form    .= '</div></div></div>';
                            helpers::output_wp_kses( $form );
                        }
                        helpers::output_wp_kses( $fn_inventory->get_loan_calculator( $this->theme_options['theme']['inventory_display']['payment_calculator'], $vehicle['primary_price'], true, $loan_parameters ) );

                        // GForm Buttons.
                        if ( function_exists( 'gravity_form' ) && count( $forms ) >= 1 ) {
                            array_shift( $forms ); // skip the first one.
                            $gform_buttons = $fn_inventory->get_gform_button_display( $forms, $vehicle['saleclass'] );
                            helpers::output_wp_kses( $gform_buttons );
                        }

                        // inventory Similar Vehicles.
                        if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
                            echo '<div id="detail-similar-wrapper">';
                            helpers::output_wp_kses( $fn_inventory->get_similar_vehicles(
                                    $this->vms,
                                    $vehicle['vin'],
                                    $vehicle['saleclass'],
                                    $vehicle['vehicleclass'],
                                    $price['primary_price'],
                                    $vehicle['make'],
                                    $this->theme_options['theme']['overrides']['makes_new']['included'],
                                    array(
                                            'city'  => $city,
                                            'state' => $state,
                                    )
                            ) );
                            echo '</div>';
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div id="inventory-content-bottom"> <!-- inventory Content Bottom -->
            </div>
        </div>
        <div id="inventory-bottom"> <!-- inventory Bottom -->
            <div id="inventory-disclaimer">
                <?php
                helpers::output_wp_kses( $fn_inventory->get_disclaimer() );
                ?>
            </div>
            <?php helpers::output_wp_kses( $fn_inventory->cdp_hidden_vehicle_data( $vehicle ) ); ?>
        </div>

        <?php
        if ( is_active_sidebar( 'vehicle-detail-page' ) ) :
            echo '<div id="inventory-widget-area" class="sidebar">';
            dynamic_sidebar( 'vehicle-detail-page' );
            echo '</div>';
        endif;
        ?>

    </div>
</div>
