<?php
namespace CarDealerPress\Templates\Inventory;

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

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

$phone_number = $contact_name = $loan_display = $ac_display = $gform_id = $gform_button_display = $tag_display = $fuel_display = $equipment_display = '';

// GForm Buttons.
if ( function_exists( 'gravity_form' ) && ! empty( $this->theme_options['theme']['inventory_display']['detail']['forms'][0] ) ) {
    $gforms               = $this->theme_options['theme']['inventory_display']['detail']['forms'];
    $gform_id             = array_shift( $gforms )['id']; // so forms aren't duplicated, legacy behavior was two separate areas.
    $gform_button_display = $fn_inventory->get_gform_button_display( $gforms, $saleclass );
}
// Loan Display.
$loan_display = $fn_inventory->get_loan_calculator( $this->theme_options['theme']['inventory_display']['payment_calculator'], $vehicle['primary_price'], true, $loan_parameters );
$loan_value   = $fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $vehicle['primary_price'], $loan_parameters );

// Phone Display.
$phone_number = $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' );
// Contact Display.
$contact_name = $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'name' );
// AutoCheck Display.
if ( $vehicle['autocheck'] ) {
    $ac_display = $fn_inventory->display_autocheck_image( $vehicle['vin'], $saleclass, 'detail' );
}
// Fuel Display.
$fuel_info = $fn_inventory->get_fuel_economy_display( $fuel_economy, $country_code, 0 );
if ( ! empty( $fuel_info ) ) {
    $fuel_display = $fuel_info;
}
// Tag Display.
if ( $this->theme_options['theme']['inventory_display']['tags']['display_tags'] ) {
    if ( ! empty( $vehicle['tags'] ) ) {
        $tag_display = '<div class="inventory-detail-tags">' . $fn_inventory->build_tag_icons( $vehicle['tags'], $vehicle['vin'] ) . '</div>';
    }
}

// Standard Equipment Display.
$equipment_flag = false;
if ( isset( $vehicle['standard_equipment'] ) && ! $fn_inventory->is_Empty_check( $vehicle['standard_equipment'] ) && $this->theme_options['theme']['inventory_display']['detail']['general']['show_standard_eq'] ) {
    // $equipment_display = '<div id="standard-equipment"><h3>Standard Equipment</h3><div id="eq-wrapper">'.display_equipment($vehicle['standard_equipment']).'</div></div>';
    $equipment_flag = true;
}
// Options/Description Display.
if ( ! empty( $vehicle['dealer_options'] ) && is_array( $vehicle['dealer_options'] ) ) {
    sort( $vehicle['dealer_options'] );
}
$detail_tabs_info = $fn_inventory->get_vehicle_detail_display( $vehicle['dealer_options'], $vehicle['description'], $equipment_flag, $vehicle['standard_equipment'], $this->theme_options['theme']['inventory_display']['detail']['general']['default_info_tab'] );

// Photo Display.
$image_tab     = $this->theme_options['theme']['inventory_display']['detail']['general']['default_image_tab'] ?? '';
$photo_display = $fn_inventory->get_photo_detail_display( $vehicle, $image_tab, '' );

?>


<div id="inventory-wrapper">
    <div id="inventory-detail" class="inventory-saleclass-<?php echo esc_attr( $saleclass ); ?>">
        <?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 class="breadcrumb-wrapper">
            <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 class="breadcrumbs"><?php helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'], $saleclass ) ); ?></div>
        </div>
        <?php
        helpers::output_wp_kses( $ext_functions->detail_page_sold_vehicle( $vehicle ) );
        ?>
        <div class="inventory-main-line">
            <h2><?php helpers::output_wp_kses( '<span class="inventory-year">' . $vehicle['year'] . '</span> <span class="inventory-make">' . $vehicle['make'] . '</span> <span class="inventory-model">' . $vehicle['model'] . '</span> <span class="inventory-trim">' . $vehicle['trim'] . '</span> <span class="inventory-drivetrain">' . $vehicle['drivetrain'] . '</span>' ); ?></h2>
            <p><?php helpers::output_wp_kses( $vehicle['headline'] ); ?></p>
        </div>
        <div id="detail-content-wrapper">
            <?php
            switch ( $this->theme_options['theme']['general']['detail_format'] ) {
                case 'Default':
                    $theme_style = 'column-float-right';
                    break;
                case 'Style 2':
                    $theme_style = 'column-float-left';
                    break;
            }
            $info_column   = $fn_inventory->get_info_column( $theme_style, $vehicle, $phone_number, $contact_name, $loan_display, $ac_display, $gform_id, $gform_button_display, $contact_info );
            $detail_column = $fn_inventory->get_detail_column( $theme_style, $vehicle, $price, $fuel_display, $tag_display, $detail_tabs_info, $equipment_display, $loan_value );
            $photo_column  = $fn_inventory->get_photo_column( $theme_style, $photo_display );

            helpers::output_wp_kses( $photo_column . $detail_column . $info_column );
            ?>
        </div>
        <?php
        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'],
                    $saleclass,
                    $vehicle['vehicleclass'],
                    $price['primary_price'],
                    $vehicle['make'],
                    $this->theme_options['theme']['overrides']['makes_new']['included'],
                    array(
                            'city'  => $city,
                            'state' => $state,
                    )
            ) );
            echo '</div>';
        }
        ?>
        <div class="inventory-disclaimer">
            <p>
                <?php
                helpers::output_wp_kses( $fn_inventory->get_disclaimer() );
                ?>
            </p>
        </div>
        <?php helpers::output_wp_kses( $fn_inventory->cdp_hidden_vehicle_data( $vehicle ) ); ?>
    </div>
</div>
<?php
if ( is_active_sidebar( 'vehicle-detail-page' ) ) :
    echo '<div id="sidebar-widget-area" class="sidebar">';
    dynamic_sidebar( 'vehicle-detail-page' );
    echo '</div>';
endif;
?>
