<?php
namespace CarDealerPress\Templates\Inventory;

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

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

$flex_wrapper = $flex_one = $flex_two = $flex_three = $flex_four = $flex_five = $flex_six = '';
// if( $theme_settings[ 'style' ][ 'flex' ][ 'toggle' ] ){
// $containers = $theme_settings[ 'style' ][ 'flex' ][ 'settings' ][ 'containers' ];
// $flex_wrapper = ' style="display: flex; flex-flow: row wrap; justify-content: space-around; margin: 0 auto; width: 96%" ';
// $flex_one = ' style="order: '.$containers[ 'one' ][ 'order' ].'; max-width: '.$containers[ 'one' ][ 'max-width' ].'px;" ';
// $flex_two = ' style="order: '.$containers[ 'two' ][ 'order' ].'; max-width: '.$containers[ 'two' ][ 'max-width' ].'px;" ';
// $flex_three = ' style="order: '.$containers[ 'three' ][ 'order' ].'; max-width: '.$containers[ 'three' ][ 'max-width' ].'px;" ';
// $flex_four = ' style="order: '.$containers[ 'four' ][ 'order' ].'; max-width: '.$containers[ 'four' ][ 'max-width' ].'px;" ';
// $flex_five = ' style="order: '.$containers[ 'five' ][ 'order' ].'; max-width: '.$containers[ 'five' ][ 'max-width' ].'px;" ';
// $flex_six = ' style="order: '.$containers[ 'six' ][ 'order' ].'; max-width: '.$containers[ 'six' ][ 'max-width' ].'px;" ';
// }

?>

<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 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="inventory-top">
            <?php helpers::output_wp_kses( $vehicle['headline'] ? '<div id="inventory-detail-headline">' . $vehicle['headline'] . '</div>' : '' ); ?>
            <div id="top-inner-wrapper">
                <div id="top-inner-left">
                    <div id="top-vehicle-info">
                        <span id="top-year"><?php echo esc_html( $vehicle['year'] ); ?></span>
                        <span id="top-make"><?php echo esc_html( $vehicle['make'] ); ?></span>
                        <span id="top-model"><?php echo esc_html( $vehicle['model'] ); ?></span>
                        <span id="top-trim"><?php echo esc_html( $vehicle['trim'] ); ?></span>
                    </div>
                    <hr class="inventory-hr-half">
                    <div id="top-dealer-info">
                        <span id="dealer-name"><?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'name' ) ); ?></span>
                        -
                        <span id="dealer-phone"><?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?></span>
                        <span style="display: none;"
                              id="dealer-id"><?php helpers::output_wp_kses( $contact_info['dealer_id'] ); ?></span>
                    </div>
                </div>
                <div id="top-inner-right">
                    <div id="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'] );
                        helpers::output_wp_kses( $fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $vehicle['primary_price'], $loan_parameters ) );
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <div id="inventory-content-wrapper" class="theme-container-wrapper" <?php echo esc_attr( $flex_wrapper ); ?> >
            <div class="container-wrap container-one" <?php echo esc_attr( $flex_one ); ?> >
                <?php
                helpers::output_wp_kses( $fn_inventory->get_photo_detail_display( $vehicle, $this->theme_options['theme']['inventory_display']['detail']['general']['default_image_tab'], $flex_wrapper ) );
                ?>
            </div>
            <div class="container-wrap container-two" <?php echo esc_attr( $flex_two ); ?> >
                <div id="inventory-vehicle-info">
                    <div id="info-headline">Vehicle Information</div>
                    <?php
                    helpers::output_wp_kses( '
							<div class="info-divider">Stock Number: <span id="info-stock-number" class="info-value">' . $vehicle['stock'] . '</span></div>
							<div class="info-divider">VIN: <span id="info-vin" class="info-value">' . $vehicle['vin'] . '</span></div>
							<div class="info-divider">Condition: <span id="info-saleclass" class="info-value">' . $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>
            </div>
            <div class="container-wrap container-three" <?php echo esc_attr( $flex_three ); ?> >
                <?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>
            <div class="container-wrap container-four" <?php echo esc_attr( $flex_four ); ?> >
                <?php
                helpers::output_wp_kses( $fn_inventory->get_fuel_economy_display( $fuel_economy, $country_code, 2 ) );

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

                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>' );
                }

                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>';
                    }
                }

                ?>
            </div>
            <div class="container-wrap container-five" <?php echo esc_attr( $flex_five ); ?> >
                <?php

                $tab_buttons = array(
                        0 => array( 'options', 'Vehicle Options' ),
                        1 => array( 'description', 'Description' ),
                        2 => array( 'equipment', 'Standard Equipment' ),
                        3 => array( 'loan', 'Loan 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( 0, '' ),
                        'similar'     => array( 0, '' ),
                        'form'        => array(
                                ( function_exists( 'gravity_form' ) && $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 ), $flex_wrapper );
                ?>
            </div>
            <div class="container-wrap container-six" <?php echo esc_attr( $flex_six ); ?> >
                <?php
                if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
                    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,
                            ),
                            $flex_wrapper
                    ) );
                }
                ?>
            </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>
