<?php

namespace CarDealerPress\Templates\Inventory;

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

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

$flex_wrapper = ' style="display: flex; flex-flow: row wrap; justify-content: space-around; margin: 0 auto; width: 96%" ';

?>

<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" class="saleclass-<?php echo esc_attr( strtolower( $vehicle['saleclass'] ) ); ?>">
        <div id="inventory-title">
            <span class="title-year inventory-year"><?php echo esc_html( $vehicle['year'] ); ?></span>
            <span class="title-make inventory-make"><?php echo esc_html( $vehicle['make'] ); ?></span>
            <span class="title-model inventory-model"><?php echo esc_html( $vehicle['model'] ); ?></span>
            <span class="title-trim inventory-trim"><?php echo esc_html( $vehicle['trim'] ); ?></span>
        </div>
        <div class="breadcrumbs">
            <?php helpers::output_wp_kses( $fn_inventory->display_breadcrumb( $this->parameters, $this->company, $this->theme_options['theme']['overrides']['custom_contact'], $saleclass ) ); ?>
            <a id="friendly-print"
               onclick="window.open('?print_page','popup','width=550,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=yes,status=no,left=0,top=0'); return false">Print
                Page</a>
        </div>
        <?php
        helpers::output_wp_kses( $ext_functions->detail_page_sold_vehicle( $vehicle ) );
        ?>
        <div id="vehicle-dealer-info">
            <span class="vehicle-dealer-name"><?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'name' ) ); ?></span>
            -
            <span class="vehicle-dealer-phone"><?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?></span>
            <span class="vehicle-dealer-location"><?php echo esc_html( $vehicle['location'] ); ?></span>
            <span style="display: none);"
                  class="vehicle-dealer-id"><?php helpers::output_wp_kses( $contact_info['dealer_id'] ); ?></span>
        </div>
        <div id="inventory-content-wrapper">
            <div id="content-inner-left">
                <?php
                helpers::output_wp_kses( $fn_inventory->get_photo_detail_display( $vehicle, $this->theme_options['theme']['inventory_display']['detail']['general']['default_image_tab'], '' ) );
                ?>
            </div>

            <div id="content-inner-right">
                <div id="inventory-price-wrap">
                    <div id="price-main" class="inventory-price">
                        <?php helpers::output_wp_kses( $price['primary_text'] ); ?>
                    </div>
                    <div id="price-extra">
                        <?php
                        helpers::output_wp_kses( ( ! empty( $price['msrp_text'] ) && 'new' === strtolower( $vehicle['saleclass'] ) ? $price['msrp_text'] : '' ) . '
							' . $price['compare_price_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>
                </div>
                <?php helpers::output_wp_kses( $vehicle['headline'] ? '<div id="inventory-detail-headline">' . $vehicle['headline'] . '</div>' : '' ); ?>
                <div id="inventory-vehicle-info">
                    <?php
                    helpers::output_wp_kses( '
								<div class="info-divider">Stock Number: <span id="info-stock-number" class="info-value inventory-stock-number">' . $vehicle['stock'] . '</span></div>
								<div class="info-divider">VIN: <span id="info-vin" class="info-value inventory-vin">' . $vehicle['vin'] . '</span></div>
								<div class="info-divider">Condition: <span id="info-saleclass" class="info-value iventory-saleclass">' . $vehicle['saleclass'] . '</span></div>
								' . ( 'false' !== $vehicle['certified'] ? '<div class="info-divider">Certified: <span id="info-certified" class="info-value">Yes</span></div>' : '' ) . ( ! empty( $vehicle['odometer'] ) ? '<div class="info-divider">Mileage: <span id="info-mileage" class="info-value">' . $vehicle['odometer'] . '</span></div>' : '' ) . ( ! empty( $vehicle['exterior'] ) ? '<div class="info-divider">Exterior: <span id="info-exterior" class="info-value">' . $vehicle['exterior'] . '</span></div>' : '' ) . ( ! empty( $vehicle['interior'] ) ? '<div class="info-divider">Interior: <span id="info-interior" class="info-value">' . $vehicle['interior'] . '</span></div>' : '' ) . ( ! empty( $vehicle['engine'] ) ? '<div class="info-divider">Engine: <span id="info-engine" class="info-value">' . $vehicle['engine'] . '</span></div>' : '' ) . ( ! empty( $vehicle['fuel_type'] ) ? '<div class="info-divider">Fuel Type: <span id="info-fuel-type" class="info-value">' . $vehicle['fuel_type'] . '</span></div>' : '' ) . ( ! empty( $vehicle['transmission'] ) ? '<div class="info-divider">Transmission: <span id="info-transmission" class="info-value">' . $vehicle['transmission'] . '</span></div>' : '' ) . ( ! empty( $vehicle['drivetrain'] ) ? '<div class="info-divider">Drivetrain: <span id="info-drivetrain" class="info-value">' . $vehicle['drivetrain'] . '</span></div>' : '' ) . ( ! empty( $vehicle['doors'] ) ? '<div class="info-divider">Doors: <span id="info-doors" class="info-value">' . $vehicle['doors'] . '</span></div>' : '' ) . ( ! empty( $vehicle['body'] ) ? '<div class="info-divider">Body: <span id="info-body" class="info-value">' . $vehicle['body'] . '</span></div>' : '' ) );
                    ?>
                </div>
                <?php
                helpers::output_wp_kses( $fn_inventory->get_fuel_economy_display( $fuel_economy, $country_code, 1 ) );

                if ( $this->theme_options['theme']['inventory_display']['tags']['display_tags'] ) {
                    if ( ! empty( $vehicle['tags'] ) ) {
                        echo '<div id="inventory-detail-tags">';
                        $tag_icons = $fn_inventory->build_tag_icons( $vehicle['tags'], $vehicle['vin'] );
                        helpers::output_wp_kses( $tag_icons );
                        echo '</div>';
                    }
                }

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

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

                ?>
            </div>
            <?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 );
            }
            if ( function_exists( 'gravity_form' ) && count( $forms ) >= 1 ) {
                array_shift( $forms ); // skip the first one.
            }
            ?>
            <div id="content-inner-bottom">
                <?php
                $similar_output = '';
                if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
                    $similar_output = $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,
                            )
                    );
                }
                if ( ! empty( $vehicle['dealer_options'] ) && is_array( $vehicle['dealer_options'] ) ) {
                    sort( $vehicle['dealer_options'] );
                }

                $tab_buttons = array(
                        0 => array( 'options', 'Vehicle Options' ),
                        1 => array( 'description', 'Description' ),
                        2 => array( 'equipment', 'Standard Equipment' ),
                        3 => array( 'loan', 'Payment Calculator' ),
                        4 => array( 'similar', 'Similar' ),
                        5 => array( 'form', '' ),
                );

                $tab_data         = array(
                        'options'     => array(
                                ( count( $vehicle['dealer_options'] ) > 0 ? 1 : 0 ),
                                $vehicle['dealer_options'],
                        ),
                        'description' => array(
                                ( strlen( $vehicle['description'] ) > 0 ? 1 : 0 ),
                                $vehicle['description'],
                        ),
                        'equipment'   => array(
                                ( $this->theme_options['theme']['inventory_display']['detail']['general']['show_standard_eq'] && ! $fn_inventory->is_Empty_check( $vehicle['standard_equipment'] ) ? 1 : 0 ),
                                $vehicle['standard_equipment'],
                        ),
                        'loan'        => array(
                                $this->theme_options['theme']['inventory_display']['payment_calculator'][ strtolower( $vehicle['saleclass'] ) ]['default']['display_calc'],
                                $this->theme_options['theme']['inventory_display']['payment_calculator'],
                                $loan_parameters,
                        ),
                        'similar'     => array(
                                $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'],
                                $similar_output,
                        ),
                        'form'        => array(
                                ( function_exists( 'gravity_form' ) && ! empty( $forms ) ),
                                $forms,
                        ),
                        'values'      => array(
                                'saleclass' => $vehicle['saleclass'],
                                'price'     => $price['primary_price'],
                        ),
                );
                $default_info_tab = $this->theme_options['theme']['inventory_display']['detail']['general']['default_info_tab'];
                $fn_inventory->build_tab_display( $tab_buttons, $tab_data, ( 'options' === strtolower( $default_info_tab ) ? 0 : 1 ) );

                ?>
            </div>
            <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="detail-widget-area">';
            dynamic_sidebar( 'vehicle-detail-page' );
            echo '</div>';
        endif;
        ?>
    </div>
</div>
